source: icGREP/icgrep-devel/icgrep/kernels/kernel.h @ 5111

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

DoBlock/FinalBlock/DoSegment? Kernel functions always return void; clean-up

File size: 3.9 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#ifndef KERNEL_BUILDER_H
7#define KERNEL_BUILDER_H
8
9
10#include "interface.h"
11#include <vector>
12#include <llvm/IR/Type.h>
13#include <IDISA/idisa_builder.h>
14#include <boost/container/flat_map.hpp>
15
16const std::string blockNoScalar = "blockNo";
17const std::string basePtrSuffix = "_basePtr";
18const std::string blkMaskSuffix = "_blkMask";
19
20
21namespace kernel {
22   
23class KernelBuilder : public KernelInterface {
24    using NameMap = boost::container::flat_map<std::string, unsigned>;
25
26public:
27    KernelBuilder(IDISA::IDISA_Builder * builder,
28                    std::string kernelName,
29                    std::vector<StreamSetBinding> stream_inputs,
30                    std::vector<StreamSetBinding> stream_outputs,
31                    std::vector<ScalarBinding> scalar_parameters,
32                    std::vector<ScalarBinding> scalar_outputs,
33                    std::vector<ScalarBinding> internal_scalars);
34   
35    // Create a module for the kernel, including the kernel state type declaration and
36    // the full implementation of all required methods.     
37    //
38    std::unique_ptr<llvm::Module> createKernelModule();
39   
40    // Generate the Kernel to the current module (iBuilder->getModule()).
41    void generateKernel();
42   
43protected:
44    //
45    // Kernel builder subtypes define their logic of kernel construction
46    // in terms of 3 virtual methods for
47    // (a) preparing the Kernel state data structure
48    // (b) defining the logic of the doBlock function, and
49    // (c) defining the logic of the finalBlock function.
50    //
51    // Note: the kernel state data structure must only be finalized after
52    // all scalar fields have been added.   If there are no fields to
53    // be added, the default method for preparing kernel state may be used.
54   
55    virtual void prepareKernel();
56   
57    // Each kernel builder subtype must provide its own logic for generating
58    // doBlock calls.
59    virtual void generateDoBlockMethod() = 0;
60   
61    // Each kernel builder subtypre must also specify the logic for processing the
62    // final block of stream data, if there is any special processing required
63    // beyond simply calling the doBlock function.   In the case that the final block
64    // processing may be trivially implemented by dispatching to the doBlock method
65    // without additional preparation, the default generateFinalBlockMethod need
66    // not be overridden.
67   
68    virtual void generateFinalBlockMethod();
69   
70    virtual void generateDoSegmentMethod();
71   
72    // Add an additional scalar field to the KernelState struct.
73    // Must occur before any call to addKernelDeclarations or createKernelModule.
74    void addScalar(llvm::Type * t, std::string scalarName);
75   
76    // Run-time access of Kernel State and parameters of methods for
77    // use in implementing kernels.
78   
79    // Get the index of a named scalar field within the kernel state struct.
80    llvm::Value * getScalarIndex(std::string);
81   
82    // Get the value of a scalar field for a given instance.
83    llvm::Value * getScalarField(llvm::Value * self, std::string fieldName);
84   
85    // Set the value of a scalar field for a given instance.
86    void setScalarField(llvm::Value * self, std::string fieldName, llvm::Value * newFieldVal);
87   
88    // Get a parameter by name.
89    llvm::Value * getParameter(llvm::Function * f, std::string paramName);
90   
91    // Stream set helpers.
92    unsigned getStreamSetIndex(std::string ssName);
93   
94    llvm::Value * getStreamSetBasePtr(Value * self, std::string ssName);
95    size_t getStreamSetBufferSize(Value * self, std::string ssName);
96
97    llvm::Value * getStreamSetBlockPtr(Value * self, std::string ssName, Value * blockNo);
98       
99protected:
100
101    std::vector<llvm::Type *>  mKernelFields;
102    NameMap                    mInternalStateNameMap;
103    NameMap                    mStreamSetNameMap;
104};
105}
106#endif
Note: See TracBrowser for help on using the repository browser.