source: icGREP/icgrep-devel/icgrep/lz4d.cpp @ 5474

Last change on this file since 5474 was 5474, checked in by nmedfort, 2 years ago

Eliminated ExecutionEngine? memory leak. Intentionally broke compatibility with prior versions to ensure unchecked in projects are restructured.

File size: 7.0 KB
Line 
1/*
2 *  Copyright (c) 2017 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7
8#include <llvm/IR/Module.h>
9#include <llvm/IR/Function.h>
10#include <llvm/Linker/Linker.h>
11#include <llvm/Support/CommandLine.h>
12#include <llvm/Support/PrettyStackTrace.h>
13#include <llvm/Support/Signals.h>
14#include <llvm/Support/ManagedStatic.h>
15#include <IR_Gen/idisa_target.h>
16#include <boost/filesystem.hpp>
17#include <boost/iostreams/device/mapped_file.hpp>
18
19#include <lz4FrameDecoder.h>
20#include <cc/cc_compiler.h>
21#include <toolchain/toolchain.h>
22#include <kernels/cc_kernel.h>
23#include <kernels/streamset.h>
24#include <kernels/s2p_kernel.h>
25#include <kernels/source_kernel.h>
26#include <kernels/stdout_kernel.h>
27#include <kernels/lz4_index_decoder.h>
28#include <kernels/lz4_bytestream_decoder.h>
29
30#include <kernels/kernel_builder.h>
31#include <toolchain/cpudriver.h>
32
33#include <string>
34#include <iostream>
35namespace re { class CC; }
36
37using namespace llvm;
38using namespace parabix;
39using namespace kernel;
40
41static cl::OptionCategory lz4dFlags("Command Flags", "lz4d options");
42static cl::opt<std::string> inputFile(cl::Positional, cl::desc("<input file>"), cl::Required, cl::cat(lz4dFlags));
43static cl::opt<std::string> outputFile(cl::Positional, cl::desc("<output file>"), cl::Required, cl::cat(lz4dFlags));
44static cl::opt<bool> overwriteOutput("f", cl::desc("Overwrite existing output file."), cl::init(false), cl::cat(lz4dFlags));
45
46
47typedef void (*MainFunctionType)(char * byte_data, size_t filesize, bool hasBlockChecksum);
48
49void generatePipeline(ParabixDriver & pxDriver) {
50    auto & iBuilder = pxDriver.getBuilder();
51    Module * M = iBuilder->getModule();
52
53    Type * const size_ty = iBuilder->getSizeTy();
54    Type * const bool_ty = iBuilder->getIntNTy(sizeof(bool) * 8);
55    Type * const voidTy = iBuilder->getVoidTy();
56    Type * const inputType = iBuilder->getInt8PtrTy();
57   
58    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, size_ty, bool_ty, nullptr));
59    main->setCallingConv(CallingConv::C);
60    Function::arg_iterator args = main->arg_begin();
61    Value * const inputStream = &*(args++);
62    inputStream->setName("input");
63    Value * const fileSize = &*(args++);
64    fileSize->setName("fileSize");
65    Value * const hasBlockChecksum = &*(args++);
66    hasBlockChecksum->setName("hasBlockChecksum");
67
68    const unsigned segmentSize = codegen::SegmentSize;
69    const unsigned bufferSegments = codegen::BufferSegments * codegen::ThreadNum;
70    // Output buffer should be at least one whole LZ4 block (4MB) large in case of uncompressed blocks.
71    // And the size (in bytes) also needs to be a power of two.
72    const unsigned decompressBufBlocks = 4U * 1024 * 1024 / codegen::BlockSize;
73
74    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
75
76    StreamSetBuffer * const ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), segmentSize * bufferSegments));
77    StreamSetBuffer * const BasisBits = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), segmentSize * bufferSegments));
78    StreamSetBuffer * const Extenders = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments));
79    StreamSetBuffer * const LiteralIndexes = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(2, 32), segmentSize * bufferSegments));
80    StreamSetBuffer * const MatchIndexes = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(2, 32), segmentSize * bufferSegments));
81    StreamSetBuffer * const DecompressedByteStream = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), decompressBufBlocks));
82
83   
84    kernel::Kernel * sourceK = pxDriver.addKernelInstance(make_unique<MemorySourceKernel>(iBuilder, iBuilder->getInt8PtrTy(), segmentSize));
85    sourceK->setInitialArguments({inputStream, fileSize});
86    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
87
88    // Input stream is not aligned due to the offset.
89    Kernel * s2pk = pxDriver.addKernelInstance(make_unique<S2PKernel>(iBuilder, /*aligned = */ false));
90    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
91   
92    Kernel * extenderK = pxDriver.addKernelInstance(make_unique<ParabixCharacterClassKernelBuilder>(iBuilder, "extenders", std::vector<re::CC *>{re::makeCC(0xFF)}, 8));
93    pxDriver.makeKernelCall(extenderK, {BasisBits}, {Extenders});
94
95    Kernel * lz4iK = pxDriver.addKernelInstance(make_unique<LZ4IndexDecoderKernel>(iBuilder));
96    lz4iK->setInitialArguments({iBuilder->CreateTrunc(hasBlockChecksum, iBuilder->getInt1Ty())});
97    pxDriver.makeKernelCall(lz4iK, {ByteStream, Extenders}, {LiteralIndexes, MatchIndexes});
98
99    Kernel * lz4bK = pxDriver.addKernelInstance(make_unique<LZ4ByteStreamDecoderKernel>(iBuilder, decompressBufBlocks * codegen::BlockSize));
100    pxDriver.makeKernelCall(lz4bK, {LiteralIndexes, MatchIndexes, ByteStream}, {DecompressedByteStream});
101
102    Kernel * outK = pxDriver.addKernelInstance(make_unique<FileSink>(iBuilder, 8));
103    outK->setInitialArguments({iBuilder->GetString(outputFile)});
104    pxDriver.makeKernelCall(outK, {DecompressedByteStream}, {});
105 
106    pxDriver.generatePipelineIR();
107
108    iBuilder->CreateRetVoid();
109 
110    pxDriver.finalizeObject();
111}
112
113
114MainFunctionType codeGen() {
115    ParabixDriver pxDriver("lz4d");
116    generatePipeline(pxDriver);
117    return reinterpret_cast<MainFunctionType>(pxDriver.getMain());
118}
119
120
121int main(int argc, char *argv[]) {
122    // This boilerplate provides convenient stack traces and clean LLVM exit
123    // handling. It also initializes the built in support for convenient
124    // command line option handling.
125    sys::PrintStackTraceOnErrorSignal(argv[0]);
126    llvm::PrettyStackTraceProgram X(argc, argv);
127    llvm_shutdown_obj shutdown;
128    cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *>{&lz4dFlags, codegen::codegen_flags()});
129    cl::ParseCommandLineOptions(argc, argv);
130    std::string fileName = inputFile;
131    LZ4FrameDecoder lz4Frame(fileName);
132    if (!lz4Frame.isValid()) {
133        errs() << "Invalid LZ4 file.\n";
134        return -1;
135    }
136
137    if (boost::filesystem::exists(outputFile)) {
138        if (overwriteOutput) {
139            boost::filesystem::remove(outputFile);
140        } else {
141            errs() << outputFile + " existed. Use -f argument to overwrite.\n";
142            return -1;
143        }
144    }
145
146    boost::iostreams::mapped_file_source mappedFile;
147    // Since mmap offset has to be multiples of pages, we can't use it to skip headers.
148    mappedFile.open(fileName, lz4Frame.getBlocksLength() + lz4Frame.getBlocksStart());
149    char *fileBuffer = const_cast<char *>(mappedFile.data()) + lz4Frame.getBlocksStart();
150
151    ParabixDriver pxDriver("lz4d");
152    generatePipeline(pxDriver);
153    auto main = reinterpret_cast<MainFunctionType>(pxDriver.getMain());
154
155    main(fileBuffer, lz4Frame.getBlocksLength(), lz4Frame.hasBlockChecksum());
156
157    mappedFile.close();
158    return 0;
159}
Note: See TracBrowser for help on using the repository browser.