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

s2p kernel with new infrastructure, includes s2p_FinalBlock

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.