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

Last change on this file since 5220 was 5220, checked in by cameron, 2 years ago

Automate instance creation

File size: 5.6 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 "streamset.h"
11#include "interface.h"
12#include <vector>
13#include <llvm/IR/Type.h>
14#include <IDISA/idisa_builder.h>
15#include <boost/container/flat_map.hpp>
16
17const std::string blockNoScalar = "blockNo";
18const std::string logicalSegmentNoScalar = "logicalSegNo";
19const std::string processedItemCount = "processedItemCount";
20const std::string producedItemCount = "producedItemCount";
21const std::string terminationSignal = "terminationSignal";
22const std::string structPtrSuffix = "_structPtr";
23const std::string blkMaskSuffix = "_blkMask";
24
25using namespace parabix;
26namespace kernel {
27   
28class KernelBuilder : public KernelInterface {
29    using NameMap = boost::container::flat_map<std::string, unsigned>;
30
31public:
32    KernelBuilder(IDISA::IDISA_Builder * builder,
33                    std::string kernelName,
34                    std::vector<Binding> stream_inputs,
35                    std::vector<Binding> stream_outputs,
36                    std::vector<Binding> scalar_parameters,
37                    std::vector<Binding> scalar_outputs,
38                    std::vector<Binding> internal_scalars);
39   
40    // Create a module for the kernel, including the kernel state type declaration and
41    // the full implementation of all required methods.     
42    //
43    std::unique_ptr<llvm::Module> createKernelModule(std::vector<StreamSetBuffer *> input_buffers, std::vector<StreamSetBuffer *> output_buffers);
44   
45    // Generate the Kernel to the current module (iBuilder->getModule()).
46    void generateKernel(std::vector<StreamSetBuffer *> input_buffers, std::vector<StreamSetBuffer *> output_buffers);
47   
48    void createInstance() override;
49
50    Function * generateThreadFunction(std::string name);
51
52    Value * getBlockNo(Value * self);
53    virtual llvm::Value * getLogicalSegmentNo(llvm::Value * kernelInstance) override;
54    virtual llvm::Value * getProcessedItemCount(llvm::Value * kernelInstance) override;
55    virtual llvm::Value * getProducedItemCount(llvm::Value * kernelInstance) override;
56    llvm::Value * getTerminationSignal(llvm::Value * kernelInstance);
57   
58    inline llvm::IntegerType * getSizeTy() {
59        return getBuilder()->getSizeTy();
60    }
61
62    inline Type * getStreamSetTy(const unsigned NumElements = 1, const unsigned FieldWidth = 1) {
63        return getBuilder()->getStreamSetTy(NumElements, FieldWidth);
64    }
65
66protected:
67    //
68    // Kernel builder subtypes define their logic of kernel construction
69    // in terms of 3 virtual methods for
70    // (a) preparing the Kernel state data structure
71    // (b) defining the logic of the doBlock function, and
72    // (c) defining the logic of the finalBlock function.
73    //
74    // Note: the kernel state data structure must only be finalized after
75    // all scalar fields have been added.   If there are no fields to
76    // be added, the default method for preparing kernel state may be used.
77   
78    virtual void prepareKernel();
79   
80    // Each kernel builder subtype must provide its own logic for generating
81    // doBlock calls.
82    virtual void generateDoBlockMethod() = 0;
83    virtual void generateDoBlockLogic(Value * self, Value * blockNo);
84
85    // Each kernel builder subtypre must also specify the logic for processing the
86    // final block of stream data, if there is any special processing required
87    // beyond simply calling the doBlock function.   In the case that the final block
88    // processing may be trivially implemented by dispatching to the doBlock method
89    // without additional preparation, the default generateFinalBlockMethod need
90    // not be overridden.
91   
92    virtual void generateFinalBlockMethod();
93   
94    virtual void generateDoSegmentMethod();
95   
96    // Add an additional scalar field to the KernelState struct.
97    // Must occur before any call to addKernelDeclarations or createKernelModule.
98    void addScalar(llvm::Type * t, std::string name);
99   
100    // Run-time access of Kernel State and parameters of methods for
101    // use in implementing kernels.
102   
103    // Get the index of a named scalar field within the kernel state struct.
104    llvm::Value * getScalarIndex(std::string);
105   
106    // Get the value of a scalar field for a given instance.
107    llvm::Value * getScalarField(llvm::Value * self, std::string fieldName);
108
109    // Set the value of a scalar field for a given instance.
110    void setScalarField(llvm::Value * self, std::string fieldName, llvm::Value * newFieldVal);
111   
112    // Get a parameter by name.
113    llvm::Value * getParameter(llvm::Function * f, std::string paramName);
114   
115    // Stream set helpers.
116    unsigned getStreamSetIndex(std::string name);
117   
118    llvm::Value * getScalarFieldPtr(Value * self, std::string name);
119
120    llvm::Value * getStreamSetStructPtr(Value * self, std::string name);
121    size_t getStreamSetBufferSize(Value * self, std::string name);
122
123    llvm::Value * getStreamSetBlockPtr(Value * self, std::string name, Value * blockNo);
124   
125    void setBlockNo(Value * self, Value * newFieldVal);
126    virtual void setLogicalSegmentNo(llvm::Value * self, Value * newFieldVal);
127    virtual void setProcessedItemCount(llvm::Value * self, Value * newFieldVal);
128    virtual void setProducedItemCount(llvm::Value * self, Value * newFieldVal);
129    void setTerminationSignal(llvm::Value * self);
130
131protected:
132
133    std::vector<llvm::Type *>  mKernelFields;
134    NameMap                    mInternalStateNameMap;
135    NameMap                    mStreamSetNameMap;
136    std::vector<StreamSetBuffer *> mStreamSetInputBuffers;
137    std::vector<StreamSetBuffer *> mStreamSetOutputBuffers;
138
139};
140}
141#endif
Note: See TracBrowser for help on using the repository browser.