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

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

Fix for u8u16

File size: 8.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#include "pipeline.h"
7#include "utf_encoding.h"
8
9#include <kernels/scanmatchgen.h>
10#include <kernels/s2p_kernel.h>
11#include <kernels/instance.h>
12
13#include <pablo/function.h>
14#include <pablo/pablo_compiler.h>
15#include <pablo/pablo_toolchain.h>
16
17#include <llvm/Support/CommandLine.h>
18
19static cl::opt<unsigned> SegmentSize("segment-size", cl::desc("Segment Size"), cl::value_desc("positive integer"), cl::init(1));
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", SegmentSize);
40    mICgrepKernel = new KernelBuilder(iBuilder, "icgrep", SegmentSize);
41    mScanMatchKernel = new KernelBuilder(iBuilder, "scanMatch", 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
59Function * PipelineBuilder::ExecuteKernels() {
60    Type * const int64ty = iBuilder->getInt64Ty();
61    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
62    Type * const inputType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), std::vector<Type *>({ArrayType::get(mBitBlockType, 8)})), 1), 0);
63
64    Function * const main = cast<Function>(mMod->getOrInsertFunction("Main", Type::getVoidTy(mMod->getContext()), inputType, int64ty, int8PtrTy, iBuilder->getInt1Ty(), nullptr));
65    main->setCallingConv(CallingConv::C);
66    Function::arg_iterator args = main->arg_begin();
67
68    Value * const inputStream = args++;
69    inputStream->setName("input");
70    Value * const bufferSize = args++;
71    bufferSize->setName("bufferSize");
72    Value * const fileName = args++;
73    fileName->setName("fileName");
74    Value * const finalLineUnterminated = args++;
75    finalLineUnterminated->setName("finalLineUnterminated");
76
77    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", main,0));
78
79
80    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
81    BasicBlock * segmentCondBlock = nullptr;
82    BasicBlock * segmentBodyBlock = nullptr;
83    const unsigned segmentSize = SegmentSize;
84    if (segmentSize > 1) {
85        segmentCondBlock = BasicBlock::Create(mMod->getContext(), "segmentCond", main, 0);
86        segmentBodyBlock = BasicBlock::Create(mMod->getContext(), "segmentBody", main, 0);
87    }
88    BasicBlock * fullCondBlock = BasicBlock::Create(mMod->getContext(), "fullCond", main, 0);
89    BasicBlock * fullBodyBlock = BasicBlock::Create(mMod->getContext(), "fullBody", main, 0);
90    BasicBlock * finalBlock = BasicBlock::Create(mMod->getContext(), "final", main, 0);
91    BasicBlock * finalPartialBlock = BasicBlock::Create(mMod->getContext(), "partial", main, 0);
92    BasicBlock * finalEmptyBlock = BasicBlock::Create(mMod->getContext(), "empty", main, 0);
93    BasicBlock * endBlock = BasicBlock::Create(mMod->getContext(), "end", main, 0);
94    BasicBlock * unterminatedBlock = BasicBlock::Create(mMod->getContext(), "unterminated", main, 0);
95    BasicBlock * exitBlock = BasicBlock::Create(mMod->getContext(), "exit", main, 0);
96
97    Instance * s2pInstance = mS2PKernel->instantiate(inputStream);
98    Instance * icGrepInstance = mICgrepKernel->instantiate(s2pInstance->getOutputStreamBuffer());
99    Instance * scanMatchInstance = mScanMatchKernel->instantiate(icGrepInstance->getOutputStreamBuffer());
100
101    scanMatchInstance->setInternalState("FileBuf", iBuilder->CreateBitCast(inputStream, int8PtrTy));
102    scanMatchInstance->setInternalState("FileSize", bufferSize);
103    scanMatchInstance->setInternalState("FileName", fileName);
104
105    Value * initialBufferSize = nullptr;
106    BasicBlock * initialBlock = nullptr;
107
108    if (segmentSize > 1) {
109        iBuilder->CreateBr(segmentCondBlock);
110        iBuilder->SetInsertPoint(segmentCondBlock);
111        PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
112        remainingBytes->addIncoming(bufferSize, entryBlock);
113        Constant * const step = ConstantInt::get(int64ty, mBlockSize * segmentSize);
114        Value * segmentCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
115        iBuilder->CreateCondBr(segmentCondTest, fullCondBlock, segmentBodyBlock);
116        iBuilder->SetInsertPoint(segmentBodyBlock);
117        for (unsigned i = 0; i < segmentSize; ++i) {
118            s2pInstance->CreateDoBlockCall();
119        }
120        for (unsigned i = 0; i < segmentSize; ++i) {
121            icGrepInstance->CreateDoBlockCall();
122        }
123        for (unsigned i = 0; i < segmentSize; ++i) {
124            scanMatchInstance->CreateDoBlockCall();
125        }
126        remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), segmentBodyBlock);
127        iBuilder->CreateBr(segmentCondBlock);
128        initialBufferSize = remainingBytes;
129        initialBlock = segmentCondBlock;
130    } else {
131        initialBufferSize = bufferSize;
132        initialBlock = entryBlock;
133        iBuilder->CreateBr(fullCondBlock);
134    }
135
136    iBuilder->SetInsertPoint(fullCondBlock);
137    PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
138    remainingBytes->addIncoming(initialBufferSize, initialBlock);
139
140    Constant * const step = ConstantInt::get(int64ty, mBlockSize);
141    Value * fullCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
142    iBuilder->CreateCondBr(fullCondTest, finalBlock, fullBodyBlock);
143
144    iBuilder->SetInsertPoint(fullBodyBlock);
145    s2pInstance->CreateDoBlockCall();
146    icGrepInstance->CreateDoBlockCall();
147    scanMatchInstance->CreateDoBlockCall();
148
149    remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), fullBodyBlock);
150    iBuilder->CreateBr(fullCondBlock);
151
152    iBuilder->SetInsertPoint(finalBlock);
153    Value * const b4 = s2pInstance->getOutputStream(4);
154    Value * const b6 = s2pInstance->getOutputStream(6);
155    Value * emptyBlockCond = iBuilder->CreateICmpEQ(remainingBytes, ConstantInt::get(int64ty, 0));
156    iBuilder->CreateCondBr(emptyBlockCond, finalEmptyBlock, finalPartialBlock);
157
158
159    iBuilder->SetInsertPoint(finalPartialBlock);
160    s2pInstance->CreateDoBlockCall();
161    iBuilder->CreateBr(endBlock);
162
163    iBuilder->SetInsertPoint(finalEmptyBlock);
164    s2pInstance->clearOutputStreamSet();
165    iBuilder->CreateBr(endBlock);
166
167    iBuilder->SetInsertPoint(endBlock);
168    Value * isFinalLineUnterminated = iBuilder->CreateICmpEQ(finalLineUnterminated, ConstantInt::getNullValue(finalLineUnterminated->getType()));
169    iBuilder->CreateCondBr(isFinalLineUnterminated, exitBlock, unterminatedBlock);
170   
171    iBuilder->SetInsertPoint(unterminatedBlock);
172
173    Value * remaining = iBuilder->CreateZExt(remainingBytes, iBuilder->getIntNTy(mBlockSize));
174    Value * EOF_pos = iBuilder->CreateShl(ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1), remaining);
175    EOF_pos = iBuilder->CreateBitCast(EOF_pos, mBitBlockType);
176
177
178    Value * b4val = iBuilder->CreateBlockAlignedLoad(b4);
179    b4val = iBuilder->CreateOr(b4val, EOF_pos);
180    iBuilder->CreateBlockAlignedStore(b4val, b4);
181
182    Value * b6val = iBuilder->CreateBlockAlignedLoad(b6);
183    b6val = iBuilder->CreateOr(b6val, EOF_pos);
184    iBuilder->CreateBlockAlignedStore(b6val, b6);
185
186    iBuilder->CreateBr(exitBlock);
187
188    iBuilder->SetInsertPoint(exitBlock);
189
190    icGrepInstance->CreateDoBlockCall();
191    scanMatchInstance->CreateDoBlockCall();
192    iBuilder->CreateRetVoid();
193
194    return main;
195}
Note: See TracBrowser for help on using the repository browser.