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

Last change on this file since 5423 was 5423, checked in by cameron, 2 years ago

lz4d cmake entry and test case

File size: 7.2 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_builder.h>
16#include <IR_Gen/idisa_target.h>
17#include <boost/filesystem.hpp>
18#include <boost/iostreams/device/mapped_file.hpp>
19
20#include <lz4FrameDecoder.h>
21#include <cc/cc_compiler.h>
22#include <kernels/toolchain.h>
23#include <kernels/cc_kernel.h>
24#include <kernels/streamset.h>
25#include <kernels/s2p_kernel.h>
26#include <kernels/stdin_kernel.h>
27#include <kernels/stdout_kernel.h>
28#include <kernels/mmap_kernel.h>
29#include <kernels/lz4_index_decoder.h>
30#include <kernels/lz4_bytestream_decoder.h>
31#include <kernels/pipeline.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
49ParabixDriver * pxDriver = nullptr;
50
51
52void generatePipeline(ParabixDriver & pxDriver) {
53    IDISA::IDISA_Builder * iBuilder = pxDriver.getIDISA_Builder();
54    Module * M = iBuilder->getModule();
55
56    Type * const size_ty = iBuilder->getSizeTy();
57    Type * const bool_ty = iBuilder->getIntNTy(sizeof(bool) * 8);
58    Type * const voidTy = iBuilder->getVoidTy();
59    Type * const inputType = iBuilder->getInt8PtrTy();
60   
61    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, size_ty, bool_ty, nullptr));
62    main->setCallingConv(CallingConv::C);
63    Function::arg_iterator args = main->arg_begin();
64    Value * const inputStream = &*(args++);
65    inputStream->setName("input");
66    Value * const fileSize = &*(args++);
67    fileSize->setName("fileSize");
68    Value * const hasBlockChecksum = &*(args++);
69    hasBlockChecksum->setName("hasBlockChecksum");
70
71    const unsigned segmentSize = codegen::SegmentSize;
72    const unsigned bufferSegments = codegen::BufferSegments * codegen::ThreadNum;
73    // Output buffer should be at least one whole LZ4 block (4MB) large in case of uncompressed blocks.
74    // And the size (in bytes) also needs to be a power of two.
75    const unsigned decompressBufBlocks = 4U * 1024 * 1024 / codegen::BlockSize;
76
77    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
78
79    StreamSetBuffer * const ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
80    StreamSetBuffer * const BasisBits = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), segmentSize * bufferSegments));
81    StreamSetBuffer * const Extenders = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments));
82    StreamSetBuffer * const LiteralIndexes = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(2, 32), segmentSize * bufferSegments));
83    StreamSetBuffer * const MatchIndexes = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(2, 32), segmentSize * bufferSegments));
84    StreamSetBuffer * const DecompressedByteStream = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), decompressBufBlocks));
85
86   
87    kernel::KernelBuilder * sourceK = pxDriver.addKernelInstance(make_unique<kernel::FileSourceKernel>(iBuilder, iBuilder->getInt8PtrTy(), segmentSize));
88    sourceK->setInitialArguments({inputStream, fileSize});
89    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
90
91    // Input stream is not aligned due to the offset.
92    KernelBuilder * s2pk = pxDriver.addKernelInstance(make_unique<S2PKernel>(iBuilder, /*aligned = */ false));
93    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
94   
95    KernelBuilder * extenderK = pxDriver.addKernelInstance(make_unique<ParabixCharacterClassKernelBuilder>(iBuilder, "extenders", std::vector<re::CC *>{re::makeCC(0xFF)}, 8));
96    pxDriver.makeKernelCall(extenderK, {BasisBits}, {Extenders});
97
98    KernelBuilder * lz4iK = pxDriver.addKernelInstance(make_unique<LZ4IndexDecoderKernel>(iBuilder));
99    lz4iK->setInitialArguments({iBuilder->CreateTrunc(hasBlockChecksum, iBuilder->getInt1Ty())});
100    pxDriver.makeKernelCall(lz4iK, {ByteStream, Extenders}, {LiteralIndexes, MatchIndexes});
101
102    KernelBuilder * lz4bK = pxDriver.addKernelInstance(make_unique<LZ4ByteStreamDecoderKernel>(iBuilder, decompressBufBlocks * codegen::BlockSize));
103    pxDriver.makeKernelCall(lz4bK, {LiteralIndexes, MatchIndexes, ByteStream}, {DecompressedByteStream});
104
105    KernelBuilder * outK = pxDriver.addKernelInstance(make_unique<FileSink>(iBuilder, 8));
106    outK->setInitialArguments({iBuilder->CreatePointerCast(iBuilder->CreateGlobalString(outputFile), iBuilder->getInt8PtrTy())});
107    pxDriver.makeKernelCall(outK, {DecompressedByteStream}, {});
108 
109    pxDriver.generatePipelineIR();
110
111    iBuilder->CreateRetVoid();
112 
113    pxDriver.linkAndFinalize();
114}
115
116
117MainFunctionType codeGen() {
118    Module * M = new Module("lz4d", getGlobalContext());
119    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
120    pxDriver = new ParabixDriver(idb);
121
122    generatePipeline(*pxDriver);
123
124    delete idb;
125    return reinterpret_cast<MainFunctionType>(pxDriver->getPointerToMain());
126}
127
128
129int main(int argc, char *argv[]) {
130    // This boilerplate provides convenient stack traces and clean LLVM exit
131    // handling. It also initializes the built in support for convenient
132    // command line option handling.
133    sys::PrintStackTraceOnErrorSignal(argv[0]);
134    llvm::PrettyStackTraceProgram X(argc, argv);
135    llvm_shutdown_obj shutdown;
136    cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *>{&lz4dFlags, codegen::codegen_flags()});
137    cl::ParseCommandLineOptions(argc, argv);
138    std::string fileName = inputFile;
139    LZ4FrameDecoder lz4Frame(fileName);
140    if (!lz4Frame.isValid()) {
141        errs() << "Invalid LZ4 file.\n";
142        return -1;
143    }
144
145    if (boost::filesystem::exists(outputFile)) {
146        if (overwriteOutput) {
147            boost::filesystem::remove(outputFile);
148        } else {
149            errs() << outputFile + " existed. Use -f argument to overwrite.\n";
150            return -1;
151        }
152    }
153
154    boost::iostreams::mapped_file_source mappedFile;
155    // Since mmap offset has to be multiples of pages, we can't use it to skip headers.
156    mappedFile.open(fileName, lz4Frame.getBlocksLength() + lz4Frame.getBlocksStart());
157    char *fileBuffer = const_cast<char *>(mappedFile.data()) + lz4Frame.getBlocksStart();
158
159    MainFunctionType fn_ptr = codeGen();
160    fn_ptr(fileBuffer, lz4Frame.getBlocksLength(), lz4Frame.hasBlockChecksum());
161
162    mappedFile.close();
163    return 0;
164}
Note: See TracBrowser for help on using the repository browser.