Ignore:
Timestamp:
Nov 18, 2016, 1:46:55 PM (2 years ago)
Author:
nmedfort
Message:

Merged PabloFunction? and PabloKernel? classes. Updated projects where necessary.

File:
1 edited

Legend:

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

    r5204 r5217  
    1010#include <pablo/carry_manager.h>
    1111#include <pablo/printer_pablos.h>
    12 #include <pablo/function.h>
     12#include <pablo/prototype.h>
    1313#include <re/re_name.h>
    1414#include <stdexcept>
     
    2323namespace pablo {
    2424
    25 PabloCompiler::PabloCompiler(PabloKernel * k, PabloFunction * const function)
    26 : iBuilder(k->getBuilder())
     25PabloCompiler::PabloCompiler(PabloKernel * kernel)
     26: iBuilder(kernel->getBuilder())
    2727, mBitBlockType(iBuilder->getBitBlockType())
    2828, mCarryManager(nullptr)
    29 , mPabloFunction(function)
    30 , mKernelBuilder(k)
     29, mKernel(kernel)
    3130, mWhileDepth(0)
    3231, mIfDepth(0)
     
    3736
    3837Type * PabloCompiler::initializeKernelData() {
    39     Examine(mPabloFunction);   
     38    Examine();
    4039    mCarryManager = std::unique_ptr<CarryManager>(new CarryManager(iBuilder));
    41     Type * carryDataType = mCarryManager->initializeCarryData(mPabloFunction);
    42     return carryDataType;
    43 }
    44    
    45 void PabloCompiler::verifyParameter(const Var * var, const Value * param) {
    46     if (LLVM_UNLIKELY(&(param->getContext()) != &(iBuilder->getContext()))) {
    47         std::string tmp;
    48         raw_string_ostream out(tmp);
    49         out << "Cannot compile ";
    50         mPabloFunction->print(out);
    51         out << ": LLVM Context for ";
    52         var->print(out);
    53         out << " differs from that of the kernel.";
    54         throw std::runtime_error(out.str());
    55     }
    56 }
    57 
    58 void PabloCompiler::compile(Function * doBlockFunction) {
     40    return mCarryManager->initializeCarryData(mKernel);
     41}
     42   
     43void PabloCompiler::compile(Value * const self, Function * doBlockFunction) {
    5944
    6045    // Make sure that we generate code into the right module.
    6146    mFunction = doBlockFunction;
     47    mSelf = self;
     48
    6249    #ifdef PRINT_TIMING_INFORMATION
    6350    const timestamp_t pablo_compilation_start = read_cycle_counter();
     
    6653    //Generate Kernel//
    6754    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
    68     mSelf = mKernelBuilder->getParameter(doBlockFunction, "self");
    69 
    70     mCarryManager->initializeCodeGen(mKernelBuilder, mSelf);
     55
     56    mCarryManager->initializeCodeGen(mKernel, mSelf);
    7157     
    72     PabloBlock * const entryBlock = mPabloFunction->getEntryBlock(); assert (entryBlock);
     58    PabloBlock * const entryBlock = mKernel->getEntryBlock(); assert (entryBlock);
    7359    mMarkerMap.emplace(entryBlock->createZeroes(), iBuilder->allZeroes());
    7460    mMarkerMap.emplace(entryBlock->createOnes(), iBuilder->allOnes());
    7561
    76     Value * const blockNo = mKernelBuilder->getScalarField(mSelf, blockNoScalar);
    77 
    78     for (unsigned i = 0, j = 0; i < mPabloFunction->getNumOfParameters(); ++i) {
    79         Var * var = mPabloFunction->getParameter(i);
     62    Value * const blockNo = mKernel->getScalarField(mSelf, blockNoScalar);
     63
     64    for (unsigned i = 0; i < mKernel->getNumOfInputs(); ++i) {
     65        Var * var = mKernel->getInput(i);
    8066        std::string name = var->getName()->to_string();
    8167        Value * input = nullptr;
    8268        if (var->getType()->isSingleValueType()) {
    83             input = mKernelBuilder->getScalarFieldPtr(mSelf, name);
     69            input = mKernel->getScalarFieldPtr(mSelf, name);
    8470        } else {
    85             input = mKernelBuilder->getStreamSetBlockPtr(mSelf, name, blockNo);
    86             input = iBuilder->CreateGEP(input, {iBuilder->getInt32(0), iBuilder->getInt32(j++)});
    87         }
    88         verifyParameter(var, input);
     71            input = mKernel->getStreamSetBlockPtr(mSelf, name, blockNo);
     72        }
    8973        mMarkerMap.emplace(var, input);
    9074    }
    9175
    92     for (unsigned i = 0, j = 0; i < mPabloFunction->getNumOfResults(); ++i) {
    93         Var * var = mPabloFunction->getResult(i);
     76    for (unsigned i = 0; i < mKernel->getNumOfOutputs(); ++i) {
     77        Var * var = mKernel->getOutput(i);
    9478        std::string name = var->getName()->to_string();
    9579        Value * output = nullptr;
    9680        if (var->getType()->isSingleValueType()) {
    97             output = mKernelBuilder->getScalarFieldPtr(mSelf, name);
     81            output = mKernel->getScalarFieldPtr(mSelf, name);
    9882        } else {
    99             output = mKernelBuilder->getStreamSetBlockPtr(mSelf, name, blockNo);
    100             output = iBuilder->CreateGEP(output, {iBuilder->getInt32(0), iBuilder->getInt32(j++)});
    101         }
    102         verifyParameter(var, output);
     83            output = mKernel->getStreamSetBlockPtr(mSelf, name, blockNo);
     84        }
    10385        mMarkerMap.emplace(var, output);
    10486    }
     
    11294}
    11395
    114 inline void PabloCompiler::Examine(const PabloFunction * const function) {
     96inline void PabloCompiler::Examine() {
    11597    mWhileDepth = 0;
    11698    mIfDepth = 0;
    11799    mMaxWhileDepth = 0;
    118     Examine(function->getEntryBlock());
     100    Examine(mKernel->getEntryBlock());
    119101}
    120102
     
    124106            const Lookahead * const la = cast<Lookahead>(stmt);
    125107            assert (isa<Var>(la->getExpr()));
    126             if (LLVM_LIKELY(la->getAmount() > mKernelBuilder->getLookAhead())) {
    127                 mKernelBuilder->setLookAhead(la->getAmount());
     108            if (LLVM_LIKELY(la->getAmount() > mKernel->getLookAhead())) {
     109                mKernel->setLookAhead(la->getAmount());
    128110            }
    129111        } else {
     
    277259
    278260    // for any Next nodes in the loop body, initialize to (a) pre-loop value.
    279     for (const auto var : whileStatement->getEscaped()) {
    280         PHINode * phi = iBuilder->CreatePHI(mBitBlockType, 2, getName(var));
     261    for (const auto var : whileStatement->getEscaped()) {       
    281262        auto f = mMarkerMap.find(var);
    282263        if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
     
    284265            raw_string_ostream out(tmp);
    285266            PabloPrinter::print(var, out);
    286             out << " was not assigned a value.";
     267            out << " is uninitialized prior to entering ";
     268            PabloPrinter::print(whileStatement, out);
    287269            llvm::report_fatal_error(out.str());
    288270        }
     271
     272        PHINode * phi = iBuilder->CreatePHI(mBitBlockType, 2, getName(var));
    289273        phi->addIncoming(f->second, whileEntryBlock);
    290274        f->second = phi;
     
    362346            bool storeInstRequired = false;
    363347            if (LLVM_LIKELY(isa<Var>(expr))) {
    364                 for (unsigned i = 0; i < mPabloFunction->getNumOfResults(); ++i) {
    365                     if (expr == mPabloFunction->getResult(i)) {
     348                for (unsigned i = 0; i < mKernel->getNumOfOutputs(); ++i) {
     349                    if (expr == mKernel->getOutput(i)) {
    366350                        storeInstRequired = true;
    367351                        break;
     
    371355
    372356            if (LLVM_UNLIKELY(storeInstRequired || isa<Extract>(expr))) {
    373 
    374357                const auto f = mMarkerMap.find(expr);
    375358                if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
    376359                    std::string tmp;
    377360                    raw_string_ostream out(tmp);
    378                     PabloPrinter::print(expr, out);
    379                     out << " was not defined prior to ";
    380                     PabloPrinter::print(stmt, out);
     361                    out << "Use-before-definition error: ";
     362                    expr->print(out);
     363                    out << " does not dominate ";
     364                    stmt->print(out);
    381365                    throw std::runtime_error(out.str());
    382366                }
     
    389373                    value = iBuilder->CreateTruncOrBitCast(value, count->getType());
    390374                    value = iBuilder->CreateAdd(value, count);
     375                }
     376
     377//                cast<PointerType>(ptr->getType())->getElementType()->getPrimitiveSizeInBits() / 8;
     378
     379                const Type * const type = value->getType();
     380                if (isa<VectorType>(type) || isa<IntegerType>(type)) {
     381                    const auto bitWidth = isa<VectorType>(type)
     382                            ? cast<VectorType>(type)->getBitWidth()
     383                            : cast<IntegerType>(type)->getBitWidth();
     384                    iBuilder->CreateAlignedStore(value, ptr, bitWidth / 8);
     385                } else {
    391386                    iBuilder->CreateStore(value, ptr);
    392                 } else {
    393                     iBuilder->CreateBlockAlignedStore(value, ptr);
    394387                }
    395388            }
     
    398391            Value * array = compileExpression(extract->getArray(), false);
    399392            Value * index = compileExpression(extract->getIndex());
    400             value = iBuilder->CreateGEP(array, index, getName(stmt));
     393            value = iBuilder->CreateGEP(array, {ConstantInt::getNullValue(index->getType()), index}, getName(stmt));
    401394        } else if (const And * pablo_and = dyn_cast<And>(stmt)) {
    402395            value = iBuilder->simd_and(compileExpression(pablo_and->getOperand(0)), compileExpression(pablo_and->getOperand(1)));
     
    427420            value = iBuilder->simd_and(sum, iBuilder->simd_not(cc_expr));
    428421        } else if (const InFile * e = dyn_cast<InFile>(stmt)) {
    429             Value * EOFmask = mKernelBuilder->getScalarField(mSelf, "EOFmask");
     422            Value * EOFmask = mKernel->getScalarField(mSelf, "EOFmask");
    430423            value = iBuilder->simd_xor(compileExpression(e->getExpr()), EOFmask);
    431424        } else if (const AtEOF * e = dyn_cast<AtEOF>(stmt)) {
    432             Value * EOFbit = mKernelBuilder->getScalarField(mSelf, "EOFbit");
     425            Value * EOFbit = mKernel->getScalarField(mSelf, "EOFbit");
    433426            value = iBuilder->simd_and(compileExpression(e->getExpr()), EOFbit);
    434427        } else if (const Count * c = dyn_cast<Count>(stmt)) {
    435428            Value * const to_count = compileExpression(c->getExpr());
    436             const unsigned counterSize = 64;
    437             Value * fieldCounts = iBuilder->simd_popcount(counterSize, to_count);
    438             value = iBuilder->mvmd_extract(counterSize, fieldCounts, 0);
    439             for (unsigned i = 1; i < (iBuilder->getBitBlockWidth() / counterSize); ++i) {
    440                 Value * temp = iBuilder->mvmd_extract(counterSize, fieldCounts, i);
    441                 value = iBuilder->CreateAdd(value, temp);
     429            const unsigned counterSize = iBuilder->getSizeTy()->getBitWidth();
     430            Value * const partial = iBuilder->simd_popcount(counterSize, to_count);
     431            if (LLVM_UNLIKELY(counterSize <= 1)) {
     432                value = partial;
     433            } else {
     434                value = iBuilder->mvmd_extract(counterSize, partial, 0);
     435                const auto fields = (iBuilder->getBitBlockWidth() / counterSize);
     436                for (unsigned i = 1; i < fields; ++i) {
     437                    Value * temp = iBuilder->mvmd_extract(counterSize, partial, i);
     438                    value = iBuilder->CreateAdd(value, temp);
     439                }
    442440            }
    443441        } else if (const Lookahead * l = dyn_cast<Lookahead>(stmt)) {
     
    447445            }
    448446            unsigned index = 0;
    449             for (; index < mPabloFunction->getNumOfParameters(); ++index) {
    450                 if (mPabloFunction->getParameter(index) == var) {
     447            for (; index < mKernel->getNumOfInputs(); ++index) {
     448                if (mKernel->getInput(index) == var) {
    451449                    break;
    452450                }
    453451            }
    454             if (LLVM_UNLIKELY(index >= mPabloFunction->getNumOfParameters())) {
     452            if (LLVM_UNLIKELY(index >= mKernel->getNumOfInputs())) {
    455453                throw std::runtime_error("Lookahead has an illegal Var operand");
    456454            }
     
    458456            const unsigned block_shift = (l->getAmount() / iBuilder->getBitBlockWidth());
    459457            std::string inputName = var->getName()->to_string();;
    460             Value * blockNo = mKernelBuilder->getScalarField(mSelf, blockNoScalar);
    461             Value * lookAhead_blockPtr  = mKernelBuilder->getStreamSetBlockPtr(mSelf, inputName, iBuilder->CreateAdd(blockNo, ConstantInt::get(iBuilder->getSizeTy(), block_shift)));
     458            Value * blockNo = mKernel->getScalarField(mSelf, blockNoScalar);
     459            Value * lookAhead_blockPtr  = mKernel->getStreamSetBlockPtr(mSelf, inputName, iBuilder->CreateAdd(blockNo, ConstantInt::get(iBuilder->getSizeTy(), block_shift)));
    462460            Value * lookAhead_inputPtr = iBuilder->CreateGEP(lookAhead_blockPtr, {iBuilder->getInt32(0), iBuilder->getInt32(index)});
    463461            Value * lookAhead = iBuilder->CreateBlockAlignedLoad(lookAhead_inputPtr);
     
    465463                value = lookAhead;
    466464            } else { // Need to form shift result from two adjacent blocks.
    467                 Value * lookAhead_blockPtr1  = mKernelBuilder->getStreamSetBlockPtr(mSelf, inputName, iBuilder->CreateAdd(blockNo, ConstantInt::get(iBuilder->getSizeTy(), block_shift + 1)));
     465                Value * lookAhead_blockPtr1  = mKernel->getStreamSetBlockPtr(mSelf, inputName, iBuilder->CreateAdd(blockNo, ConstantInt::get(iBuilder->getSizeTy(), block_shift + 1)));
    468466                Value * lookAhead_inputPtr1 = iBuilder->CreateGEP(lookAhead_blockPtr1, {iBuilder->getInt32(0), iBuilder->getInt32(index)});
    469467                Value * lookAhead1 = iBuilder->CreateBlockAlignedLoad(lookAhead_inputPtr1);
Note: See TracChangeset for help on using the changeset viewer.