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

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

Distinguishing between StreamType? and StreamSetType?; fixing StreamSetBlock? pointer calculations

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