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

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

Experimental s2p implementation using the s2p_ideal algorithm

File size: 7.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 "toolchain.h"
8#include "utf_encoding.h"
9
10#include <kernels/scanmatchgen.h>
11#include <kernels/s2p_kernel.h>
12#include <kernels/instance.h>
13
14#include <pablo/function.h>
15#include <pablo/pablo_compiler.h>
16
17
18using namespace pablo;
19using namespace kernel;
20
21PipelineBuilder::PipelineBuilder(Module * m, IDISA::IDISA_Builder * b)
22: mMod(m)
23, iBuilder(b)
24, mBitBlockType(b->getBitBlockType())
25, mBlockSize(b->getBitBlockWidth()){
26
27}
28
29PipelineBuilder::~PipelineBuilder(){
30    delete mS2PKernel;
31    delete mICgrepKernel;
32    delete mScanMatchKernel;
33}
34
35void PipelineBuilder::CreateKernels(PabloFunction * function, bool isNameExpression){
36    mS2PKernel = new KernelBuilder("s2p", mMod, iBuilder);
37    mICgrepKernel = new KernelBuilder("icgrep", mMod, iBuilder);
38    mScanMatchKernel = new KernelBuilder("scanMatch", mMod, iBuilder);
39
40    generateS2PKernel(mMod, iBuilder, mS2PKernel);
41
42    generateScanMatch(mMod, iBuilder, 64, mScanMatchKernel, isNameExpression);
43
44    pablo_function_passes(function);
45
46    PabloCompiler pablo_compiler(mMod, iBuilder);
47    try {
48        pablo_compiler.setKernel(mICgrepKernel);
49        pablo_compiler.compile(function);
50        delete function;
51        releaseSlabAllocatorMemory();
52    } catch (std::runtime_error e) {
53        delete function;
54        releaseSlabAllocatorMemory();
55        std::cerr << "Runtime error: " << e.what() << std::endl;
56        exit(1);
57    }
58}
59
60void PipelineBuilder::ExecuteKernels() {
61    Type * T = iBuilder->getIntNTy(64);   
62    Type * S = PointerType::get(iBuilder->getIntNTy(8), 0);
63    Type * inputType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), std::vector<Type *>({ArrayType::get(mBitBlockType, 8)})), 1), 0); 
64    Function * const main = cast<Function>(mMod->getOrInsertFunction("Main", Type::getVoidTy(mMod->getContext()), inputType, T, S, T, 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* buffersize_param = args++;
71    buffersize_param->setName("buffersize");   
72    Value* filename_param = args++;
73    filename_param->setName("filename");     
74    Value* finalLineUnterminated_param = args++;
75    finalLineUnterminated_param->setName("finalLineUnterminated");
76
77    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", main,0));
78
79    BasicBlock * entry_block = iBuilder->GetInsertBlock();
80    BasicBlock * pipeline_test_block = BasicBlock::Create(mMod->getContext(), "pipeline_test_block", main, 0);
81    BasicBlock * pipeline_do_block = BasicBlock::Create(mMod->getContext(), "pipeline_do_block", main, 0);
82    BasicBlock * pipeline_final_block = BasicBlock::Create(mMod->getContext(), "pipeline_final_block", main, 0);
83    BasicBlock * pipeline_partial_block = BasicBlock::Create(mMod->getContext(), "pipeline_partial_block", main, 0);
84    BasicBlock * pipeline_empty_block = BasicBlock::Create(mMod->getContext(), "pipeline_empty_block", main, 0);
85    BasicBlock * pipeline_end_block = BasicBlock::Create(mMod->getContext(), "pipeline_end_block", main, 0);
86    BasicBlock * pipeline_Unterminated_block = BasicBlock::Create(mMod->getContext(), "pipeline_Unterminated_block", main, 0);
87    BasicBlock * pipeline_return_block = BasicBlock::Create(mMod->getContext(), "pipeline_return_block", main, 0);
88
89    Instance * s2pInstance = mS2PKernel->instantiate();
90    Instance * icGrepInstance = mICgrepKernel->instantiate();
91    Instance * scanMatchInstance = mScanMatchKernel->instantiate();
92
93
94    Value * gep = scanMatchInstance->getInternalState("FileBuf");
95    Value * filebuf = iBuilder->CreateBitCast(inputStream, S);
96    iBuilder->CreateStore(filebuf, gep);
97
98    gep = scanMatchInstance->getInternalState("FileSize");
99    iBuilder->CreateStore(buffersize_param, gep);
100
101    gep = scanMatchInstance->getInternalState("FileName");
102    iBuilder->CreateStore(filename_param, gep);
103
104    Value * basis_bits = s2pInstance->getOutputStreamSet();
105
106    Value * results = icGrepInstance->getOutputStreamSet();
107
108    iBuilder->CreateBr(pipeline_test_block);
109
110    iBuilder->SetInsertPoint(pipeline_test_block);
111    PHINode * blockNo = iBuilder->CreatePHI(T, 2, "blockNo");
112    blockNo->addIncoming(iBuilder->getInt64(0), entry_block);
113    PHINode * remainingBytes = iBuilder->CreatePHI(T, 2, "remainingBytes");
114    remainingBytes->addIncoming(buffersize_param, entry_block);
115
116    Constant * step = ConstantInt::get(T, mBlockSize * mS2PKernel->getSegmentBlocks());
117
118    Value * final_block_cond = iBuilder->CreateICmpSLT(remainingBytes, step);
119    iBuilder->CreateCondBr(final_block_cond, pipeline_final_block, pipeline_do_block);
120
121    iBuilder->SetInsertPoint(pipeline_do_block);
122
123    s2pInstance->call(iBuilder->CreateGEP(inputStream, blockNo));
124
125    icGrepInstance->call(basis_bits);
126    scanMatchInstance->call(results);
127
128    blockNo->addIncoming(iBuilder->CreateAdd(blockNo, iBuilder->getInt64(1)), pipeline_do_block);
129    Value * update_remaining = iBuilder->CreateSub(remainingBytes, step);
130    remainingBytes->addIncoming(update_remaining, pipeline_do_block);
131    iBuilder->CreateBr(pipeline_test_block);
132
133    iBuilder->SetInsertPoint(pipeline_final_block);
134
135    Value * empty_block_cond = iBuilder->CreateICmpEQ(remainingBytes, ConstantInt::get(T, 0));
136    iBuilder->CreateCondBr(empty_block_cond, pipeline_empty_block, pipeline_partial_block);
137
138    iBuilder->SetInsertPoint(pipeline_partial_block);
139
140    s2pInstance->call(iBuilder->CreateGEP(inputStream, blockNo));
141
142    iBuilder->CreateBr(pipeline_end_block);
143
144    iBuilder->SetInsertPoint(pipeline_empty_block);
145
146    iBuilder->CreateMemSet(basis_bits, iBuilder->getInt8(0), mBlockSize, 4);
147    iBuilder->CreateBr(pipeline_end_block);
148
149    iBuilder->SetInsertPoint(pipeline_end_block);
150
151    Value * return_block_cond = iBuilder->CreateICmpEQ(finalLineUnterminated_param, ConstantInt::get(T, 0));
152    iBuilder->CreateCondBr(return_block_cond, pipeline_return_block, pipeline_Unterminated_block);
153   
154    iBuilder->SetInsertPoint(pipeline_Unterminated_block);
155
156    Value * remaining = iBuilder->CreateZExt(remainingBytes, iBuilder->getIntNTy(mBlockSize));
157    Value * EOF_pos = iBuilder->CreateShl(ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1), remaining);
158    EOF_pos = iBuilder->CreateBitCast(EOF_pos, mBitBlockType);
159
160    Value * gep_bits4 = s2pInstance->getOutputStream(4);
161    Value * bits4 = iBuilder->CreateBlockAlignedLoad(gep_bits4);
162    bits4 = iBuilder->CreateOr(bits4, EOF_pos);
163    iBuilder->CreateBlockAlignedStore(bits4, gep_bits4);
164
165    Value * gep_bits6 = s2pInstance->getOutputStream(6);
166    Value * bits6 = iBuilder->CreateBlockAlignedLoad(gep_bits6);
167    bits6 = iBuilder->CreateOr(bits6, EOF_pos);
168    iBuilder->CreateBlockAlignedStore(bits6, gep_bits6);
169
170    iBuilder->CreateBr(pipeline_return_block);
171
172    iBuilder->SetInsertPoint(pipeline_return_block);
173
174    icGrepInstance->call(basis_bits);
175    scanMatchInstance->call(results);
176    iBuilder->CreateRetVoid();
177
178}
Note: See TracBrowser for help on using the repository browser.