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

Last change on this file since 5038 was 5038, checked in by xuedongx, 3 years ago

Bug fixes for '-c'

File size: 10.9 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 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    generateS2PKernel(mMod, iBuilder, mS2PKernel);
43    generateScanMatch(mMod, iBuilder, 64, mScanMatchKernel, isNameExpression);
44    pablo_function_passes(function);
45    PabloCompiler pablo_compiler(mMod, iBuilder);
46    try {
47        pablo_compiler.setKernel(mICgrepKernel);
48        pablo_compiler.compile(function);
49        delete function;
50        releaseSlabAllocatorMemory();
51    } catch (std::runtime_error e) {
52        delete function;
53        releaseSlabAllocatorMemory();
54        std::cerr << "Runtime error: " << e.what() << std::endl;
55        exit(1);
56    }
57}
58
59inline Value * generatePopcount(IDISA::IDISA_Builder * iBuilder, Value * bits) {
60    Value * ctpopFunc = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::ctpop, bits->getType());
61    return iBuilder->CreateCall(ctpopFunc, {bits});
62}
63
64inline Value * Cal_Count(Instance * icGrepInstance, IDISA::IDISA_Builder * iBuilder) {
65    Value * match = icGrepInstance->getOutputStream(0, 0);
66    Value * matches = iBuilder->CreateLoad(match, false, "match");
67    return generatePopcount(iBuilder, matches);
68}
69
70Function * PipelineBuilder::ExecuteKernels(bool CountOnly) {
71    Type * const int64ty = iBuilder->getInt64Ty();
72    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
73    Type * const inputType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), std::vector<Type *>({ArrayType::get(mBitBlockType, 8)})), 1), 0);
74    Type * const resultTy = CountOnly ? int64ty : iBuilder->getVoidTy();
75    Function * const main = cast<Function>(mMod->getOrInsertFunction("Main", resultTy, inputType, int64ty, int64ty, iBuilder->getInt1Ty(), nullptr));
76    main->setCallingConv(CallingConv::C);
77    Function::arg_iterator args = main->arg_begin();
78
79    Value * const inputStream = &*(args++);
80    inputStream->setName("input");
81    Value * const bufferSize = &*(args++);
82    bufferSize->setName("bufferSize");
83    Value * const fileIdx = &*(args++);
84    fileIdx->setName("fileIdx");
85    Value * const finalLineUnterminated = &*(args++);
86    finalLineUnterminated->setName("finalLineUnterminated");
87
88    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", main,0));
89
90
91    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
92    BasicBlock * segmentCondBlock = nullptr;
93    BasicBlock * segmentBodyBlock = nullptr;
94    const unsigned segmentSize = codegen::SegmentSize;
95    if (segmentSize > 1) {
96        segmentCondBlock = BasicBlock::Create(mMod->getContext(), "segmentCond", main, 0);
97        segmentBodyBlock = BasicBlock::Create(mMod->getContext(), "segmentBody", main, 0);
98    }
99    BasicBlock * fullCondBlock = BasicBlock::Create(mMod->getContext(), "fullCond", main, 0);
100    BasicBlock * fullBodyBlock = BasicBlock::Create(mMod->getContext(), "fullBody", main, 0);
101    BasicBlock * finalBlock = BasicBlock::Create(mMod->getContext(), "final", main, 0);
102    BasicBlock * finalPartialBlock = BasicBlock::Create(mMod->getContext(), "partial", main, 0);
103    BasicBlock * finalEmptyBlock = BasicBlock::Create(mMod->getContext(), "empty", main, 0);
104    BasicBlock * endBlock = BasicBlock::Create(mMod->getContext(), "end", main, 0);
105    BasicBlock * unterminatedBlock = BasicBlock::Create(mMod->getContext(), "unterminated", main, 0);
106    BasicBlock * exitBlock = BasicBlock::Create(mMod->getContext(), "exit", main, 0);
107
108    Value * count = nullptr;
109    if (CountOnly) {
110        count = iBuilder->CreateAlloca(mBitBlockType, nullptr, "count");
111        iBuilder->CreateStore(ConstantInt::getNullValue(mBitBlockType), count);
112    }
113
114    Instance * s2pInstance = mS2PKernel->instantiate(inputStream);
115    Instance * icGrepInstance = mICgrepKernel->instantiate(s2pInstance->getOutputStreamBuffer());
116    Instance * scanMatchInstance = nullptr;
117   
118    if (!CountOnly) {
119        scanMatchInstance = mScanMatchKernel->instantiate(icGrepInstance->getOutputStreamBuffer());
120        scanMatchInstance->setInternalState("FileBuf", iBuilder->CreateBitCast(inputStream, int8PtrTy));
121        scanMatchInstance->setInternalState("FileSize", bufferSize);
122        scanMatchInstance->setInternalState("FileIdx", fileIdx);
123    }
124    Value * initialBufferSize = nullptr;
125    BasicBlock * initialBlock = nullptr;
126
127    if (segmentSize > 1) {
128        iBuilder->CreateBr(segmentCondBlock);
129        iBuilder->SetInsertPoint(segmentCondBlock);
130        PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
131        remainingBytes->addIncoming(bufferSize, entryBlock);
132        Constant * const step = ConstantInt::get(int64ty, mBlockSize * segmentSize);
133        Value * segmentCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
134        iBuilder->CreateCondBr(segmentCondTest, fullCondBlock, segmentBodyBlock);
135        iBuilder->SetInsertPoint(segmentBodyBlock);
136        for (unsigned i = 0; i < segmentSize; ++i) {
137            s2pInstance->CreateDoBlockCall();
138        }
139        for (unsigned i = 0; i < segmentSize; ++i) {
140            Value * match = (icGrepInstance->getOutputStream(0, 0)); 
141            icGrepInstance->CreateDoBlockCall();
142            Value * temp = iBuilder->CreateLoad(match);
143            Value * matches = iBuilder->CreateBitCast(temp, iBuilder->getIntNTy(mBlockSize));
144            Value * popcount_for = generatePopcount(iBuilder, matches);
145            if(CountOnly){
146                Value * temp_count = iBuilder->CreateLoad(count);
147                Value * prev_count = iBuilder->CreateBitCast(temp_count, iBuilder->getIntNTy(mBlockSize));
148                Value * add_for = iBuilder->CreateAdd(prev_count, popcount_for);
149                iBuilder->CreateStore(add_for, count);
150            }
151        }
152        if (!CountOnly) {
153            for (unsigned i = 0; i < segmentSize; ++i) {
154                scanMatchInstance->CreateDoBlockCall();
155            }
156        }
157        remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), segmentBodyBlock);
158        iBuilder->CreateBr(segmentCondBlock);
159        initialBufferSize = remainingBytes;
160        initialBlock = segmentCondBlock;
161    } else {
162        initialBufferSize = bufferSize;
163        initialBlock = entryBlock;
164        iBuilder->CreateBr(fullCondBlock);
165    }
166
167    iBuilder->SetInsertPoint(fullCondBlock);
168    PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
169    remainingBytes->addIncoming(initialBufferSize, initialBlock);
170
171    Constant * const step = ConstantInt::get(int64ty, mBlockSize);
172    Value * fullCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
173    iBuilder->CreateCondBr(fullCondTest, finalBlock, fullBodyBlock);
174
175    iBuilder->SetInsertPoint(fullBodyBlock);
176    s2pInstance->CreateDoBlockCall();
177    icGrepInstance->CreateDoBlockCall();
178    if (CountOnly) {
179        Value * popcount = Cal_Count(icGrepInstance, iBuilder);
180        Value * temp_count = iBuilder->CreateLoad(count);
181        Value * add = iBuilder->CreateAdd(temp_count, popcount);
182        iBuilder->CreateStore(add, count);
183    } else {
184        scanMatchInstance->CreateDoBlockCall();
185    }
186
187    remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), fullBodyBlock);
188    iBuilder->CreateBr(fullCondBlock);
189
190    iBuilder->SetInsertPoint(finalBlock);
191    Value * const b4 = s2pInstance->getOutputStream(4);
192    Value * const b6 = s2pInstance->getOutputStream(6);
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(endBlock);
200
201    iBuilder->SetInsertPoint(finalEmptyBlock);
202    s2pInstance->clearOutputStreamSet();
203    iBuilder->CreateBr(endBlock);
204
205    iBuilder->SetInsertPoint(endBlock);
206    Value * isFinalLineUnterminated = iBuilder->CreateICmpEQ(finalLineUnterminated, ConstantInt::getNullValue(finalLineUnterminated->getType()));
207    iBuilder->CreateCondBr(isFinalLineUnterminated, exitBlock, unterminatedBlock);
208   
209    iBuilder->SetInsertPoint(unterminatedBlock);
210
211    Value * remaining = iBuilder->CreateZExt(remainingBytes, iBuilder->getIntNTy(mBlockSize));
212    Value * EOF_pos = iBuilder->CreateShl(ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1), remaining);
213    EOF_pos = iBuilder->CreateBitCast(EOF_pos, mBitBlockType);
214    Value * EOF_mask = iBuilder->CreateShl(Constant::getAllOnesValue(iBuilder->getIntNTy(mBlockSize)), remaining);
215        icGrepInstance->setInternalState("EOFmask", iBuilder->CreateBitCast(EOF_mask, mBitBlockType));
216
217
218    Value * b4val = iBuilder->CreateBlockAlignedLoad(b4);
219    b4val = iBuilder->CreateOr(b4val, EOF_pos);
220    iBuilder->CreateBlockAlignedStore(b4val, b4);
221
222    Value * b6val = iBuilder->CreateBlockAlignedLoad(b6);
223    b6val = iBuilder->CreateOr(b6val, EOF_pos);
224    iBuilder->CreateBlockAlignedStore(b6val, b6);
225
226    iBuilder->CreateBr(exitBlock);
227
228    iBuilder->SetInsertPoint(exitBlock);
229
230    icGrepInstance->CreateDoBlockCall();
231    if (CountOnly) {
232        Value * popcount1 = Cal_Count(icGrepInstance, iBuilder);
233        Value * temp_count1 = iBuilder->CreateLoad(count);
234        Value * result = iBuilder->CreateAdd(temp_count1, popcount1);
235        for (unsigned width = (mBlockSize / 64); width > 1; width /= 2) {
236            std::vector<Constant *> mask(width / 2);
237            for (unsigned i = 0; i < (width / 2); ++i) {
238                mask[i] = iBuilder->getInt32(i);
239            }
240            Value * const undef = UndefValue::get(VectorType::get(int64ty, width));
241            Value * const lh = iBuilder->CreateShuffleVector(result, undef, ConstantVector::get(mask));
242            for (unsigned i = 0; i < (width / 2); ++i) {
243                mask[i] = iBuilder->getInt32(i + (width / 2));
244            }
245            Value * const rh = iBuilder->CreateShuffleVector(result, undef, ConstantVector::get(mask));
246            result = iBuilder->CreateAdd(lh, rh);
247        }
248        iBuilder->CreateRet(iBuilder->CreateExtractElement(result, iBuilder->getInt32(0)));
249    } else {
250        scanMatchInstance->CreateDoBlockCall();
251        iBuilder->CreateRetVoid();
252    }
253    return main;
254}
Note: See TracBrowser for help on using the repository browser.