Ignore:
Timestamp:
Jun 13, 2016, 11:12:08 AM (3 years ago)
Author:
cameron
Message:

s2p kernel with new infrastructure, includes s2p_FinalBlock

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

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/grep_engine.cpp

    r5048 r5051  
    124124
    125125    pipelineBuilder.CreateKernels(function, UTF_16, isNameExpression);
     126    std::cerr << "CreateKernels complete\n";
    126127
    127128    llvm::Function * grepIR = pipelineBuilder.ExecuteKernels(CountOnly, UTF_16);
     129    std::cerr << "ExecuteKernels complete\n";
    128130
    129131    mEngine = JIT_to_ExecutionEngine(M);
    130132    ApplyObjectCache(mEngine);
    131133    icgrep_Linking(M, mEngine);
    132    
    133     #ifndef NDEBUG
     134    std::cerr << "icgrep_Linking complete\n";
     135
     136    //#ifndef NDEBUG
    134137    verifyModule(*M, &dbgs());
    135     #endif
     138    //#endif
    136139
    137140    mEngine->finalizeObject();
     141    std::cerr << "finalizeObject complete\n";
    138142    delete idb;
    139143
  • icGREP/icgrep-devel/icgrep/icgrep.cpp

    r5045 r5051  
    214214    GrepEngine grepEngine;
    215215    grepEngine.grepCodeGen(module_name, re_ast, CountOnly, UTF_16);
    216    
     216    std::cerr << "grepCodeGen complete";
    217217    initResult(inputFiles);
    218218    for (unsigned i=0; i<inputFiles.size(); ++i){
  • icGREP/icgrep-devel/icgrep/kernels/instance.cpp

    r5000 r5051  
    4747    std::vector<Value *> params;
    4848    params.push_back(mKernelState);
    49     if (mInputScalarSet) {
    50         params.push_back(mKernelState);
    51     }
    5249    if (mInputStreamSet) {
    5350        for (unsigned offset : mDefinition->getInputStreamOffsets()) {
    5451            params.push_back(getInputStreamSet(offset));
    5552        }
    56     }
    57     if (mOutputScalarSet) {
    58         params.push_back(mOutputScalarSet);
    5953    }
    6054    if (mOutputStreamSet) {
  • icGREP/icgrep-devel/icgrep/kernels/instance.h

    r5037 r5051  
    1717    llvm::Value * CreateDoBlockCall();
    1818
     19    llvm::Value * getKernelState() {
     20        return mKernelState;
     21    }
     22   
    1923    llvm::Value * getInternalState(const std::string & name) {
    2024        return mDefinition->getInternalStateInternal(mKernelState, name);
    2125    }
    22 
     26   
    2327    void setInternalState(const std::string & name, llvm::Value * value) {
    2428        mDefinition->setInternalStateInternal(mKernelState, name, value);
     
    5761    }
    5862
    59     llvm::Value * getInputScalar(const unsigned index) {
    60         return mDefinition->getInputScalarInternal(mInputScalarSet, iBuilder->getInt32(index));
    61     }
    62 
    63     llvm::Value * getInputScalar(disable_implicit_conversion<llvm::Value *> index) {
    64         return mDefinition->getInputScalarInternal(mInputScalarSet, index);
    65     }
    66 
    67     llvm::Type * getInputScalarType() const {
    68         return mDefinition->getInputScalarType();
    69     }
    70 
    7163
    7264    inline llvm::Value * getOutputStreamSet(const unsigned streamOffset = 0) {
     
    8476
    8577    void clearOutputStreamSet();
    86 
    87     llvm::Value * getOutputScalar(const unsigned index) {
    88         return mDefinition->getOutputScalarInternal(mOutputScalarSet, iBuilder->getInt32(index));
    89     }
    90 
    91     llvm::Value * getOutputScalar(disable_implicit_conversion<llvm::Value *> index) {
    92         return mDefinition->getOutputScalarInternal(mOutputScalarSet, index);
    93     }
    9478
    9579    llvm::Value * getBlockNo() {
     
    11296
    11397    Instance(KernelBuilder * const definition, llvm::Value * const kernelState,
    114              llvm::Value * const inputScalarSet, llvm::Value * const inputStreamSet, const unsigned inputBufferSize,
    115              llvm::Value * const outputScalarSet, llvm::Value * const outputStreamSet, const unsigned outputBufferSize)
     98             llvm::Value * const inputStreamSet, const unsigned inputBufferSize,
     99             llvm::Value * const outputStreamSet, const unsigned outputBufferSize)
    116100    : mDefinition(definition)
    117101    , iBuilder(definition->iBuilder)
    118102    , mKernelState(kernelState)
    119     , mInputScalarSet(inputScalarSet)
    120103    , mInputStreamSet(inputStreamSet)
    121104    , mInputBufferSize(inputBufferSize)
    122     , mOutputScalarSet(outputScalarSet)
    123105    , mOutputStreamSet(outputStreamSet)
    124106    , mOutputBufferSize(outputBufferSize) {
     
    132114    IDISA::IDISA_Builder * const                    iBuilder;
    133115    llvm::Value * const                             mKernelState;
    134     llvm::Value * const                             mInputScalarSet;
    135116    llvm::Value * const                             mInputStreamSet;
    136117    const unsigned                                  mInputBufferSize;
    137     llvm::Value * const                             mOutputScalarSet;
    138118    llvm::Value * const                             mOutputStreamSet;
    139119    const unsigned                                  mOutputBufferSize;
  • icGREP/icgrep-devel/icgrep/kernels/kernel.cpp

    r5037 r5051  
    2323, mDefaultBufferSize(defaultBufferSize)
    2424, mBitBlockType(builder->getBitBlockType())
    25 , mBlockNoIndex(0) {
     25, mBlockNoIndex(0)
     26, mKernelStateType(nullptr) {
    2627    assert (mDefaultBufferSize > 0);
    2728}
     
    3738}
    3839
    39 unsigned KernelBuilder::addInternalState(llvm::Type * const type, std::string && name) {
    40     if (LLVM_UNLIKELY(mInternalStateNameMap.count(name) != 0)) {
    41         throw std::runtime_error("Kernel already contains internal state '" + name + "'");
    42     }
    43     const unsigned index = addInternalState(type);
    44     mInternalStateNameMap.emplace(name, iBuilder->getInt32(index));
    45     return index;
    46 }
    47 
     40    unsigned KernelBuilder::addInternalState(llvm::Type * const type, std::string name) {
     41        if (LLVM_UNLIKELY(mInternalStateNameMap.count(name) != 0)) {
     42            throw std::runtime_error("Kernel already contains internal state '" + name + "'");
     43        }
     44        const unsigned index = addInternalState(type);
     45        mInternalStateNameMap.emplace(name, iBuilder->getInt32(index));
     46        return index;
     47    }
     48   
    4849/** ------------------------------------------------------------------------------------------------------------- *
    4950 * @brief getInternalState
     
    116117}
    117118
    118 /** ------------------------------------------------------------------------------------------------------------- *
    119  * @brief addInputScalar
    120  ** ------------------------------------------------------------------------------------------------------------- */
    121 void KernelBuilder::addInputScalar(Type * const type, std::string && name) {
    122     assert (type && !name.empty());
    123     mInputScalarName.push_back(name);
    124     mInputScalar.push_back(type);
    125 }
    126 
    127 void KernelBuilder::addInputScalar(Type * const type) {
    128     addInputScalar(type, mKernelName + "_InputScalar_" + std::to_string(mInputScalar.size()));
    129 }
    130 
    131 /** ------------------------------------------------------------------------------------------------------------- *
    132  * @brief getInputScalar
    133  ** ------------------------------------------------------------------------------------------------------------- */
    134 Value * KernelBuilder::getInputScalarInternal(Value * const inputScalarSet, disable_implicit_conversion<Value *>) {
    135     assert (inputScalarSet);
    136     throw std::runtime_error("currently not supported!");
    137 }
    138119
    139120/** ------------------------------------------------------------------------------------------------------------- *
     
    144125    const unsigned index = mOutputStream.size();
    145126    mOutputStream.push_back((fields == 1) ? mBitBlockType : ArrayType::get(mBitBlockType, fields));
    146     return index;
    147 }
    148 
    149 /** ------------------------------------------------------------------------------------------------------------- *
    150  * @brief addOutputScalar
    151  ** ------------------------------------------------------------------------------------------------------------- */
    152 unsigned KernelBuilder::addOutputScalar(Type * const type) {
    153     assert (type);
    154     const unsigned index = mOutputScalar.size();
    155     mOutputScalar.push_back(type);
    156127    return index;
    157128}
     
    171142
    172143/** ------------------------------------------------------------------------------------------------------------- *
    173  * @brief getOutputScalar
    174  ** ------------------------------------------------------------------------------------------------------------- */
    175 Value * KernelBuilder::getOutputScalarInternal(Value * const, disable_implicit_conversion<Value *> ) {
    176     throw std::runtime_error("currently not supported!");
    177 }
    178 
    179 /** ------------------------------------------------------------------------------------------------------------- *
    180144 * @brief packDataTypes
    181145 ** ------------------------------------------------------------------------------------------------------------- */
     
    199163    mBlockNoIndex = iBuilder->getInt32(addInternalState(iBuilder->getInt64Ty(), "BlockNo"));
    200164
    201     mKernelStateType = StructType::create(iBuilder->getContext(), mInternalState, mKernelName);
    202     mInputScalarType = packDataTypes(mInputScalar);
     165    if (!mKernelStateType) {
     166        mKernelStateType = StructType::create(iBuilder->getContext(), mInternalState, mKernelName);
     167    }
    203168    mInputStreamType = packDataTypes(mInputStream);
    204     mOutputScalarType = packDataTypes(mInputScalar);
    205169    mOutputStreamType = packDataTypes(mOutputStream);
    206170    mInputStreamOffsets = inputStreamOffsets;
     
    208172    std::vector<Type *> params;
    209173    params.push_back(mKernelStateType->getPointerTo());
    210     if (mInputScalarType) {
    211         params.push_back(mInputScalarType->getPointerTo());
    212     }
    213174    if (mInputStreamType) {
    214175        for (unsigned i = 0; i < mInputStreamOffsets.size(); ++i) {
    215176            params.push_back(mInputStreamType->getPointerTo());
    216177        }
    217     }
    218     if (mOutputScalarType) {
    219         params.push_back(mOutputScalarType->getPointerTo());
    220178    }
    221179    if (mOutputStreamType) {
     
    237195    mKernelStateParam = &*(args++);
    238196    mKernelStateParam->setName("this");
    239     if (mInputScalarType) {
    240         mInputScalarParam = &*(args++);
    241         mInputScalarParam->setName("inputScalars");
    242     }
    243197    if (mInputStreamType) {
    244198        for (const unsigned offset : mInputStreamOffsets) {
     
    248202        }
    249203    }
    250     if (mOutputScalarType) {
    251         mOutputScalarParam = &*(args++);
    252         mOutputScalarParam->setName("outputScalars");
    253     }
    254204    if (mOutputStreamType) {
    255205        mOutputStreamParam = &*args;
     
    259209    return mDoBlock;
    260210}
     211
     212void KernelBuilder::setInstanceParameters(std::vector<ParameterBinding> parms) {
     213    mInstanceParameters = parms;
     214    mInstanceParametersOffset = mInternalState.size();
     215    for (auto binding : mInstanceParameters) {
     216        addInternalState(binding.parameterType, binding.parameterName);
     217    }
     218}
     219
     220
     221Function *  KernelBuilder::createInitMethod() {
     222    if (!mKernelStateType) {
     223        mKernelStateType = StructType::create(iBuilder->getContext(), mInternalState, mKernelName);
     224    }
     225    std::vector<Type *> initParameters = {PointerType::getUnqual(mKernelStateType)};
     226    for (auto binding : mInstanceParameters) {
     227        initParameters.push_back(binding.parameterType);
     228    }
     229    FunctionType * mInitFunctionType = FunctionType::get(iBuilder->getVoidTy(), initParameters, false);
     230    Function * mInitFunction = Function::Create(mInitFunctionType, GlobalValue::ExternalLinkage, mKernelName + "_Init", iBuilder->getModule());
     231    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "Init_entry", mInitFunction, 0));
     232
     233    Function::arg_iterator args = mInitFunction->arg_begin();
     234    Value * self = &*(args++);
     235    self->setName("self");
     236    for (auto binding : mInstanceParameters) {
     237        Value * parm = &*(args++);
     238        parm->setName(binding.parameterName);
     239    }
     240
     241    iBuilder->CreateStore(Constant::getNullValue(mKernelStateType), self);
     242    args = mInitFunction->arg_begin();
     243    args++;   // skip self argument.
     244    for (auto binding : mInstanceParameters) {
     245        Value * parm = &*(args++);
     246        setInternalStateInternal(self, binding.parameterName, parm);
     247    }
     248    iBuilder->CreateRetVoid();
     249    return mInitFunction;
     250}
     251
     252
     253
     254
     255
    261256
    262257/** ------------------------------------------------------------------------------------------------------------- *
     
    272267
    273268    mKernelStateParam = nullptr;
    274     mInputScalarParam = nullptr;
    275269    mInputStreamParam.clear();
    276     mOutputScalarParam = nullptr;
    277270    mOutputStreamParam = nullptr;
    278271    iBuilder->ClearInsertionPoint();
     
    287280    AllocaInst * const kernelState = iBuilder->CreateAlloca(mKernelStateType);
    288281    iBuilder->CreateStore(Constant::getNullValue(mKernelStateType), kernelState);
    289     AllocaInst * outputScalars = nullptr;
    290     if (mOutputScalarType) {
    291         outputScalars = iBuilder->CreateAlloca(mOutputScalarType);
    292     }
    293282    AllocaInst * outputStreamSets = nullptr;
    294283    if (mOutputStreamType) {
    295284        outputStreamSets = iBuilder->CreateAlloca(mOutputStreamType, iBuilder->getInt32(outputBufferSize));
    296285    }
    297     return new Instance(this, kernelState, nullptr, std::get<0>(inputStreamSet), std::get<1>(inputStreamSet), outputScalars, outputStreamSets, outputBufferSize);
     286    return new Instance(this, kernelState, std::get<0>(inputStreamSet), std::get<1>(inputStreamSet), outputStreamSets, outputBufferSize);
    298287}
    299288
     
    320309    return f->second;
    321310}
     311   
     312llvm::Value * make_New(IDISA::IDISA_Builder * iBuilder, std::string kernel_name, std::vector<Value *> args) {
     313    Module * m = iBuilder->getModule();
     314    Type * kernelType = m->getTypeByName(kernel_name);
     315    if (!kernelType) {
     316        throw std::runtime_error("Cannot find kernel type " + kernel_name);
     317    }
     318    Value * kernelInstance = iBuilder->CreateAlloca(kernelType);
     319    std::vector<Value *> init_args = {kernelInstance};
     320    for (auto a : args) {
     321        init_args.push_back(a);
     322    }
     323    //iBuilder->CreateStore(Constant::getNullValue(kernelType), kernelInstance);
     324    Function * initMethod = m->getFunction(kernel_name + "_Init");
     325    if (!initMethod) {
     326        //throw std::runtime_error("Cannot find " + kernel_name + "_Init");
     327        iBuilder->CreateStore(Constant::getNullValue(kernelType), kernelInstance);
     328        return kernelInstance;
     329    }
     330    iBuilder->CreateCall(initMethod, init_args);
     331    return kernelInstance;
     332}
     333    llvm::Value * make_DoBlock_Call(IDISA::IDISA_Builder * iBuilder, std::string kernel_name, std::vector<Value *> args) {
     334        Module * m = iBuilder->getModule();
     335        Function * doBlockMethod = m->getFunction(kernel_name + "_DoBlock");
     336        if (!doBlockMethod) {
     337            throw std::runtime_error("Cannot find " + kernel_name + "_DoBlock");
     338        }
     339        return iBuilder->CreateCall(doBlockMethod, args);
     340    }
     341   
     342    llvm::Value * make_FinalBlock_Call(IDISA::IDISA_Builder * iBuilder, std::string kernel_name, std::vector<Value *> args) {
     343        Module * m = iBuilder->getModule();
     344        Function * finalBlockMethod = m->getFunction(kernel_name + "_FinalBlock");
     345        if (!finalBlockMethod) {
     346            throw std::runtime_error("Cannot find " + kernel_name + "_FinalBlock");
     347        }
     348        return iBuilder->CreateCall(finalBlockMethod, args);
     349    }
     350   
     351   
    322352
    323353} // end of namespace kernel
  • icGREP/icgrep-devel/icgrep/kernels/kernel.h

    r5037 r5051  
    4242
    4343namespace kernel {
    44 
     44    struct ParameterBinding {
     45        llvm::Type * parameterType;
     46        std::string parameterName;
     47    };
     48   
     49   
     50   
    4551class Instance;
    4652
     
    5460    KernelBuilder(IDISA::IDISA_Builder * builder, std::string && name, const unsigned defaultBufferSize);
    5561
     62   
     63    void setInstanceParameters(std::vector<ParameterBinding> binding);
     64
    5665    unsigned addInternalState(llvm::Type * const type);
    57     unsigned addInternalState(llvm::Type * const type, std::string && name);
     66    unsigned addInternalState(llvm::Type * const type, std::string name);
    5867
    5968    void addInputStream(const unsigned fields);
    6069    void addInputStream(const unsigned fields, std::string && name);
    6170
    62     void addInputScalar(llvm::Type * const type);
    63     void addInputScalar(llvm::Type * const type, std::string && name);
    64 
    6571    unsigned addOutputStream(const unsigned fields);
    66     unsigned addOutputScalar(llvm::Type * const type);
    67 
     72
     73   
     74   
     75    llvm::Function * createInitMethod();
     76
     77   
    6878    inline llvm::Function * prepareFunction() {
    6979        return prepareFunction({0});
     
    118128    }
    119129
    120     inline llvm::Value * getInputScalar(const unsigned index) {
    121         assert (index < getNumOfInputScalars());
    122         return getInputScalarInternal(mInputScalarParam, iBuilder->getInt32(index));
    123     }
    124 
    125     inline llvm::Value * getInputScalar(disable_implicit_conversion<llvm::Value *> const index) {
    126         return getInputScalarInternal(mInputScalarParam, index);
    127     }
    128 
    129     inline unsigned getNumOfInputScalars() const {
    130         return mInputScalar.size();
    131     }
    132 
    133     inline llvm::Type * getInputScalarType() const {
    134         return mInputScalarType;
    135     }
    136 
    137130    inline llvm::Value * getOutputStream(const unsigned index) {
    138131        assert (index < getNumOfOutputStreams());
     
    152145    }
    153146
    154     inline llvm::Value * getOutputScalar(const unsigned index) {
    155         assert (index < getNumOfOutputScalars());
    156         return getOutputScalarInternal(mOutputScalarParam, iBuilder->getInt32(index));
    157     }
    158 
    159     inline llvm::Value * getOutputScalar(disable_implicit_conversion<llvm::Value *> const index) {
    160         return getOutputScalarInternal(mOutputScalarParam, index);
    161     }
    162 
    163     inline unsigned getNumOfOutputScalars() const {
    164         return mOutputScalar.size();
    165     }
    166 
    167     inline llvm::Type * getOutputScalarType() const {
    168         return mOutputStreamType;
    169     }
    170 
    171147    inline llvm::Value * getBlockNo() {
    172148        return getBlockNoInternal(mKernelStateParam);
     
    199175    llvm::Value * getInputStreamInternal(llvm::Value * const inputStreamSet, disable_implicit_conversion<llvm::Value *> index);
    200176
    201     llvm::Value * getInputScalarInternal(llvm::Value * const inputScalarSet, disable_implicit_conversion<llvm::Value *> index);
    202 
    203177    llvm::Value * getInternalStateInternal(llvm::Value * const kernelState, const std::string & name);
    204178
     
    212186
    213187    llvm::Value * getOutputStreamInternal(llvm::Value * const outputStreamSet, disable_implicit_conversion<llvm::Value *> index);
    214 
    215     llvm::Value * getOutputScalarInternal(llvm::Value * const outputScalarSet, disable_implicit_conversion<llvm::Value *> index);
    216188
    217189    llvm::Value * getBlockNoInternal(llvm::Value * const instance) {
     
    238210    llvm::Function *                                    mDoBlock;
    239211
     212    std::vector<ParameterBinding>           mInstanceParameters;
     213    unsigned                            mInstanceParametersOffset;
     214   
    240215    llvm::Type *                        mKernelStateType;
    241     llvm::Type *                        mInputScalarType;
    242216    llvm::Type *                        mInputStreamType;
    243     llvm::Type *                        mOutputScalarType;
    244217    llvm::Type *                        mOutputStreamType;
    245218
    246219    llvm::Value *                       mKernelStateParam;
    247     llvm::Value *                       mInputScalarParam;
    248220    InputStreamMap                      mInputStreamParam;
    249     llvm::Value *                       mOutputScalarParam;
    250221    llvm::Value *                       mOutputStreamParam;
    251222
    252     std::vector<llvm::Type *>           mInputScalar;
    253223    std::vector<std::string>            mInputScalarName;   
    254224    std::vector<llvm::Type *>           mInputStream;
    255225    std::vector<std::string>            mInputStreamName;
    256226    std::vector<unsigned>               mInputStreamOffsets;
    257     std::vector<llvm::Type *>           mOutputScalar;
    258227    std::vector<llvm::Type *>           mOutputStream;
    259228    std::vector<llvm::Type *>                   mInternalState;
     
    272241    return mDefaultBufferSize;
    273242}
    274 
     243   
     244llvm::Value * make_New(IDISA::IDISA_Builder * iBuilder, std::string kernel_name, std::vector<Value *> args);
     245
     246    llvm::Value * make_DoBlock_Call(IDISA::IDISA_Builder * iBuilder, std::string kernel_name, std::vector<Value *> args);
     247    llvm::Value * make_FinalBlock_Call(IDISA::IDISA_Builder * iBuilder, std::string kernel_name, std::vector<Value *> args);
     248   
    275249} // end of namespace kernel
    276250
  • icGREP/icgrep-devel/icgrep/kernels/pipeline.cpp

    r5048 r5051  
    66
    77#include <toolchain.h>
     8#include "streamset.h"
     9#include "interface.h"
     10
    811#include "pipeline.h"
    912#include "utf_encoding.h"
     
    1821
    1922#include <llvm/IR/Intrinsics.h>
     23#include "llvm/Support/SourceMgr.h"
     24#include "llvm/IRReader/IRReader.h"
     25#include "llvm/Linker/Linker.h"
    2026
    2127using namespace pablo;
     
    3137
    3238PipelineBuilder::~PipelineBuilder() {
    33     delete mS2PKernel;
    34     delete mICgrepKernel;
    35     delete mScanMatchKernel;
    3639}
    3740
    3841void PipelineBuilder::CreateKernels(PabloFunction * function, bool UTF_16, bool isNameExpression){
    39     mS2PKernel = new KernelBuilder(iBuilder, "s2p", codegen::SegmentSize);
     42    //mS2PKernel = new KernelBuilder(iBuilder, "s2p", codegen::SegmentSize);
    4043    mICgrepKernel = new KernelBuilder(iBuilder, "icgrep", codegen::SegmentSize);
    4144    mScanMatchKernel = new KernelBuilder(iBuilder, "scanMatch", codegen::SegmentSize);
    42     if (UTF_16) {
    43         generateS2P_16Kernel(mMod, iBuilder, mS2PKernel);
    44     }
    45     else {
    46         generateS2PKernel(mMod, iBuilder, mS2PKernel);
    47     }
     45//#define LOAD_S2P_LL
     46#ifdef LOAD_S2P_LL
     47    SMDiagnostic Err;
     48    std::unique_ptr<Module> s2p_Module = parseIRFile("s2p.ll", Err, mMod->getContext());
     49    if (!s2p_Module) {
     50         Err.print("icgrep", errs());
     51         exit(1);
     52    }
     53    Linker L(*mMod);
     54    //s2p_Module->dump();
    4855    generateScanMatch(mMod, iBuilder, 64, mScanMatchKernel, isNameExpression);
     56    L.linkInModule(std::move(s2p_Module));
     57    errs() << "s2p.ll loaded\n";
     58#else
     59    errs() << "A\n";
     60    //mS2PKernel = new KernelBuilder(iBuilder, "s2p", codegen::SegmentSize);
     61    s2pKernel  s2pk(iBuilder);
     62    //s2pk.addKernelDeclarations(mMod);
     63    //mMod->dump();
     64    std::unique_ptr<Module> s2pM = s2pk.createKernelModule();
     65    Linker L(*mMod);
     66    L.linkInModule(std::move(s2pM));
     67    errs() << "b\n";
     68   
     69   
     70    generateScanMatch(mMod, iBuilder, 64, mScanMatchKernel, isNameExpression);
     71#endif
     72   
    4973    pablo_function_passes(function);
    5074    PabloCompiler pablo_compiler(mMod, iBuilder);
     
    6791}
    6892
    69 inline Value * Cal_Count(Instance * icGrepInstance, IDISA::IDISA_Builder * iBuilder) {
    70     Value * match = icGrepInstance->getOutputStream(0, 0);
    71     Value * matches = iBuilder->CreateLoad(match, false, "match");
     93inline Value * Cal_Count(Value * match_ptr, IDISA::IDISA_Builder * iBuilder) {
     94    Value * matches = iBuilder->CreateLoad(match_ptr, false, "match");
    7295    return generatePopcount(iBuilder, matches);
    7396}
    7497
    7598Function * PipelineBuilder::ExecuteKernels(bool CountOnly, bool UTF_16) {
     99
    76100    Type * const int64ty = iBuilder->getInt64Ty();
    77101    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
    78     Type * const inputType = PointerType::get(ArrayType::get(StructType::get(mMod->getContext(), std::vector<Type *>({ArrayType::get(mBitBlockType, (UTF_16 ? 16 : 8))})), 1), 0);
     102    Type * const inputType = PointerType::get(ArrayType::get(ArrayType::get(mBitBlockType, (UTF_16 ? 16 : 8)), 1), 0);
    79103    Type * const resultTy = CountOnly ? int64ty : iBuilder->getVoidTy();
    80104    Function * const main = cast<Function>(mMod->getOrInsertFunction("Main", resultTy, inputType, int64ty, int64ty, nullptr));
     
    88112    Value * const fileIdx = &*(args++);
    89113    fileIdx->setName("fileIdx");
     114    errs() << "B\n";
    90115
    91116    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", main,0));
    92 
    93 
     117    iBuilder->CallPrintInt("bufferSize", bufferSize);
    94118    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
    95119    BasicBlock * segmentCondBlock = nullptr;
     
    103127    BasicBlock * fullBodyBlock = BasicBlock::Create(mMod->getContext(), "fullBody", main, 0);
    104128    BasicBlock * finalBlock = BasicBlock::Create(mMod->getContext(), "final", main, 0);
    105     BasicBlock * finalPartialBlock = BasicBlock::Create(mMod->getContext(), "partial", main, 0);
    106     BasicBlock * finalEmptyBlock = BasicBlock::Create(mMod->getContext(), "empty", main, 0);
    107     BasicBlock * exitBlock = BasicBlock::Create(mMod->getContext(), "exit", main, 0);
    108129
    109130    Value * count = nullptr;
     
    112133        iBuilder->CreateStore(ConstantInt::getNullValue(mBitBlockType), count);
    113134    }
    114 
    115     Instance * s2pInstance = mS2PKernel->instantiate(inputStream);
    116     Instance * icGrepInstance = mICgrepKernel->instantiate(s2pInstance->getOutputStreamBuffer());
     135    errs() << "C\n";
     136
     137    Value * s2pI = make_New(iBuilder,  "s2p", {});
     138    errs() << "D\n";
     139
     140    StreamSetBuffer ByteStream(iBuilder, StreamSetType(1, (UTF_16 ? 16 : 8)), 0);
     141    ByteStream.setStreamSetBuffer(inputStream);
     142   
     143    StreamSetBuffer BasisBits(iBuilder, StreamSetType((UTF_16 ? 16 : 8), 1), codegen::SegmentSize);
     144    Value * basis_bits_ptr = BasisBits.allocateBuffer();
     145   
     146    Value * grepI = make_New(iBuilder,  "icgrep", {});
     147   
     148    StreamSetBuffer MatchResults(iBuilder, StreamSetType(2, 1), codegen::SegmentSize);
     149    Value * match_results_ptr = MatchResults.allocateBuffer();
     150
    117151    Instance * scanMatchInstance = nullptr;
    118152   
    119153    if (!CountOnly) {
    120         scanMatchInstance = mScanMatchKernel->instantiate(icGrepInstance->getOutputStreamBuffer());
     154        scanMatchInstance = mScanMatchKernel->instantiate(match_results_ptr);
    121155        scanMatchInstance->setInternalState("FileBuf", iBuilder->CreateBitCast(inputStream, int8PtrTy));
    122156        scanMatchInstance->setInternalState("FileSize", bufferSize);
     
    124158    }
    125159    Value * initialBufferSize = nullptr;
     160    Value * initialBlockNo = nullptr;
    126161    BasicBlock * initialBlock = nullptr;
    127162
     
    131166        PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
    132167        remainingBytes->addIncoming(bufferSize, entryBlock);
     168        PHINode * blockNo = iBuilder->CreatePHI(int64ty, 2, "blockNo");
     169        blockNo->addIncoming(iBuilder->getInt64(0), entryBlock);
    133170        Constant * const step = ConstantInt::get(int64ty, mBlockSize * segmentSize * (UTF_16 ? 2 : 1));
    134171        Value * segmentCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
     
    136173        iBuilder->SetInsertPoint(segmentBodyBlock);
    137174        for (unsigned i = 0; i < segmentSize; ++i) {
    138             s2pInstance->CreateDoBlockCall();
     175            Value * blkNo = iBuilder->CreateAdd(blockNo, iBuilder->getInt64(i));
     176            make_DoBlock_Call(iBuilder, "s2p", {s2pI, ByteStream.getBlockPointer(blkNo), BasisBits.getBlockPointer(blkNo)});
    139177        }
    140178        for (unsigned i = 0; i < segmentSize; ++i) {
    141             Value * match = (icGrepInstance->getOutputStream(0, 0));
    142             icGrepInstance->CreateDoBlockCall();
    143             Value * temp = iBuilder->CreateLoad(match);
    144             Value * matches = iBuilder->CreateBitCast(temp, iBuilder->getIntNTy(mBlockSize));
    145             Value * popcount_for = generatePopcount(iBuilder, matches);
    146             if(CountOnly){
    147                 Value * temp_count = iBuilder->CreateLoad(count);
    148                 Value * prev_count = iBuilder->CreateBitCast(temp_count, iBuilder->getIntNTy(mBlockSize));
    149                 Value * add_for = iBuilder->CreateAdd(prev_count, popcount_for);
    150                 Value * add = iBuilder->CreateBitCast(add_for, mBitBlockType);
    151                 iBuilder->CreateStore(add, count);
    152             }
     179            Value * blkNo = iBuilder->CreateAdd(blockNo, iBuilder->getInt64(i));
     180            match_results_ptr = MatchResults.getBlockPointer(blkNo);
     181            make_DoBlock_Call(iBuilder, "icgrep", {grepI, BasisBits.getBlockPointer(blkNo), match_results_ptr});
     182            if (CountOnly) {
     183                Value * matchptr = iBuilder->CreateGEP(mBitBlockType, match_results_ptr, {iBuilder->getInt64(0), iBuilder->getInt32(0), iBuilder->getInt32(0)});
     184                Value * temp = iBuilder->CreateLoad(matchptr);
     185                Value * matches = iBuilder->CreateBitCast(temp, iBuilder->getIntNTy(mBlockSize));
     186                Value * popcount_for = generatePopcount(iBuilder, matches);
     187                Value * temp_count = iBuilder->CreateLoad(count);
     188                Value * prev_count = iBuilder->CreateBitCast(temp_count, iBuilder->getIntNTy(mBlockSize));
     189                Value * add_for = iBuilder->CreateAdd(prev_count, popcount_for);
     190                Value * add = iBuilder->CreateBitCast(add_for, mBitBlockType);
     191                iBuilder->CreateStore(add, count);
     192            }
    153193        }
    154194        if (!CountOnly) {
    155195            for (unsigned i = 0; i < segmentSize; ++i) {
    156                 scanMatchInstance->CreateDoBlockCall();
     196                Value * blkNo = iBuilder->CreateAdd(blockNo, iBuilder->getInt64(i));
     197                match_results_ptr = MatchResults.getBlockPointer(blkNo);
     198                make_DoBlock_Call(iBuilder, "scanMatch", {scanMatchInstance->getKernelState(), match_results_ptr});
    157199            }
    158200        }
    159201        remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), segmentBodyBlock);
     202        blockNo->addIncoming(iBuilder->CreateAdd(blockNo, iBuilder->getInt64(segmentSize)), segmentBodyBlock);
    160203        iBuilder->CreateBr(segmentCondBlock);
    161204        initialBufferSize = remainingBytes;
    162205        initialBlock = segmentCondBlock;
     206        initialBlockNo = blockNo;
    163207    } else {
    164208        initialBufferSize = bufferSize;
    165209        initialBlock = entryBlock;
     210        initialBlockNo = iBuilder->getInt64(0);
    166211        iBuilder->CreateBr(fullCondBlock);
    167212    }
     
    170215    PHINode * remainingBytes = iBuilder->CreatePHI(int64ty, 2, "remainingBytes");
    171216    remainingBytes->addIncoming(initialBufferSize, initialBlock);
    172 
     217    PHINode * blockNo = iBuilder->CreatePHI(int64ty, 2, "blockNo");
     218    blockNo->addIncoming(initialBlockNo, initialBlock);
     219   
    173220    Constant * const step = ConstantInt::get(int64ty, mBlockSize * (UTF_16 ? 2 : 1));
    174221    Value * fullCondTest = iBuilder->CreateICmpULT(remainingBytes, step);
     
    176223
    177224    iBuilder->SetInsertPoint(fullBodyBlock);
    178     s2pInstance->CreateDoBlockCall();
    179     icGrepInstance->CreateDoBlockCall();
     225    errs() << "E\n";
     226
     227    make_DoBlock_Call(iBuilder, "s2p", {s2pI, ByteStream.getBlockPointer(blockNo), BasisBits.getBlockPointer(blockNo)});
     228    errs() << "F\n";
     229    make_DoBlock_Call(iBuilder, "icgrep", {grepI, BasisBits.getBlockPointer(blockNo), MatchResults.getBlockPointer(blockNo)});
    180230    if (CountOnly) {
    181         Value * popcount = Cal_Count(icGrepInstance, iBuilder);
     231        Value * matchptr = iBuilder->CreateGEP(mBitBlockType, match_results_ptr, {iBuilder->getInt64(0), iBuilder->getInt32(0)});
     232        Value * popcount = Cal_Count(matchptr, iBuilder);
    182233        Value * temp_count = iBuilder->CreateLoad(count);
    183234        Value * add = iBuilder->CreateAdd(temp_count, popcount);
    184235        iBuilder->CreateStore(add, count);
    185236    } else {
    186         scanMatchInstance->CreateDoBlockCall();
     237        make_DoBlock_Call(iBuilder, "scanMatch", {scanMatchInstance->getKernelState(), MatchResults.getBlockPointer(blockNo)});
    187238    }
    188239
    189240    remainingBytes->addIncoming(iBuilder->CreateSub(remainingBytes, step), fullBodyBlock);
     241    blockNo->addIncoming(iBuilder->CreateAdd(blockNo, iBuilder->getInt64(1)), fullBodyBlock);
    190242    iBuilder->CreateBr(fullCondBlock);
    191243
    192244    iBuilder->SetInsertPoint(finalBlock);
    193     Value * emptyBlockCond = iBuilder->CreateICmpEQ(remainingBytes, ConstantInt::get(int64ty, 0));
    194     iBuilder->CreateCondBr(emptyBlockCond, finalEmptyBlock, finalPartialBlock);
    195 
    196 
    197     iBuilder->SetInsertPoint(finalPartialBlock);
    198     s2pInstance->CreateDoBlockCall();
    199     iBuilder->CreateBr(exitBlock);
    200 
    201     iBuilder->SetInsertPoint(finalEmptyBlock);
    202     s2pInstance->clearOutputStreamSet();
    203     iBuilder->CreateBr(exitBlock);
    204 
    205     iBuilder->SetInsertPoint(exitBlock);
     245    basis_bits_ptr = BasisBits.getBlockPointer(blockNo);
     246
     247    make_FinalBlock_Call(iBuilder, "s2p", {s2pI, remainingBytes, ByteStream.getBlockPointer(blockNo), basis_bits_ptr});
     248   
     249    errs() << "G\n";
    206250
    207251    Value * remainingByte = iBuilder->CreateZExt(remainingBytes, iBuilder->getIntNTy(mBlockSize));
    208252    Value * remainingUnit = iBuilder->CreateLShr(remainingByte, ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1));
    209253    Value * EOFmark = iBuilder->CreateShl(ConstantInt::get(iBuilder->getIntNTy(mBlockSize), 1), UTF_16 ? remainingUnit : remainingByte);
    210     icGrepInstance->setInternalState("EOFmark", iBuilder->CreateBitCast(EOFmark, mBitBlockType));
    211 
    212     icGrepInstance->CreateDoBlockCall();
     254    //icGrepInstance->setInternalState("EOFmark", iBuilder->CreateBitCast(EOFmark, mBitBlockType));
     255    match_results_ptr = MatchResults.getBlockPointer(blockNo);
     256    make_DoBlock_Call(iBuilder, "icgrep", {grepI, basis_bits_ptr, match_results_ptr});
     257    errs() << "H\n";
    213258    if (CountOnly) {
    214         Value * popcount1 = Cal_Count(icGrepInstance, iBuilder);
     259        Value * matchptr = iBuilder->CreateGEP(mBitBlockType, match_results_ptr, {iBuilder->getInt64(0), iBuilder->getInt32(0)});
     260        Value * popcount1 = Cal_Count(matchptr, iBuilder);
    215261        Value * temp_count1 = iBuilder->CreateLoad(count);
    216262        Value * result = iBuilder->CreateAdd(temp_count1, popcount1);
     
    230276        iBuilder->CreateRet(iBuilder->CreateExtractElement(result, iBuilder->getInt32(0)));
    231277    } else {
    232         scanMatchInstance->CreateDoBlockCall();
     278        make_DoBlock_Call(iBuilder, "scanMatch", {scanMatchInstance->getKernelState(), match_results_ptr});
    233279        iBuilder->CreateRetVoid();
    234280    }
  • icGREP/icgrep-devel/icgrep/kernels/s2p_kernel.cpp

    r5046 r5051  
    148148}
    149149   
    150    
    151    
    152 }
     150std::unique_ptr<llvm::Module> s2pKernel::createKernelModule() {
     151    std::unique_ptr<llvm::Module> theModule = KernelInterface::createKernelModule();
     152   
     153    /***********************
     154     WARNING iBuilder has a different module than theModule at this point.
     155    ***********************/
     156    Function * doBlockFunction = theModule.get()->getFunction(mKernelName + "_DoBlock");
     157   
     158    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
     159   
     160    Value * byteStreamBlock_ptr = getParameter(doBlockFunction, "byteStream");
     161    Value * basisBitsBlock_ptr = getParameter(doBlockFunction, "basisBits");
     162    Value * s_bytepack[8];
     163    for (unsigned i = 0; i < 8; i++) {
     164        s_bytepack[i] = iBuilder->CreateBlockAlignedLoad(byteStreamBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(i)});
     165    }
     166    Value * p_bitblock[8];
     167    s2p(iBuilder, s_bytepack, p_bitblock);
     168    for (unsigned j = 0; j < 8; ++j) {
     169        iBuilder->CreateBlockAlignedStore(p_bitblock[j], basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(j)});
     170    }
     171    iBuilder->CreateRetVoid();
     172
     173    /* Now the prepare the s2p final block function:
     174     assumption: if remaining bytes is greater than 0, it is safe to read a full block of bytes.
     175     if remaining bytes is zero, no read should be performed (e.g. for mmapped buffer).
     176     */
     177    Function * finalBlockFunction = theModule.get()->getFunction(mKernelName + "_FinalBlock");
     178    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_entry", finalBlockFunction, 0));
     179
     180    Value * self = getParameter(finalBlockFunction, "self");
     181    Value * remainingBytes = getParameter(finalBlockFunction, "remainingBytes");
     182    byteStreamBlock_ptr = getParameter(finalBlockFunction, "byteStream");
     183    basisBitsBlock_ptr = getParameter(finalBlockFunction, "basisBits");
     184   
     185    BasicBlock * finalPartialBlock = BasicBlock::Create(iBuilder->getContext(), "partial", finalBlockFunction, 0);
     186    BasicBlock * finalEmptyBlock = BasicBlock::Create(iBuilder->getContext(), "empty", finalBlockFunction, 0);
     187    BasicBlock * exitBlock = BasicBlock::Create(iBuilder->getContext(), "exit", finalBlockFunction, 0);
     188   
     189    Value * emptyBlockCond = iBuilder->CreateICmpEQ(remainingBytes, ConstantInt::get(iBuilder->getInt64Ty(), 0));
     190    iBuilder->CreateCondBr(emptyBlockCond, finalEmptyBlock, finalPartialBlock);
     191    iBuilder->SetInsertPoint(finalPartialBlock);
     192    iBuilder->CreateCall(doBlockFunction, {self, byteStreamBlock_ptr, basisBitsBlock_ptr});
     193   
     194    iBuilder->CreateBr(exitBlock);
     195   
     196    iBuilder->SetInsertPoint(finalEmptyBlock);
     197    iBuilder->CreateStore(Constant::getNullValue(basisBitsBlock_ptr->getType()->getPointerElementType()), basisBitsBlock_ptr);
     198    iBuilder->CreateBr(exitBlock);
     199   
     200    iBuilder->SetInsertPoint(exitBlock);
     201    iBuilder->CreateRetVoid();
     202
     203    return theModule;
     204}
     205
     206   
     207}
  • icGREP/icgrep-devel/icgrep/kernels/s2p_kernel.h

    r5046 r5051  
    55#ifndef S2P_KERNEL_H
    66#define S2P_KERNEL_H
     7
     8#include "streamset.h"
     9#include "interface.h"
    710
    811namespace llvm { class Module; }
     
    1417class KernelBuilder;
    1518
    16     void generateS2PKernel(llvm::Module *, IDISA::IDISA_Builder * iBuilder, KernelBuilder * kBuilder);
    17     void generateS2P_16Kernel(llvm::Module *, IDISA::IDISA_Builder * iBuilder, KernelBuilder * kBuilder);
    18     void generateS2P_idealKernel(llvm::Module *, IDISA::IDISA_Builder * iBuilder, KernelBuilder * kBuilder);
     19void generateS2PKernel(llvm::Module *, IDISA::IDISA_Builder * iBuilder, KernelBuilder * kBuilder);
     20void generateS2P_16Kernel(llvm::Module *, IDISA::IDISA_Builder * iBuilder, KernelBuilder * kBuilder);
     21void generateS2P_idealKernel(llvm::Module *, IDISA::IDISA_Builder * iBuilder, KernelBuilder * kBuilder);
    1922
     23
     24
     25class s2pKernel : public KernelInterface {
     26public:
     27    s2pKernel(IDISA::IDISA_Builder * iBuilder) :
     28    KernelInterface(iBuilder, "s2p",
     29                    {StreamSetBinding{StreamSetType(1, 8), "byteStream"}},
     30                    {StreamSetBinding{StreamSetType(8, 1), "basisBits"}},
     31                    {}, {}, {}) {}
     32   
     33    std::unique_ptr<llvm::Module> createKernelModule() override;
     34
     35};
    2036}
    21 
    2237#endif
Note: See TracChangeset for help on using the changeset viewer.