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

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

Changes towards separate compilation

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