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

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

ShowKernelCycles? option

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