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

Last change on this file since 5227 was 5227, checked in by nmedfort, 2 years ago

Rewrite of the CarryManager? to support non-carry-collapsing loops.

File size: 6.3 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 * getProcessedItemCount(llvm::Value * kernelInstance) override;
54    virtual llvm::Value * getProducedItemCount(llvm::Value * kernelInstance) override;
55    llvm::Value * getTerminationSignal(llvm::Value * kernelInstance);
56   
57    inline llvm::IntegerType * getSizeTy() {
58        return getBuilder()->getSizeTy();
59    }
60
61    inline Type * getStreamSetTy(const unsigned NumElements = 1, const unsigned FieldWidth = 1) {
62        return getBuilder()->getStreamSetTy(NumElements, FieldWidth);
63    }
64   
65    // Synchronization actions for executing a kernel for a particular logical segment.
66    //
67    // Before the segment is processed, acquireLogicalSegmentNo must be used to load
68    // the segment number of the kernel state to ensure that the previous segment is
69    // complete (by checking that the acquired segment number is equal to the desired segment
70    // number).
71    // After all segment processing actions for the kernel are complete, and any necessary
72    // data has been extracted from the kernel for further pipeline processing, the
73    // segment number must be incremented and stored using releaseLogicalSegmentNo.
74    llvm::Value * acquireLogicalSegmentNo(llvm::Value * kernelInstance);
75    void releaseLogicalSegmentNo(llvm::Value * kernelInstance, Value * newFieldVal);
76
77
78protected:
79    //
80    // Kernel builder subtypes define their logic of kernel construction
81    // in terms of 3 virtual methods for
82    // (a) preparing the Kernel state data structure
83    // (b) defining the logic of the doBlock function, and
84    // (c) defining the logic of the finalBlock function.
85    //
86    // Note: the kernel state data structure must only be finalized after
87    // all scalar fields have been added.   If there are no fields to
88    // be added, the default method for preparing kernel state may be used.
89   
90    virtual void prepareKernel();
91   
92    // Each kernel builder subtype must provide its own logic for generating
93    // doBlock calls.
94    virtual void generateDoBlockMethod() = 0;
95    virtual void generateDoBlockLogic(Value * self, Value * blockNo);
96
97    // Each kernel builder subtypre must also specify the logic for processing the
98    // final block of stream data, if there is any special processing required
99    // beyond simply calling the doBlock function.   In the case that the final block
100    // processing may be trivially implemented by dispatching to the doBlock method
101    // without additional preparation, the default generateFinalBlockMethod need
102    // not be overridden.
103   
104    virtual void generateFinalBlockMethod();
105   
106    virtual void generateDoSegmentMethod();
107   
108    // Add an additional scalar field to the KernelState struct.
109    // Must occur before any call to addKernelDeclarations or createKernelModule.
110    unsigned addScalar(llvm::Type * type, std::string name);
111
112    unsigned getScalarCount() const;
113
114    // Run-time access of Kernel State and parameters of methods for
115    // use in implementing kernels.
116   
117    // Get the index of a named scalar field within the kernel state struct.
118    ConstantInt * getScalarIndex(const std::string & name) const;
119   
120    // Get the value of a scalar field for a given instance.
121    llvm::Value * getScalarField(llvm::Value * self, std::string fieldName);
122
123    // Set the value of a scalar field for a given instance.
124    void setScalarField(llvm::Value * self, std::string fieldName, llvm::Value * newFieldVal);
125   
126    // Get a parameter by name.
127    llvm::Value * getParameter(llvm::Function * f, std::string paramName);
128   
129    // Stream set helpers.
130    unsigned getStreamSetIndex(std::string name);
131   
132    llvm::Value * getScalarFieldPtr(Value * self, const std::string & name);
133
134    llvm::Value * getStreamSetStructPtr(Value * self, std::string name);
135    size_t getStreamSetBufferSize(Value * self, std::string name);
136
137    llvm::Value * getStreamSetBlockPtr(Value * self, std::string name, Value * blockNo);
138   
139    void setBlockNo(Value * self, Value * newFieldVal);
140    virtual void setProcessedItemCount(llvm::Value * self, Value * newFieldVal);
141    virtual void setProducedItemCount(llvm::Value * self, Value * newFieldVal);
142    void setTerminationSignal(llvm::Value * self);
143
144protected:
145
146    std::vector<llvm::Type *>  mKernelFields;
147    NameMap                    mKernelMap;
148    NameMap                    mStreamSetNameMap;
149    std::vector<StreamSetBuffer *> mStreamSetInputBuffers;
150    std::vector<StreamSetBuffer *> mStreamSetOutputBuffers;
151
152};
153}
154#endif
Note: See TracBrowser for help on using the repository browser.