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

Last change on this file since 5037 was 5037, checked in by nmedfort, 3 years ago

UnicodeSet? bug fix and compile warning clean-up.

File size: 10.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 * 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            icGrepInstance->CreateDoBlockCall();
141            if (CountOnly) {
142                Value * popcount_for = Cal_Count(icGrepInstance, iBuilder);
143                Value * temp_countfor = iBuilder->CreateLoad(count);
144                Value * add_for = iBuilder->CreateAdd(temp_countfor, popcount_for);
145                iBuilder->CreateStore(add_for, count);
146            }
147        }
148        if (!CountOnly) {
149            for (unsigned i = 0; i < segmentSize; ++i) {
150                scanMatchInstance->CreateDoBlockCall();
151            }
152        }
153        remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), segmentBodyBlock);
154        iBuilder->CreateBr(segmentCondBlock);
155        initialBufferSize = remainingBytes;
156        initialBlock = segmentCondBlock;
157    } else {
158        initialBufferSize = bufferSize;
159        initialBlock = entryBlock;
160        iBuilder->CreateBr(fullCondBlock);
161    }
162
163    iBuilder->SetInsertPoint(fullCondBlock);
164    PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
165    remainingBytes->addIncoming(initialBufferSize, initialBlock);
166
167    Constant * const step = ConstantInt::get(int64ty, mBlockSize);
168    Value * fullCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
169    iBuilder->CreateCondBr(fullCondTest, finalBlock, fullBodyBlock);
170
171    iBuilder->SetInsertPoint(fullBodyBlock);
172    s2pInstance->CreateDoBlockCall();
173    icGrepInstance->CreateDoBlockCall();
174    if (CountOnly) {
175        Value * popcount = Cal_Count(icGrepInstance, iBuilder);
176        Value * temp_count = iBuilder->CreateLoad(count);
177        Value * add = iBuilder->CreateAdd(temp_count, popcount);
178        iBuilder->CreateStore(add, count);
179    } else {
180        scanMatchInstance->CreateDoBlockCall();
181    }
182
183    remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), fullBodyBlock);
184    iBuilder->CreateBr(fullCondBlock);
185
186    iBuilder->SetInsertPoint(finalBlock);
187    Value * const b4 = s2pInstance->getOutputStream(4);
188    Value * const b6 = s2pInstance->getOutputStream(6);
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(endBlock);
196
197    iBuilder->SetInsertPoint(finalEmptyBlock);
198    s2pInstance->clearOutputStreamSet();
199    iBuilder->CreateBr(endBlock);
200
201    iBuilder->SetInsertPoint(endBlock);
202    Value * isFinalLineUnterminated = iBuilder->CreateICmpEQ(finalLineUnterminated, ConstantInt::getNullValue(finalLineUnterminated->getType()));
203    iBuilder->CreateCondBr(isFinalLineUnterminated, exitBlock, unterminatedBlock);
204   
205    iBuilder->SetInsertPoint(unterminatedBlock);
206
207    Value * remaining = iBuilder->CreateZExt(remainingBytes, iBuilder->getIntNTy(mBlockSize));
208    Value * EOF_pos = iBuilder->CreateShl(ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1), remaining);
209    EOF_pos = iBuilder->CreateBitCast(EOF_pos, mBitBlockType);
210    Value * EOF_mask = iBuilder->CreateShl(Constant::getAllOnesValue(iBuilder->getIntNTy(mBlockSize)), remaining);
211        icGrepInstance->setInternalState("EOFmask", iBuilder->CreateBitCast(EOF_mask, mBitBlockType));
212
213
214    Value * b4val = iBuilder->CreateBlockAlignedLoad(b4);
215    b4val = iBuilder->CreateOr(b4val, EOF_pos);
216    iBuilder->CreateBlockAlignedStore(b4val, b4);
217
218    Value * b6val = iBuilder->CreateBlockAlignedLoad(b6);
219    b6val = iBuilder->CreateOr(b6val, EOF_pos);
220    iBuilder->CreateBlockAlignedStore(b6val, b6);
221
222    iBuilder->CreateBr(exitBlock);
223
224    iBuilder->SetInsertPoint(exitBlock);
225
226    icGrepInstance->CreateDoBlockCall();
227    if (CountOnly) {
228        Value * popcount1 = Cal_Count(icGrepInstance, iBuilder);
229        Value * temp_count1 = iBuilder->CreateLoad(count);
230        Value * result = iBuilder->CreateAdd(temp_count1, popcount1);
231        for (unsigned width = (mBlockSize / 64); width > 1; width /= 2) {
232            std::vector<Constant *> mask(width / 2);
233            for (unsigned i = 0; i < (width / 2); ++i) {
234                mask[i] = iBuilder->getInt32(i);
235            }
236            Value * const undef = UndefValue::get(VectorType::get(int64ty, width));
237            Value * const lh = iBuilder->CreateShuffleVector(result, undef, ConstantVector::get(mask));
238            for (unsigned i = 0; i < (width / 2); ++i) {
239                mask[i] = iBuilder->getInt32(i + (width / 2));
240            }
241            Value * const rh = iBuilder->CreateShuffleVector(result, undef, ConstantVector::get(mask));
242            result = iBuilder->CreateAdd(lh, rh);
243        }
244        iBuilder->CreateRet(iBuilder->CreateExtractElement(result, iBuilder->getInt32(0)));
245    } else {
246        scanMatchInstance->CreateDoBlockCall();
247        iBuilder->CreateRetVoid();
248    }
249    return main;
250}
Note: See TracBrowser for help on using the repository browser.