Changeset 5074


Ignore:
Timestamp:
Jun 25, 2016, 9:07:07 AM (3 years ago)
Author:
cameron
Message:

Kernel infrastructure: move common logic into KernelBuilder? base class; demo linking in wc

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

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/kernels/deletion.cpp

    r5071 r5074  
    4949
    5050
    51 void deletionKernel::generateKernel() {
     51void deletionKernel::generateDoBlockMethod() {
    5252    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    53     if (mKernelStateType == nullptr) finalizeKernelStateType();
    54     KernelBuilder::generateKernel();
     53    Module * m = iBuilder->getModule();
    5554   
    56     Module * m = iBuilder->getModule();
    57     unsigned blockSize = iBuilder->getBitBlockWidth();
    5855    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
    59     Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
    6056   
    6157    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
     
    6864   
    6965    std::vector<Value *> move_masks = parallel_prefix_deletion_masks(iBuilder, mDeletionFieldWidth, del_mask);
    70        
     66   
    7167    for (unsigned j = 0; j < mStreamCount; ++j) {
    7268        Value * input = iBuilder->CreateBlockAlignedLoad(inputStreamBlock, {iBuilder->getInt32(0), iBuilder->getInt32(j)});
     
    7672    Value * counts = partial_sum_popcount(iBuilder, mDeletionFieldWidth, iBuilder->simd_not(del_mask));
    7773    iBuilder->CreateBlockAlignedStore(iBuilder->bitCast(counts), delCountBlock, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
    78                                          
     74   
    7975    iBuilder->CreateRetVoid();
     76    iBuilder->restoreIP(savePoint);
     77}
     78
     79void deletionKernel::generateFinalBlockMethod() {
     80    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
     81    Module * m = iBuilder->getModule();
    8082   
     83    unsigned blockSize = iBuilder->getBitBlockWidth();
     84    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
     85    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
     86
    8187    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", finalBlockFunction, 0));
    8288    Value * remainingBytes = getParameter(finalBlockFunction, "remainingBytes");
    83     inputStreamBlock = getParameter(finalBlockFunction, "inputStreamSet");
    84     outputStreamBlock = getParameter(finalBlockFunction, "outputStreamSet");
    85     delCountBlock = getParameter(finalBlockFunction, "deletionCounts");
     89    Value * inputStreamBlock = getParameter(finalBlockFunction, "inputStreamSet");
    8690    Value * remaining = iBuilder->CreateZExt(remainingBytes, iBuilder->getIntNTy(blockSize));
    8791    Value * EOF_del = iBuilder->bitCast(iBuilder->CreateShl(Constant::getAllOnesValue(iBuilder->getIntNTy(blockSize)), remaining));
  • icGREP/icgrep-devel/icgrep/kernels/deletion.h

    r5071 r5074  
    4545    mStreamCount(stream_count) {}
    4646   
    47     void generateKernel() override;
    4847private:
     48    void generateDoBlockMethod() override;
     49    void generateFinalBlockMethod() override;
    4950    unsigned mDeletionFieldWidth;
    5051    unsigned mStreamCount;
  • icGREP/icgrep-devel/icgrep/kernels/kernel.cpp

    r5073 r5074  
    4242}
    4343
    44 void KernelBuilder::finalizeKernelStateType() {
     44void KernelBuilder::prepareKernelStateType() {
    4545    mKernelStateType = StructType::create(getGlobalContext(), mKernelFields, mKernelName);
    4646}
     
    4949    Module * saveModule = iBuilder->getModule();
    5050    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    51     if (mKernelStateType == nullptr) finalizeKernelStateType();
    5251    std::unique_ptr<Module> theModule = make_unique<Module>(mKernelName + "_" + iBuilder->getBitBlockTypeName(), getGlobalContext());
    5352    Module * m = theModule.get();
    5453    iBuilder->setModule(m);
    55     KernelBuilder::generateKernel();
     54    generateKernel();
    5655    iBuilder->setModule(saveModule);
    5756    iBuilder->restoreIP(savePoint);
     
    6059
    6160void KernelBuilder::generateKernel() {
     61    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    6262    Module * m = iBuilder->getModule();
    63     IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    64     addKernelDeclarations(m);
     63
     64    prepareKernelStateType();  // possibly overriden by the KernelBuilder subtype
     65    KernelInterface::addKernelDeclarations(m);
     66    generateDoBlockMethod();     // must be implemented by the KernelBuilder subtype
     67    generateFinalBlockMethod();  // possibly overriden by the KernelBuilder subtype
     68
    6569    // Implement the accumulator get functions
    6670    for (auto binding : mScalarOutputs) {
     
    8993}
    9094
    91 void KernelBuilder::addTrivialFinalBlockMethod(Module * m) {
     95//  The default finalBlock method simply dispatches to the doBlock routine.
     96void KernelBuilder::generateFinalBlockMethod() {
    9297    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    93     Module * saveModule = iBuilder->getModule();
    94     iBuilder->setModule(m);
     98    Module * m = iBuilder->getModule();
    9599    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
    96100    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
     
    106110    iBuilder->CreateCall(doBlockFunction, doBlockArgs);
    107111    iBuilder->CreateRetVoid();
    108     iBuilder->setModule(saveModule);
    109112    iBuilder->restoreIP(savePoint);
    110113}
  • icGREP/icgrep-devel/icgrep/kernels/kernel.h

    r5063 r5074  
    2929                    std::vector<ScalarBinding> internal_scalars);
    3030   
     31    // Create a module for the kernel, including the kernel state type declaration and
     32    // the full implementation of all required methods.     
     33    //
     34    std::unique_ptr<llvm::Module> createKernelModule();
     35   
     36    // Generate the Kernel to the current module (iBuilder->getModule()).
     37    void generateKernel();
     38   
     39protected:
     40    //
     41    // Kernel builder subtypes define their logic of kernel construction
     42    // in terms of 3 virtual methods for
     43    // (a) preparing the Kernel state data structure
     44    // (b) defining the logic of the doBlock function, and
     45    // (c) defining the logic of the finalBlock function.
     46    //
     47    // Note: the kernel state data structure must only be finalized after
     48    // all scalar fields have been added.   If there are no fields to
     49    // be added, the default method for preparing kernel state may be used.
     50   
     51    virtual void prepareKernelStateType();
     52   
     53    // Each kernel builder subtype must provide its own logic for generating
     54    // doBlock calls.
     55    virtual void generateDoBlockMethod() = 0;
     56   
     57    // Each kernel builder subtypre must also specify the logic for processing the
     58    // final block of stream data, if there is any special processing required
     59    // beyond simply calling the doBlock function.   In the case that the final block
     60    // processing may be trivially implemented by dispatching to the doBlock method
     61    // without additional preparation, the default generateFinalBlockMethod need
     62    // not be overridden.
     63   
     64    virtual void generateFinalBlockMethod();
     65   
    3166    // Add an additional scalar field to the KernelState struct.
    3267    // Must occur before any call to addKernelDeclarations or createKernelModule.
    3368    void addScalar(llvm::Type * t, std::string scalarName);
    34    
    35     void finalizeKernelStateType();
    36    
    37     // Create a module for the kernel, including the kernel state type and
    38     // all required methods.  The init and accumulator output methods will be
    39     // defined, while the doBlock and finalBlock methods will initially be empty.
    40     //
    41     virtual std::unique_ptr<llvm::Module> createKernelModule();
    42    
    43     // Generate Kernel to the current module.
    44     virtual void generateKernel();
    45    
    46     // Add a FinalBlock method that simply calls DoBlock without additional
    47     // preparation.
    48     void addTrivialFinalBlockMethod(Module * m);
    49    
     69       
    5070    // Run-time access of Kernel State and parameters of methods for
    5171    // use in implementing kernels.
  • icGREP/icgrep-devel/icgrep/kernels/p2s_kernel.cpp

    r5071 r5074  
    4747}
    4848               
    49 void p2sKernel::generateKernel() {
     49void p2sKernel::generateDoBlockMethod() {
    5050    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    51     if (mKernelStateType == nullptr) finalizeKernelStateType();
    52     KernelBuilder::generateKernel();
    53 
    5451    Module * m = iBuilder->getModule();
    55     addTrivialFinalBlockMethod(m);
     52   
    5653    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
    5754   
     
    7370}
    7471       
    75 void p2s_16Kernel::generateKernel() {
     72void p2s_16Kernel::generateDoBlockMethod() {
    7673    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    77     if (mKernelStateType == nullptr) finalizeKernelStateType();
    78     KernelBuilder::generateKernel();
    79 
    8074    Module * m = iBuilder->getModule();
    81     addTrivialFinalBlockMethod(m);
     75   
    8276    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
    8377   
     
    125119const size_t OutputBufferSize=65536;
    126120
    127 void p2s_16Kernel_withCompressedOutputKernel::generateKernel() {
     121void p2s_16Kernel_withCompressedOutputKernel::generateDoBlockMethod() {
    128122    outs().SetBufferSize(OutputBufferSize);
    129123    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    130     if (mKernelStateType == nullptr) finalizeKernelStateType();
    131     KernelBuilder::generateKernel();
    132 
    133    
    134124    Module * m = iBuilder->getModule();
    135125    Type * i8PtrTy = iBuilder->getInt8PtrTy();
     
    139129    Function * writefn = cast<Function>(m->getOrInsertFunction("buffered_write", iBuilder->getVoidTy(), i8PtrTy, i64, nullptr));
    140130
    141     addTrivialFinalBlockMethod(m);
    142131    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
    143132   
  • icGREP/icgrep-devel/icgrep/kernels/p2s_kernel.h

    r5071 r5074  
    3232                  {}, {}, {}) {}
    3333   
    34     void generateKernel() override;
     34private:
     35    void generateDoBlockMethod() override;
    3536   
    3637};
     
    4445                  {}, {}, {}) {}
    4546   
    46     void generateKernel() override;
     47private:
     48    void generateDoBlockMethod() override;
    4749   
    4850};
     
    5759                  {}, {}, {}) {}
    5860       
    59     void generateKernel() override;
     61private:
     62    void generateDoBlockMethod() override;
    6063};
    6164   
  • icGREP/icgrep-devel/icgrep/kernels/pipeline.cpp

    r5063 r5074  
    6363    pablo_function_passes(function);
    6464    PabloKernel  icgrepK(iBuilder, "icgrep", function, {"matchedLineCount"});
    65     icgrepK.prepareKernel();
    6665    icgrepK.generateKernel();
    6766
  • icGREP/icgrep-devel/icgrep/kernels/s2p_kernel.cpp

    r5063 r5074  
    121121   
    122122void s2pKernel::generateFinalBlockMethod() {
    123     /* Now the prepare the s2p final block function:
     123    /* Prepare the s2p final block function:
    124124     assumption: if remaining bytes is greater than 0, it is safe to read a full block of bytes.
    125125     if remaining bytes is zero, no read should be performed (e.g. for mmapped buffer).
     
    156156}
    157157
    158 void s2pKernel::generateKernel() {
     158   
     159void s2pKernel::generateDoBlockMethod() {
    159160    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    160     if (mKernelStateType == nullptr) finalizeKernelStateType();
    161     KernelBuilder::generateKernel();
    162     generateFinalBlockMethod();
     161    Module * m = iBuilder->getModule();
    163162
    164     Module * m = iBuilder->getModule();
    165163    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
    166164   
  • icGREP/icgrep-devel/icgrep/kernels/s2p_kernel.h

    r5063 r5074  
    2929                  {StreamSetBinding{StreamSetType(8, 1), "basisBits"}},
    3030                  {}, {}, {}) {}
    31    
    32     void generateFinalBlockMethod();
    33     void generateKernel() override;
     31
     32private:
     33    void generateDoBlockMethod() override;
     34    void generateFinalBlockMethod() override;
    3435   
    3536};
  • icGREP/icgrep-devel/icgrep/kernels/scanmatchgen.cpp

    r5063 r5074  
    4040
    4141       
    42 void scanMatchKernel::generateKernel() {
     42void scanMatchKernel::generateDoBlockMethod() {
    4343    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    44     if (mKernelStateType == nullptr) finalizeKernelStateType();
    45     KernelBuilder::generateKernel();
    4644    Module * m = iBuilder->getModule();
    47    
    4845    Function * scanWordFunction = generateScanWordRoutine(m);
    4946    const unsigned fieldCount = iBuilder->getBitBlockWidth() / mScanwordBitWidth;
     
    7774    setScalarField(kernelStuctParam, "BlockNo", iBuilder->CreateAdd(getScalarField(kernelStuctParam, "BlockNo"), iBuilder->getInt64(1)));
    7875    iBuilder -> CreateRetVoid();
    79    
    80     // scanMatch FinalBlock function simply dispatches to the DoBlock function
    81     addTrivialFinalBlockMethod(m);
    8276    iBuilder->restoreIP(savePoint);
    8377}
  • icGREP/icgrep-devel/icgrep/kernels/scanmatchgen.h

    r5063 r5074  
    2828    mIsNameExpression(isNameExpression) {}
    2929       
    30     void generateKernel() override;
    31 
    3230private:
     31    void generateDoBlockMethod() override;
    3332    llvm::Function * generateScanWordRoutine(llvm::Module * m);
    3433       
  • icGREP/icgrep-devel/icgrep/pablo/pablo_kernel.cpp

    r5064 r5074  
    4242}
    4343
    44 void PabloKernel::prepareKernel() {
     44void PabloKernel::prepareKernelStateType() {
    4545    Type * carryDataType = pablo_compiler->initializeCarryData();
    4646    addScalar(carryDataType, "carries");
    47     finalizeKernelStateType();
     47    KernelBuilder::prepareKernelStateType();
    4848}
    4949
    50 void PabloKernel::generateKernel() {
     50void PabloKernel::generateDoBlockMethod() {
    5151    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    52     KernelBuilder::generateKernel();
    5352    Module * m = iBuilder->getModule();
    54     addFinalBlockMethod(m);
    5553    pablo_compiler->compile(m->getFunction(mKernelName + doBlock_suffix));
    5654    iBuilder->restoreIP(savePoint);
    5755}
    5856
    59 void PabloKernel::addFinalBlockMethod(Module * m) {
     57void PabloKernel::generateFinalBlockMethod() {
    6058    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    61     Module * saveModule = iBuilder->getModule();
    62     iBuilder->setModule(m);
     59    Module * m = iBuilder->getModule();
    6360    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
    6461    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
     
    7976    iBuilder->CreateCall(doBlockFunction, doBlockArgs);
    8077    iBuilder->CreateRetVoid();
    81     iBuilder->setModule(saveModule);
    8278    iBuilder->restoreIP(savePoint);
    8379}
  • icGREP/icgrep-devel/icgrep/pablo/pablo_kernel.h

    r5062 r5074  
    2525    // using the pablo.Count operation.
    2626   
     27protected:
    2728    // A custom method for preparing kernel declarations is needed,
    2829    // so that the carry data requirements may be accommodated before
    2930    // finalizing the KernelStateType.
    30     void prepareKernel();
     31    void prepareKernelStateType() override;
    3132
    32     void generateKernel() override;
     33    void generateDoBlockMethod() override;
    3334   
    34 protected:
    3535    // The default method for Pablo final block processing sets the
    3636    // EOFmark bit and then calls the standard DoBlock function.
    3737    // This may be overridden for specialized processing.
    38     virtual void addFinalBlockMethod(Module * m);
     38    virtual void generateFinalBlockMethod() override;
    3939   
    4040    PabloFunction * mPabloFunction;
  • icGREP/icgrep-devel/icgrep/u8u16.cpp

    r5071 r5074  
    229229    pablo_function_passes(function);
    230230    pablo::PabloKernel  u8u16k(iBuilder, "u8u16", function, {});
    231     u8u16k.prepareKernel();
    232231    u8u16k.generateKernel();
    233232   
  • icGREP/icgrep-devel/icgrep/wc.cpp

    r5070 r5074  
    1818#include <llvm/ExecutionEngine/ExecutionEngine.h>
    1919#include <llvm/ExecutionEngine/MCJIT.h>
     20#include "llvm/Linker/Linker.h"
    2021
    2122#include <llvm/Support/CommandLine.h>
     
    142143    unsigned mBlockSize = iBuilder->getBitBlockWidth();
    143144    s2pKernel  s2pk(iBuilder);
    144     s2pk.generateKernel();
    145    
     145    std::unique_ptr<Module> s2pM = s2pk.createKernelModule();
    146146    pablo_function_passes(function);
    147147    pablo::PabloKernel  wck(iBuilder, "wc", function, {"lineCount", "wordCount", "charCount"});
    148     wck.prepareKernel();
    149     wck.generateKernel();
     148    std::unique_ptr<Module> wcM = wck.createKernelModule();
     149   
     150    s2pk.addKernelDeclarations(mMod);
     151    wck.addKernelDeclarations(mMod);
    150152
    151153    Constant * record_counts_routine;
     
    221223   
    222224    iBuilder->CreateRetVoid();
     225   
     226    Linker L(*mMod);
     227    L.linkInModule(std::move(s2pM));
     228    L.linkInModule(std::move(wcM));
     229   
    223230    return main;
    224231}
Note: See TracChangeset for help on using the changeset viewer.