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

Last change on this file since 6184 was 6184, checked in by nmedfort, 5 months ago

Initial version of PipelineKernel? + revised StreamSet? model.

File size: 2.5 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/stdout_kernel.h>
9#include <kernels/pipeline_builder.h>
10#include <llvm/Support/raw_ostream.h>
11
12namespace re { class CC; }
13
14using namespace llvm;
15using namespace kernel;
16
17LZ4DecompressionGenerator::LZ4DecompressionGenerator()
18: LZ4BaseGenerator() {
19    mPipeline = std::move(makeInternalPipeline());
20}
21
22inline std::unique_ptr<kernel::PipelineBuilder> LZ4DecompressionGenerator::makeInternalPipeline() {
23    Bindings inputs;
24
25    auto & b = mPxDriver.getBuilder();
26
27    Type * const int8PtrTy = b->getInt8PtrTy();
28    Type * const sizeTy = b->getSizeTy();
29    Type * const boolTy = b->getIntNTy(sizeof(bool) * 8);
30
31    inputs.emplace_back(int8PtrTy, "input");
32    inputs.emplace_back(sizeTy, "headerSize");
33    inputs.emplace_back(sizeTy, "fileSize");
34    inputs.emplace_back(boolTy, "hasBlockChecksum");
35    inputs.emplace_back(int8PtrTy, "outputFile");
36
37    return mPxDriver.makePipeline(inputs, Bindings{});
38}
39
40int LZ4DecompressionGenerator::decompress(std::string&& inputFileName, std::string&& outputFileName, bool overwriteOutput) {
41    LZ4FrameDecoder lz4Frame(inputFileName);
42    if (!lz4Frame.isValid()) {
43        llvm::errs() << "Invalid LZ4 file.\n";
44        return -1;
45    }
46
47    if (boost::filesystem::exists(outputFileName)) {
48        if (overwriteOutput) {
49            boost::filesystem::remove(outputFileName);
50        } else {
51            llvm::errs() << outputFileName + " existed. Use -f argument to overwrite.\n";
52            return -1;
53        }
54    }
55
56    boost::iostreams::mapped_file_source mappedFile;
57    mappedFile.open(inputFileName, lz4Frame.getBlocksLength() + lz4Frame.getBlocksStart());
58    char * fileBuffer = const_cast<char *>(mappedFile.data());
59
60    auto main = generateDecompressionPipeline();
61
62    main(fileBuffer, lz4Frame.getBlocksStart(), lz4Frame.getBlocksStart() + lz4Frame.getBlocksLength(), lz4Frame.hasBlockChecksum(), outputFileName.c_str());
63    mappedFile.close();
64    return 0;
65}
66
67MainFunctionType LZ4DecompressionGenerator::generateDecompressionPipeline() {
68    StreamSet * compressedByteStream = loadByteStream();
69    StreamSet * uncompressedByteStream = byteStreamDecompression(compressedByteStream);
70    Scalar * outputFileName = mPipeline->getInputScalar("outputFile");
71    mPipeline->CreateKernelCall<FileSink>(outputFileName, uncompressedByteStream);
72    return reinterpret_cast<MainFunctionType>(mPipeline->compile());
73}
Note: See TracBrowser for help on using the repository browser.