source: icGREP/icgrep-devel/icgrep/lz4/lz4_decompression_generator.cpp @ 6173

Last change on this file since 6173 was 6137, checked in by xwa163, 14 months ago
  1. LZ4 ScanMatch? pipeline
  2. Refactor LZ4 Generator
  3. Adjust some naming
File size: 3.3 KB
Line 
1
2#include "lz4_decompression_generator.h"
3#include "lz4_frame_decoder.h"
4
5#include <boost/filesystem.hpp>
6#include <boost/iostreams/device/mapped_file.hpp>
7#include <kernels/kernel_builder.h>
8#include <kernels/p2s_kernel.h>
9#include <kernels/stdout_kernel.h>
10
11
12#include <llvm/Support/raw_ostream.h>
13
14namespace re { class CC; }
15
16using namespace llvm;
17using namespace parabix;
18using namespace kernel;
19
20LZ4DecompressionGenerator::LZ4DecompressionGenerator():LZ4BaseGenerator() {
21}
22
23int LZ4DecompressionGenerator::decompress(std::string&& inputFileName, std::string&& outputFileName, bool overwriteOutput) {
24    LZ4FrameDecoder lz4Frame(inputFileName);
25    if (!lz4Frame.isValid()) {
26        llvm::errs() << "Invalid LZ4 file.\n";
27        return -1;
28    }
29
30    if (boost::filesystem::exists(outputFileName)) {
31        if (overwriteOutput) {
32            boost::filesystem::remove(outputFileName);
33        } else {
34            llvm::errs() << outputFileName + " existed. Use -f argument to overwrite.\n";
35            return -1;
36        }
37    }
38
39    boost::iostreams::mapped_file_source mappedFile;
40    mappedFile.open(inputFileName, lz4Frame.getBlocksLength() + lz4Frame.getBlocksStart());
41    char *fileBuffer = const_cast<char *>(mappedFile.data());
42
43    this->generateDecompressionPipeline(outputFileName);
44
45    auto main = this->getMainFunc();
46    main(fileBuffer, lz4Frame.getBlocksStart(), lz4Frame.getBlocksStart() + lz4Frame.getBlocksLength(), lz4Frame.hasBlockChecksum());
47    mappedFile.close();
48    return 0;
49}
50
51
52MainFunctionType LZ4DecompressionGenerator::getMainFunc() {
53    return reinterpret_cast<MainFunctionType>(mPxDriver.getMain());
54}
55
56void LZ4DecompressionGenerator::generateDecompressionPipeline(const std::string &outputFile) {
57    auto & b = mPxDriver.getBuilder();
58    this->generateMainFunc(b);
59
60
61
62    StreamSetBuffer* compressedByteStream = this->loadByteStream();
63    StreamSetBuffer* uncompressedByteStream = this->byteStreamDecompression(compressedByteStream);
64
65    Kernel * outK = mPxDriver.addKernelInstance<FileSink>(b, 8);
66    outK->setInitialArguments({b->GetString(outputFile)});
67    mPxDriver.makeKernelCall(outK, {uncompressedByteStream}, {});
68
69    mPxDriver.generatePipelineIR();
70    mPxDriver.deallocateBuffers();
71
72    b->CreateRetVoid();
73
74    mPxDriver.finalizeObject();
75}
76
77
78void LZ4DecompressionGenerator::generateMainFunc(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
79    Module * M = iBuilder->getModule();
80    Type * const sizeTy = iBuilder->getSizeTy();
81    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
82    Type * const voidTy = iBuilder->getVoidTy();
83    Type * const inputType = iBuilder->getInt8PtrTy();
84
85    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, sizeTy, sizeTy, boolTy, nullptr));
86    main->setCallingConv(CallingConv::C);
87    Function::arg_iterator args = main->arg_begin();
88    mInputStream = &*(args++);
89    mInputStream->setName("input");
90
91    mHeaderSize = &*(args++);
92    mHeaderSize->setName("mHeaderSize");
93
94    mFileSize = &*(args++);
95    mFileSize->setName("mFileSize");
96
97    mHasBlockChecksum = &*(args++);
98    mHasBlockChecksum->setName("mHasBlockChecksum");
99    // TODO for now, we do not handle blockCheckSum
100    mHasBlockChecksum = iBuilder->getInt1(false);
101
102    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
103}
Note: See TracBrowser for help on using the repository browser.