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

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

Removed non-functional CUDA code from icgrep and consolidated grep and multigrep mode into a single function; allowed segment parallel pipeline to utilize process as its initial thread; modified MMapSourceKernel to map and perform mmap directly and advise the OS to drop consumed data streams.

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