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

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

Moved toolchain and object_cache to kernels directory. Continued work on providing input consumed information.

File size: 4.4 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; }
21namespace parabix { class StreamSetBuffer; }
22class ParabixObjectCache;
23
24namespace codegen {
25const llvm::cl::OptionCategory * codegen_flags();
26
27// Command Parameters
28enum DebugFlags {
29    ShowIR,
30#ifndef USE_LLVM_3_6
31    ShowASM,
32#endif
33    SerializeThreads
34};
35
36bool DebugOptionIsSet(DebugFlags flag);
37
38
39extern char OptLevel;  // set from command line
40extern int BlockSize;  // set from command line
41extern int SegmentSize;  // set from command line
42extern int BufferSegments;
43extern int ThreadNum;
44extern bool EnableAsserts;
45#ifdef CUDA_ENABLED
46extern bool NVPTX;
47extern int GroupNum;
48#endif
49}
50
51#ifdef CUDA_ENABLED
52void setNVPTXOption();
53void Compile2PTX (llvm::Module * m, std::string IRFilename, std::string PTXFilename);
54#endif
55
56void AddParabixVersionPrinter();
57
58bool AVX2_available();
59
60class ParabixDriver {
61    using ModuleMap = boost::container::flat_map<kernel::KernelBuilder *, llvm::Module *>;
62public:
63    ParabixDriver(IDISA::IDISA_Builder * iBuilder);
64
65    ~ParabixDriver();
66   
67    IDISA::IDISA_Builder * getIDISA_Builder() {return iBuilder;}
68   
69    void addKernelCall(kernel::KernelBuilder & kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
70   
71    void generatePipelineIR();
72   
73    template <typename ExternalFunctionType>
74    void addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const;
75
76    void addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, llvm::FunctionType * type, void * functionPtr) const;
77
78    void linkAndFinalize();
79   
80    void * getPointerToMain();
81
82private:
83    IDISA::IDISA_Builder * const            iBuilder;
84    llvm::Module * const                    mMainModule;
85    llvm::TargetMachine *                   mTarget;
86    llvm::ExecutionEngine *                 mEngine;
87    ParabixObjectCache *                    mCache;
88    std::vector<kernel::KernelBuilder *>    mKernelList;
89    ModuleMap                               mModuleMap;
90};
91
92namespace {
93
94// NOTE: Currently, LLVM TypeBuilder can deduce FuntionTypes for up to 5 arguments. The following
95// templates have no limit but should be deprecated if the TypeBuilder ever supports n-ary functions.
96
97template<unsigned i, typename... Args>
98struct ParameterTypeBuilder;
99
100template<unsigned i, typename A1, typename... An>
101struct ParameterTypeBuilder<i, A1, An...> {
102    static void get(llvm::LLVMContext & C, llvm::Type ** params) {
103        ParameterTypeBuilder<i, A1>::get(C, params);
104        ParameterTypeBuilder<i + 1, An...>::get(C, params);
105    }
106};
107
108template<unsigned i, typename A>
109struct ParameterTypeBuilder<i, A> {
110    static void get(llvm::LLVMContext & C, llvm::Type ** params) {
111        params[i] = llvm::TypeBuilder<A, false>::get(C);
112    }
113};
114
115template<typename T>
116struct FunctionTypeBuilder;
117
118template<typename R, typename... Args>
119struct FunctionTypeBuilder<R(Args...)> {
120    static llvm::FunctionType * get(llvm::LLVMContext & C) {
121        llvm::Type * params[sizeof...(Args)];
122        ParameterTypeBuilder<0, Args...>::get(C, params);
123        return llvm::FunctionType::get(llvm::TypeBuilder<R, false>::get(C), params, false);
124    }
125};
126
127template<typename R>
128struct FunctionTypeBuilder<R()> {
129    static llvm::FunctionType * get(llvm::LLVMContext & C) {
130        return llvm::FunctionType::get(llvm::TypeBuilder<R, false>::get(C), false);
131    }
132};
133
134}
135
136template <typename ExternalFunctionType>
137void ParabixDriver::addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const {
138    llvm::FunctionType * const type = FunctionTypeBuilder<ExternalFunctionType>::get(iBuilder->getContext());
139    assert ("FunctionTypeBuilder did not resolve a function type." && type);
140    addExternalLink(kb, name, type, reinterpret_cast<void *>(functionPtr));
141}
142
143#endif
Note: See TracBrowser for help on using the repository browser.