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
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;
[5151]47#ifdef CUDA_ENABLED
48extern bool NVPTX;
[5314]49extern int GroupNum;
[5151]50#endif
[5033]51}
[4900]52
[5151]53#ifdef CUDA_ENABLED
54void setNVPTXOption();
55void Compile2PTX (llvm::Module * m, std::string IRFilename, std::string PTXFilename);
56#endif
57
[5373]58void AddParabixVersionPrinter();
59
[5108]60bool AVX2_available();
61
[5391]62class ParabixDriver {
[5401]63    using ModuleMap = boost::container::flat_map<kernel::KernelBuilder *, llvm::Module *>;
[5391]64public:
65    ParabixDriver(IDISA::IDISA_Builder * iBuilder);
[5402]66
67    ~ParabixDriver();
[5391]68   
69    IDISA::IDISA_Builder * getIDISA_Builder() {return iBuilder;}
70   
[5409]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   
[5414]75    kernel::KernelBuilder * addKernelInstance(std::unique_ptr<kernel::KernelBuilder> kb);
76   
[5391]77    void addKernelCall(kernel::KernelBuilder & kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
[5414]78    void makeKernelCall(kernel::KernelBuilder * kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
[5391]79   
80    void generatePipelineIR();
81   
[5398]82    template <typename ExternalFunctionType>
83    void addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const;
84
[5391]85    void linkAndFinalize();
86   
87    void * getPointerToMain();
88
89private:
[5418]90
91
92    void addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, llvm::FunctionType * type, void * functionPtr) const;
93
94
95private:
[5398]96    IDISA::IDISA_Builder * const            iBuilder;
97    llvm::Module * const                    mMainModule;
98    llvm::TargetMachine *                   mTarget;
99    llvm::ExecutionEngine *                 mEngine;
[5402]100    ParabixObjectCache *                    mCache;
[5414]101    std::vector<kernel::KernelBuilder *>    mPipeline;
[5409]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;
[5391]105};
[5398]106
107namespace {
108
[5401]109// NOTE: Currently, LLVM TypeBuilder can deduce FuntionTypes for up to 5 arguments. The following
[5399]110// templates have no limit but should be deprecated if the TypeBuilder ever supports n-ary functions.
[5398]111
[5399]112template<unsigned i, typename... Args>
[5400]113struct ParameterTypeBuilder;
[5398]114
[5399]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);
[5398]120    }
121};
122
[5399]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);
[5398]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) {
[5399]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);
[5398]139    }
140};
141
142template<typename R>
143struct FunctionTypeBuilder<R()> {
144    static llvm::FunctionType * get(llvm::LLVMContext & C) {
[5399]145        return llvm::FunctionType::get(llvm::TypeBuilder<R, false>::get(C), false);
[5398]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());
[5399]154    assert ("FunctionTypeBuilder did not resolve a function type." && type);
[5398]155    addExternalLink(kb, name, type, reinterpret_cast<void *>(functionPtr));
156}
157
[4801]158#endif
Note: See TracBrowser for help on using the repository browser.