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

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

Some fixes for threading and kernel builder.

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
16using namespace pablo;
17
18PipelineBuilder::PipelineBuilder(Module * m, IDISA::IDISA_Builder * b)
19: mMod(m)
20, iBuilder(b)
21, mFileBufIdx(7)
22, mFileSizeIdx(8)
23, mFileNameIdx(9)
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    generateScanMatch(mMod, iBuilder, 64, mScanMatchKernel, isNameExpression);
42
43    pablo_function_passes(function);
44
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
59void PipelineBuilder::ExecuteKernels(){
60    Type * T = iBuilder->getIntNTy(64);   
61    Type * S = PointerType::get(iBuilder->getIntNTy(8), 0);
62    Type * inputType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), std::vector<Type *>({ArrayType::get(mBitBlockType, 8)})), 1), 0); 
63    Function * const main = cast<Function>(mMod->getOrInsertFunction("Main", Type::getVoidTy(mMod->getContext()), inputType, T, S, T, nullptr));
64    main->setCallingConv(CallingConv::C);
65    Function::arg_iterator args = main->arg_begin();
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", main,0));
77
78    BasicBlock * entry_block = iBuilder->GetInsertBlock();
79    BasicBlock * pipeline_test_block = BasicBlock::Create(mMod->getContext(), "pipeline_test_block", main, 0);
80    BasicBlock * pipeline_do_block = BasicBlock::Create(mMod->getContext(), "pipeline_do_block", main, 0);
81    BasicBlock * pipeline_final_block = BasicBlock::Create(mMod->getContext(), "pipeline_final_block", main, 0);
82    BasicBlock * pipeline_partial_block = BasicBlock::Create(mMod->getContext(), "pipeline_partial_block", main, 0);
83    BasicBlock * pipeline_empty_block = BasicBlock::Create(mMod->getContext(), "pipeline_empty_block", main, 0);
84    BasicBlock * pipeline_end_block = BasicBlock::Create(mMod->getContext(), "pipeline_end_block", main, 0);
85    BasicBlock * pipeline_Unterminated_block = BasicBlock::Create(mMod->getContext(), "pipeline_Unterminated_block", main, 0);
86    BasicBlock * pipeline_return_block = BasicBlock::Create(mMod->getContext(), "pipeline_return_block", main, 0);
87
88    Value * s2pKernelStruct = mS2PKernel->generateKernelInstance();
89    Value * icGrepKernelStruct = mICgrepKernel->generateKernelInstance();
90    Value * scanMatchKernelStruct = mScanMatchKernel->generateKernelInstance();
91
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
97    gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileSizeIdx)});
98    iBuilder->CreateStore(buffersize_param, gep);
99
100
101    gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(mFileNameIdx)});
102    iBuilder->CreateStore(filename_param, gep);
103
104    Value * basis_bits = iBuilder->CreateGEP(s2pKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
105    Value * results = iBuilder->CreateGEP(icGrepKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
106 
107    iBuilder->CreateBr(pipeline_test_block);
108
109    iBuilder->SetInsertPoint(pipeline_test_block);
110    PHINode * remaining_phi = iBuilder->CreatePHI(T, 2, "remaining");   
111    PHINode * blkNo_phi = iBuilder->CreatePHI(T, 2, "blkNo");
112    remaining_phi->addIncoming(buffersize_param, entry_block);
113    blkNo_phi->addIncoming(iBuilder->getInt64(0), entry_block);
114
115    Value * final_block_cond = iBuilder->CreateICmpSLT(remaining_phi, ConstantInt::get(T, mBlockSize));
116    iBuilder->CreateCondBr(final_block_cond, pipeline_final_block, pipeline_do_block);
117
118    iBuilder->SetInsertPoint(pipeline_do_block);
119
120    gep = iBuilder->CreateGEP(input_param, blkNo_phi);
121    Value * update_blkNo = iBuilder->CreateAdd(blkNo_phi, iBuilder->getInt64(1));
122    blkNo_phi->addIncoming(update_blkNo, pipeline_do_block);
123
124    mS2PKernel->generateDoBlockCall(gep);
125    mICgrepKernel->generateDoBlockCall(basis_bits);
126    mScanMatchKernel->generateDoBlockCall(results);
127
128    Value * update_remaining = iBuilder->CreateSub(remaining_phi, iBuilder->getInt64(mBlockSize));
129    remaining_phi->addIncoming(update_remaining, pipeline_do_block);
130    iBuilder->CreateBr(pipeline_test_block);
131
132    iBuilder->SetInsertPoint(pipeline_final_block);
133
134    Value * empty_block_cond = iBuilder->CreateICmpEQ(remaining_phi, ConstantInt::get(T, 0));
135    iBuilder->CreateCondBr(empty_block_cond, pipeline_empty_block, pipeline_partial_block);
136
137    iBuilder->SetInsertPoint(pipeline_partial_block);
138
139    gep = iBuilder->CreateGEP(input_param, blkNo_phi);
140    mS2PKernel->generateDoBlockCall(gep);
141    iBuilder->CreateBr(pipeline_end_block);
142
143    iBuilder->SetInsertPoint(pipeline_empty_block);
144
145    iBuilder->CreateMemSet(basis_bits, iBuilder->getInt8(0), mBlockSize, 4);
146    iBuilder->CreateBr(pipeline_end_block);
147
148    iBuilder->SetInsertPoint(pipeline_end_block);
149
150    Value * return_block_cond = iBuilder->CreateICmpEQ(finalLineUnterminated_param, ConstantInt::get(T, 0));
151    iBuilder->CreateCondBr(return_block_cond, pipeline_return_block, pipeline_Unterminated_block);
152   
153    iBuilder->SetInsertPoint(pipeline_Unterminated_block);
154
155    Value * remaining = iBuilder->CreateZExt(remaining_phi, iBuilder->getIntNTy(mBlockSize));
156    Value * EOF_pos = iBuilder->CreateShl(ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1), remaining);
157    EOF_pos = iBuilder->CreateBitCast(EOF_pos, mBitBlockType);
158
159    Value * gep_bits4 = iBuilder->CreateGEP(basis_bits, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(4)});
160    Value * bits4 = iBuilder->CreateAlignedLoad(gep_bits4, mBlockSize/8, false, "bits4");
161    bits4 = iBuilder->CreateOr(bits4, EOF_pos);
162    iBuilder->CreateAlignedStore(bits4, gep_bits4, mBlockSize/8, false);
163
164    Value * gep_bits6 = iBuilder->CreateGEP(basis_bits, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(6)});
165    Value * bits6 = iBuilder->CreateAlignedLoad(gep_bits6, mBlockSize/8, false, "bits6");
166    bits6 = iBuilder->CreateOr(bits6, EOF_pos);
167    iBuilder->CreateAlignedStore(bits6, gep_bits6, mBlockSize/8, false);
168    iBuilder->CreateBr(pipeline_return_block);
169
170    iBuilder->SetInsertPoint(pipeline_return_block);
171
172    mICgrepKernel->generateDoBlockCall(basis_bits);
173    mScanMatchKernel->generateDoBlockCall(results);
174    iBuilder->CreateRetVoid();
175
176}
Note: See TracBrowser for help on using the repository browser.