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
RevLine 
[4924]1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5
[5063]6#ifndef KERNEL_BUILDER_H
7#define KERNEL_BUILDER_H
8
9
10#include "interface.h"
[4959]11#include <vector>
[5063]12#include <llvm/IR/Type.h>
13#include <IDISA/idisa_builder.h>
[4970]14#include <boost/container/flat_map.hpp>
[4924]15
16
[4974]17namespace kernel {
[5051]18   
[5063]19class KernelBuilder : public KernelInterface {
20    using NameMap = boost::container::flat_map<std::string, llvm::ConstantInt *>;
[4974]21
[4924]22public:
[5063]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);
[5051]30   
[5074]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();
[5051]35   
[5074]36    // Generate the Kernel to the current module (iBuilder->getModule()).
37    void generateKernel();
[5051]38   
[5074]39protected:
[5063]40    //
[5074]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.
[5051]50   
[5074]51    virtual void prepareKernelStateType();
[5063]52   
[5074]53    // Each kernel builder subtype must provide its own logic for generating
54    // doBlock calls.
55    virtual void generateDoBlockMethod() = 0;
[5063]56   
[5074]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       
[5063]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);
[4959]84
[4974]85protected:
[4959]86
[5063]87    std::vector<llvm::Type *>  mKernelFields;
88    NameMap                    mInternalStateNameMap;
[4924]89};
[4959]90}
[5063]91#endif
Note: See TracBrowser for help on using the repository browser.