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

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

Add pablo.atEOF; clean out bit4/6 hack for unterminated final lines in icgrep.

File size: 9.7 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 * exitBlock = BasicBlock::Create(mMod->getContext(), "exit", main, 0);
105
106    Value * count = nullptr;
107    if (CountOnly) {
108        count = iBuilder->CreateAlloca(mBitBlockType, nullptr, "count");
109        iBuilder->CreateStore(ConstantInt::getNullValue(mBitBlockType), count);
110    }
111
112    Instance * s2pInstance = mS2PKernel->instantiate(inputStream);
113    Instance * icGrepInstance = mICgrepKernel->instantiate(s2pInstance->getOutputStreamBuffer());
114    Instance * scanMatchInstance = nullptr;
115   
116    if (!CountOnly) {
117        scanMatchInstance = mScanMatchKernel->instantiate(icGrepInstance->getOutputStreamBuffer());
118        scanMatchInstance->setInternalState("FileBuf", iBuilder->CreateBitCast(inputStream, int8PtrTy));
119        scanMatchInstance->setInternalState("FileSize", bufferSize);
120        scanMatchInstance->setInternalState("FileIdx", fileIdx);
121    }
122    Value * initialBufferSize = nullptr;
123    BasicBlock * initialBlock = nullptr;
124
125    if (segmentSize > 1) {
126        iBuilder->CreateBr(segmentCondBlock);
127        iBuilder->SetInsertPoint(segmentCondBlock);
128        PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
129        remainingBytes->addIncoming(bufferSize, entryBlock);
130        Constant * const step = ConstantInt::get(int64ty, mBlockSize * segmentSize);
131        Value * segmentCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
132        iBuilder->CreateCondBr(segmentCondTest, fullCondBlock, segmentBodyBlock);
133        iBuilder->SetInsertPoint(segmentBodyBlock);
134        for (unsigned i = 0; i < segmentSize; ++i) {
135            s2pInstance->CreateDoBlockCall();
136        }
137        for (unsigned i = 0; i < segmentSize; ++i) {
138            Value * match = (icGrepInstance->getOutputStream(0, 0)); 
139            icGrepInstance->CreateDoBlockCall();
140            Value * temp = iBuilder->CreateLoad(match);
141            Value * matches = iBuilder->CreateBitCast(temp, iBuilder->getIntNTy(mBlockSize));
142            Value * popcount_for = generatePopcount(iBuilder, matches);
143            if(CountOnly){
144                Value * temp_count = iBuilder->CreateLoad(count);
145                Value * prev_count = iBuilder->CreateBitCast(temp_count, iBuilder->getIntNTy(mBlockSize));
146                Value * add_for = iBuilder->CreateAdd(prev_count, popcount_for);
147                iBuilder->CreateStore(add_for, count);
148            }
149        }
150        if (!CountOnly) {
151            for (unsigned i = 0; i < segmentSize; ++i) {
152                scanMatchInstance->CreateDoBlockCall();
153            }
154        }
155        remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), segmentBodyBlock);
156        iBuilder->CreateBr(segmentCondBlock);
157        initialBufferSize = remainingBytes;
158        initialBlock = segmentCondBlock;
159    } else {
160        initialBufferSize = bufferSize;
161        initialBlock = entryBlock;
162        iBuilder->CreateBr(fullCondBlock);
163    }
164
165    iBuilder->SetInsertPoint(fullCondBlock);
166    PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
167    remainingBytes->addIncoming(initialBufferSize, initialBlock);
168
169    Constant * const step = ConstantInt::get(int64ty, mBlockSize);
170    Value * fullCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
171    iBuilder->CreateCondBr(fullCondTest, finalBlock, fullBodyBlock);
172
173    iBuilder->SetInsertPoint(fullBodyBlock);
174    s2pInstance->CreateDoBlockCall();
175    icGrepInstance->CreateDoBlockCall();
176    if (CountOnly) {
177        Value * popcount = Cal_Count(icGrepInstance, iBuilder);
178        Value * temp_count = iBuilder->CreateLoad(count);
179        Value * add = iBuilder->CreateAdd(temp_count, popcount);
180        iBuilder->CreateStore(add, count);
181    } else {
182        scanMatchInstance->CreateDoBlockCall();
183    }
184
185    remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), fullBodyBlock);
186    iBuilder->CreateBr(fullCondBlock);
187
188    iBuilder->SetInsertPoint(finalBlock);
189    Value * emptyBlockCond = iBuilder->CreateICmpEQ(remainingBytes, ConstantInt::get(int64ty, 0));
190    iBuilder->CreateCondBr(emptyBlockCond, finalEmptyBlock, finalPartialBlock);
191
192
193    iBuilder->SetInsertPoint(finalPartialBlock);
194    s2pInstance->CreateDoBlockCall();
195    iBuilder->CreateBr(exitBlock);
196
197    iBuilder->SetInsertPoint(finalEmptyBlock);
198    s2pInstance->clearOutputStreamSet();
199    iBuilder->CreateBr(exitBlock);
200
201    iBuilder->SetInsertPoint(exitBlock);
202
203    Value * remaining = iBuilder->CreateZExt(remainingBytes, iBuilder->getIntNTy(mBlockSize));
204    Value * EOFmark = iBuilder->CreateShl(ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1), remaining);
205        icGrepInstance->setInternalState("EOFmark", iBuilder->CreateBitCast(EOFmark, mBitBlockType));
206
207    icGrepInstance->CreateDoBlockCall();
208    if (CountOnly) {
209        Value * popcount1 = Cal_Count(icGrepInstance, iBuilder);
210        Value * temp_count1 = iBuilder->CreateLoad(count);
211        Value * result = iBuilder->CreateAdd(temp_count1, popcount1);
212        for (unsigned width = (mBlockSize / 64); width > 1; width /= 2) {
213            std::vector<Constant *> mask(width / 2);
214            for (unsigned i = 0; i < (width / 2); ++i) {
215                mask[i] = iBuilder->getInt32(i);
216            }
217            Value * const undef = UndefValue::get(VectorType::get(int64ty, width));
218            Value * const lh = iBuilder->CreateShuffleVector(result, undef, ConstantVector::get(mask));
219            for (unsigned i = 0; i < (width / 2); ++i) {
220                mask[i] = iBuilder->getInt32(i + (width / 2));
221            }
222            Value * const rh = iBuilder->CreateShuffleVector(result, undef, ConstantVector::get(mask));
223            result = iBuilder->CreateAdd(lh, rh);
224        }
225        iBuilder->CreateRet(iBuilder->CreateExtractElement(result, iBuilder->getInt32(0)));
226    } else {
227        scanMatchInstance->CreateDoBlockCall();
228        iBuilder->CreateRetVoid();
229    }
230    return main;
231}
Note: See TracBrowser for help on using the repository browser.