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

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

Kernels compute and check produced item counts

File size: 3.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 <pablo/pablo_kernel.h>
7#include <pablo/pablo_compiler.h>
8#include <llvm/Support/Debug.h>
9
10
11using namespace pablo;
12using namespace kernel;
13using namespace parabix;
14
15PabloKernel::PabloKernel(IDISA::IDISA_Builder * builder,
16                         std::string kernelName,
17                         PabloFunction * function,
18                         std::vector<std::string> accumulators) :
19    KernelBuilder(builder, kernelName,
20                    {StreamSetBinding{StreamSetType(function->getNumOfParameters(), 1), "inputs"}},
21                    {},
22                    {},
23                    {},
24                    {ScalarBinding{builder->getBitBlockType(), "EOFbit"}, ScalarBinding{builder->getBitBlockType(), "EOFmask"}}),
25    mPabloFunction(function) {
26    unsigned output_streams = function->getNumOfResults();
27    if (output_streams > 0) {
28        mStreamSetOutputs.push_back(StreamSetBinding{StreamSetType(output_streams, 1), "outputs"});
29    }
30    mScalarOutputs = accumBindings(accumulators);
31    pablo_compiler = new PabloCompiler(builder, this, function);
32}
33
34std::vector<ScalarBinding> PabloKernel::accumBindings(std::vector<std::string> accum_names) {
35    std::vector<ScalarBinding> vec;
36    Type * accum_t = iBuilder->getSizeTy();
37    for (auto a : accum_names) {
38        vec.push_back(ScalarBinding{accum_t, a});
39        addScalar(accum_t, a);
40    }
41    return vec;
42}
43
44void PabloKernel::prepareKernel() {
45    Type * carryDataType = pablo_compiler->initializeKernelData();
46    addScalar(carryDataType, "carries");
47    KernelBuilder::prepareKernel();
48}
49
50void PabloKernel::generateDoBlockMethod() {
51    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
52    Module * m = iBuilder->getModule();
53    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
54    pablo_compiler->compile(doBlockFunction);
55    Function::arg_iterator args = doBlockFunction->arg_begin();
56    Value * self = &*(args);
57    Value * produced = getProducedItemCount(self);
58    produced = iBuilder->CreateAdd(produced, ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getStride()));
59    setProducedItemCount(self, produced);
60    iBuilder->CreateRetVoid();
61    iBuilder->restoreIP(savePoint);
62}
63
64void PabloKernel::generateFinalBlockMethod() {
65    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
66    Module * m = iBuilder->getModule();
67    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
68    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
69    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_entry", finalBlockFunction, 0));
70    // Final Block arguments: self, remaining, then the standard DoBlock args.
71    Function::arg_iterator args = finalBlockFunction->arg_begin();
72    Value * self = &*(args++);
73    Value * remaining = &*(args++);
74    std::vector<Value *> doBlockArgs = {self};
75    while (args != finalBlockFunction->arg_end()){
76        doBlockArgs.push_back(&*args++);
77    }
78    // Standard Pablo convention for final block processing: set a bit marking
79    // the position just past EOF, as well as a mask marking all positions past EOF.
80    setScalarField(self, "EOFbit", iBuilder->bitblock_set_bit(remaining));
81    setScalarField(self, "EOFmask", iBuilder->bitblock_mask_from(remaining));
82    iBuilder->CreateCall(doBlockFunction, doBlockArgs);
83    /* Adjust the produced item count */
84    Value * produced = getProducedItemCount(self);
85    produced = iBuilder->CreateSub(produced, ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getStride()));
86    setProducedItemCount(self, iBuilder->CreateAdd(produced, remaining));
87    iBuilder->CreateRetVoid();
88    iBuilder->restoreIP(savePoint);
89}
90
Note: See TracBrowser for help on using the repository browser.