source: icGREP/icgrep-devel/icgrep/kernels/casefold_pipeline.cpp @ 5010

Last change on this file since 5010 was 5010, checked in by nmedfort, 4 years ago

Fix for u8u16

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