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

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

Updates for kernels with variable output length; stdout kernel

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