source: icGREP/icgrep-devel/icgrep/kernels/u8u16_pipeline.cpp @ 5007

Last change on this file since 5007 was 5007, checked in by cameron, 2 years ago

u8u16 progress

File size: 7.3 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 <kernels/u8u16_pipeline.h>
7#include <utf_encoding.h>
8
9#include <kernels/s2p_kernel.h>
10#include <kernels/deletion.h>
11#include <kernels/p2s_kernel.h>
12#include <kernels/stdout_kernel.h>
13#include <kernels/instance.h>
14
15#include <pablo/function.h>
16#include <pablo/pablo_compiler.h>
17#include <pablo/pablo_toolchain.h>
18
19static cl::opt<unsigned> SegmentSize("segment-size", cl::desc("Segment Size"), cl::value_desc("positive integer"), cl::init(1));
20
21
22using namespace pablo;
23using namespace kernel;
24
25PipelineBuilder::PipelineBuilder(Module * m, IDISA::IDISA_Builder * b)
26: mMod(m)
27, iBuilder(b)
28, mBitBlockType(b->getBitBlockType())
29, mBlockSize(b->getBitBlockWidth()){
30
31}
32
33PipelineBuilder::~PipelineBuilder(){
34    delete mS2PKernel;
35    delete mU8U16Kernel;
36    delete mP2SKernel;
37    delete mStdOutKernel;
38}
39
40void PipelineBuilder::CreateKernels(PabloFunction * function){
41    mS2PKernel = new KernelBuilder(iBuilder, "s2p", SegmentSize);
42    mU8U16Kernel = new KernelBuilder(iBuilder, "u8u16", SegmentSize);
43    mDelKernel = new KernelBuilder(iBuilder, "del", SegmentSize);
44    mP2SKernel = new KernelBuilder(iBuilder, "p2s", SegmentSize);   
45    mStdOutKernel = new KernelBuilder(iBuilder, "stdout", SegmentSize);
46
47    generateS2PKernel(mMod, iBuilder, mS2PKernel);
48    generateP2S_16Kernel(mMod, iBuilder, mP2SKernel);
49    generateDeletionKernel(mMod, iBuilder, /*fw=*/8, /*stream_count=*/16, mDelKernel);
50    generateStdOutKernel(mMod, iBuilder, mStdOutKernel, 16);
51
52    pablo_function_passes(function);
53
54    PabloCompiler pablo_compiler(mMod, iBuilder);
55    try {
56        pablo_compiler.setKernel(mU8U16Kernel);
57        pablo_compiler.compile(function);
58        delete function;
59        releaseSlabAllocatorMemory();
60    } catch (std::runtime_error e) {
61        delete function;
62        releaseSlabAllocatorMemory();
63        std::cerr << "Runtime error: " << e.what() << std::endl;
64        exit(1);
65    }
66   
67}
68
69Function *  PipelineBuilder::ExecuteKernels() {
70    Type * const int64ty = iBuilder->getInt64Ty();
71    Type * const inputType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), std::vector<Type *>({ArrayType::get(mBitBlockType, 8)})), 1), 0);
72   
73    Function * const main = cast<Function>(mMod->getOrInsertFunction("Main", Type::getVoidTy(mMod->getContext()), inputType, int64ty, nullptr));
74    main->setCallingConv(CallingConv::C);
75    Function::arg_iterator args = main->arg_begin();
76   
77    Value * const inputStream = args++;
78    inputStream->setName("input");
79    Value * const bufferSize = args++;
80    bufferSize->setName("bufferSize");
81   
82    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", main,0));
83   
84    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
85
86    BasicBlock * segmentCondBlock = nullptr;
87    BasicBlock * segmentBodyBlock = nullptr;
88    const unsigned segmentSize = SegmentSize;
89    if (segmentSize > 1) {
90        segmentCondBlock = BasicBlock::Create(mMod->getContext(), "segmentCond", main, 0);
91        segmentBodyBlock = BasicBlock::Create(mMod->getContext(), "segmentBody", main, 0);
92    }
93    BasicBlock * fullCondBlock = BasicBlock::Create(mMod->getContext(), "fullCond", main, 0);
94    BasicBlock * fullBodyBlock = BasicBlock::Create(mMod->getContext(), "fullBody", main, 0);
95    BasicBlock * finalBlock = BasicBlock::Create(mMod->getContext(), "final", main, 0);
96    BasicBlock * finalPartialBlock = BasicBlock::Create(mMod->getContext(), "partial", main, 0);
97    BasicBlock * finalEmptyBlock = BasicBlock::Create(mMod->getContext(), "empty", main, 0);
98    BasicBlock * endBlock = BasicBlock::Create(mMod->getContext(), "end", main, 0);
99
100    Instance * s2pInstance = mS2PKernel->instantiate(inputStream);
101    Instance * u8u16Instance = mU8U16Kernel->instantiate(s2pInstance->getOutputStreamSet());
102    Instance * delInstance = mDelKernel->instantiate(u8u16Instance->getOutputStreamSet());
103    Instance * p2sInstance = mP2SKernel->instantiate(delInstance->getOutputStreamSet());
104    Instance * stdOutInstance = mStdOutKernel->instantiate(p2sInstance->getOutputStreamSet());
105
106    stdOutInstance->setInternalState("RemainingBytes", bufferSize);  // The total number of bytes remaining in input.
107
108   
109    Value * initialBufferSize = nullptr;
110    BasicBlock * initialBlock = nullptr;
111   
112    if (segmentSize > 1) {
113        iBuilder->CreateBr(segmentCondBlock);
114        iBuilder->SetInsertPoint(segmentCondBlock);
115        PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
116        remainingBytes->addIncoming(bufferSize, entryBlock);
117        Constant * const step = ConstantInt::get(int64ty, mBlockSize * segmentSize);
118        Value * segmentCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
119        iBuilder->CreateCondBr(segmentCondTest, fullCondBlock, segmentBodyBlock);
120        iBuilder->SetInsertPoint(segmentBodyBlock);
121        for (unsigned i = 0; i < segmentSize; ++i) {
122            s2pInstance->CreateDoBlockCall();
123        }
124        for (unsigned i = 0; i < segmentSize; ++i) {
125            u8u16Instance->CreateDoBlockCall();
126        }
127        for (unsigned i = 0; i < segmentSize; ++i) {
128            delInstance->CreateDoBlockCall();
129        }
130        for (unsigned i = 0; i < segmentSize; ++i) {
131            p2sInstance->CreateDoBlockCall();
132        }
133        for (unsigned i = 0; i < segmentSize; ++i) {
134            stdOutInstance->CreateDoBlockCall();
135        }
136        remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), segmentBodyBlock);
137        iBuilder->CreateBr(segmentCondBlock);
138        initialBufferSize = remainingBytes;
139        initialBlock = segmentCondBlock;
140    } else {
141        initialBufferSize = bufferSize;
142        initialBlock = entryBlock;
143        iBuilder->CreateBr(fullCondBlock);
144    }
145
146    iBuilder->SetInsertPoint(fullCondBlock);
147    PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
148    remainingBytes->addIncoming(initialBufferSize, initialBlock);
149
150    Constant * const step = ConstantInt::get(int64ty, mBlockSize);
151    Value * fullCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
152    iBuilder->CreateCondBr(fullCondTest, finalBlock, fullBodyBlock);
153   
154    iBuilder->SetInsertPoint(fullBodyBlock);
155
156    s2pInstance->CreateDoBlockCall();
157    u8u16Instance->CreateDoBlockCall();
158    delInstance->CreateDoBlockCall();
159    p2sInstance->CreateDoBlockCall();
160    stdOutInstance->CreateDoBlockCall();
161
162    Value * diff = iBuilder->CreateSub(remainingBytes, step);
163
164    remainingBytes->addIncoming(diff, fullBodyBlock);
165    iBuilder->CreateBr(fullCondBlock);
166   
167    iBuilder->SetInsertPoint(finalBlock);
168    Value * emptyBlockCond = iBuilder->CreateICmpEQ(remainingBytes, ConstantInt::get(int64ty, 0));
169    iBuilder->CreateCondBr(emptyBlockCond, finalEmptyBlock, finalPartialBlock);
170   
171   
172    iBuilder->SetInsertPoint(finalPartialBlock);
173    s2pInstance->CreateDoBlockCall();
174    iBuilder->CreateBr(endBlock);
175   
176    iBuilder->SetInsertPoint(finalEmptyBlock);
177    s2pInstance->clearOutputStreamSet();
178    iBuilder->CreateBr(endBlock);
179   
180    iBuilder->SetInsertPoint(endBlock);
181
182    u8u16Instance->CreateDoBlockCall();
183    delInstance->CreateDoBlockCall();
184    p2sInstance->CreateDoBlockCall();
185    stdOutInstance->CreateDoBlockCall();
186    iBuilder->CreateRetVoid();
187    return main;
188}
Note: See TracBrowser for help on using the repository browser.