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

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

Bug fix; simplification

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