Ignore:
Timestamp:
Aug 26, 2016, 9:33:51 AM (3 years ago)
Author:
cameron
Message:

Defer binding of buffers to stream sets until kernel generation

File:
1 edited

Legend:

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

    r5127 r5133  
    3232
    3333void KernelBuilder::prepareKernel() {
     34    if (mStreamSetInputs.size() != mStreamSetInputBuffers.size()) {
     35        throw std::runtime_error("Kernel preparation: Incorrect number of input buffers");
     36    }
     37    if (mStreamSetOutputs.size() != mStreamSetOutputBuffers.size()) {
     38        throw std::runtime_error("Kernel preparation: Incorrect number of input buffers");
     39    }
    3440    addScalar(iBuilder->getSizeTy(), blockNoScalar);
    3541    int streamSetNo = 0;
    36     for (auto sSet : mStreamSetInputs) {
    37         mScalarInputs.push_back(ScalarBinding{sSet.ssType.getStreamBufferPointerType(), sSet.ssName + basePtrSuffix});
    38         mStreamSetNameMap.emplace(sSet.ssName, streamSetNo);
     42    for (unsigned i = 0; i < mStreamSetInputs.size(); i++) {
     43        if (!(mStreamSetInputBuffers[i]->getBufferStreamSetType() == mStreamSetInputs[i].ssType)) {
     44             throw std::runtime_error("Kernel preparation: Incorrect input buffer type");
     45        }
     46        mScalarInputs.push_back(ScalarBinding{mStreamSetInputBuffers[i]->getStreamBufferPointerType(), mStreamSetInputs[i].ssName + basePtrSuffix});
     47        mStreamSetNameMap.emplace(mStreamSetInputs[i].ssName, streamSetNo);
    3948        streamSetNo++;
    4049    }
    41     for (auto sSet : mStreamSetOutputs) {
    42         mScalarInputs.push_back(ScalarBinding{sSet.ssType.getStreamBufferPointerType(), sSet.ssName + basePtrSuffix});
    43         mStreamSetNameMap.emplace(sSet.ssName, streamSetNo);
     50    for (unsigned i = 0; i < mStreamSetOutputs.size(); i++) {
     51        if (!(mStreamSetOutputBuffers[i]->getBufferStreamSetType() == mStreamSetOutputs[i].ssType)) {
     52             throw std::runtime_error("Kernel preparation: Incorrect input buffer type");
     53        }
     54        mScalarInputs.push_back(ScalarBinding{mStreamSetOutputBuffers[i]->getStreamBufferPointerType(), mStreamSetOutputs[i].ssName + basePtrSuffix});
     55        mStreamSetNameMap.emplace(mStreamSetOutputs[i].ssName, streamSetNo);
    4456        streamSetNo++;
    4557    }
     
    5668}
    5769
    58 std::unique_ptr<Module> KernelBuilder::createKernelModule() {
     70std::unique_ptr<Module> KernelBuilder::createKernelModule(std::vector<StreamSetBuffer *> input_buffers, std::vector<StreamSetBuffer *> output_buffers) {
    5971    Module * saveModule = iBuilder->getModule();
    6072    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
     
    6274    Module * m = theModule.get();
    6375    iBuilder->setModule(m);
    64     generateKernel();
     76    generateKernel(input_buffers, output_buffers);
    6577    iBuilder->setModule(saveModule);
    6678    iBuilder->restoreIP(savePoint);
     
    6880}
    6981
    70 void KernelBuilder::generateKernel() {
    71     IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
    72     Module * m = iBuilder->getModule();
    73 
     82void KernelBuilder::generateKernel(std::vector<StreamSetBuffer *> input_buffers, std::vector<StreamSetBuffer*> output_buffers) {
     83    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
     84    Module * m = iBuilder->getModule();
     85    mStreamSetInputBuffers = input_buffers;
     86    mStreamSetOutputBuffers = output_buffers;
    7487    prepareKernel();  // possibly overriden by the KernelBuilder subtype
    7588    KernelInterface::addKernelDeclarations(m);
     
    200213    unsigned ssIndex = getStreamSetIndex(ssName);
    201214    if (ssIndex < mStreamSetInputs.size()) {
    202         return mStreamSetInputs[ssIndex].ssType.getBufferSize();
     215        return mStreamSetInputBuffers[ssIndex]->getBufferSize();
    203216    }
    204217    else {
    205         return mStreamSetOutputs[ssIndex - mStreamSetInputs.size()].ssType.getBufferSize();
     218        return mStreamSetOutputBuffers[ssIndex - mStreamSetInputs.size()]->getBufferSize();
    206219    }
    207220}
     
    215228    unsigned ssIndex = getStreamSetIndex(ssName);
    216229    if (ssIndex < mStreamSetInputs.size()) {
    217         return mStreamSetInputs[ssIndex].ssType.getStreamSetBlockPointer(basePtr, blockNo);
     230        return mStreamSetInputBuffers[ssIndex]->getStreamSetBlockPointer(basePtr, blockNo);
    218231    }
    219232    else {
    220         return mStreamSetOutputs[ssIndex - mStreamSetInputs.size()].ssType.getStreamSetBlockPointer(basePtr, blockNo);
    221     }
    222 }
    223 
    224 
    225 
    226 
     233        return mStreamSetOutputBuffers[ssIndex - mStreamSetInputs.size()]->getStreamSetBlockPointer(basePtr, blockNo);
     234    }
     235}
     236
     237Value * KernelBuilder::createInstance(std::vector<Value *> args) {
     238    Value * kernelInstance = iBuilder->CreateAlloca(mKernelStateType);
     239    Module * m = iBuilder->getModule();
     240    std::vector<Value *> init_args = {kernelInstance};
     241    for (auto a : args) {
     242        init_args.push_back(a);
     243    }
     244    for (auto b : mStreamSetInputBuffers) {
     245        init_args.push_back(b->getStreamSetBasePtr());
     246    }
     247    for (auto b : mStreamSetOutputBuffers) {
     248        init_args.push_back(b->getStreamSetBasePtr());
     249    }
     250    std::string initFnName = mKernelName + init_suffix;
     251    Function * initMethod = m->getFunction(initFnName);
     252    if (!initMethod) {
     253        throw std::runtime_error("Cannot find " + initFnName);
     254    }
     255    iBuilder->CreateCall(initMethod, init_args);
     256    return kernelInstance;
     257}
     258
     259
     260
     261
Note: See TracChangeset for help on using the changeset viewer.