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

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

Kernel infrastructure: move common logic into KernelBuilder? base class; demo linking in wc

File size: 3.4 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 prepareKernelStateType();
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    // Run-time access of Kernel State and parameters of methods for
71    // use in implementing kernels.
72   
73    // Get the index of a named scalar field within the kernel state struct.
74    llvm::Value * getScalarIndex(std::string);
75   
76    // Get the value of a scalar field for a given instance.
77    llvm::Value * getScalarField(llvm::Value * self, std::string fieldName);
78   
79    // Set the value of a scalar field for a given instance.
80    void setScalarField(llvm::Value * self, std::string fieldName, llvm::Value * newFieldVal);
81   
82    // Get a parameter by name.
83    llvm::Value * getParameter(llvm::Function * f, std::string paramName);
84
85protected:
86
87    std::vector<llvm::Type *>  mKernelFields;
88    NameMap                    mInternalStateNameMap;
89};
90}
91#endif
Note: See TracBrowser for help on using the repository browser.