Ignore:
Timestamp:
Apr 25, 2017, 2:33:31 PM (2 years ago)
Author:
nmedfort
Message:

Changes towards separate compilation

Location:
icGREP/icgrep-devel/icgrep/toolchain
Files:
1 added
6 moved

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/toolchain/pipeline.cpp

    r5424 r5425  
    55
    66#include "pipeline.h"
    7 #include <kernels/toolchain.h>
     7#include <toolchain/toolchain.h>
    88#include <kernels/kernel.h>
    99#include <kernels/streamset.h>
     
    2222using FlatSet = boost::container::flat_set<Value>;
    2323
    24 Function * makeThreadFunction(IDISA::IDISA_Builder * const b, const std::string & name) {
     24Function * makeThreadFunction(std::unique_ptr<IDISA::IDISA_Builder> & b, const std::string & name) {
    2525    Function * const f = Function::Create(FunctionType::get(b->getVoidTy(), {b->getVoidPtrTy()}, false), Function::InternalLinkage, name, b->getModule());
    2626    f->setCallingConv(CallingConv::C);
     
    3838 * fashion such that processing of segment S_i by the full pipeline is carried out by thread i mod T.
    3939 ** ------------------------------------------------------------------------------------------------------------- */
    40 void generateSegmentParallelPipeline(IDISA::IDISA_Builder * iBuilder, const std::vector<KernelBuilder *> & kernels) {
     40void generateSegmentParallelPipeline(std::unique_ptr<IDISA::IDISA_Builder> & iBuilder, const std::vector<KernelBuilder *> & kernels) {
    4141
    4242    const unsigned n = kernels.size();
     
    243243 * @brief generateParallelPipeline
    244244 ** ------------------------------------------------------------------------------------------------------------- */
    245 void generateParallelPipeline(IDISA::IDISA_Builder * iBuilder, const std::vector<KernelBuilder *> &kernels) {
     245void generateParallelPipeline(std::unique_ptr<IDISA::IDISA_Builder> & iBuilder, const std::vector<KernelBuilder *> &kernels) {
    246246
    247247    Module * const m = iBuilder->getModule();
     
    435435 * @brief generatePipelineLoop
    436436 ** ------------------------------------------------------------------------------------------------------------- */
    437 void generatePipelineLoop(IDISA::IDISA_Builder * iBuilder, const std::vector<KernelBuilder *> & kernels) {
     437void generatePipelineLoop(std::unique_ptr<IDISA::IDISA_Builder> & iBuilder, const std::vector<KernelBuilder *> & kernels) {
    438438
    439439    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
  • icGREP/icgrep-devel/icgrep/toolchain/pipeline.h

    r5424 r5425  
    77
    88#include <vector>
     9#include <memory>
     10
    911namespace IDISA { class IDISA_Builder; }
    1012namespace kernel { class KernelBuilder; }
    1113
    12 void generateSegmentParallelPipeline(IDISA::IDISA_Builder * iBuilder, const std::vector<kernel::KernelBuilder *> & kernels);
    13 void generatePipelineLoop(IDISA::IDISA_Builder * iBuilder, const std::vector<kernel::KernelBuilder *> & kernels);
    14 void generateParallelPipeline(IDISA::IDISA_Builder * iBuilder, const std::vector<kernel::KernelBuilder *> & kernels);
     14void generateSegmentParallelPipeline(std::unique_ptr<IDISA::IDISA_Builder> & iBuilder, const std::vector<kernel::KernelBuilder *> & kernels);
     15void generatePipelineLoop(std::unique_ptr<IDISA::IDISA_Builder> & iBuilder, const std::vector<kernel::KernelBuilder *> & kernels);
     16void generateParallelPipeline(std::unique_ptr<IDISA::IDISA_Builder> &iBuilder, const std::vector<kernel::KernelBuilder *> & kernels);
    1517
    1618#endif // PIPELINE_H
  • icGREP/icgrep-devel/icgrep/toolchain/toolchain.cpp

    r5424 r5425  
    66
    77#include "toolchain.h"
     8#include <IR_Gen/idisa_target.h>
    89#include <llvm/CodeGen/CommandFlags.h>             // for InitTargetOptionsF...
    910#include <llvm/ExecutionEngine/ExecutionEngine.h>  // for EngineBuilder
     
    2627#include <llvm/Transforms/Utils/Local.h>
    2728#include <llvm/IR/Module.h>
    28 #include <kernels/object_cache.h>
    29 #include <kernels/pipeline.h>
     29#include <toolchain/object_cache.h>
     30#include <toolchain/pipeline.h>
    3031#include <kernels/kernel.h>
    3132#include <sys/stat.h>
     
    7172bool EnableCycleCounter;
    7273#ifndef NDEBUG
    73 #define DEFAULT_TO_TRUE_IN_DEBUG_MODE true
     74#define IN_DEBUG_MODE true
    7475#else
    75 #define DEFAULT_TO_TRUE_IN_DEBUG_MODE false
     76#define IN_DEBUG_MODE false
    7677#endif
    7778
     
    8081static cl::opt<int, true> BufferSegmentsOption("buffer-segments", cl::location(BufferSegments), cl::desc("Buffer Segments"), cl::value_desc("positive integer"), cl::init(1));
    8182static cl::opt<int, true> ThreadNumOption("thread-num", cl::location(ThreadNum), cl::desc("Number of threads used for segment pipeline parallel"), cl::value_desc("positive integer"), cl::init(2));
    82 static cl::opt<bool, true> EnableAssertsOption("ea", cl::location(EnableAsserts), cl::desc("Enable Asserts"), cl::init(DEFAULT_TO_TRUE_IN_DEBUG_MODE));
     83static cl::opt<bool, true> EnableAssertsOption("ea", cl::location(EnableAsserts), cl::desc("Enable Asserts"), cl::init(IN_DEBUG_MODE));
    8384static cl::opt<bool, true> EnableCycleCountOption("ShowKernelCycles", cl::location(EnableCycleCounter), cl::desc("Count and report CPU cycles per kernel"), cl::init(false), cl::cat(CodeGenOptions));
    8485
     
    159160}
    160161
    161 ParabixDriver::ParabixDriver(IDISA::IDISA_Builder * iBuilder)
    162 : iBuilder(iBuilder)
    163 , mMainModule(iBuilder->getModule())
     162ParabixDriver::ParabixDriver(std::string && moduleName)
     163: mContext(new llvm::LLVMContext())
     164, mMainModule(new Module(moduleName, *mContext))
     165, iBuilder(nullptr)
    164166, mTarget(nullptr)
    165167, mEngine(nullptr)
    166 , mCache(nullptr)
    167 {
     168, mCache(nullptr) {
     169
    168170    InitializeNativeTarget();
    169171    InitializeNativeTargetAsmPrinter();
     
    182184
    183185    builder.setTargetOptions(opts);
    184     builder.setVerifyModules(true);
     186    builder.setVerifyModules(IN_DEBUG_MODE);
    185187    CodeGenOpt::Level optLevel = CodeGenOpt::Level::None;
    186188    switch (codegen::OptLevel) {
     
    194196
    195197    setAllFeatures(builder);
    196 
    197198    mEngine = builder.create();
    198199    if (mEngine == nullptr) {
     
    209210        mEngine->setObjectCache(mCache);
    210211    }
     212
     213    mMainModule->setTargetTriple(mTarget->getTargetTriple().getTriple());
     214
     215    iBuilder.reset(IDISA::GetIDISA_Builder(mMainModule));
     216    iBuilder->setDriver(this);
    211217}
    212218
    213219ExternalFileBuffer * ParabixDriver::addExternalBuffer(std::unique_ptr<ExternalFileBuffer> b, Value * externalBuf) {
    214     ExternalFileBuffer * rawBuf = b.get();
    215220    mOwnedBuffers.push_back(std::move(b));
     221    ExternalFileBuffer * rawBuf = cast<ExternalFileBuffer>(mOwnedBuffers.back().get());
    216222    rawBuf->setStreamSetBuffer(externalBuf);
    217223    return rawBuf;
     
    252258    }
    253259    #endif
     260
    254261    // note: instantiation of all kernels must occur prior to initialization
    255262    for (const auto & k : mPipeline) {
     
    275282}
    276283
    277 void ParabixDriver::addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, FunctionType * type, void * functionPtr) const {
    278     assert ("addKernelCall or makeKernelCall must be called before addExternalLink" && (kb.getModule() != nullptr));
    279     mEngine->addGlobalMapping(cast<Function>(kb.getModule()->getOrInsertFunction(name, type)), functionPtr);
    280 }
    281 
    282 uint64_t file_size(const uint32_t fd) {
    283     struct stat st;
    284     if (LLVM_UNLIKELY(fstat(fd, &st) != 0)) {
    285         st.st_size = 0;
    286     }
    287     return st.st_size;
     284Function * ParabixDriver::LinkFunction(Module * mod, llvm::StringRef name, FunctionType * type, void * functionPtr) const {
     285    assert ("addKernelCall or makeKernelCall must be called before LinkFunction" && (mod != nullptr));
     286    Function * f = cast<Function>(mod->getOrInsertFunction(name, type));
     287    mEngine->addGlobalMapping(f, functionPtr);
     288    return f;
    288289}
    289290
     
    328329    }
    329330    #endif
    330 
    331     FunctionType * fileSizeType = FunctionType::get(iBuilder->getInt64Ty(), { iBuilder->getInt32Ty() });
    332     mEngine->addGlobalMapping(cast<Function>(mMainModule->getOrInsertFunction("file_size", fileSizeType)), (void *)&file_size);
    333331
    334332    PM.run(*m);
  • icGREP/icgrep-devel/icgrep/toolchain/toolchain.h

    r5424 r5425  
    99#include <string>
    1010#include <IR_Gen/idisa_builder.h>
    11 #include <llvm/IR/TypeBuilder.h>
     11#include <IR_Gen/FunctionTypeBuilder.h>
    1212#include <kernels/kernel.h>
    1313#include <kernels/streamset.h>
    14 #include <boost/container/flat_map.hpp>
    1514
    1615namespace llvm { class ExecutionEngine; }
     
    6261
    6362class ParabixDriver {
    64     using ModuleMap = boost::container::flat_map<kernel::KernelBuilder *, llvm::Module *>;
     63    friend class CBuilder;
    6564public:
    66     ParabixDriver(IDISA::IDISA_Builder * iBuilder);
     65    ParabixDriver(std::string && moduleName);
    6766
    6867    ~ParabixDriver();
    6968   
    70     IDISA::IDISA_Builder * getIDISA_Builder() {return iBuilder;}
     69    IDISA::IDISA_Builder * getIDISA_Builder() { return iBuilder.get(); }
    7170   
    7271    parabix::ExternalFileBuffer * addExternalBuffer(std::unique_ptr<parabix::ExternalFileBuffer> b, llvm::Value * externalBuf);
     
    7776   
    7877    void addKernelCall(kernel::KernelBuilder & kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
     78
    7979    void makeKernelCall(kernel::KernelBuilder * kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs);
    8080   
     
    8282   
    8383    template <typename ExternalFunctionType>
    84     void addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const;
     84    llvm::Function * LinkFunction(kernel::KernelBuilder & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const;
    8585
    8686    void linkAndFinalize();
     
    8888    void * getPointerToMain();
    8989
    90 private:
     90protected:
    9191
    92 
    93     void addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, llvm::FunctionType * type, void * functionPtr) const;
    94 
     92    llvm::Function * LinkFunction(llvm::Module * mod, llvm::StringRef name, llvm::FunctionType * type, void * functionPtr) const;
    9593
    9694private:
    97     IDISA::IDISA_Builder * const            iBuilder;
     95    std::unique_ptr<llvm::LLVMContext>      mContext;
    9896    llvm::Module * const                    mMainModule;
     97    std::unique_ptr<IDISA::IDISA_Builder>   iBuilder;
    9998    llvm::TargetMachine *                   mTarget;
    10099    llvm::ExecutionEngine *                 mEngine;
    101100    ParabixObjectCache *                    mCache;
     101
    102102    std::vector<kernel::KernelBuilder *>    mPipeline;
    103103    // Owned kernels and buffers that will persist with this ParabixDriver instance.
     
    106106};
    107107
    108 namespace {
    109 
    110 // NOTE: Currently, LLVM TypeBuilder can deduce FuntionTypes for up to 5 arguments. The following
    111 // templates have no limit but should be deprecated if the TypeBuilder ever supports n-ary functions.
    112 
    113 template<unsigned i, typename... Args>
    114 struct ParameterTypeBuilder;
    115 
    116 template<unsigned i, typename A1, typename... An>
    117 struct 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);
    121     }
    122 };
    123 
    124 template<unsigned i, typename A>
    125 struct ParameterTypeBuilder<i, A> {
    126     static void get(llvm::LLVMContext & C, llvm::Type ** params) {
    127         params[i] = llvm::TypeBuilder<A, false>::get(C);
    128     }
    129 };
    130 
    131 template<typename T>
    132 struct FunctionTypeBuilder;
    133 
    134 template<typename R, typename... Args>
    135 struct FunctionTypeBuilder<R(Args...)> {
    136     static llvm::FunctionType * get(llvm::LLVMContext & C) {
    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);
    140     }
    141 };
    142 
    143 template<typename R>
    144 struct FunctionTypeBuilder<R()> {
    145     static llvm::FunctionType * get(llvm::LLVMContext & C) {
    146         return llvm::FunctionType::get(llvm::TypeBuilder<R, false>::get(C), false);
    147     }
    148 };
    149 
    150 }
    151 
    152108template <typename ExternalFunctionType>
    153 void ParabixDriver::addExternalLink(kernel::KernelBuilder & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const {
     109llvm::Function * ParabixDriver::LinkFunction(kernel::KernelBuilder & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const {
    154110    llvm::FunctionType * const type = FunctionTypeBuilder<ExternalFunctionType>::get(iBuilder->getContext());
    155111    assert ("FunctionTypeBuilder did not resolve a function type." && type);
    156     addExternalLink(kb, name, type, reinterpret_cast<void *>(functionPtr));
     112    return LinkFunction(kb.getModule(), name, type, reinterpret_cast<void *>(functionPtr));
    157113}
    158114
Note: See TracChangeset for help on using the changeset viewer.