source: icGREP/icgrep-devel/icgrep/kernels/toolchain.h @ 5414

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

Parabix driver can take ownership of kernelbuilder instances; uniquify mmap kernel name

File size: 5.1 KB
Line 
1/*
2 *  Copyright (c) 2017 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#ifndef TOOLCHAIN_H
8#define TOOLCHAIN_H
9#include <string>
10#include <IR_Gen/idisa_builder.h>
11#include <llvm/IR/TypeBuilder.h>
12#include <boost/container/flat_map.hpp>
13
14namespace llvm { class ExecutionEngine; }
15namespace llvm { class Module; }
16namespace llvm { class TargetMachine; }
17namespace llvm { class formatted_raw_ostream; }
18namespace llvm { namespace cl { class OptionCategory; } }
19namespace IDISA { class IDISA_Builder; }
20namespace kernel { class KernelBuilder; }
21//namespace parabix { class StreamSetBuffer; }
22#include <kernels/streamset.h>
23class ParabixObjectCache;
24
25namespace codegen {
26const llvm::cl::OptionCategory * codegen_flags();
27
28// Command Parameters
29enum DebugFlags {
30    ShowIR,
31#ifndef USE_LLVM_3_6
32    ShowASM,
33#endif
34    SerializeThreads
35};
36
37bool DebugOptionIsSet(DebugFlags flag);
38
39
40extern char OptLevel;  // set from command line
41extern int BlockSize;  // set from command line
42extern int SegmentSize;  // set from command line
43extern int BufferSegments;
44extern int ThreadNum;
45extern bool EnableAsserts;
46#ifdef CUDA_ENABLED
47extern bool NVPTX;
48extern int GroupNum;
49#endif
50}
51
52#ifdef CUDA_ENABLED
53void setNVPTXOption();
54void Compile2PTX (llvm::Module * m, std::string IRFilename, std::string PTXFilename);
55#endif
56
57void AddParabixVersionPrinter();
58
59bool AVX2_available();
60
61class ParabixDriver {
62    using ModuleMap = boost::container::flat_map<kernel::KernelBuilder *, llvm::Module *>;
63public:
64    ParabixDriver(IDISA::IDISA_Builder * iBuilder);
65
66    ~ParabixDriver();
67   
68    IDISA::IDISA_Builder * getIDISA_Builder() {return iBuilder;}
69   
70    parabix::ExternalFileBuffer * addExternalBuffer(std::unique_ptr<parabix::ExternalFileBuffer> b, llvm::Value * externalBuf);
71   
72    parabix::StreamSetBuffer * addBuffer(std::unique_ptr<parabix::StreamSetBuffer> b);
73   
74    kernel::KernelBuilder * addKernelInstance(std::unique_ptr<kernel::KernelBuilder> kb);
75   
76    void addKernelCall(kernel::KernelBuilder & kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
77    void makeKernelCall(kernel::KernelBuilder * kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
78   
79    void generatePipelineIR();
80   
81    template <typename ExternalFunctionType>
82    void addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const;
83
84    void addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, llvm::FunctionType * type, void * functionPtr) const;
85
86    void linkAndFinalize();
87   
88    void * getPointerToMain();
89
90private:
91    IDISA::IDISA_Builder * const            iBuilder;
92    llvm::Module * const                    mMainModule;
93    llvm::TargetMachine *                   mTarget;
94    llvm::ExecutionEngine *                 mEngine;
95    ParabixObjectCache *                    mCache;
96    std::vector<kernel::KernelBuilder *>    mPipeline;
97    // Owned kernels and buffers that will persist with this ParabixDriver instance.
98    std::vector<std::unique_ptr<kernel::KernelBuilder>> mOwnedKernels;
99    std::vector<std::unique_ptr<parabix::StreamSetBuffer>> mOwnedBuffers;
100    ModuleMap                               mModuleMap;
101};
102
103namespace {
104
105// NOTE: Currently, LLVM TypeBuilder can deduce FuntionTypes for up to 5 arguments. The following
106// templates have no limit but should be deprecated if the TypeBuilder ever supports n-ary functions.
107
108template<unsigned i, typename... Args>
109struct ParameterTypeBuilder;
110
111template<unsigned i, typename A1, typename... An>
112struct ParameterTypeBuilder<i, A1, An...> {
113    static void get(llvm::LLVMContext & C, llvm::Type ** params) {
114        ParameterTypeBuilder<i, A1>::get(C, params);
115        ParameterTypeBuilder<i + 1, An...>::get(C, params);
116    }
117};
118
119template<unsigned i, typename A>
120struct ParameterTypeBuilder<i, A> {
121    static void get(llvm::LLVMContext & C, llvm::Type ** params) {
122        params[i] = llvm::TypeBuilder<A, false>::get(C);
123    }
124};
125
126template<typename T>
127struct FunctionTypeBuilder;
128
129template<typename R, typename... Args>
130struct FunctionTypeBuilder<R(Args...)> {
131    static llvm::FunctionType * get(llvm::LLVMContext & C) {
132        llvm::Type * params[sizeof...(Args)];
133        ParameterTypeBuilder<0, Args...>::get(C, params);
134        return llvm::FunctionType::get(llvm::TypeBuilder<R, false>::get(C), params, false);
135    }
136};
137
138template<typename R>
139struct FunctionTypeBuilder<R()> {
140    static llvm::FunctionType * get(llvm::LLVMContext & C) {
141        return llvm::FunctionType::get(llvm::TypeBuilder<R, false>::get(C), false);
142    }
143};
144
145}
146
147template <typename ExternalFunctionType>
148void ParabixDriver::addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const {
149    llvm::FunctionType * const type = FunctionTypeBuilder<ExternalFunctionType>::get(iBuilder->getContext());
150    assert ("FunctionTypeBuilder did not resolve a function type." && type);
151    addExternalLink(kb, name, type, reinterpret_cast<void *>(functionPtr));
152}
153
154#endif
Note: See TracBrowser for help on using the repository browser.