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

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

Parabix driver can take ownership and allocate buffers

File size: 4.9 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    void addKernelCall(kernel::KernelBuilder & kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
75   
76    void generatePipelineIR();
77   
78    template <typename ExternalFunctionType>
79    void addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const;
80
81    void addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, llvm::FunctionType * type, void * functionPtr) const;
82
83    void linkAndFinalize();
84   
85    void * getPointerToMain();
86
87private:
88    IDISA::IDISA_Builder * const            iBuilder;
89    llvm::Module * const                    mMainModule;
90    llvm::TargetMachine *                   mTarget;
91    llvm::ExecutionEngine *                 mEngine;
92    ParabixObjectCache *                    mCache;
93    std::vector<kernel::KernelBuilder *>    mKernelList;
94    // Owned kernels and buffers that will persist with this ParabixDriver instance.
95    std::vector<std::unique_ptr<kernel::KernelBuilder>> mOwnedKernels;
96    std::vector<std::unique_ptr<parabix::StreamSetBuffer>> mOwnedBuffers;
97    ModuleMap                               mModuleMap;
98};
99
100namespace {
101
102// NOTE: Currently, LLVM TypeBuilder can deduce FuntionTypes for up to 5 arguments. The following
103// templates have no limit but should be deprecated if the TypeBuilder ever supports n-ary functions.
104
105template<unsigned i, typename... Args>
106struct ParameterTypeBuilder;
107
108template<unsigned i, typename A1, typename... An>
109struct ParameterTypeBuilder<i, A1, An...> {
110    static void get(llvm::LLVMContext & C, llvm::Type ** params) {
111        ParameterTypeBuilder<i, A1>::get(C, params);
112        ParameterTypeBuilder<i + 1, An...>::get(C, params);
113    }
114};
115
116template<unsigned i, typename A>
117struct ParameterTypeBuilder<i, A> {
118    static void get(llvm::LLVMContext & C, llvm::Type ** params) {
119        params[i] = llvm::TypeBuilder<A, false>::get(C);
120    }
121};
122
123template<typename T>
124struct FunctionTypeBuilder;
125
126template<typename R, typename... Args>
127struct FunctionTypeBuilder<R(Args...)> {
128    static llvm::FunctionType * get(llvm::LLVMContext & C) {
129        llvm::Type * params[sizeof...(Args)];
130        ParameterTypeBuilder<0, Args...>::get(C, params);
131        return llvm::FunctionType::get(llvm::TypeBuilder<R, false>::get(C), params, false);
132    }
133};
134
135template<typename R>
136struct FunctionTypeBuilder<R()> {
137    static llvm::FunctionType * get(llvm::LLVMContext & C) {
138        return llvm::FunctionType::get(llvm::TypeBuilder<R, false>::get(C), false);
139    }
140};
141
142}
143
144template <typename ExternalFunctionType>
145void ParabixDriver::addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const {
146    llvm::FunctionType * const type = FunctionTypeBuilder<ExternalFunctionType>::get(iBuilder->getContext());
147    assert ("FunctionTypeBuilder did not resolve a function type." && type);
148    addExternalLink(kb, name, type, reinterpret_cast<void *>(functionPtr));
149}
150
151#endif
Note: See TracBrowser for help on using the repository browser.