source: icGREP/icgrep-devel/icgrep/kernels/pipeline.cpp @ 5052

Last change on this file since 5052 was 5052, checked in by cameron, 3 years ago

Revert test version of pipeline,...

File size: 10.0 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5
6
7#include <toolchain.h>
8#include "pipeline.h"
9#include "utf_encoding.h"
10
11#include <kernels/scanmatchgen.h>
12#include <kernels/s2p_kernel.h>
13#include <kernels/instance.h>
14
15#include <pablo/function.h>
16#include <pablo/pablo_compiler.h>
17#include <pablo/pablo_toolchain.h>
18
19#include <llvm/IR/Intrinsics.h>
20
21using namespace pablo;
22using namespace kernel;
23
24PipelineBuilder::PipelineBuilder(Module * m, IDISA::IDISA_Builder * b)
25: mMod(m)
26, iBuilder(b)
27, mBitBlockType(b->getBitBlockType())
28, mBlockSize(b->getBitBlockWidth()) {
29
30}
31
32PipelineBuilder::~PipelineBuilder() {
33    delete mS2PKernel;
34    delete mICgrepKernel;
35    delete mScanMatchKernel;
36}
37
38void PipelineBuilder::CreateKernels(PabloFunction * function, bool UTF_16, bool isNameExpression){
39    mS2PKernel = new KernelBuilder(iBuilder, "s2p", codegen::SegmentSize);
40    mICgrepKernel = new KernelBuilder(iBuilder, "icgrep", codegen::SegmentSize);
41    mScanMatchKernel = new KernelBuilder(iBuilder, "scanMatch", codegen::SegmentSize);
42    if (UTF_16) {
43        generateS2P_16Kernel(mMod, iBuilder, mS2PKernel);
44    }
45    else {
46        generateS2PKernel(mMod, iBuilder, mS2PKernel);
47    }
48    generateScanMatch(mMod, iBuilder, 64, mScanMatchKernel, isNameExpression);
49    pablo_function_passes(function);
50    PabloCompiler pablo_compiler(mMod, iBuilder);
51    try {
52        pablo_compiler.setKernel(mICgrepKernel);
53        pablo_compiler.compile(function);
54        delete function;
55        releaseSlabAllocatorMemory();
56    } catch (std::runtime_error e) {
57        delete function;
58        releaseSlabAllocatorMemory();
59        std::cerr << "Runtime error: " << e.what() << std::endl;
60        exit(1);
61    }
62}
63
64inline Value * generatePopcount(IDISA::IDISA_Builder * iBuilder, Value * bits) {
65    Value * ctpopFunc = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::ctpop, bits->getType());
66    return iBuilder->CreateCall(ctpopFunc, {bits});
67}
68
69inline Value * Cal_Count(Instance * icGrepInstance, IDISA::IDISA_Builder * iBuilder) {
70    Value * match = icGrepInstance->getOutputStream(0, 0);
71    Value * matches = iBuilder->CreateLoad(match, false, "match");
72    return generatePopcount(iBuilder, matches);
73}
74
75Function * PipelineBuilder::ExecuteKernels(bool CountOnly, bool UTF_16) {
76    Type * const int64ty = iBuilder->getInt64Ty();
77    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
78    Type * const inputType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), std::vector<Type *>({ArrayType::get(mBitBlockType, (UTF_16 ? 16 : 8))})), 1), 0);
79    Type * const resultTy = CountOnly ? int64ty : iBuilder->getVoidTy();
80    Function * const main = cast<Function>(mMod->getOrInsertFunction("Main", resultTy, inputType, int64ty, int64ty, nullptr));
81    main->setCallingConv(CallingConv::C);
82    Function::arg_iterator args = main->arg_begin();
83
84    Value * const inputStream = &*(args++);
85    inputStream->setName("input");
86    Value * const bufferSize = &*(args++);
87    bufferSize->setName("bufferSize");
88    Value * const fileIdx = &*(args++);
89    fileIdx->setName("fileIdx");
90
91    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", main,0));
92
93
94    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
95    BasicBlock * segmentCondBlock = nullptr;
96    BasicBlock * segmentBodyBlock = nullptr;
97    const unsigned segmentSize = codegen::SegmentSize;
98    if (segmentSize > 1) {
99        segmentCondBlock = BasicBlock::Create(mMod->getContext(), "segmentCond", main, 0);
100        segmentBodyBlock = BasicBlock::Create(mMod->getContext(), "segmentBody", main, 0);
101    }
102    BasicBlock * fullCondBlock = BasicBlock::Create(mMod->getContext(), "fullCond", main, 0);
103    BasicBlock * fullBodyBlock = BasicBlock::Create(mMod->getContext(), "fullBody", main, 0);
104    BasicBlock * finalBlock = BasicBlock::Create(mMod->getContext(), "final", main, 0);
105    BasicBlock * finalPartialBlock = BasicBlock::Create(mMod->getContext(), "partial", main, 0);
106    BasicBlock * finalEmptyBlock = BasicBlock::Create(mMod->getContext(), "empty", main, 0);
107    BasicBlock * exitBlock = BasicBlock::Create(mMod->getContext(), "exit", main, 0);
108
109    Value * count = nullptr;
110    if (CountOnly) {
111        count = iBuilder->CreateAlloca(mBitBlockType, nullptr, "count");
112        iBuilder->CreateStore(ConstantInt::getNullValue(mBitBlockType), count);
113    }
114
115    Instance * s2pInstance = mS2PKernel->instantiate(inputStream);
116    Instance * icGrepInstance = mICgrepKernel->instantiate(s2pInstance->getOutputStreamBuffer());
117    Instance * scanMatchInstance = nullptr;
118   
119    if (!CountOnly) {
120        scanMatchInstance = mScanMatchKernel->instantiate(icGrepInstance->getOutputStreamBuffer());
121        scanMatchInstance->setInternalState("FileBuf", iBuilder->CreateBitCast(inputStream, int8PtrTy));
122        scanMatchInstance->setInternalState("FileSize", bufferSize);
123        scanMatchInstance->setInternalState("FileIdx", fileIdx);
124    }
125    Value * initialBufferSize = nullptr;
126    BasicBlock * initialBlock = nullptr;
127
128    if (segmentSize > 1) {
129        iBuilder->CreateBr(segmentCondBlock);
130        iBuilder->SetInsertPoint(segmentCondBlock);
131        PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
132        remainingBytes->addIncoming(bufferSize, entryBlock);
133        Constant * const step = ConstantInt::get(int64ty, mBlockSize * segmentSize * (UTF_16 ? 2 : 1));
134        Value * segmentCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
135        iBuilder->CreateCondBr(segmentCondTest, fullCondBlock, segmentBodyBlock);
136        iBuilder->SetInsertPoint(segmentBodyBlock);
137        for (unsigned i = 0; i < segmentSize; ++i) {
138            s2pInstance->CreateDoBlockCall();
139        }
140        for (unsigned i = 0; i < segmentSize; ++i) {
141            Value * match = (icGrepInstance->getOutputStream(0, 0)); 
142            icGrepInstance->CreateDoBlockCall();
143            Value * temp = iBuilder->CreateLoad(match);
144            Value * matches = iBuilder->CreateBitCast(temp, iBuilder->getIntNTy(mBlockSize));
145            Value * popcount_for = generatePopcount(iBuilder, matches);
146            if(CountOnly){
147                Value * temp_count = iBuilder->CreateLoad(count);
148                Value * prev_count = iBuilder->CreateBitCast(temp_count, iBuilder->getIntNTy(mBlockSize));
149                Value * add_for = iBuilder->CreateAdd(prev_count, popcount_for);
150                Value * add = iBuilder->CreateBitCast(add_for, mBitBlockType);
151                iBuilder->CreateStore(add, count);
152            }
153        }
154        if (!CountOnly) {
155            for (unsigned i = 0; i < segmentSize; ++i) {
156                scanMatchInstance->CreateDoBlockCall();
157            }
158        }
159        remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), segmentBodyBlock);
160        iBuilder->CreateBr(segmentCondBlock);
161        initialBufferSize = remainingBytes;
162        initialBlock = segmentCondBlock;
163    } else {
164        initialBufferSize = bufferSize;
165        initialBlock = entryBlock;
166        iBuilder->CreateBr(fullCondBlock);
167    }
168
169    iBuilder->SetInsertPoint(fullCondBlock);
170    PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
171    remainingBytes->addIncoming(initialBufferSize, initialBlock);
172
173    Constant * const step = ConstantInt::get(int64ty, mBlockSize * (UTF_16 ? 2 : 1));
174    Value * fullCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
175    iBuilder->CreateCondBr(fullCondTest, finalBlock, fullBodyBlock);
176
177    iBuilder->SetInsertPoint(fullBodyBlock);
178    s2pInstance->CreateDoBlockCall();
179    icGrepInstance->CreateDoBlockCall();
180    if (CountOnly) {
181        Value * popcount = Cal_Count(icGrepInstance, iBuilder);
182        Value * temp_count = iBuilder->CreateLoad(count);
183        Value * add = iBuilder->CreateAdd(temp_count, popcount);
184        iBuilder->CreateStore(add, count);
185    } else {
186        scanMatchInstance->CreateDoBlockCall();
187    }
188
189    remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), fullBodyBlock);
190    iBuilder->CreateBr(fullCondBlock);
191
192    iBuilder->SetInsertPoint(finalBlock);
193    Value * emptyBlockCond = iBuilder->CreateICmpEQ(remainingBytes, ConstantInt::get(int64ty, 0));
194    iBuilder->CreateCondBr(emptyBlockCond, finalEmptyBlock, finalPartialBlock);
195
196
197    iBuilder->SetInsertPoint(finalPartialBlock);
198    s2pInstance->CreateDoBlockCall();
199    iBuilder->CreateBr(exitBlock);
200
201    iBuilder->SetInsertPoint(finalEmptyBlock);
202    s2pInstance->clearOutputStreamSet();
203    iBuilder->CreateBr(exitBlock);
204
205    iBuilder->SetInsertPoint(exitBlock);
206
207    Value * remainingByte = iBuilder->CreateZExt(remainingBytes, iBuilder->getIntNTy(mBlockSize));
208    Value * remainingUnit = iBuilder->CreateLShr(remainingByte, ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1));
209    Value * EOFmark = iBuilder->CreateShl(ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1), UTF_16 ? remainingUnit : remainingByte);
210    icGrepInstance->setInternalState("EOFmark", iBuilder->CreateBitCast(EOFmark, mBitBlockType));
211
212    icGrepInstance->CreateDoBlockCall();
213    if (CountOnly) {
214        Value * popcount1 = Cal_Count(icGrepInstance, iBuilder);
215        Value * temp_count1 = iBuilder->CreateLoad(count);
216        Value * result = iBuilder->CreateAdd(temp_count1, popcount1);
217        for (unsigned width = (mBlockSize / 64); width > 1; width /= 2) {
218            std::vector<Constant *> mask(width / 2);
219            for (unsigned i = 0; i < (width / 2); ++i) {
220                mask[i] = iBuilder->getInt32(i);
221            }
222            Value * const undef = UndefValue::get(VectorType::get(int64ty, width));
223            Value * const lh = iBuilder->CreateShuffleVector(result, undef, ConstantVector::get(mask));
224            for (unsigned i = 0; i < (width / 2); ++i) {
225                mask[i] = iBuilder->getInt32(i + (width / 2));
226            }
227            Value * const rh = iBuilder->CreateShuffleVector(result, undef, ConstantVector::get(mask));
228            result = iBuilder->CreateAdd(lh, rh);
229        }
230        iBuilder->CreateRet(iBuilder->CreateExtractElement(result, iBuilder->getInt32(0)));
231    } else {
232        scanMatchInstance->CreateDoBlockCall();
233        iBuilder->CreateRetVoid();
234    }
235    return main;
236}
Note: See TracBrowser for help on using the repository browser.