source: icGREP/icgrep-devel/icgrep/pablo/pablo_kernel.cpp @ 5059

Last change on this file since 5059 was 5059, checked in by cameron, 3 years ago

Pablo Kernel progress

File size: 2.8 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/streamset.h>
7#include <pablo/pablo_kernel.h>
8#include <pablo/pablo_compiler.h>
9#include <llvm/Support/Debug.h>
10
11
12using namespace pablo;
13using namespace kernel;
14
15PabloKernel::PabloKernel(IDISA::IDISA_Builder * builder,
16                         std::string kernelName,
17                         PabloFunction * pf,
18                         std::vector<std::string> accumulators) :
19    KernelInterface(builder, kernelName,
20                    {StreamSetBinding{StreamSetType(pf->getNumOfParameters(), 1), "inputs"}},
21                    {StreamSetBinding{StreamSetType(pf->getNumOfResults(), 1), "outputs"}},
22                    {},
23                    {},
24                    {ScalarBinding{builder->getBitBlockType(), "EOFmark"}}),
25    mPabloFunction(pf) {
26    mScalarOutputs = accumBindings(accumulators);
27}
28
29
30std::vector<ScalarBinding> PabloKernel::accumBindings(std::vector<std::string> accum_names) {
31    std::vector<ScalarBinding> vec;
32    Type * accum_t = iBuilder->getInt64Ty();
33    for (auto a : accum_names) {
34        vec.push_back(ScalarBinding{accum_t, a});
35    }
36    return vec;
37}
38
39std::unique_ptr<llvm::Module> PabloKernel::createKernelModule() {
40    std::unique_ptr<llvm::Module> theModule = KernelInterface::createKernelModule();
41
42    Module * m = theModule.get();
43    addFinalBlockMethod(m);
44    iBuilder->setModule(m);
45    PabloCompiler pablo_compiler(iBuilder);
46    pablo_compiler.setKernel(this);
47    pablo_compiler.compile(mPabloFunction, m->getFunction(mKernelName + "_DoBlock"));
48    return theModule;
49}
50
51void PabloKernel::addFinalBlockMethod(Module * m) {
52    Function * doBlockFunction = m->getFunction(mKernelName + "_DoBlock");
53    Function * finalBlockFunction = m->getFunction(mKernelName + "_FinalBlock");
54    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_entry", finalBlockFunction, 0));
55    // Final Block arguments: self, remaining, then the standard DoBlock args.
56    Function::arg_iterator args = finalBlockFunction->arg_begin();
57    Value * self = &*(args++);
58    Value * remaining = &*(args++);
59    std::vector<Value *> doBlockArgs = {self};
60    while (args != finalBlockFunction->arg_end()){
61        doBlockArgs.push_back(&*args++);
62    }
63    // Standard Pablo convention for final block processing: set a bit marking
64    // the position just past EOF.
65    Type * bitBlockInt = iBuilder->getIntNTy(iBuilder->getBitBlockWidth());
66    Value * EOFmark = iBuilder->CreateShl(ConstantInt::get(bitBlockInt, 1), iBuilder->CreateZExt(remaining, bitBlockInt));
67    setScalarField(self, "EOFmark", iBuilder->CreateBitCast(EOFmark, iBuilder->getBitBlockType()));
68    iBuilder->CreateCall(doBlockFunction, doBlockArgs);
69    iBuilder->CreateRetVoid();
70}
71
Note: See TracBrowser for help on using the repository browser.