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

Last change on this file since 5240 was 5240, checked in by nmedfort, 18 months ago

Cleaned up memory leaks + some warning messages.

File size: 5.2 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/pablo_kernel.h>
7#include <pablo/codegenstate.h>
8#include <pablo/pablo_compiler.h>
9// #include <llvm/Support/Debug.h>
10#include <pablo/pe_var.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() {
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    Value * produced = getProducedItemCount(self);
89    produced = iBuilder->CreateAdd(produced, ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getStride()));
90    setProducedItemCount(self, produced);
91    iBuilder->CreateRetVoid();
92    #ifndef NDEBUG
93    llvm::verifyFunction(*f, &errs());
94    #endif
95    iBuilder->restoreIP(savePoint);
96}
97
98void PabloKernel::generateFinalBlockMethod() {
99    auto savePoint = iBuilder->saveIP();
100    Module * m = iBuilder->getModule();
101    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
102    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
103    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_entry", finalBlockFunction, 0));
104    // Final Block arguments: self, remaining, then the standard DoBlock args.
105    Function::arg_iterator args = finalBlockFunction->arg_begin();
106    Value * self = &*(args++);
107    Value * remaining = &*(args++);
108    std::vector<Value *> doBlockArgs = {self};
109    while (args != finalBlockFunction->arg_end()){
110        doBlockArgs.push_back(&*args++);
111    }
112    // Standard Pablo convention for final block processing: set a bit marking
113    // the position just past EOF, as well as a mask marking all positions past EOF.
114    setScalarField(self, "EOFbit", iBuilder->bitblock_set_bit(remaining));
115    setScalarField(self, "EOFmask", iBuilder->bitblock_mask_from(remaining));
116    iBuilder->CreateCall(doBlockFunction, doBlockArgs);
117    /* Adjust the produced item count */
118    Value * produced = getProducedItemCount(self);
119    produced = iBuilder->CreateSub(produced, ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getStride()));
120    setProducedItemCount(self, iBuilder->CreateAdd(produced, remaining));
121    iBuilder->CreateRetVoid();
122    #ifndef NDEBUG
123    llvm::verifyFunction(*finalBlockFunction, &errs());
124    #endif
125    iBuilder->restoreIP(savePoint);
126}
127
128void PabloKernel::initializeKernelState(Value * self) {
129    iBuilder->CreateStore(Constant::getNullValue(mKernelStateType), self);
130}
131
132PabloKernel::PabloKernel(IDISA::IDISA_Builder * builder, const std::string & kernelName)
133: KernelBuilder(builder, kernelName, {}, {}, {}, {}, {Binding{builder->getBitBlockType(), "EOFbit"}, Binding{builder->getBitBlockType(), "EOFmask"}})
134, mPabloCompiler(new PabloCompiler(this))
135, mSymbolTable(new SymbolGenerator(mAllocator))
136, mEntryBlock(PabloBlock::Create(this))
137{
138
139}
140
141PabloKernel::~PabloKernel() {
142    delete mPabloCompiler;
143    delete mSymbolTable; 
144}
145
Note: See TracBrowser for help on using the repository browser.