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

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

Redesigned buffer system to allow the pipeline to control selection of the current input and output streams; DoBlock? functions containing lookahead now take multiple input stream arguments. Selection and passing occurs automatically. Some work on Symbol Table.

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, int64ty, 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    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
80    BasicBlock * segmentCondBlock = nullptr;
81    BasicBlock * segmentBodyBlock = nullptr;
82    const unsigned segmentSize = SegmentSize;
83    if (segmentSize > 1) {
84        segmentCondBlock = BasicBlock::Create(mMod->getContext(), "segmentCond", main, 0);
85        segmentBodyBlock = BasicBlock::Create(mMod->getContext(), "segmentBody", main, 0);
86    }
87    BasicBlock * fullCondBlock = BasicBlock::Create(mMod->getContext(), "fullCond", main, 0);
88    BasicBlock * fullBodyBlock = BasicBlock::Create(mMod->getContext(), "fullBody", main, 0);
89    BasicBlock * finalBlock = BasicBlock::Create(mMod->getContext(), "final", main, 0);
90    BasicBlock * finalPartialBlock = BasicBlock::Create(mMod->getContext(), "partial", main, 0);
91    BasicBlock * finalEmptyBlock = BasicBlock::Create(mMod->getContext(), "empty", main, 0);
92    BasicBlock * endBlock = BasicBlock::Create(mMod->getContext(), "end", main, 0);
93    BasicBlock * unterminatedBlock = BasicBlock::Create(mMod->getContext(), "unterminated", main, 0);
94    BasicBlock * exitBlock = BasicBlock::Create(mMod->getContext(), "exit", main, 0);
95
96    Instance * s2pInstance = mS2PKernel->instantiate(inputStream);
97    Instance * icGrepInstance = mICgrepKernel->instantiate(s2pInstance->getResultSet());
98    Instance * scanMatchInstance = mScanMatchKernel->instantiate(icGrepInstance->getResultSet());
99
100    Value * ptr = iBuilder->CreateBitCast(inputStream, int8PtrTy);
101
102    scanMatchInstance->setInternalState("FileBuf", ptr);
103    scanMatchInstance->setInternalState("FileSize", bufferSize);
104    scanMatchInstance->setInternalState("FileName", fileName);
105
106    Value * initialBufferSize = nullptr;
107    BasicBlock * initialBlock = nullptr;
108
109    if (segmentSize > 1) {
110        iBuilder->CreateBr(segmentCondBlock);
111        iBuilder->SetInsertPoint(segmentCondBlock);
112        PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
113        remainingBytes->addIncoming(bufferSize, entryBlock);
114        Constant * const step = ConstantInt::get(int64ty, mBlockSize * segmentSize);
115        Value * segmentCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
116        iBuilder->CreateCondBr(segmentCondTest, fullCondBlock, segmentBodyBlock);
117        iBuilder->SetInsertPoint(segmentBodyBlock);
118        for (unsigned i = 0; i < segmentSize; ++i) {
119            s2pInstance->CreateDoBlockCall();
120        }
121        for (unsigned i = 0; i < segmentSize; ++i) {
122            icGrepInstance->CreateDoBlockCall();
123        }
124        for (unsigned i = 0; i < segmentSize; ++i) {
125            scanMatchInstance->CreateDoBlockCall();
126        }
127        remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), segmentBodyBlock);
128        iBuilder->CreateBr(segmentCondBlock);
129        initialBufferSize = remainingBytes;
130        initialBlock = segmentCondBlock;
131    } else {
132        initialBufferSize = bufferSize;
133        initialBlock = entryBlock;
134        iBuilder->CreateBr(fullCondBlock);
135    }
136
137    iBuilder->SetInsertPoint(fullCondBlock);
138    PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
139    remainingBytes->addIncoming(initialBufferSize, initialBlock);
140
141    Constant * const step = ConstantInt::get(int64ty, mBlockSize);
142    Value * fullCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
143    iBuilder->CreateCondBr(fullCondTest, finalBlock, fullBodyBlock);
144
145    iBuilder->SetInsertPoint(fullBodyBlock);
146    s2pInstance->CreateDoBlockCall();
147    icGrepInstance->CreateDoBlockCall();
148    scanMatchInstance->CreateDoBlockCall();
149
150    remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), fullBodyBlock);
151    iBuilder->CreateBr(fullCondBlock);
152
153    iBuilder->SetInsertPoint(finalBlock);
154    Value * const b4 = s2pInstance->getOutputStream(4);
155    Value * const b6 = s2pInstance->getOutputStream(6);
156    Value * emptyBlockCond = iBuilder->CreateICmpEQ(remainingBytes, ConstantInt::get(int64ty, 0));
157    iBuilder->CreateCondBr(emptyBlockCond, finalEmptyBlock, finalPartialBlock);
158
159
160    iBuilder->SetInsertPoint(finalPartialBlock);
161    s2pInstance->CreateDoBlockCall();
162    iBuilder->CreateBr(endBlock);
163
164    iBuilder->SetInsertPoint(finalEmptyBlock);
165    s2pInstance->clearOutputStreamSet();
166    iBuilder->CreateBr(endBlock);
167
168    iBuilder->SetInsertPoint(endBlock);
169    Value * isFinalLineUnterminated = iBuilder->CreateICmpEQ(finalLineUnterminated, ConstantInt::get(int64ty, 0));
170    iBuilder->CreateCondBr(isFinalLineUnterminated, exitBlock, unterminatedBlock);
171   
172    iBuilder->SetInsertPoint(unterminatedBlock);
173
174    Value * remaining = iBuilder->CreateZExt(remainingBytes, iBuilder->getIntNTy(mBlockSize));
175    Value * EOF_pos = iBuilder->CreateShl(ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1), remaining);
176    EOF_pos = iBuilder->CreateBitCast(EOF_pos, mBitBlockType);
177
178
179    Value * b4val = iBuilder->CreateBlockAlignedLoad(b4);
180    b4val = iBuilder->CreateOr(b4val, EOF_pos);
181    iBuilder->CreateBlockAlignedStore(b4val, b4);
182
183    Value * b6val = iBuilder->CreateBlockAlignedLoad(b6);
184    b6val = iBuilder->CreateOr(b6val, EOF_pos);
185    iBuilder->CreateBlockAlignedStore(b6val, b6);
186
187    iBuilder->CreateBr(exitBlock);
188
189    iBuilder->SetInsertPoint(exitBlock);
190
191    icGrepInstance->CreateDoBlockCall();
192    scanMatchInstance->CreateDoBlockCall();
193    iBuilder->CreateRetVoid();
194
195    return main;
196}
Note: See TracBrowser for help on using the repository browser.