Changeset 5350 for icGREP/icgrep-devel


Ignore:
Timestamp:
Feb 26, 2017, 4:30:51 PM (3 years ago)
Author:
nmedfort
Message:

First attempt at inlining all DoBlock? and FinalBlock? functions by using indirect jumps. Disabled for NVPTX until Linda can check whether they're supported by the LLVM NVPTX library.

Location:
icGREP/icgrep-devel/icgrep
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.cpp

    r5347 r5350  
    1010#include <llvm/IR/Intrinsics.h>
    1111#include <llvm/IR/TypeBuilder.h>
     12#include <llvm/IR/MDBuilder.h>
    1213#include <fcntl.h>
    1314#include <toolchain.h>
     
    1516using namespace llvm;
    1617
    17 llvm::Value * CBuilder::CreateOpenCall(Value * filename, Value * oflag, Value * mode) {
     18Value * CBuilder::CreateOpenCall(Value * filename, Value * oflag, Value * mode) {
    1819    Function * openFn = mMod->getFunction("open");
    1920    if (openFn == nullptr) {
     
    5354}
    5455
    55 llvm::Value * CBuilder::CreateCloseCall(Value * fildes) {
     56Value * CBuilder::CreateCloseCall(Value * fildes) {
    5657    Function * closeFn = mMod->getFunction("close");
    5758    if (closeFn == nullptr) {
     
    293294    if (fOpenFunc == nullptr) {
    294295        fOpenFunc = cast<Function>(mMod->getOrInsertFunction("fopen", getFILEptrTy(), getInt8Ty()->getPointerTo(), getInt8Ty()->getPointerTo(), nullptr));
    295         fOpenFunc->setCallingConv(llvm::CallingConv::C);
     296        fOpenFunc->setCallingConv(CallingConv::C);
    296297    }
    297298    return CreateCall(fOpenFunc, {filename, mode});
     
    302303    if (fReadFunc == nullptr) {
    303304        fReadFunc = cast<Function>(mMod->getOrInsertFunction("fread", getSizeTy(), getVoidPtrTy(), getSizeTy(), getSizeTy(), getFILEptrTy(), nullptr));
    304         fReadFunc->setCallingConv(llvm::CallingConv::C);
     305        fReadFunc->setCallingConv(CallingConv::C);
    305306    }
    306307    return CreateCall(fReadFunc, {ptr, size, nitems, stream});
     
    311312    if (fWriteFunc == nullptr) {
    312313        fWriteFunc = cast<Function>(mMod->getOrInsertFunction("fwrite", getSizeTy(), getVoidPtrTy(), getSizeTy(), getSizeTy(), getFILEptrTy(), nullptr));
    313         fWriteFunc->setCallingConv(llvm::CallingConv::C);
     314        fWriteFunc->setCallingConv(CallingConv::C);
    314315    }
    315316    return CreateCall(fWriteFunc, {ptr, size, nitems, stream});
     
    320321    if (fCloseFunc == nullptr) {
    321322        fCloseFunc = cast<Function>(mMod->getOrInsertFunction("fclose", getInt32Ty(), getFILEptrTy(), nullptr));
    322         fCloseFunc->setCallingConv(llvm::CallingConv::C);
     323        fCloseFunc->setCallingConv(CallingConv::C);
    323324    }
    324325    return CreateCall(fCloseFunc, {stream});
     
    337338                                                                     static_cast<Type *>(funVoidPtrVoidTy)->getPointerTo(),
    338339                                                                     getVoidPtrTy(), nullptr));
    339         pthreadCreateFunc->setCallingConv(llvm::CallingConv::C);
     340        pthreadCreateFunc->setCallingConv(CallingConv::C);
    340341    }
    341342    return CreateCall(pthreadCreateFunc, {thread, attr, start_routine, arg});
     
    346347    if (pthreadExitFunc == nullptr) {
    347348        pthreadExitFunc = cast<Function>(mMod->getOrInsertFunction("pthread_exit", getVoidTy(), getVoidPtrTy(), nullptr));
    348         pthreadExitFunc->addFnAttr(llvm::Attribute::NoReturn);
    349         pthreadExitFunc->setCallingConv(llvm::CallingConv::C);
     349        pthreadExitFunc->addFnAttr(Attribute::NoReturn);
     350        pthreadExitFunc->setCallingConv(CallingConv::C);
    350351    }
    351352    CallInst * exitThread = CreateCall(pthreadExitFunc, {value_ptr});
     
    360361                                                                       pthreadTy,
    361362                                                                       getVoidPtrTy()->getPointerTo(), nullptr));
    362     pthreadJoinFunc->setCallingConv(llvm::CallingConv::C);
     363    pthreadJoinFunc->setCallingConv(CallingConv::C);
    363364    return CreateCall(pthreadJoinFunc, {thread, value_ptr});
    364365}
    365366
    366 void CBuilder::CreateAssert(llvm::Value * const assertion, llvm::StringRef failureMessage) {
     367void CBuilder::CreateAssert(Value * const assertion, StringRef failureMessage) {
    367368    if (codegen::EnableAsserts) {
    368369        Module * const m = getModule();
     
    414415}
    415416
    416 CBuilder::CBuilder(llvm::Module * m, unsigned GeneralRegisterWidthInBits, unsigned CacheLineAlignmentInBytes)
     417BranchInst * CBuilder::CreateLikelyCondBr(Value * Cond, BasicBlock * True, BasicBlock * False, const int probability) {
     418    MDBuilder mdb(getContext());
     419    if (probability < 0 || probability > 100) {
     420        report_fatal_error("branch weight probability must be in [0,100]");
     421    }
     422    return CreateCondBr(Cond, True, False, mdb.createBranchWeights(probability, 100 - probability));
     423}
     424
     425CBuilder::CBuilder(Module * const m, const unsigned GeneralRegisterWidthInBits, const bool SupportsIndirectBr, const unsigned CacheLineAlignmentInBytes)
    417426: IRBuilder<>(m->getContext())
    418427, mMod(m)
    419428, mCacheLineAlignment(CacheLineAlignmentInBytes)
    420429, mSizeType(getIntNTy(GeneralRegisterWidthInBits))
    421 , mFILEtype(nullptr) {
    422 }
     430, mFILEtype(nullptr)
     431, mSupportsIndirectBr(SupportsIndirectBr) {
     432}
  • icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.h

    r5347 r5350  
    2020public:
    2121   
    22     CBuilder(llvm::Module * m, unsigned GeneralRegisterWidthInBits, unsigned CacheLineAlignmentInBytes = 64);
     22    CBuilder(llvm::Module * m, const unsigned GeneralRegisterWidthInBits, const bool SupportsIndirectBr, const unsigned CacheLineAlignmentInBytes = 64);
    2323   
    2424    virtual ~CBuilder() {}
     
    113113    void CreateExit(const int exitCode);
    114114
     115    llvm::BranchInst * CreateLikelyCondBr(llvm::Value * Cond, llvm::BasicBlock * True, llvm::BasicBlock * False, const int probability = 90);
     116
     117    llvm::BranchInst * CreateUnlikelyCondBr(llvm::Value * Cond, llvm::BasicBlock * True, llvm::BasicBlock * False, const int probability = 90) {
     118        return CreateLikelyCondBr(Cond, True, False, 100 - probability);
     119    }
     120
     121    bool supportsIndirectBr() const {
     122        return mSupportsIndirectBr;
     123    }
     124
    115125protected:
    116126    llvm::Module *      mMod;
     
    118128    llvm::IntegerType * mSizeType;
    119129    llvm::StructType *  mFILEtype;
     130    const bool          mSupportsIndirectBr;
    120131};
    121132
  • icGREP/icgrep-devel/icgrep/IR_Gen/idisa_builder.cpp

    r5309 r5350  
    356356}
    357357
    358 IDISA_Builder::IDISA_Builder(Module * m, unsigned archBitWidth, unsigned bitBlockWidth, unsigned stride, unsigned CacheAlignment)
    359 : CBuilder(m, archBitWidth, CacheAlignment)
     358IDISA_Builder::IDISA_Builder(Module * m, unsigned archBitWidth, unsigned bitBlockWidth, unsigned stride, const bool SupportsIndirectBr, unsigned CacheAlignment)
     359: CBuilder(m, archBitWidth, SupportsIndirectBr, CacheAlignment)
    360360, mBitBlockWidth(bitBlockWidth)
    361361, mStride(stride)
  • icGREP/icgrep-devel/icgrep/IR_Gen/idisa_builder.h

    r5307 r5350  
    2020public:
    2121
    22     IDISA_Builder(llvm::Module * m, unsigned archBitWidth, unsigned bitBlockWidth, unsigned stride, unsigned CacheAlignment=64);
     22    IDISA_Builder(llvm::Module * m, unsigned archBitWidth, unsigned bitBlockWidth, unsigned stride, const bool SupportsIndirectBr=true, unsigned CacheAlignment=64);
    2323
    2424    virtual ~IDISA_Builder();
     
    4646    }
    4747   
    48 
    4948    llvm::LoadInst * CreateBlockAlignedLoad(llvm::Value * const ptr);
    5049    llvm::LoadInst * CreateBlockAlignedLoad(llvm::Value * const ptr, llvm::Value * const index);
  • icGREP/icgrep-devel/icgrep/IR_Gen/idisa_i64_builder.h

    r5260 r5350  
    1616public:
    1717 
    18     IDISA_I64_Builder(Module * m, unsigned archBitWidth, unsigned bitBlockWidth = 64, unsigned stride = 64)
    19     : IDISA_Builder(m, archBitWidth, bitBlockWidth, stride) {
     18    IDISA_I64_Builder(Module * m, unsigned archBitWidth, unsigned bitBlockWidth = 64, unsigned stride = 64, const bool SupportsIndirectBr = true)
     19    : IDISA_Builder(m, archBitWidth, bitBlockWidth, stride, SupportsIndirectBr) {
    2020    }
    2121
  • icGREP/icgrep-devel/icgrep/IR_Gen/idisa_nvptx_builder.h

    r5260 r5350  
    1717   
    1818    IDISA_NVPTX20_Builder(Module * m, unsigned groupSize)
    19     : IDISA_I64_Builder(m, 64, 64, 64 * groupSize)
     19    : IDISA_I64_Builder(m, 64, 64, 64 * groupSize, false)
    2020    , groupThreads(groupSize) {
    2121        CreateGlobals();
  • icGREP/icgrep-devel/icgrep/kernels/kernel.cpp

    r5347 r5350  
    1010#include <llvm/IR/Function.h>
    1111#include <llvm/IR/Instructions.h>
     12#include <llvm/IR/MDBuilder.h>
    1213#include <llvm/IR/Module.h>
     14#include <llvm/IR/Verifier.h>
    1315#include <llvm/Support/raw_ostream.h>
    14 #include <llvm/IR/LegacyPassManager.h>
    15 #include <llvm/Transforms/Scalar.h>
    16 #include <llvm/IR/Verifier.h>
     16#include <llvm/Transforms/Utils/Local.h>
    1717
    1818static const auto DO_BLOCK_SUFFIX = "_DoBlock";
     
    3535using namespace kernel;
    3636using namespace parabix;
    37 using namespace llvm::legacy;
    3837
    3938unsigned KernelBuilder::addScalar(Type * const type, const std::string & name) {
     
    440439void BlockOrientedKernel::generateDoSegmentMethod(Value * doFinal, const std::vector<Value *> & producerPos) {
    441440
    442     // Use the pass manager to optimize the function.
    443     FunctionPassManager fpm(iBuilder->getModule());
    444     #ifndef NDEBUG
    445     fpm.add(createVerifierPass());
    446     #endif
    447     fpm.add(createReassociatePass());             //Reassociate expressions.
    448     fpm.add(createGVNPass());                     //Eliminate common subexpressions.
    449     fpm.add(createInstructionCombiningPass());    //Simple peephole optimizations and bit-twiddling.
    450     fpm.doInitialization();
    451 
    452441    BasicBlock * const entryBlock = iBuilder->GetInsertBlock();
    453442    BasicBlock * const strideLoopCond = CreateBasicBlock(getName() + "_strideLoopCond");
    454     BasicBlock * const strideLoopBody = CreateBasicBlock(getName() + "_strideLoopBody");
     443    mStrideLoopBody = CreateBasicBlock(getName() + "_strideLoopBody");
    455444    BasicBlock * const stridesDone = CreateBasicBlock(getName() + "_stridesDone");
    456445
    457446    ConstantInt * stride = iBuilder->getSize(iBuilder->getStride());
    458 
    459447    Value * availablePos = producerPos[0];
    460448    for (unsigned i = 1; i < mStreamSetInputs.size(); i++) {
     
    466454    Value * itemsAvail = iBuilder->CreateSub(availablePos, processed);
    467455    Value * stridesToDo = iBuilder->CreateUDiv(itemsAvail, stride);
     456
    468457    iBuilder->CreateBr(strideLoopCond);
    469458
     
    472461    stridesRemaining->addIncoming(stridesToDo, entryBlock);
    473462    Value * notDone = iBuilder->CreateICmpNE(stridesRemaining, iBuilder->getSize(0));
    474     iBuilder->CreateCondBr(notDone, strideLoopBody, stridesDone);
    475 
    476     iBuilder->SetInsertPoint(strideLoopBody);
     463    iBuilder->CreateCondBr(notDone, mStrideLoopBody, stridesDone);
     464
     465    iBuilder->SetInsertPoint(mStrideLoopBody);
     466
     467    if (useIndirectBr()) {
     468        mStrideLoopBranchAddress = iBuilder->CreatePHI(iBuilder->getInt8PtrTy(), 2);
     469        mStrideLoopBranchAddress->addIncoming(BlockAddress::get(strideLoopCond), strideLoopCond);       
     470    }
    477471
    478472    /// GENERATE DO BLOCK METHOD
    479473
    480     generateDoBlockMethod(fpm);
     474    writeDoBlockMethod();
    481475
    482476    /// UPDATE PROCESSED COUNTS
     
    487481
    488482    stridesRemaining->addIncoming(iBuilder->CreateSub(stridesRemaining, iBuilder->getSize(1)), iBuilder->GetInsertBlock());
    489     iBuilder->CreateBr(strideLoopCond);
     483
     484    if (useIndirectBr()) {
     485        mStrideLoopBranch = iBuilder->CreateIndirectBr(mStrideLoopBranchAddress, 2);
     486        mStrideLoopBranch->addDestination(strideLoopCond);
     487    } else {
     488        iBuilder->CreateBr(strideLoopCond);
     489    }
    490490
    491491    iBuilder->SetInsertPoint(stridesDone);
     
    498498
    499499    Value * remainingItems = iBuilder->CreateSub(producerPos[0], getProcessedItemCount(mStreamSetInputs[0].name));
    500     generateFinalBlockMethod(remainingItems, fpm);
     500    writeFinalBlockMethod(remainingItems);
     501    // if remainingItems was not used, this will eliminate it.
     502    RecursivelyDeleteTriviallyDeadInstructions(remainingItems);
    501503
    502504    itemsDone = producerPos[0];
     
    505507    iBuilder->CreateBr(segmentDone);
    506508
     509    if (useIndirectBr()) {
     510        const auto destinations = mStrideLoopBranch->getNumDestinations();
     511        assert (mStrideLoopBranchAddress->getNumIncomingValues() == destinations);
     512        if (destinations == 1) {
     513            // Final block does not call DoBlock. Replace the indirect branch with a direct one.
     514            iBuilder->SetInsertPoint(mStrideLoopBranch);
     515            iBuilder->CreateBr(strideLoopCond);
     516            mStrideLoopBranch->eraseFromParent();
     517            mStrideLoopBranch = nullptr;
     518            mStrideLoopBranchAddress->eraseFromParent();
     519            mStrideLoopBranchAddress = nullptr;
     520        } else {
     521            MDBuilder mdb(iBuilder->getContext());
     522            uint32_t weights[destinations] = { 100, 0 };
     523            ArrayRef<uint32_t> bw(weights, destinations);
     524            mStrideLoopBranch->setMetadata(LLVMContext::MD_prof, mdb.createBranchWeights(bw));
     525        }
     526    }
     527
     528    segmentDone->moveAfter(iBuilder->GetInsertBlock());
     529
    507530    iBuilder->SetInsertPoint(segmentDone);
    508 }
    509 
    510 void BlockOrientedKernel::generateDoBlockMethod(FunctionPassManager & fpm) {
     531
     532}
     533
     534void BlockOrientedKernel::writeDoBlockMethod() {
    511535
    512536    Value * const self = mSelf;
     
    515539
    516540    /// Check if the do block method is called and create the function if necessary   
    517     if (isCalled()) {
     541    if (!useIndirectBr()) {
    518542        FunctionType * const type = FunctionType::get(iBuilder->getVoidTy(), {mSelf->getType()}, false);
    519543        mCurrentMethod = Function::Create(type, GlobalValue::ExternalLinkage, getName() + DO_BLOCK_SUFFIX, iBuilder->getModule());
     
    527551        iBuilder->SetInsertPoint(CreateBasicBlock("entry"));
    528552    }
    529 
    530     writeDoBlockMethod();
    531 
    532     /// Call the do block method if necessary then restore the current function state to the do segement method
    533 
    534     if (isCalled()) {
    535         iBuilder->CreateRetVoid();
    536         mDoBlockMethod = mCurrentMethod;
    537         fpm.run(*mCurrentMethod);
    538         iBuilder->restoreIP(ip);
    539         iBuilder->CreateCall(mCurrentMethod, self);
    540 
    541         mSelf = self;
    542         mCurrentMethod = cp;
    543     }
    544 
    545 }
    546 
    547 void BlockOrientedKernel::writeDoBlockMethod() {
    548553
    549554    std::vector<Value *> priorProduced;
     
    574579    }
    575580
    576 }
    577 
    578 void BlockOrientedKernel::generateFinalBlockMethod(Value * remainingItems, FunctionPassManager & fpm) {
     581    /// Call the do block method if necessary then restore the current function state to the do segement method
     582
     583    if (!useIndirectBr()) {
     584        iBuilder->CreateRetVoid();
     585        mDoBlockMethod = mCurrentMethod;
     586        iBuilder->restoreIP(ip);
     587        iBuilder->CreateCall(mCurrentMethod, self);
     588        mSelf = self;
     589        mCurrentMethod = cp;
     590    }
     591
     592}
     593
     594void BlockOrientedKernel::writeFinalBlockMethod(Value * remainingItems) {
    579595
    580596    Value * const self = mSelf;
     
    583599    auto ip = iBuilder->saveIP();
    584600
    585     if (isCalled()) {
     601    if (!useIndirectBr()) {
    586602        FunctionType * const type = FunctionType::get(iBuilder->getVoidTy(), {mSelf->getType(), iBuilder->getSizeTy()}, false);
    587603        mCurrentMethod = Function::Create(type, GlobalValue::ExternalLinkage, getName() + FINAL_BLOCK_SUFFIX, iBuilder->getModule());
     
    599615    generateFinalBlockMethod(remainingItems); // may be implemented by the BlockOrientedKernelBuilder subtype
    600616
    601     if (isCalled()) {
     617    if (!useIndirectBr()) {
    602618        iBuilder->CreateRetVoid();       
    603         fpm.run(*mCurrentMethod);
    604619        iBuilder->restoreIP(ip);
    605620        iBuilder->CreateCall(mCurrentMethod, {self, remainingItemCount});
     
    616631
    617632void BlockOrientedKernel::CreateDoBlockMethodCall() {
    618     if (isCalled()) {
     633    if (useIndirectBr()) {
     634        BasicBlock * bb = CreateBasicBlock("resume");
     635        mStrideLoopBranch->addDestination(bb);
     636        mStrideLoopBranchAddress->addIncoming(BlockAddress::get(bb), iBuilder->GetInsertBlock());
     637        iBuilder->CreateBr(mStrideLoopBody);
     638        iBuilder->SetInsertPoint(bb);
     639    } else {
    619640        iBuilder->CreateCall(mDoBlockMethod, mSelf);
    620     } else {
    621         // TODO: can we clone the DoBlock method instead of regenerating it?
    622         writeDoBlockMethod();
    623641    }
    624642}
     
    635653: KernelBuilder(builder, std::move(kernelName), std::move(stream_inputs), std::move(stream_outputs), std::move(scalar_parameters), std::move(scalar_outputs), std::move(internal_scalars))
    636654, mDoBlockMethod(nullptr)
    637 , mInlined(false) {
     655, mStrideLoopBody(nullptr)
     656, mStrideLoopBranch(nullptr)
     657, mStrideLoopBranchAddress(nullptr) {
    638658
    639659}
     
    649669                             std::vector<Binding> && internal_scalars)
    650670: KernelInterface(builder, std::move(kernelName), std::move(stream_inputs), std::move(stream_outputs), std::move(scalar_parameters), std::move(scalar_outputs), std::move(internal_scalars))
     671, mSelf(nullptr)
     672, mCurrentMethod(nullptr)
    651673, mNoTerminateAttribute(false)
    652674, mDoBlockUpdatesProducedItemCountsAttribute(false) {
  • icGREP/icgrep-devel/icgrep/kernels/kernel.h

    r5347 r5350  
    1212#include <boost/container/flat_map.hpp>
    1313#include <IR_Gen/idisa_builder.h>
    14 #include "llvm/Support/Debug.h"
     14
    1515namespace llvm { class ConstantInt; }
    1616namespace llvm { class Function; }
    17 namespace llvm { namespace legacy { class FunctionPassManager; } }
    1817namespace llvm { class IntegerType; }
    1918namespace llvm { class LoadInst; }
     
    138137    virtual void generateDoSegmentMethod(llvm::Value * doFinal, const std::vector<llvm::Value *> & producerPos) = 0;
    139138
    140 //    virtual void generateInternalMethods() { }
    141 
    142139    // Add an additional scalar field to the KernelState struct.
    143140    // Must occur before any call to addKernelDeclarations or createKernelModule.
     
    323320    virtual ~BlockOrientedKernel() { }
    324321
    325     bool isCalled() const {
    326         return !mInlined;
    327     }
    328 
    329     bool isInlined() const {
    330         return mInlined;
    331     }
    332 
    333     void setInlined(const bool value = true) {
    334         mInlined = value;
    335     }
    336 
    337322private:
    338323
    339     void generateDoBlockMethod(llvm::legacy::FunctionPassManager & fpm);
     324    bool useIndirectBr() const {
     325        return iBuilder->supportsIndirectBr();
     326    }
    340327
    341328    void writeDoBlockMethod();
    342329
    343     void generateFinalBlockMethod(llvm::Value *remainingItems, llvm::legacy::FunctionPassManager & fpm);
     330    void writeFinalBlockMethod(llvm::Value * remainingItems);
    344331
    345332private:
    346333
    347     llvm::Function * mDoBlockMethod;
    348     bool             mInlined;
     334    llvm::Function *        mDoBlockMethod;
     335    llvm::BasicBlock *      mStrideLoopBody;
     336    llvm::IndirectBrInst *  mStrideLoopBranch;
     337    llvm::PHINode *         mStrideLoopBranchAddress;
    349338};
    350339
  • icGREP/icgrep-devel/icgrep/kernels/s2p_kernel.cpp

    r5347 r5350  
    166166    setNoTerminateAttribute(true);
    167167    setDoBlockUpdatesProducedItemCountsAttribute(false);
    168     setInlined(true);
    169168}
    170169
  • icGREP/icgrep-devel/icgrep/kernels/scanmatchgen.cpp

    r5347 r5350  
    254254    {Binding{iBuilder->getSizeTy(), "BlockNo"}, Binding{iBuilder->getSizeTy(), "LineStart"}, Binding{iBuilder->getSizeTy(), "LineNum"}})
    255255, mGrepType(grepType) {
    256     setInlined(true);
    257 }
    258 
    259 }
     256}
     257
     258}
  • icGREP/icgrep-devel/icgrep/kernels/unicode_linebreak_kernel.cpp

    r5347 r5350  
    6969    Var * const r = addOutput("unicodeLineBreak", getStreamTy());
    7070    builder.createAssign(r, UnicodeLineBreak);
    71 
    72     setInlined(true);
    7371}
  • icGREP/icgrep-devel/icgrep/toolchain.cpp

    r5347 r5350  
    1919#include <llvm/Target/TargetMachine.h>             // for TargetMachine, Tar...
    2020#include <llvm/Target/TargetOptions.h>             // for TargetOptions
     21#include <llvm/Transforms/Scalar.h>
     22#include <llvm/Transforms/Utils/Local.h>
     23#ifndef NDEBUG
     24#include <llvm/IR/Verifier.h>
     25#endif
    2126#include <object_cache.h>
    2227namespace llvm { class Module; }
     
    110115
    111116    std::error_code error;
    112     llvm::raw_fd_ostream out(IRFilename, error, sys::fs::OpenFlags::F_None);
     117    raw_fd_ostream out(IRFilename, error, sys::fs::OpenFlags::F_None);
    113118    m->print(out, nullptr);
    114119
     
    122127
    123128void setAllFeatures(EngineBuilder &builder) {
    124     llvm::StringMap<bool> HostCPUFeatures;
    125     if (llvm::sys::getHostCPUFeatures(HostCPUFeatures)) {
     129    StringMap<bool> HostCPUFeatures;
     130    if (sys::getHostCPUFeatures(HostCPUFeatures)) {
    126131        std::vector<std::string> attrs;
    127132        for (auto &flag : HostCPUFeatures) {
     
    134139
    135140bool AVX2_available() {
    136     llvm::StringMap<bool> HostCPUFeatures;
    137     if (llvm::sys::getHostCPUFeatures(HostCPUFeatures)) {
     141    StringMap<bool> HostCPUFeatures;
     142    if (sys::getHostCPUFeatures(HostCPUFeatures)) {
    138143        auto f = HostCPUFeatures.find("avx2");
    139144        return ((f != HostCPUFeatures.end()) && f->second);
     
    143148
    144149#ifndef USE_LLVM_3_6
    145 void WriteAssembly (llvm::TargetMachine *TM, Module * m) {
    146     llvm::legacy::PassManager PM;
    147 
    148     llvm::SmallString<128> Str;
    149     llvm::raw_svector_ostream dest(Str);
    150 
    151     if (TM->addPassesToEmitFile(PM, dest, llvm::TargetMachine::CGFT_AssemblyFile ) ) {
     150void WriteAssembly (TargetMachine *TM, Module * m) {
     151    legacy::PassManager PM;
     152
     153    SmallString<128> Str;
     154    raw_svector_ostream dest(Str);
     155
     156    if (TM->addPassesToEmitFile(PM, dest, TargetMachine::CGFT_AssemblyFile ) ) {
    152157        throw std::runtime_error("LLVM error: addPassesToEmitFile failed.");
    153158    }
     
    158163    } else {
    159164        std::error_code error;
    160         llvm::raw_fd_ostream out(codegen::ASMOutputFilename, error, sys::fs::OpenFlags::F_None);
     165        raw_fd_ostream out(codegen::ASMOutputFilename, error, sys::fs::OpenFlags::F_None);
    161166        out << Str;
    162167    }
     
    165170
    166171ExecutionEngine * JIT_to_ExecutionEngine (Module * m) {
     172
     173    // Use the pass manager to optimize the function.
     174    legacy::PassManager PM;
     175    #ifndef NDEBUG
     176    PM.add(createVerifierPass());
     177    #endif
     178    PM.add(createReassociatePass());             //Reassociate expressions.
     179    PM.add(createGVNPass());                     //Eliminate common subexpressions.
     180    PM.add(createInstructionCombiningPass());    //Simple peephole optimizations and bit-twiddling.
     181    PM.add(createCFGSimplificationPass());
     182    PM.run(*m);
    167183
    168184    InitializeNativeTarget();
     
    200216        } else {
    201217            std::error_code error;
    202             llvm::raw_fd_ostream out(codegen::IROutputFilename, error, sys::fs::OpenFlags::F_None);
     218            raw_fd_ostream out(codegen::IROutputFilename, error, sys::fs::OpenFlags::F_None);
    203219            m->print(out, nullptr);
    204220        }
Note: See TracChangeset for help on using the changeset viewer.