Ignore:
Timestamp:
Jun 19, 2016, 3:00:47 PM (3 years ago)
Author:
cameron
Message:

New kernel infrastructure

File:
1 edited

Legend:

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

    r5045 r5063  
    1919#include <iostream>
    2020#include <hrtime.h>
     21#include <llvm/Support/Debug.h>
    2122
    2223
     
    2526#define DSSLI_FIELDWIDTH 64
    2627
    27 PabloCompiler::PabloCompiler(Module * m, IDISA::IDISA_Builder * b)
    28 : mMod(m)
     28PabloCompiler::PabloCompiler(IDISA::IDISA_Builder * b, PabloKernel * k, PabloFunction * const function)
     29: mMod(b->getModule())
    2930, iBuilder(b)
    3031, mBitBlockType(b->getBitBlockType())
    3132, mCarryManager(nullptr)
    32 , mPabloFunction(nullptr)
     33, mPabloFunction(function)
    3334, mPabloBlock(nullptr)
    34 , mKernelBuilder(nullptr)
     35, mKernelBuilder(k)
    3536, mWhileDepth(0)
    3637, mIfDepth(0)
     
    4041}
    4142
    42 void PabloCompiler::setKernel(kernel::KernelBuilder * kBuilder){
    43     mKernelBuilder = kBuilder;
    44 }
    45 
    46 llvm::Function * PabloCompiler::compile(PabloFunction * function) {
    47 
     43
     44Type * PabloCompiler::initializeCarryData() {
     45    mCarryManager = make_unique<CarryManager>(iBuilder);
     46    Type * carryDataType = mCarryManager->initializeCarryData(mPabloFunction);
     47    return carryDataType;
     48}
     49   
     50void PabloCompiler::compile(Function * doBlockFunction) {
     51    // Make sure that we generate code into the right module.
     52    mMod = iBuilder->getModule();
     53    mFunction = doBlockFunction;
    4854    #ifdef PRINT_TIMING_INFORMATION
    4955    const timestamp_t pablo_compilation_start = read_cycle_counter();
    5056    #endif
    51  
    52     Examine(function);
    53 
    54     mCarryManager = new CarryManager(iBuilder);
    55 
    56     GenerateKernel(function);
    57        
    58     delete mCarryManager;
    59     mCarryManager = nullptr;
     57
     58    Examine(mPabloFunction);
     59   
     60    //Generate Kernel//
     61    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
     62    mSelf = mKernelBuilder->getParameter(doBlockFunction, "self");
     63    mCarryManager->initializeCodeGen(mKernelBuilder, mSelf);
     64     
     65    Value * inputSet_ptr = mKernelBuilder->getParameter(doBlockFunction, "inputs");
     66   
     67    Value * outputSet_ptr = nullptr;
     68    if (mPabloFunction->getNumOfResults() > 0) {
     69        outputSet_ptr = mKernelBuilder->getParameter(doBlockFunction, "outputs");
     70    }
     71    for (unsigned j = 0; j < mPabloFunction->getNumOfParameters(); ++j) {
     72        Value * inputVal = iBuilder->CreateGEP(inputSet_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(j)});
     73        //Value * inputVal = iBuilder->CreateBlockAlignedLoad(inputSet_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(j)});
     74        const Var * const var = mPabloFunction->getParameter(j);
     75        if (DebugOptionIsSet(DumpTrace)) {
     76            iBuilder->CallPrintRegister(var->getName()->to_string(), iBuilder->CreateBlockAlignedLoad(inputVal));
     77        }
     78        mMarkerMap.insert(std::make_pair(var, inputVal));
     79    }
     80   
     81    compileBlock(mPabloFunction->getEntryBlock());
     82   
     83    for (unsigned j = 0; j < mPabloFunction->getNumOfResults(); ++j) {
     84        const auto f = mMarkerMap.find(mPabloFunction->getResult(j));
     85        if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
     86            throw std::runtime_error("PabloCompiler: result " + std::to_string(j) + " was not assigned a value!");
     87        }
     88        iBuilder->CreateBlockAlignedStore(f->second, outputSet_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(j)});
     89    }
     90    iBuilder->CreateRetVoid();
     91
    6092   
    6193    #ifdef PRINT_TIMING_INFORMATION
     
    6496    #endif
    6597
    66     return mFunction;
    67 }
    68 
    69 inline void PabloCompiler::GenerateKernel(PabloFunction * const function) {
    70  
    71     mPabloFunction = function;
    72 
    73     for (unsigned i = 0; i < function->getNumOfParameters(); ++i) {
    74         mKernelBuilder->addInputStream(1, function->getParameter(i)->getName()->to_string());
    75     }
    76     for (unsigned i = 0; i < function->getNumOfResults(); ++i) {
    77         mKernelBuilder->addOutputStream(1);
    78     }
    79 
    80     mCarryManager->initialize(function, mKernelBuilder);
    81    
    82     mKernelBuilder->addInternalState(mBitBlockType, "EOFmark");
    83    
    84     mFunction = mKernelBuilder->prepareFunction({mInputStreamOffset.begin(), mInputStreamOffset.end()});
    85 
    86     mCarryManager->reset();
    87 
    88     for (unsigned j = 0; j < function->getNumOfParameters(); ++j) {
    89         Value * inputVal = mKernelBuilder->getInputStream(j);
    90         const Var * const var = function->getParameter(j);
    91         if (DebugOptionIsSet(DumpTrace)) {
    92             iBuilder->CallPrintRegister(var->getName()->to_string(), iBuilder->CreateBlockAlignedLoad(inputVal));
    93         }
    94         mMarkerMap.insert(std::make_pair(var, inputVal));
    95     }
    96 
    97     compileBlock(function->getEntryBlock());
    98 
    99     for (unsigned j = 0; j < function->getNumOfResults(); ++j) {
    100         const auto f = mMarkerMap.find(function->getResult(j));
    101         if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
    102             throw std::runtime_error("PabloCompiler: result " + std::to_string(j) + " was not assigned a value!");
    103         }
    104         iBuilder->CreateBlockAlignedStore(f->second, mKernelBuilder->getOutputStream(j));
    105     }
    106 
    107     mKernelBuilder->finalize();
    10898}
    10999
     
    364354        expr = iBuilder->simd_and(sum, iBuilder->simd_not(cc_expr));
    365355    } else if (const InFile * e = dyn_cast<InFile>(stmt)) {
    366         Value * EOFmark = iBuilder->CreateLoad(mKernelBuilder->getInternalState("EOFmark"));
     356        Value * EOFmark = mKernelBuilder->getScalarField(mSelf, "EOFmark");
    367357        Value * infileMask = iBuilder->simd_add(iBuilder->getBitBlockWidth(), EOFmark, iBuilder->allOnes());
    368358        expr = iBuilder->simd_and(compileExpression(e->getExpr()), infileMask);
    369359    } else if (const AtEOF * e = dyn_cast<AtEOF>(stmt)) {
    370         Value * EOFmark = iBuilder->CreateLoad(mKernelBuilder->getInternalState("EOFmark"));
     360        Value * EOFmark = mKernelBuilder->getScalarField(mSelf, "EOFmark");
    371361                expr = iBuilder->simd_and(compileExpression(e->getExpr()), EOFmark);
    372362    } else if (const Count * c = dyn_cast<Count>(stmt)) {
    373363        Value * const to_count = compileExpression(c->getExpr());
    374         expr = mCarryManager->popCount(to_count, c->getGlobalCountIndex());
     364        std::string counter = c->getName()->to_string();
     365        Value * countSoFar = mKernelBuilder->getScalarField(mSelf, counter);
     366        Value * fieldCounts = iBuilder->simd_popcount(64, to_count);
     367        for (unsigned i = 0; i < iBuilder->getBitBlockWidth()/64; ++i) {
     368            countSoFar = iBuilder->CreateAdd(countSoFar, iBuilder->mvmd_extract(64, fieldCounts, i));
     369        }
     370        mKernelBuilder->setScalarField(mSelf, counter, countSoFar);
     371        expr = iBuilder->bitCast(iBuilder->CreateZExt(countSoFar, iBuilder->getIntNTy(iBuilder->getBitBlockWidth())));
    375372    } else if (const Lookahead * l = dyn_cast<Lookahead>(stmt)) {
    376373        PabloAST * const var = l->getExpr();
     
    390387        const unsigned offset1 = ((l->getAmount() + iBuilder->getBitBlockWidth() - 1) / iBuilder->getBitBlockWidth());
    391388        const unsigned shift = (l->getAmount() % iBuilder->getBitBlockWidth());
    392         Value * const v0 = iBuilder->CreateBlockAlignedLoad(mKernelBuilder->getInputStream(index, offset0));
    393         Value * const v1 = iBuilder->CreateBlockAlignedLoad(mKernelBuilder->getInputStream(index, offset1));
     389        Value * const v0 = nullptr;//iBuilder->CreateBlockAlignedLoad(mKernelBuilder->getInputStream(index, offset0));
     390        Value * const v1 = nullptr;//iBuilder->CreateBlockAlignedLoad(mKernelBuilder->getInputStream(index, offset1));
    394391        if (LLVM_UNLIKELY((shift % 8) == 0)) { // Use a single whole-byte shift, if possible.
    395392            expr = iBuilder->mvmd_dslli(8, v1, v0, (shift / 8));
Note: See TracChangeset for help on using the changeset viewer.