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

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

Simplify kernel DoBlock? interface

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#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, llvm::ConstantInt *>;
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   
43//protected:
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    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    // Set a specific _DoBlock return type;
77   
78    void setDoBlockReturnType(llvm::Type * t);
79       
80    // Run-time access of Kernel State and parameters of methods for
81    // use in implementing kernels.
82   
83    // Get the index of a named scalar field within the kernel state struct.
84    llvm::Value * getScalarIndex(std::string);
85   
86    // Get the value of a scalar field for a given instance.
87    llvm::Value * getScalarField(llvm::Value * self, std::string fieldName);
88   
89    // Set the value of a scalar field for a given instance.
90    void setScalarField(llvm::Value * self, std::string fieldName, llvm::Value * newFieldVal);
91   
92    // Get a parameter by name.
93    llvm::Value * getParameter(llvm::Function * f, std::string paramName);
94   
95    // Helper.
96    Value * getCircularBufferBlockPointer(llvm::Value * self, std::string streamName, llvm::Value * blockNo);
97
98
99//protected:
100
101    std::vector<llvm::Type *>  mKernelFields;
102    NameMap                    mInternalStateNameMap;
103};
104}
105#endif
Note: See TracBrowser for help on using the repository browser.