Ignore:
Timestamp:
Mar 25, 2016, 5:42:03 PM (4 years ago)
Author:
nmedfort
Message:

Symbol table work and untested kernel instatiate method for multiple input streams

File:
1 edited

Legend:

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

    r4988 r4991  
    88#include <IDISA/idisa_builder.h>
    99#include <kernels/instance.h>
     10#include <tuple>
     11#include <boost/functional/hash_fwd.hpp>
     12#include <unordered_map>
    1013
    1114using namespace llvm;
     
    5154unsigned KernelBuilder::addInternalState(llvm::Type * const type, std::string && name) {
    5255    if (LLVM_UNLIKELY(mInternalStateNameMap.count(name) != 0)) {
    53         throw std::runtime_error("Kernel already contains internal state " + name);
     56        throw std::runtime_error("Kernel already contains internal state '" + name + "'");
    5457    }
    5558    const unsigned index = addInternalState(type);
     
    121124Value * KernelBuilder::getInputStream(Value * const instance, const unsigned index, const unsigned streamOffset) {
    122125    assert (instance);
     126    assert (index < mInputStream.size());
    123127    Value * inputStream = iBuilder->CreateLoad(iBuilder->CreateGEP(instance,
    124128        {iBuilder->getInt32(0), iBuilder->getInt32(INPUT_STREAM_SET), iBuilder->getInt32(0)}));
     
    129133        offset = iBuilder->CreateAdd(offset, ConstantInt::get(offset->getType(), streamOffset));
    130134    }   
    131     offset = iBuilder->CreateCall(modFunction, offset, "offset");
    132     return iBuilder->CreateGEP(inputStream, { offset, iBuilder->getInt32(index) });
     135    return iBuilder->CreateGEP(inputStream, { iBuilder->CreateCall(modFunction, offset), iBuilder->getInt32(index) });
    133136}
    134137
     
    178181Value * KernelBuilder::getOutputStream(Value * const instance, const unsigned index, const unsigned streamOffset) {
    179182    assert (instance);
    180     Value * offset = getOffset(instance, streamOffset);
     183    Value * const offset = getOffset(instance, streamOffset);
    181184    Value * const indices[] = {iBuilder->getInt32(0), iBuilder->getInt32(OUTPUT_STREAM_SET), offset, iBuilder->getInt32(index)};
    182185    return iBuilder->CreateGEP(instance, indices);
     
    208211    FunctionType * const functionType = FunctionType::get(iBuilder->getVoidTy(), {PointerType::get(mKernelStateType, 0)}, false);
    209212    mDoBlock = Function::Create(functionType, GlobalValue::ExternalLinkage, mKernelName + "_DoBlock", mMod);
    210     mDoBlock->setCallingConv(CallingConv::C);
    211   //  mDoBlock->addAttribute(1, Attribute::NoCapture);
    212  //   mDoBlock->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
    213  //   mDoBlock->addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
     213    mDoBlock->setCallingConv(CallingConv::C);   
     214    mDoBlock->setDoesNotCapture(1);
     215    mDoBlock->setDoesNotThrow();
    214216
    215217    Function::arg_iterator args = mDoBlock->arg_begin();
    216     mKernelParam = args++;
    217     mKernelParam->setName("this");
     218    mKernelState = args++;
     219    mKernelState->setName("this");
    218220
    219221    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", mDoBlock, 0));
    220 
    221 //    mLocalBlockNo = iBuilder->CreateLoad(getBlockNo());
    222 //    Value * blockNo = iBuilder->CreateLoad(getBlockNo());
    223 //    iBuilder->CallPrintInt(mKernelName + "_BlockNo", blockNo);
    224 //    Value * modFunction = iBuilder->CreateLoad(iBuilder->CreateGEP(mKernelParam, {iBuilder->getInt32(0), iBuilder->getInt32(INPUT_STREAM_SET), iBuilder->getInt32(1)}));
    225 //    blockNo = iBuilder->CreateCall(modFunction, blockNo);
    226 //    iBuilder->CallPrintInt(mKernelName + "_Offset", blockNo);
    227 
    228222
    229223    return mDoBlock;
     
    242236    iBuilder->CreateRetVoid();
    243237
     238    eliminateRedundantMemoryOperations(mDoBlock);
     239
    244240    // Generate the zero initializer
    245241    PointerType * modFunctionType = PointerType::get(FunctionType::get(iBuilder->getInt64Ty(), {iBuilder->getInt64Ty()}, false), 0);
     
    248244    mConstructor = Function::Create(constructorType, GlobalValue::ExternalLinkage, mKernelName + "_Constructor", mMod);
    249245    mConstructor->setCallingConv(CallingConv::C);
    250     mConstructor->addAttribute(1, Attribute::NoCapture);
    251     //mConstructor->addAttribute(AttributeSet::FunctionIndex, Attribute::InlineHint);
    252    // mConstructor->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
    253     //mConstructor->addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
     246    mDoBlock->setDoesNotCapture(1);
     247    mConstructor->addAttribute(AttributeSet::FunctionIndex, Attribute::InlineHint);
     248    mDoBlock->setDoesNotThrow();
     249
    254250    auto args = mConstructor->arg_begin();
    255     mKernelParam = args++;
    256     mKernelParam->setName("this");
     251    mKernelState = args++;
     252    mKernelState->setName("this");
    257253    Value * const inputStream = args++;
    258254    inputStream->setName("inputStream");
     
    272268    }
    273269
    274     Value * const input = iBuilder->CreateGEP(mKernelParam, {iBuilder->getInt32(0), iBuilder->getInt32(INPUT_STREAM_SET)});
     270    Value * const input = iBuilder->CreateGEP(mKernelState, {iBuilder->getInt32(0), iBuilder->getInt32(INPUT_STREAM_SET)});
    275271    iBuilder->CreateStore(inputStream, iBuilder->CreateGEP(input, {iBuilder->getInt32(0), iBuilder->getInt32(0)}));
    276272    iBuilder->CreateStore(modFunction, iBuilder->CreateGEP(input, {iBuilder->getInt32(0), iBuilder->getInt32(1)}));
     
    287283//        mStreamSetFunction->addAttribute(2, Attribute::NoCapture);
    288284//        mStreamSetFunction->addAttribute(AttributeSet::FunctionIndex, Attribute::InlineHint);
    289 //        mStreamSetFunction->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
    290285//        mStreamSetFunction->addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
    291286//        Value * offset = arg;
     
    308303
    309304/** ------------------------------------------------------------------------------------------------------------- *
     305 * @brief eliminateRedundantMemoryOperations
     306 ** ------------------------------------------------------------------------------------------------------------- */
     307inline void KernelBuilder::eliminateRedundantMemoryOperations(Function * const function) {
     308
     309
     310}
     311
     312/** ------------------------------------------------------------------------------------------------------------- *
    310313 * @brief instantiate
    311314 *
     
    327330Instance * KernelBuilder::instantiate(llvm::Value * const inputStream) {
    328331    AllocaInst * const memory = iBuilder->CreateAlloca(mKernelStateType);
    329     Value * ptr = inputStream;
    330     iBuilder->CreateCall3(mConstructor, memory, iBuilder->CreatePointerCast(ptr, mInputStreamType), CreateModFunction(0));
     332    iBuilder->CreateCall3(mConstructor, memory, iBuilder->CreatePointerCast(inputStream, mInputStreamType), CreateModFunction(0));
     333    return new Instance(this, memory);
     334}
     335
     336/** ------------------------------------------------------------------------------------------------------------- *
     337 * @brief instantiate
     338 *
     339 * Generate a new instance of this kernel and call the default constructor to initialize it
     340 ** ------------------------------------------------------------------------------------------------------------- */
     341Instance * KernelBuilder::instantiate(std::initializer_list<llvm::Value *> inputStreams) {
     342    if (mInputStreamType->getStructNumElements() != inputStreams.size()) {
     343        throw std::runtime_error(mKernelName + ".instantiate expected " + std::to_string(inputStreams.size()) +
     344                                 "elements but was given " + std::to_string(mInputStreamType->getStructNumElements()));
     345    }
     346    AllocaInst * const memory = iBuilder->CreateAlloca(mKernelStateType);
     347    AllocaInst * inputStruct = iBuilder->CreateAlloca(mInputStreamType, 0);
     348    unsigned i = 0;
     349    for (Value * inputStream : inputStreams) {
     350        Value * ptr = iBuilder->CreateGEP(inputStruct, { iBuilder->getInt32(0), iBuilder->getInt32(i++)});
     351        iBuilder->CreateStore(inputStream, ptr);
     352    }
     353    iBuilder->CreateCall3(mConstructor, memory, iBuilder->CreatePointerCast(inputStruct, mInputStreamType), CreateModFunction(0));
    331354    return new Instance(this, memory);
    332355}
     
    341364
    342365/** ------------------------------------------------------------------------------------------------------------- *
    343  * @brief clearOutputStream
    344  ** ------------------------------------------------------------------------------------------------------------- */
    345 void KernelBuilder::clearOutputStream(Value * const instance, const unsigned streamOffset) {
     366 * @brief clearOutputStreamSet
     367 *
     368 * Zero out the i + streamOffset stream set memory, where i is the current stream set indicated by the BlockNo.
     369 ** ------------------------------------------------------------------------------------------------------------- */
     370void KernelBuilder::clearOutputStreamSet(Value * const instance, const unsigned streamOffset) {
    346371    Value * const indices[] = {iBuilder->getInt32(0), iBuilder->getInt32(OUTPUT_STREAM_SET), getOffset(instance, streamOffset)};
    347     Value * ptr = iBuilder->CreateGEP(instance, indices, "ptr");
     372    Value * ptr = iBuilder->CreateGEP(instance, indices);
    348373    unsigned size = 0;
    349374    for (unsigned i = 0; i < mOutputStreamType->getStructNumElements(); ++i) {
     
    408433}
    409434
    410 /** ------------------------------------------------------------------------------------------------------------- *
    411  * @brief setLongestLookaheadAmount
    412  ** ------------------------------------------------------------------------------------------------------------- */
    413 void KernelBuilder::setLongestLookaheadAmount(const unsigned bits) {
    414     const unsigned blockWidth = iBuilder->getBitBlockWidth();
    415     const unsigned lookaheadBlocks = (bits + blockWidth - 1) / blockWidth;
    416     mBufferSize = (lookaheadBlocks + 1);
    417 }
    418 
    419435} // end of namespace kernel
Note: See TracChangeset for help on using the changeset viewer.