Ignore:
Timestamp:
Aug 28, 2017, 4:00:17 PM (22 months ago)
Author:
nmedfort
Message:

Bug fixes for multigrep mode. Optional PabloKernel? branch hit counter added. Minor optimizations.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.cpp

    r5510 r5620  
    4040using TypeId = PabloAST::ClassTypeId;
    4141
    42 inline static unsigned getAlignment(const Value * const ptr) {
    43     return ptr->getType()->getPrimitiveSizeInBits() / 8;
     42inline static unsigned getAlignment(const Value * const type) {
     43    return type->getType()->getPrimitiveSizeInBits() / 8;
    4444}
    4545
     
    4848}
    4949
    50 void PabloCompiler::initializeKernelData(const std::unique_ptr<kernel::KernelBuilder> &  iBuilder) {
     50void PabloCompiler::initializeKernelData(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
    5151    assert ("PabloCompiler does not have a IDISA iBuilder" && iBuilder);
     52    mBranchCount = 0;
    5253    examineBlock(iBuilder, mKernel->getEntryBlock());
    5354    mCarryManager->initializeCarryData(iBuilder, mKernel);
    54 }
    55 
    56 void PabloCompiler::releaseKernelData(const std::unique_ptr<kernel::KernelBuilder> &  iBuilder) {
     55    if (CompileOptionIsSet(PabloCompilationFlags::EnableProfiling)) {
     56        const auto count = (mBranchCount * 2) + 1;
     57        mKernel->addScalar(ArrayType::get(mKernel->getSizeTy(), count), "profile");
     58        mBasicBlock.reserve(count);
     59    }
     60}
     61
     62void PabloCompiler::releaseKernelData(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
    5763    assert ("PabloCompiler does not have a IDISA iBuilder" && iBuilder);
    5864    mCarryManager->releaseCarryData(iBuilder);
    5965}
    6066
    61 void PabloCompiler::compile(const std::unique_ptr<kernel::KernelBuilder> &  iBuilder) {
     67void PabloCompiler::compile(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
    6268    assert ("PabloCompiler does not have a IDISA iBuilder" && iBuilder);
    6369    mCarryManager->initializeCodeGen(iBuilder);
     
    6571    mMarker.emplace(entryBlock->createZeroes(), iBuilder->allZeroes());
    6672    mMarker.emplace(entryBlock->createOnes(), iBuilder->allOnes());
     73    mBranchCount = 0;
     74    addBranchCounter(iBuilder);
    6775    compileBlock(iBuilder, entryBlock);
    6876    mCarryManager->finalizeCodeGen(iBuilder);
    6977}
    7078
    71 void PabloCompiler::examineBlock(const std::unique_ptr<kernel::KernelBuilder> &  iBuilder, const PabloBlock * const block) {
     79void PabloCompiler::examineBlock(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const block) {
    7280    for (const Statement * stmt : *block) {
    7381        if (LLVM_UNLIKELY(isa<Lookahead>(stmt))) {
     
    7886            }
    7987        } else if (LLVM_UNLIKELY(isa<Branch>(stmt))) {
     88            ++mBranchCount;
    8089            examineBlock(iBuilder, cast<Branch>(stmt)->getBody());
    8190        } else if (LLVM_UNLIKELY(isa<Count>(stmt))) {
     
    8594}
    8695
    87 inline void PabloCompiler::compileBlock(const std::unique_ptr<kernel::KernelBuilder> &  iBuilder, const PabloBlock * const block) {
     96void PabloCompiler::addBranchCounter(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
     97    if (CompileOptionIsSet(PabloCompilationFlags::EnableProfiling)) {       
     98        Value * ptr = iBuilder->getScalarFieldPtr("profile");
     99        assert (mBasicBlock.size() < ptr->getType()->getPointerElementType()->getArrayNumElements());
     100        ptr = iBuilder->CreateGEP(ptr, {iBuilder->getInt32(0), iBuilder->getInt32(mBasicBlock.size())});
     101        const auto alignment = getPointerElementAlignment(ptr);
     102        Value * value = iBuilder->CreateAlignedLoad(ptr, alignment, false, "branchCounter");
     103        value = iBuilder->CreateAdd(value, ConstantInt::get(cast<IntegerType>(value->getType()), 1));
     104        iBuilder->CreateAlignedStore(value, ptr, alignment);
     105        mBasicBlock.push_back(iBuilder->GetInsertBlock());
     106    }
     107}
     108
     109inline void PabloCompiler::compileBlock(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloBlock * const block) {
    88110    for (const Statement * statement : *block) {
    89111        compileStatement(iBuilder, statement);
     
    91113}
    92114
    93 void PabloCompiler::compileIf(const std::unique_ptr<kernel::KernelBuilder> &  iBuilder, const If * const ifStatement) {
     115void PabloCompiler::compileIf(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const If * const ifStatement) {
    94116    //
    95117    //  The If-ElseZero stmt:
     
    111133
    112134    BasicBlock * const ifEntryBlock = iBuilder->GetInsertBlock();
    113     BasicBlock * const ifBodyBlock = iBuilder->CreateBasicBlock("if.body");
    114     BasicBlock * const ifEndBlock = iBuilder->CreateBasicBlock("if.end");
     135    ++mBranchCount;
     136    BasicBlock * const ifBodyBlock = iBuilder->CreateBasicBlock("if.body_" + std::to_string(mBranchCount));
     137    BasicBlock * const ifEndBlock = iBuilder->CreateBasicBlock("if.end_" + std::to_string(mBranchCount));
    115138   
    116139    std::vector<std::pair<const Var *, Value *>> incoming;
     
    156179
    157180    mCarryManager->enterIfBody(iBuilder, ifEntryBlock);
     181
     182    addBranchCounter(iBuilder);
    158183
    159184    compileBlock(iBuilder, ifBody);
     
    209234        phi->addIncoming(outgoing, ifExitBlock);
    210235        f->second = phi;
    211     }   
    212 }
    213 
    214 void PabloCompiler::compileWhile(const std::unique_ptr<kernel::KernelBuilder> &  iBuilder, const While * const whileStatement) {
     236    }
     237
     238    addBranchCounter(iBuilder);
     239}
     240
     241void PabloCompiler::compileWhile(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const While * const whileStatement) {
    215242
    216243    const PabloBlock * const whileBody = whileStatement->getBody();
     
    243270    mCarryManager->enterLoopScope(iBuilder, whileBody);
    244271
    245     BasicBlock * whileBodyBlock = iBuilder->CreateBasicBlock("while.body");
     272    BasicBlock * whileBodyBlock = iBuilder->CreateBasicBlock("while.body_" + std::to_string(mBranchCount));
     273    BasicBlock * whileEndBlock = iBuilder->CreateBasicBlock("while.end_" + std::to_string(mBranchCount));
     274    ++mBranchCount;
    246275
    247276    iBuilder->CreateBr(whileBodyBlock);
     
    287316
    288317    mCarryManager->enterLoopBody(iBuilder, whileEntryBlock);
     318
     319    addBranchCounter(iBuilder);
    289320
    290321    compileBlock(iBuilder, whileBody);
     
    338369    }
    339370
    340     BasicBlock * whileEndBlock = iBuilder->CreateBasicBlock("while.end");
    341 
    342371    // Terminate the while loop body with a conditional branch back.
    343372    Value * condition = compileExpression(iBuilder, whileStatement->getCondition());
     
    348377    iBuilder->CreateCondBr(condition, whileBodyBlock, whileEndBlock);
    349378
     379    whileEndBlock->moveAfter(whileExitBlock);
     380
    350381    iBuilder->SetInsertPoint(whileEndBlock);
    351382
    352383    mCarryManager->leaveLoopScope(iBuilder, whileEntryBlock, whileExitBlock);
    353384
    354 }
    355 
    356 void PabloCompiler::compileStatement(const std::unique_ptr<kernel::KernelBuilder> &  iBuilder, const Statement * const stmt) {
     385    addBranchCounter(iBuilder);
     386}
     387
     388void PabloCompiler::compileStatement(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const Statement * const stmt) {
    357389
    358390    if (LLVM_UNLIKELY(isa<If>(stmt))) {
     
    587619}
    588620
    589 Value * PabloCompiler::compileExpression(const std::unique_ptr<kernel::KernelBuilder> &  iBuilder, const PabloAST * expr, const bool ensureLoaded) const {
     621Value * PabloCompiler::compileExpression(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, const PabloAST * expr, const bool ensureLoaded) const {
    590622    if (LLVM_UNLIKELY(isa<Ones>(expr))) {
    591623        return iBuilder->allOnes();
     
    654686PabloCompiler::PabloCompiler(PabloKernel * const kernel)
    655687: mKernel(kernel)
    656 , mCarryManager(new CarryManager) {
     688, mCarryManager(new CarryManager)
     689, mBranchCount(0) {
    657690    assert ("PabloKernel cannot be null!" && kernel);
    658691}
Note: See TracChangeset for help on using the changeset viewer.