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

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

Linear buffers; stdout_kernel; cache-aligned alloca, restructured u8u16, clean-ups

File size: 5.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 "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<StreamSetBinding> stream_inputs,
35                    std::vector<StreamSetBinding> stream_outputs,
36                    std::vector<ScalarBinding> scalar_parameters,
37                    std::vector<ScalarBinding> scalar_outputs,
38                    std::vector<ScalarBinding> 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    llvm::Value * createInstance(std::vector<Value *> args) 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    virtual llvm::Value * getTerminationSignal(llvm::Value * kernelInstance) override;
57   
58   
59protected:
60    //
61    // Kernel builder subtypes define their logic of kernel construction
62    // in terms of 3 virtual methods for
63    // (a) preparing the Kernel state data structure
64    // (b) defining the logic of the doBlock function, and
65    // (c) defining the logic of the finalBlock function.
66    //
67    // Note: the kernel state data structure must only be finalized after
68    // all scalar fields have been added.   If there are no fields to
69    // be added, the default method for preparing kernel state may be used.
70   
71    virtual void prepareKernel();
72   
73    // Each kernel builder subtype must provide its own logic for generating
74    // doBlock calls.
75    virtual void generateDoBlockMethod() = 0;
76    virtual void generateDoBlockLogic(Value * self, Value * blockNo);
77
78    // Each kernel builder subtypre must also specify the logic for processing the
79    // final block of stream data, if there is any special processing required
80    // beyond simply calling the doBlock function.   In the case that the final block
81    // processing may be trivially implemented by dispatching to the doBlock method
82    // without additional preparation, the default generateFinalBlockMethod need
83    // not be overridden.
84   
85    virtual void generateFinalBlockMethod();
86   
87    virtual void generateDoSegmentMethod();
88   
89    // Add an additional scalar field to the KernelState struct.
90    // Must occur before any call to addKernelDeclarations or createKernelModule.
91    void addScalar(llvm::Type * t, std::string scalarName);
92   
93    // Run-time access of Kernel State and parameters of methods for
94    // use in implementing kernels.
95   
96    // Get the index of a named scalar field within the kernel state struct.
97    llvm::Value * getScalarIndex(std::string);
98   
99    // Get the value of a scalar field for a given instance.
100    llvm::Value * getScalarField(llvm::Value * self, std::string fieldName);
101   
102    // Set the value of a scalar field for a given instance.
103    void setScalarField(llvm::Value * self, std::string fieldName, llvm::Value * newFieldVal);
104   
105    // Get a parameter by name.
106    llvm::Value * getParameter(llvm::Function * f, std::string paramName);
107   
108    // Stream set helpers.
109    unsigned getStreamSetIndex(std::string ssName);
110   
111    llvm::Value * getStreamSetStructPtr(Value * self, std::string ssName);
112    size_t getStreamSetBufferSize(Value * self, std::string ssName);
113
114    llvm::Value * getStreamSetBlockPtr(Value * self, std::string ssName, Value * blockNo);
115
116    void setBlockNo(Value * self, Value * newFieldVal);
117    virtual void setLogicalSegmentNo(llvm::Value * self, Value * newFieldVal);
118    virtual void setProcessedItemCount(llvm::Value * self, Value * newFieldVal);
119    virtual void setProducedItemCount(llvm::Value * self, Value * newFieldVal);
120    virtual void setTerminationSignal(llvm::Value * self, Value * newFieldVal);
121   
122   
123protected:
124
125    std::vector<llvm::Type *>  mKernelFields;
126    NameMap                    mInternalStateNameMap;
127    NameMap                    mStreamSetNameMap;
128    std::vector<StreamSetBuffer *> mStreamSetInputBuffers;
129    std::vector<StreamSetBuffer *> mStreamSetOutputBuffers;
130
131};
132}
133#endif
Note: See TracBrowser for help on using the repository browser.