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

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

Parabix driver can take ownership and allocate buffers

File size: 4.9 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>
12#include <boost/container/flat_map.hpp>
[5364]13
[5267]14namespace llvm { class ExecutionEngine; }
15namespace llvm { class Module; }
[5398]16namespace llvm { class TargetMachine; }
[5401]17namespace llvm { class formatted_raw_ostream; }
[5267]18namespace llvm { namespace cl { class OptionCategory; } }
[5364]19namespace IDISA { class IDISA_Builder; }
20namespace kernel { class KernelBuilder; }
[5409]21//namespace parabix { class StreamSetBuffer; }
22#include <kernels/streamset.h>
[5402]23class ParabixObjectCache;
[4801]24
[5033]25namespace codegen {
26const llvm::cl::OptionCategory * codegen_flags();
[4801]27
[5033]28// Command Parameters
[5295]29enum DebugFlags {
30    ShowIR,
[5401]31#ifndef USE_LLVM_3_6
[5295]32    ShowASM,
33#endif
34    SerializeThreads
35};
36
37bool DebugOptionIsSet(DebugFlags flag);
38
39
[5033]40extern char OptLevel;  // set from command line
41extern int BlockSize;  // set from command line
42extern int SegmentSize;  // set from command line
[5135]43extern int BufferSegments;
[5165]44extern int ThreadNum;
[5347]45extern bool EnableAsserts;
[5151]46#ifdef CUDA_ENABLED
47extern bool NVPTX;
[5314]48extern int GroupNum;
[5151]49#endif
[5033]50}
[4900]51
[5151]52#ifdef CUDA_ENABLED
53void setNVPTXOption();
54void Compile2PTX (llvm::Module * m, std::string IRFilename, std::string PTXFilename);
55#endif
56
[5373]57void AddParabixVersionPrinter();
58
[5108]59bool AVX2_available();
60
[5391]61class ParabixDriver {
[5401]62    using ModuleMap = boost::container::flat_map<kernel::KernelBuilder *, llvm::Module *>;
[5391]63public:
64    ParabixDriver(IDISA::IDISA_Builder * iBuilder);
[5402]65
66    ~ParabixDriver();
[5391]67   
68    IDISA::IDISA_Builder * getIDISA_Builder() {return iBuilder;}
69   
[5409]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   
[5391]74    void addKernelCall(kernel::KernelBuilder & kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
75   
76    void generatePipelineIR();
77   
[5398]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
[5391]83    void linkAndFinalize();
84   
85    void * getPointerToMain();
86
87private:
[5398]88    IDISA::IDISA_Builder * const            iBuilder;
89    llvm::Module * const                    mMainModule;
90    llvm::TargetMachine *                   mTarget;
91    llvm::ExecutionEngine *                 mEngine;
[5402]92    ParabixObjectCache *                    mCache;
[5398]93    std::vector<kernel::KernelBuilder *>    mKernelList;
[5409]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;
[5398]97    ModuleMap                               mModuleMap;
[5391]98};
[5398]99
100namespace {
101
[5401]102// NOTE: Currently, LLVM TypeBuilder can deduce FuntionTypes for up to 5 arguments. The following
[5399]103// templates have no limit but should be deprecated if the TypeBuilder ever supports n-ary functions.
[5398]104
[5399]105template<unsigned i, typename... Args>
[5400]106struct ParameterTypeBuilder;
[5398]107
[5399]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);
[5398]113    }
114};
115
[5399]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);
[5398]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) {
[5399]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);
[5398]132    }
133};
134
135template<typename R>
136struct FunctionTypeBuilder<R()> {
137    static llvm::FunctionType * get(llvm::LLVMContext & C) {
[5399]138        return llvm::FunctionType::get(llvm::TypeBuilder<R, false>::get(C), false);
[5398]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());
[5399]147    assert ("FunctionTypeBuilder did not resolve a function type." && type);
[5398]148    addExternalLink(kb, name, type, reinterpret_cast<void *>(functionPtr));
149}
150
[4801]151#endif
Note: See TracBrowser for help on using the repository browser.