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

Last change on this file since 6047 was 6047, checked in by nmedfort, 15 months ago

Major refactoring of buffer types. Static buffers replace Circular and CircularCopyback?. External buffers unify Source/External?.

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