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

Last change on this file since 4945 was 4945, checked in by lindanl, 4 years ago

Use kernel state methods.

File size: 7.5 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, bool isNameExpression){
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, isNameExpression);
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* filebuf = iBuilder->CreateBitCast(input_param, S);
94    mScanMatchKernel->changeKernelInternalState(scanMatchKernelStruct, mFileBufIdx, filebuf);
95    mScanMatchKernel->changeKernelInternalState(scanMatchKernelStruct, mFileSizeIdx, buffersize_param);
96    mScanMatchKernel->changeKernelInternalState(scanMatchKernelStruct, mFileNameIdx, filename_param);
97
98    Value * basis_bits = iBuilder->CreateGEP(s2pKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
99    Value * results = iBuilder->CreateGEP(icGrepKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
100 
101    iBuilder->CreateBr(pipeline_test_block);
102
103    iBuilder->SetInsertPoint(pipeline_test_block);
104    PHINode * remaining_phi = iBuilder->CreatePHI(T, 2, "remaining");   
105    PHINode * blkNo_phi = iBuilder->CreatePHI(T, 2, "blkNo");
106    remaining_phi->addIncoming(buffersize_param, entry_block);
107    blkNo_phi->addIncoming(iBuilder->getInt64(0), entry_block);
108
109    Value * final_block_cond = iBuilder->CreateICmpSLT(remaining_phi, ConstantInt::get(T, mBlockSize));
110    iBuilder->CreateCondBr(final_block_cond, pipeline_final_block, pipeline_do_block);
111
112    iBuilder->SetInsertPoint(pipeline_do_block);
113
114    Value * gep = iBuilder->CreateGEP(input_param, {blkNo_phi});
115    Value * update_blkNo = iBuilder->CreateAdd(blkNo_phi, iBuilder->getInt64(1));
116    blkNo_phi->addIncoming(update_blkNo, pipeline_do_block);
117
118    mS2PKernel->generateDoBlockCall(gep);
119    mICgrepKernel->generateDoBlockCall(basis_bits);
120    mScanMatchKernel->generateDoBlockCall(results);
121
122    Value * update_remaining = iBuilder->CreateSub(remaining_phi, iBuilder->getInt64(mBlockSize));
123    remaining_phi->addIncoming(update_remaining, pipeline_do_block);
124    iBuilder->CreateBr(pipeline_test_block);
125
126    iBuilder->SetInsertPoint(pipeline_final_block);
127
128    Value * empty_block_cond = iBuilder->CreateICmpEQ(remaining_phi, ConstantInt::get(T, 0));
129    iBuilder->CreateCondBr(empty_block_cond, pipeline_empty_block, pipeline_partial_block);
130
131    iBuilder->SetInsertPoint(pipeline_partial_block);
132
133    gep = iBuilder->CreateGEP(input_param, {blkNo_phi});
134    mS2PKernel->generateDoBlockCall(gep);
135    iBuilder->CreateBr(pipeline_end_block);
136
137    iBuilder->SetInsertPoint(pipeline_empty_block);
138
139    iBuilder->CreateMemSet(basis_bits, iBuilder->getInt8(0), mBlockSize, 4);
140    iBuilder->CreateBr(pipeline_end_block);
141
142    iBuilder->SetInsertPoint(pipeline_end_block);
143
144    Value * return_block_cond = iBuilder->CreateICmpEQ(finalLineUnterminated_param, ConstantInt::get(T, 0));
145    iBuilder->CreateCondBr(return_block_cond, pipeline_return_block, pipeline_Unterminated_block);
146   
147    iBuilder->SetInsertPoint(pipeline_Unterminated_block);
148
149    Value * remaining = iBuilder->CreateZExt(remaining_phi, iBuilder->getIntNTy(mBlockSize));
150    Value * EOF_pos = iBuilder->CreateShl(ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1), remaining);
151    EOF_pos = iBuilder->CreateBitCast(EOF_pos, mBitBlockType);
152
153    Value * gep_bits4 = iBuilder->CreateGEP(basis_bits, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(4)});
154    Value * bits4 = iBuilder->CreateAlignedLoad(gep_bits4, mBlockSize/8, false, "bits4");
155    bits4 = iBuilder->CreateOr(bits4, EOF_pos);
156    iBuilder->CreateAlignedStore(bits4, gep_bits4, mBlockSize/8, false);
157
158    Value * gep_bits6 = iBuilder->CreateGEP(basis_bits, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(6)});
159    Value * bits6 = iBuilder->CreateAlignedLoad(gep_bits6, mBlockSize/8, false, "bits6");
160    bits6 = iBuilder->CreateOr(bits6, EOF_pos);
161    iBuilder->CreateAlignedStore(bits6, gep_bits6, mBlockSize/8, false);
162    iBuilder->CreateBr(pipeline_return_block);
163
164    iBuilder->SetInsertPoint(pipeline_return_block);
165
166    mICgrepKernel->generateDoBlockCall(basis_bits);
167    mScanMatchKernel->generateDoBlockCall(results);
168    iBuilder->CreateRetVoid();
169
170}
171
172
173
Note: See TracBrowser for help on using the repository browser.