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

Last change on this file since 5261 was 5261, checked in by cameron, 11 months ago

Move responsibility for ProducedItemCount? into doSegment unless overridden

File size: 4.6 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 "pablo_kernel.h"
7#include <pablo/codegenstate.h>
8#include <pablo/pablo_compiler.h>
9#include <pablo/pe_var.h>
10#include <llvm/IR/Module.h>
11#include <llvm/IR/Verifier.h>
12#include <IR_Gen/idisa_builder.h>
13
14using namespace pablo;
15using namespace kernel;
16using namespace parabix;
17using namespace IDISA;
18
19Var * PabloKernel::addInput(const std::string & name, Type * const type) {
20    Var * param = new (mAllocator) Var(mSymbolTable->make(name, iBuilder), type, mAllocator, true);
21    mInputs.push_back(param);
22    if (isa<ArrayType>(type) || isa<StreamType>(type)) {
23        mStreamSetInputs.emplace_back(type, name);
24    } else {
25        mScalarInputs.emplace_back(type, name);
26    }
27    assert (mStreamSetInputs.size() + mScalarInputs.size() == mInputs.size());
28    return param;
29}
30
31Var * PabloKernel::addOutput(const std::string name, Type * const type) {
32    Var * result = new (mAllocator) Var(mSymbolTable->make(name, iBuilder), type, mAllocator, false);
33    mOutputs.push_back(result);
34    if (isa<ArrayType>(type) || isa<StreamType>(type)) {
35        mStreamSetOutputs.emplace_back(type, name);
36    } else {
37        mScalarOutputs.emplace_back(type, name);
38    }
39    assert (mStreamSetOutputs.size() + mScalarOutputs.size() == mOutputs.size());
40    return result;
41}
42
43Var * PabloKernel::makeVariable(PabloAST * name, Type * const type) {
44    Var * const var = new (mAllocator) Var(name, type, mAllocator);
45    mVariables.push_back(var);
46    return var;
47}
48
49Zeroes * PabloKernel::getNullValue(Type * type) {
50    if (type == nullptr) {
51        type = getStreamTy();
52    }
53    for (PabloAST * constant : mConstants) {
54        if (isa<Zeroes>(constant) && constant->getType() == type) {
55            return cast<Zeroes>(constant);
56        }
57    }
58    Zeroes * value = new (mAllocator) Zeroes(type, mAllocator);
59    mConstants.push_back(value);
60    return value;
61}
62
63Ones * PabloKernel::getAllOnesValue(Type * type) {
64    if (type == nullptr) {
65        type = getStreamTy();
66    }
67    for (PabloAST * constant : mConstants) {
68        if (isa<Ones>(constant) && constant->getType() == type) {
69            return cast<Ones>(constant);
70        }
71    }
72    Ones * value = new (mAllocator) Ones(type, mAllocator);
73    mConstants.push_back(value);
74    return value;
75}
76
77void PabloKernel::prepareKernel() {
78    mPabloCompiler->initializeKernelData();
79    KernelBuilder::prepareKernel();
80}
81
82void PabloKernel::generateDoBlockMethod() const {
83    auto savePoint = iBuilder->saveIP();
84    Module * const m = iBuilder->getModule();
85    Function * const f = m->getFunction(mKernelName + doBlock_suffix);
86    Value * const self = &*(f->arg_begin());
87    mPabloCompiler->compile(self, f);
88    iBuilder->CreateRetVoid();
89    #ifndef NDEBUG
90    llvm::verifyFunction(*f, &errs());
91    #endif
92    iBuilder->restoreIP(savePoint);
93}
94
95void PabloKernel::generateFinalBlockMethod() const {
96    auto savePoint = iBuilder->saveIP();
97    Module * m = iBuilder->getModule();
98    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
99    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
100    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_entry", finalBlockFunction, 0));
101    // Final Block arguments: self, remaining, then the standard DoBlock args.
102    Function::arg_iterator args = finalBlockFunction->arg_begin();
103    Value * self = &*(args++);
104    Value * remaining = &*(args++);
105    std::vector<Value *> doBlockArgs = {self};
106    while (args != finalBlockFunction->arg_end()){
107        doBlockArgs.push_back(&*args++);
108    }
109    // Standard Pablo convention for final block processing: set a bit marking
110    // the position just past EOF, as well as a mask marking all positions past EOF.
111    setScalarField(self, "EOFbit", iBuilder->bitblock_set_bit(remaining));
112    setScalarField(self, "EOFmask", iBuilder->bitblock_mask_from(remaining));
113    iBuilder->CreateCall(doBlockFunction, doBlockArgs);
114    iBuilder->CreateRetVoid();
115    #ifndef NDEBUG
116    llvm::verifyFunction(*finalBlockFunction, &errs());
117    #endif
118    iBuilder->restoreIP(savePoint);
119}
120
121PabloKernel::PabloKernel(IDISA::IDISA_Builder * builder, const std::string & kernelName)
122: KernelBuilder(builder, kernelName, {}, {}, {}, {}, {Binding{builder->getBitBlockType(), "EOFbit"}, Binding{builder->getBitBlockType(), "EOFmask"}})
123, mPabloCompiler(new PabloCompiler(this))
124, mSymbolTable(new SymbolGenerator(mAllocator))
125, mEntryBlock(PabloBlock::Create(this)) {
126setDoBlockUpdatesProducedItemCountsAttribute(false);
127}
128
129PabloKernel::~PabloKernel() {
130    delete mPabloCompiler;
131    delete mSymbolTable; 
132}
133
Note: See TracBrowser for help on using the repository browser.