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

Last change on this file since 4929 was 4929, checked in by lindanl, 3 years ago

PipelineBuilder? that creates the Main function.

File size: 5.1 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, mFilePosIdx(2)
21, mBitBlockType(b->getBitBlockType())
22, mBlockSize(b->getBitBlockWidth()){
23
24}
25PipelineBuilder::~PipelineBuilder(){
26}
27
28void PipelineBuilder::CreateKernels(re::RE * re_ast){
29    mS2PKernel = new KernelBuilder("s2p", mMod, iBuilder);
30    mICgrepKernel = new KernelBuilder("icgrep", mMod, iBuilder);
31    mScanMatchKernel = new KernelBuilder("scanMatch", mMod, iBuilder);
32
33
34    generateS2PKernel(mMod, iBuilder, mS2PKernel);
35    generateScanMatch(mMod, iBuilder, 64, mScanMatchKernel);
36
37    Encoding encoding(Encoding::Type::UTF_8, 8);
38    re_ast = regular_expression_passes(encoding, re_ast);
39   
40    pablo::PabloFunction * function = re2pablo_compiler(encoding, re_ast);
41
42    pablo_function_passes(function);
43         
44    pablo::PabloCompiler pablo_compiler(mMod, iBuilder);
45    try {
46        pablo_compiler.setKernel(mICgrepKernel);
47        pablo_compiler.compile(function);
48        delete function;
49        releaseSlabAllocatorMemory();
50    } catch (std::runtime_error e) {
51        delete function;
52        releaseSlabAllocatorMemory();
53        std::cerr << "Runtime error: " << e.what() << std::endl;
54        exit(1);
55    }
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 
64    Constant* c = mMod->getOrInsertFunction("Main", Type::getVoidTy(mMod->getContext()), inputType, T, S, NULL);
65    Function* mMainFunction = cast<Function>(c);
66    mMainFunction->setCallingConv(CallingConv::C);
67    Function::arg_iterator args = mMainFunction->arg_begin();
68
69
70    Value* input_param = args++;
71    input_param->setName("input");
72    Value* buffersize_param = args++;
73    buffersize_param->setName("buffersize");   
74    Value* filename_param = args++;
75    filename_param->setName("filename");
76
77    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", mMainFunction,0));
78
79
80    BasicBlock * entry_block = iBuilder->GetInsertBlock();
81    BasicBlock * pipeline_test_block = BasicBlock::Create(mMod->getContext(), "pipeline_test_block", mMainFunction, 0);
82    BasicBlock * pipeline_do_block = BasicBlock::Create(mMod->getContext(), "pipeline_do_block", mMainFunction, 0);
83    BasicBlock * pipeline_partial_block = BasicBlock::Create(mMod->getContext(), "pipeline_partial_block", mMainFunction, 0);
84
85    Value * s2pKernelStruct = mS2PKernel->generateKernelInstance();
86    Value * icGrepKernelStruct = mICgrepKernel->generateKernelInstance();
87    Value * scanMatchKernelStruct = mScanMatchKernel->generateKernelInstance();
88    iBuilder->CreateBr(pipeline_test_block);
89
90    iBuilder->SetInsertPoint(pipeline_test_block);
91    PHINode * remaining_phi = iBuilder->CreatePHI(T, 2, "remaining");   
92    PHINode * blkNo_phi = iBuilder->CreatePHI(T, 2, "blkNo");
93    remaining_phi->addIncoming(buffersize_param, entry_block);
94    blkNo_phi->addIncoming(iBuilder->getInt64(0), entry_block);
95    Value * cond = iBuilder->CreateICmpSLT(remaining_phi, ConstantInt::get(T, mBlockSize));
96    iBuilder->CreateCondBr(cond, pipeline_partial_block, pipeline_do_block);
97
98
99    iBuilder->SetInsertPoint(pipeline_do_block);
100
101    Value * gep = iBuilder->CreateGEP(input_param, {blkNo_phi});
102    mS2PKernel->generateDoBlockCall(gep);
103    Value * update_blkNo = iBuilder->CreateAdd(blkNo_phi, iBuilder->getInt64(1));
104    blkNo_phi->addIncoming(update_blkNo, pipeline_do_block);
105
106    Value * basis_bits = iBuilder->CreateGEP(s2pKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
107    mICgrepKernel->generateDoBlockCall(basis_bits);
108
109    gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(7)});
110    Value* filebuf = iBuilder->CreateBitCast(input_param, S);
111    iBuilder->CreateStore(filebuf, gep);
112    gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(8)});
113    iBuilder->CreateStore(buffersize_param, gep);
114    gep = iBuilder->CreateGEP(scanMatchKernelStruct, {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(9)});
115    iBuilder->CreateStore(filename_param, gep);
116
117    Value * results = iBuilder->CreateGEP(icGrepKernelStruct, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
118    mScanMatchKernel->generateDoBlockCall(results);
119
120    Value * update_remaining = iBuilder->CreateSub(remaining_phi, iBuilder->getInt64(iBuilder->getBitBlockWidth()));
121    remaining_phi->addIncoming(update_remaining, pipeline_do_block);
122    iBuilder->CreateBr(pipeline_test_block);
123
124    iBuilder->SetInsertPoint(pipeline_partial_block);
125    iBuilder->CreateRetVoid();
126
127}
128
129
130
Note: See TracBrowser for help on using the repository browser.