source: icGREP/icgrep-devel/icgrep/kernels/lz4/lz4_deposit_uncompressed.cpp @ 5939

Last change on this file since 5939 was 5939, checked in by cameron, 13 months ago

Some cleanups

File size: 6.1 KB
Line 
1//
2// Created by wxy325 on 2017/7/9.
3//
4
5#include "lz4_deposit_uncompressed.h"
6#include <kernels/kernel_builder.h>
7#include <toolchain/toolchain.h>
8#include <kernels/streamset.h>
9
10using namespace llvm;
11using namespace kernel;
12using namespace parabix;
13using namespace std;
14
15namespace kernel{
16
17    void LZ4DepositUncompressedKernel::generateDoSegmentMethod(const std::unique_ptr<KernelBuilder> &iBuilder) {
18        BasicBlock* exitBlock = iBuilder->CreateBasicBlock("exit_block");
19
20        BasicBlock* uncompressedDataLoopCon = iBuilder->CreateBasicBlock("uncompressed_data_loop_con");
21        BasicBlock* uncompressedDataLoopBody = iBuilder->CreateBasicBlock("uncompressed_data_loop_body");
22
23        iBuilder->CreateBr(uncompressedDataLoopCon);
24
25        // uncompressedDataLoopCon
26        iBuilder->SetInsertPoint(uncompressedDataLoopCon);
27        Value* uncompressedDataIndex = iBuilder->getScalarField("uncompressedDataIndex");
28        Value* availableBlockData = iBuilder->getAvailableItemCount("uncompressedStartPos");
29        iBuilder->CreateCondBr(
30                iBuilder->CreateICmpULT(uncompressedDataIndex, availableBlockData),
31                uncompressedDataLoopBody,
32                exitBlock);
33
34        // uncompressedDataLoopBody
35        iBuilder->SetInsertPoint(uncompressedDataLoopBody);
36        this->generateDepositUncompressed(iBuilder);
37        this->increaseCurrentUncompressedDataIndex(iBuilder);
38        iBuilder->CreateBr(uncompressedDataLoopCon);
39
40        // Exit
41        iBuilder->SetInsertPoint(exitBlock);
42
43    }
44    void LZ4DepositUncompressedKernel::generateDepositUncompressed(const std::unique_ptr<KernelBuilder> &iBuilder) {
45        Value* uncompressedStartPos = this->loadCurrentUncompressedData(iBuilder, "uncompressedStartPos");
46        Value* uncompressedLength = this->loadCurrentUncompressedData(iBuilder, "uncompressedLength");
47        Value* uncompressedOutputPos = this->loadCurrentUncompressedData(iBuilder, "uncompressedOutputPos");
48
49        Value* inputBufferBasePtr = iBuilder->getRawInputPointer("byteStream", iBuilder->getSize(0));
50        Value* outputBufferBasePtr = iBuilder->getRawOutputPointer("outputStream", iBuilder->getSize(0));
51
52
53        size_t outputBufferSize = this->getOutputStreamSetBuffer("outputStream")->getBufferBlocks() * iBuilder->getStride();
54        Value* outputBufferSizeValue = iBuilder->getSize(outputBufferSize);
55        Value* outputBufferSizeMask = iBuilder->getSize(outputBufferSize - 1);
56
57        Value* maskedOutputOffset = iBuilder->CreateAnd(uncompressedOutputPos, outputBufferSizeMask);
58        Value* remainBuffer = iBuilder->CreateSub(outputBufferSizeValue, maskedOutputOffset);
59        Value* copyLength1 = iBuilder->CreateSelect(iBuilder->CreateICmpUGE(remainBuffer, uncompressedLength), uncompressedLength, remainBuffer);
60        Value* copyLength2 = iBuilder->CreateSub(uncompressedLength, copyLength1);
61
62        iBuilder->CreateMemCpy(
63                iBuilder->CreateGEP(outputBufferBasePtr, maskedOutputOffset),
64                iBuilder->CreateGEP(inputBufferBasePtr, uncompressedStartPos),
65                copyLength1,
66                1); // no alignment guaranteed
67        // Assumed output buffer is Circular buffer
68        iBuilder->CreateMemCpy(
69                outputBufferBasePtr,
70                iBuilder->CreateGEP(inputBufferBasePtr, iBuilder->CreateAdd(uncompressedStartPos, copyLength1)),
71                copyLength2,
72                8
73        );
74
75        iBuilder->setProducedItemCount("outputStream", iBuilder->CreateAdd(uncompressedOutputPos, uncompressedLength));
76    }
77
78    void LZ4DepositUncompressedKernel::increaseCurrentUncompressedDataIndex(const std::unique_ptr<KernelBuilder> &iBuilder) {
79        Value* i = iBuilder->getScalarField("uncompressedDataIndex");
80        i = iBuilder->CreateAdd(i, iBuilder->getSize(1));
81        iBuilder->setScalarField("uncompressedDataIndex", i);
82    }
83
84    Value* LZ4DepositUncompressedKernel::loadCurrentUncompressedData(const unique_ptr<kernel::KernelBuilder> & iBuilder, const string& name) {
85        Value* blockDataIndex = iBuilder->getScalarField("uncompressedDataIndex");
86        return this->generateLoadCircularInput(iBuilder, name, blockDataIndex, iBuilder->getInt64Ty());
87    }
88
89    size_t LZ4DepositUncompressedKernel::getInputBufferSize(const unique_ptr<KernelBuilder> &iBuilder, string bufferName) {
90        return this->getInputStreamSetBuffer(bufferName)->getBufferBlocks() * iBuilder->getStride();
91    }
92
93    Value* LZ4DepositUncompressedKernel::generateLoadCircularInput(const unique_ptr<KernelBuilder> &iBuilder, string inputBufferName, Value* offset, Type* pointerType) {
94        size_t inputSize = this->getInputBufferSize(iBuilder, inputBufferName);
95        Value* offsetMask = iBuilder->getSize(inputSize - 1);
96        Value* maskedOffset = iBuilder->CreateAnd(offsetMask, offset);
97
98        Value* inputBufferPtr = iBuilder->getRawInputPointer(inputBufferName, iBuilder->getSize(0));
99
100        inputBufferPtr = iBuilder->CreatePointerCast(inputBufferPtr, pointerType);
101        return iBuilder->CreateLoad(iBuilder->CreateGEP(inputBufferPtr, maskedOffset));
102    }
103
104    LZ4DepositUncompressedKernel::LZ4DepositUncompressedKernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) :
105            SegmentOrientedKernel(
106                    "lz4_deposit_uncompressed_kernel",
107                    {//Inputs
108                            Binding{iBuilder->getStreamSetTy(1, 8), "byteStream"},
109
110                            // Uncompressed Data
111                            Binding{iBuilder->getStreamSetTy(1, 64), "uncompressedStartPos"},
112                            Binding{iBuilder->getStreamSetTy(1, 64), "uncompressedLength"},
113                            Binding{iBuilder->getStreamSetTy(1, 64), "uncompressedOutputPos"}
114                    },
115                    {//Outputs
116                            Binding{iBuilder->getStreamSetTy(1, 8), "outputStream", UnknownRate()}
117                    },
118                    {//Arguments
119
120                    },
121                    {},
122                    {//Internal States
123                            Binding{iBuilder->getSizeTy(), "uncompressedDataIndex"},
124                    }){
125//        setNoTerminateAttribute(true);
126    }
127}
128
Note: See TracBrowser for help on using the repository browser.