Ignore:
Timestamp:
May 6, 2017, 4:05:05 PM (2 years ago)
Author:
nmedfort
Message:

Continued refactoring work.

File:
1 edited

Legend:

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

    r5431 r5435  
    3131#include <kernels/kernel.h>
    3232#include <sys/stat.h>
     33#include <thread>
     34#include <boost/lockfree/queue.hpp>
    3335
    3436using namespace llvm;
     
    187189
    188190StreamSetBuffer * ParabixDriver::addBuffer(std::unique_ptr<StreamSetBuffer> b) {
    189     b->allocateBuffer(iBuilder.get());
     191    b->allocateBuffer(iBuilder);
    190192    mOwnedBuffers.emplace_back(std::move(b));
    191193    return mOwnedBuffers.back().get();
    192194}
    193195
    194 kernel::KernelBuilder * ParabixDriver::addKernelInstance(std::unique_ptr<kernel::KernelBuilder> kb) {
    195     kb->setBuilder(iBuilder.get());
     196kernel::Kernel * ParabixDriver::addKernelInstance(std::unique_ptr<kernel::Kernel> kb) {
    196197    mOwnedKernels.emplace_back(std::move(kb));
    197198    return mOwnedKernels.back().get();
    198199}
    199200
    200 void ParabixDriver::addKernelCall(kernel::KernelBuilder & kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs) {
     201void ParabixDriver::addKernelCall(kernel::Kernel & kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs) {
    201202    assert ("addKernelCall or makeKernelCall was already run on this kernel." && (kb.getModule() == nullptr));
    202203    mPipeline.emplace_back(&kb);
     204    kb.setBuilder(iBuilder.get());
    203205    kb.createKernelStub(inputs, outputs);
    204206}
    205207
    206 void ParabixDriver::makeKernelCall(kernel::KernelBuilder * kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs) {
     208void ParabixDriver::makeKernelCall(kernel::Kernel * kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs) {
    207209    assert ("addKernelCall or makeKernelCall was already run on this kernel." && (kb->getModule() == nullptr));
    208210    mPipeline.emplace_back(kb);
     211    kb->setBuilder(iBuilder.get());
    209212    kb->createKernelStub(inputs, outputs);
    210213}
     
    215218        report_fatal_error("Pipeline must contain at least one kernel");
    216219    } else {
    217         boost::container::flat_set<kernel::KernelBuilder *> K(mPipeline.begin(), mPipeline.end());
     220        boost::container::flat_set<kernel::Kernel *> K(mPipeline.begin(), mPipeline.end());
    218221        if (LLVM_UNLIKELY(K.size() != mPipeline.size())) {
    219222            report_fatal_error("Kernel definitions can only occur once in the pipeline");
     
    224227    // note: instantiation of all kernels must occur prior to initialization
    225228    for (const auto & k : mPipeline) {
     229        k->setBuilder(iBuilder.get());
    226230        k->addKernelDeclarations();
    227231    }
    228232    for (const auto & k : mPipeline) {
     233        k->setBuilder(iBuilder.get());
    229234        k->createInstance();
    230235    }
    231236    for (const auto & k : mPipeline) {
     237        k->setBuilder(iBuilder.get());
    232238        k->initializeInstance();
    233239    }
    234240    if (codegen::pipelineParallel) {
    235         generateParallelPipeline(iBuilder, mPipeline);
     241        generateParallelPipeline(iBuilder.get(), mPipeline);
    236242    } else if (codegen::segmentPipelineParallel) {
    237         generateSegmentParallelPipeline(iBuilder, mPipeline);
     243        generateSegmentParallelPipeline(iBuilder.get(), mPipeline);
    238244    } else {
    239245        codegen::ThreadNum = 1;
    240         generatePipelineLoop(iBuilder, mPipeline);
     246        generatePipelineLoop(iBuilder.get(), mPipeline);
    241247    }
    242248    for (const auto & k : mPipeline) {
     249        k->setBuilder(iBuilder.get());
    243250        k->finalizeInstance();
    244251    }
     
    295302    PM.run(*m);
    296303
    297     for (kernel::KernelBuilder * const kb : mPipeline) {
    298         m = kb->getModule();
     304    for (kernel::Kernel * const k : mPipeline) {
     305        m = k->getModule();
    299306        bool uncachedObject = true;
    300         if (mCache && mCache->loadCachedObjectFile(kb)) {
     307        if (mCache && mCache->loadCachedObjectFile(k)) {
    301308            uncachedObject = false;
    302309        }
    303310        if (uncachedObject) {
    304             iBuilder->setModule(kb->getModule());
    305             kb->generateKernel();
     311            iBuilder->setModule(m);
     312            k->setBuilder(iBuilder.get());
     313            k->generateKernel();
    306314            PM.run(*m);
    307315        }
     
    328336    delete mCache;
    329337}
     338
     339
     340//void ParabixDriver::linkAndFinalize() {
     341
     342//    using KernelQueue = boost::lockfree::queue<kernel::KernelBuilder *>;
     343
     344//    legacy::PassManager PM;
     345//    #ifndef NDEBUG
     346//    PM.add(createVerifierPass());
     347//    #endif
     348//    PM.add(createPromoteMemoryToRegisterPass()); //Force the use of mem2reg to promote stack variables.
     349//    PM.add(createReassociatePass());             //Reassociate expressions.
     350//    PM.add(createGVNPass());                     //Eliminate common subexpressions.
     351//    PM.add(createInstructionCombiningPass());    //Simple peephole optimizations and bit-twiddling.
     352//    PM.add(createCFGSimplificationPass());
     353
     354//    raw_fd_ostream * IROutputStream = nullptr;
     355//    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::ShowIR))) {
     356//        if (codegen::IROutputFilename.empty()) {
     357//            IROutputStream = new raw_fd_ostream(STDERR_FILENO, false, false);
     358//        } else {
     359//            std::error_code error;
     360//            IROutputStream = new raw_fd_ostream(codegen::IROutputFilename, error, sys::fs::OpenFlags::F_None);
     361//        }
     362//        PM.add(createPrintModulePass(*IROutputStream));
     363//        codegen::Jobs = 1; // TODO: set Jobs to 1 for now; these should be updated to pipe to a temporary buffer when Jobs > 1
     364//    }
     365
     366//    #ifndef USE_LLVM_3_6
     367//    raw_fd_ostream * ASMOutputStream = nullptr;
     368//    if (LLVM_UNLIKELY(codegen::DebugOptionIsSet(codegen::ShowASM))) {
     369//        if (codegen::ASMOutputFilename.empty()) {
     370//            ASMOutputStream = new raw_fd_ostream(STDERR_FILENO, false, false);
     371//        } else {
     372//            std::error_code error;
     373//            ASMOutputStream = new raw_fd_ostream(codegen::ASMOutputFilename, error, sys::fs::OpenFlags::F_None);
     374//        }
     375//        if (LLVM_UNLIKELY(mTarget->addPassesToEmitFile(PM, *ASMOutputStream, TargetMachine::CGFT_AssemblyFile))) {
     376//            report_fatal_error("LLVM error: could not add emit assembly pass");
     377//        }
     378//        codegen::Jobs = 1; // TODO: set Jobs to 1 for now; these should be updated to pipe to a temporary buffer when Jobs > 1
     379//    }
     380//    #endif
     381
     382//    KernelQueue Q(mPipeline.size() + 1);
     383//    for (kernel::KernelBuilder * kb : mPipeline) {
     384//        assert (kb);
     385//        Q.unsynchronized_push(kb);
     386//    }
     387
     388//    std::thread compilation_thread[codegen::Jobs];
     389//    for (int i = 0; i < codegen::Jobs; ++i) {
     390//        compilation_thread[i] = std::thread([&]{
     391//            kernel::KernelBuilder * kb = nullptr;
     392//            Module * m = nullptr;
     393//            try {
     394//                while (Q.pop(kb)) {
     395//                    m = kb->getModule();
     396//                    bool uncachedObject = true;
     397//                    if (mCache && mCache->loadCachedObjectFile(kb)) {
     398//                        uncachedObject = false;
     399//                    }
     400//                    if (uncachedObject) {
     401//                        Module * const cm = iBuilder->getModule();
     402//                        iBuilder->setModule(m);
     403//                        kb->generateKernel();
     404//                        PM.run(*m);
     405//                        iBuilder->setModule(cm);
     406//                    }
     407//                    mEngine->addModule(std::unique_ptr<Module>(m));
     408//                }
     409//            } catch (...) {
     410//                // clear the queue
     411//                while (Q.pop(kb));
     412//                // dump the result the module to the console
     413//                if (m) m->dump();
     414//                throw;
     415//            }
     416//        });
     417//    }
     418
     419//    PM.run(*mMainModule);
     420//    for (int i = 0; i < codegen::Jobs; ++i) {
     421//        compilation_thread[i].join();
     422//    }
     423//    mEngine->finalizeObject();
     424
     425//    delete IROutputStream;
     426//    #ifndef USE_LLVM_3_6
     427//    delete ASMOutputStream;
     428//    #endif
     429
     430//}
     431
     432
     433//            std::unique_ptr<IDISA::IDISA_Builder> idb(IDISA::GetIDISA_Builder(kb->getModule()));
     434//            idb->setDriver(this);
     435//            kb->setBuilder(idb.get());
Note: See TracChangeset for help on using the changeset viewer.