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

Last change on this file since 6020 was 6020, checked in by xwa163, 6 weeks ago
  1. New version of lz4_swizzled_match_copy kernel with higher performance
  2. Adjust related pipeline code
  3. Remove legacy comments
File size: 6.1 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
50
51        size_t outputBufferSize = this->getOutputStreamSetBuffer("outputStream")->getBufferBlocks() * iBuilder->getStride();
52        Value* outputBufferSizeValue = iBuilder->getSize(outputBufferSize);
53        Value* outputBufferSizeMask = iBuilder->getSize(outputBufferSize - 1);
54
55        Value* maskedOutputOffset = iBuilder->CreateAnd(uncompressedOutputPos, outputBufferSizeMask);
56        Value* remainBuffer = iBuilder->CreateSub(outputBufferSizeValue, maskedOutputOffset);
57        Value* copyLength1 = iBuilder->CreateSelect(iBuilder->CreateICmpUGE(remainBuffer, uncompressedLength), uncompressedLength, remainBuffer);
58        Value* copyLength2 = iBuilder->CreateSub(uncompressedLength, copyLength1);
59
60        iBuilder->CreateMemCpy(
61                iBuilder->CreateGEP(outputBufferBasePtr, maskedOutputOffset),
62                iBuilder->CreateGEP(inputBufferBasePtr, uncompressedStartPos),
63                copyLength1,
64                1); // no alignment guaranteed
65        // Assumed output buffer is Circular buffer
66        iBuilder->CreateMemCpy(
67                outputBufferBasePtr,
68                iBuilder->CreateGEP(inputBufferBasePtr, iBuilder->CreateAdd(uncompressedStartPos, copyLength1)),
69                copyLength2,
70                8
71        );
72
73        iBuilder->setProducedItemCount("outputStream", iBuilder->CreateAdd(uncompressedOutputPos, uncompressedLength));
74    }
75
76    void LZ4DepositUncompressedKernel::increaseCurrentUncompressedDataIndex(const std::unique_ptr<KernelBuilder> &iBuilder) {
77        Value* i = iBuilder->getScalarField("uncompressedDataIndex");
78        i = iBuilder->CreateAdd(i, iBuilder->getSize(1));
79        iBuilder->setScalarField("uncompressedDataIndex", i);
80    }
81
82    Value* LZ4DepositUncompressedKernel::loadCurrentUncompressedData(const unique_ptr<kernel::KernelBuilder> & iBuilder, const string& name) {
83        Value* blockDataIndex = iBuilder->getScalarField("uncompressedDataIndex");
84        return this->generateLoadCircularInput(iBuilder, name, blockDataIndex, iBuilder->getInt64Ty());
85    }
86
87    size_t LZ4DepositUncompressedKernel::getInputBufferSize(const unique_ptr<KernelBuilder> &iBuilder, string bufferName) {
88        return this->getInputStreamSetBuffer(bufferName)->getBufferBlocks() * iBuilder->getStride();
89    }
90
91    Value* LZ4DepositUncompressedKernel::generateLoadCircularInput(const unique_ptr<KernelBuilder> &iBuilder, string inputBufferName, Value* offset, Type* pointerType) {
92        size_t inputSize = this->getInputBufferSize(iBuilder, inputBufferName);
93        Value* offsetMask = iBuilder->getSize(inputSize - 1);
94        Value* maskedOffset = iBuilder->CreateAnd(offsetMask, offset);
95
96        Value* inputBufferPtr = iBuilder->getRawInputPointer(inputBufferName, iBuilder->getSize(0));
97
98        inputBufferPtr = iBuilder->CreatePointerCast(inputBufferPtr, pointerType);
99        return iBuilder->CreateLoad(iBuilder->CreateGEP(inputBufferPtr, maskedOffset));
100    }
101
102    LZ4DepositUncompressedKernel::LZ4DepositUncompressedKernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) :
103            SegmentOrientedKernel(
104                    "lz4_deposit_uncompressed_kernel",
105                    {//Inputs
106                            Binding{iBuilder->getStreamSetTy(1, 8), "byteStream"},
107
108                            // Uncompressed Data
109                            Binding{iBuilder->getStreamSetTy(1, 64), "uncompressedStartPos"},
110                            Binding{iBuilder->getStreamSetTy(1, 64), "uncompressedLength"},
111                            Binding{iBuilder->getStreamSetTy(1, 64), "uncompressedOutputPos"}
112                    },
113                    {//Outputs
114                            Binding{iBuilder->getStreamSetTy(1, 8), "outputStream", UnknownRate()}
115                    },
116                    {//Arguments
117
118                    },
119                    {},
120                    {//Internal States
121                            Binding{iBuilder->getSizeTy(), "uncompressedDataIndex"},
122                    }){
123//        setNoTerminateAttribute(true);
124    }
125}
126
Note: See TracBrowser for help on using the repository browser.