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

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

Added ability to name internal state types; removed unnecessary predefined states. Some progress towards supporting segment size > 1

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