Ignore:
Timestamp:
Feb 7, 2017, 3:23:42 PM (2 years ago)
Author:
nmedfort
Message:

Continued work on eliminating BlockNo?

File:
1 edited

Legend:

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

    r5303 r5307  
    3737unsigned KernelBuilder::addScalar(Type * const type, const std::string & name) {
    3838    if (LLVM_UNLIKELY(mKernelStateType != nullptr)) {
    39         llvm::report_fatal_error("Cannot add kernel field " + name + " after kernel state finalized");
     39        report_fatal_error("Cannot add kernel field " + name + " after kernel state finalized");
    4040    }
    4141    if (LLVM_UNLIKELY(mKernelMap.count(name))) {
    42         llvm::report_fatal_error("Kernel already contains field " + name);
     42        report_fatal_error("Kernel already contains field " + name);
    4343    }
    4444    const auto index = mKernelFields.size();
     
    5050unsigned KernelBuilder::addUnnamedScalar(Type * const type) {
    5151    if (LLVM_UNLIKELY(mKernelStateType != nullptr)) {
    52         llvm::report_fatal_error("Cannot add unnamed kernel field after kernel state finalized");
     52        report_fatal_error("Cannot add unnamed kernel field after kernel state finalized");
    5353    }
    5454    const auto index = mKernelFields.size();
     
    6868void KernelBuilder::prepareKernel() {
    6969    if (LLVM_UNLIKELY(mKernelStateType != nullptr)) {
    70         llvm::report_fatal_error("Cannot prepare kernel after kernel state finalized");
     70        report_fatal_error("Cannot prepare kernel after kernel state finalized");
    7171    }
    7272    if (mStreamSetInputs.size() != mStreamSetInputBuffers.size()) {
     
    7575        out << "kernel contains " << mStreamSetInputBuffers.size() << " input buffers for "
    7676        << mStreamSetInputs.size() << " input stream sets.";
    77         throw std::runtime_error(out.str());
     77        report_fatal_error(out.str());
    7878    }
    7979    if (mStreamSetOutputs.size() != mStreamSetOutputBuffers.size()) {
     
    8282        out << "kernel contains " << mStreamSetOutputBuffers.size() << " output buffers for "
    8383        << mStreamSetOutputs.size() << " output stream sets.";
    84         throw std::runtime_error(out.str());
    85     }
    86     unsigned blockSize = iBuilder->getBitBlockWidth();
     84        report_fatal_error(out.str());
     85    }
     86    const auto blockSize = iBuilder->getBitBlockWidth();
    8787    for (unsigned i = 0; i < mStreamSetInputs.size(); i++) {
    8888        if ((mStreamSetInputBuffers[i]->getBufferBlocks() > 0) && (mStreamSetInputBuffers[i]->getBufferBlocks() < codegen::SegmentSize + (blockSize + mLookAheadPositions - 1)/blockSize)) {
    89             llvm::report_fatal_error("Kernel preparation: Buffer size too small " + mStreamSetInputs[i].name);
     89            report_fatal_error("Kernel preparation: Buffer size too small " + mStreamSetInputs[i].name);
    9090        }
    91         mScalarInputs.push_back(Binding{mStreamSetInputBuffers[i]->getPointerType(), mStreamSetInputs[i].name + BUFFER_PTR_SUFFIX});
     91        mScalarInputs.emplace_back(mStreamSetInputBuffers[i]->getPointerType(), mStreamSetInputs[i].name + BUFFER_PTR_SUFFIX);
    9292        addScalar(iBuilder->getSizeTy(), mStreamSetInputs[i].name + PROCESSED_ITEM_COUNT_SUFFIX);
    9393    }
    9494    for (unsigned i = 0; i < mStreamSetOutputs.size(); i++) {
    95         mScalarInputs.push_back(Binding{mStreamSetOutputBuffers[i]->getPointerType(), mStreamSetOutputs[i].name + BUFFER_PTR_SUFFIX});
     95        mScalarInputs.emplace_back(mStreamSetOutputBuffers[i]->getPointerType(), mStreamSetOutputs[i].name + BUFFER_PTR_SUFFIX);
    9696        addScalar(iBuilder->getSizeTy(), mStreamSetOutputs[i].name + PRODUCED_ITEM_COUNT_SUFFIX);
    9797    }
    98     for (auto binding : mScalarInputs) {
     98    for (const auto binding : mScalarInputs) {
    9999        addScalar(binding.type, binding.name);
    100100    }
    101     for (auto binding : mScalarOutputs) {
     101    for (const auto binding : mScalarOutputs) {
    102102        addScalar(binding.type, binding.name);
    103103    }
    104     if (mStreamSetNameMap.empty()) prepareKernelSignature();
     104    if (mStreamSetNameMap.empty()) {
     105        prepareKernelSignature();
     106    }
    105107    for (auto binding : mInternalScalars) {
    106108        addScalar(binding.type, binding.name);
     
    178180    const auto f = mKernelMap.find(name);
    179181    if (LLVM_UNLIKELY(f == mKernelMap.end())) {
    180         llvm::report_fatal_error("Kernel does not contain scalar: " + name);
     182        report_fatal_error("Kernel does not contain scalar: " + name);
    181183    }
    182184    return iBuilder->getInt32(f->second);
     
    187189}
    188190
    189 Value * KernelBuilder::getScalarFieldPtr(llvm::Value * instance, Value * index) const {
     191Value * KernelBuilder::getScalarFieldPtr(Value * instance, Value * index) const {
    190192    return iBuilder->CreateGEP(instance, {iBuilder->getInt32(0), index});
    191193}
    192194
    193 Value * KernelBuilder::getScalarFieldPtr(llvm::Value * instance, const std::string & fieldName) const {
     195Value * KernelBuilder::getScalarFieldPtr(Value * instance, const std::string & fieldName) const {
    194196    return getScalarFieldPtr(instance, getScalarIndex(fieldName));
    195197}
     
    235237}
    236238
    237 LoadInst * KernelBuilder::acquireLogicalSegmentNo(llvm::Value * instance) const {
     239LoadInst * KernelBuilder::acquireLogicalSegmentNo(Value * instance) const {
    238240    return iBuilder->CreateAtomicLoadAcquire(getScalarFieldPtr(instance, LOGICAL_SEGMENT_NO_SCALAR));
    239241}
     
    251253}
    252254
    253 llvm::Value * KernelBuilder::getInputStream(const std::string & name, llvm::Value * index) const {
    254     Value * ic = getProcessedItemCount(name);
    255     const StreamSetBuffer * buf = getStreamSetBuffer(name);
    256     ic = iBuilder->CreateUDiv(ic, iBuilder->getSize(iBuilder->getBitBlockWidth()));
    257     return buf->getStream(getStreamSetBufferPtr(name), ic, index);
    258 }
    259 
    260 llvm::Value * KernelBuilder::getInputStream(const std::string & name, llvm::Value * index1, llvm::Value * index2) const {
    261     Value * ic = getProcessedItemCount(name);
    262     const StreamSetBuffer * buf = getStreamSetBuffer(name);
    263     ic = iBuilder->CreateUDiv(ic, iBuilder->getSize(iBuilder->getBitBlockWidth()));
    264     return buf->getStream(getStreamSetBufferPtr(name), ic, index1, index2);
    265 }
    266 
    267 llvm::Value * KernelBuilder::getOutputStream(const std::string & name, llvm::Value * index) const {
    268     Value * ic = getProducedItemCount(name);
    269     const StreamSetBuffer * buf = getStreamSetBuffer(name);
    270     ic = iBuilder->CreateUDiv(ic, iBuilder->getSize(iBuilder->getBitBlockWidth()));
    271     return buf->getStream(getStreamSetBufferPtr(name), ic, index);
    272 }
    273 
    274 llvm::Value * KernelBuilder::getOutputStream(const std::string & name, llvm::Value * index1, llvm::Value * index2) const {
    275     Value * ic = getProducedItemCount(name);
    276     const StreamSetBuffer * buf = getStreamSetBuffer(name);
    277     ic = iBuilder->CreateUDiv(ic, iBuilder->getSize(iBuilder->getBitBlockWidth()));
    278     return buf->getStream(getStreamSetBufferPtr(name), ic, index1, index2);
    279 }
    280 
    281 Value * KernelBuilder::getStreamView(llvm::Type * type, const std::string & name, Value * blockNo, Value * index) const {
    282     return getStreamSetBuffer(name)->getStreamView(type, getStreamSetBufferPtr(name), blockNo, index);
     255inline static uint64_t log2(const uint64_t x) {
     256    return (64 - __builtin_clzll(x)) - 1;
     257}
     258
     259inline static uint32_t log2(const uint32_t x) {
     260    return (32 - __builtin_clz(x)) - 1;
     261}
     262
     263inline Value * KernelBuilder::computeBlockIndex(const std::vector<Binding> & bindings, const std::string & name, Value * itemCount) const {
     264    for (const Binding & b : bindings) {
     265        if (b.name == name) {
     266            const auto divisor = (b.step == 0) ? iBuilder->getBitBlockWidth() : b.step;
     267            if (LLVM_LIKELY((divisor & (divisor - 1)) == 0)) {
     268                return iBuilder->CreateLShr(itemCount, log2(divisor));
     269            } else {
     270                return iBuilder->CreateUDiv(itemCount, iBuilder->getSize(divisor));
     271            }
     272        }
     273    }
     274    report_fatal_error("Error: no binding in " + getName() + " for " + name);
     275}
     276
     277Value * KernelBuilder::getInputStream(const std::string & name, Value * streamIndex) const {
     278    Value * const blockIndex = computeBlockIndex(mStreamSetInputs, name, getProcessedItemCount(name));
     279    const StreamSetBuffer * const buf = getStreamSetBuffer(name);
     280    return buf->getStream(getStreamSetBufferPtr(name), streamIndex, blockIndex);
     281}
     282
     283Value * KernelBuilder::getInputStream(const std::string & name, Value * streamIndex, Value * packIndex) const {
     284    Value * const blockIndex = computeBlockIndex(mStreamSetInputs, name, getProcessedItemCount(name));
     285    const StreamSetBuffer * const buf = getStreamSetBuffer(name);
     286    return buf->getStream(getStreamSetBufferPtr(name), streamIndex, blockIndex, packIndex);
     287}
     288
     289Value * KernelBuilder::getOutputStream(const std::string & name, Value * streamIndex) const {
     290    Value * const blockIndex = computeBlockIndex(mStreamSetOutputs, name, getProducedItemCount(name));
     291    const StreamSetBuffer * const buf = getStreamSetBuffer(name);
     292    return buf->getStream(getStreamSetBufferPtr(name), streamIndex, blockIndex);
     293}
     294
     295Value * KernelBuilder::getOutputStream(const std::string & name, Value * streamIndex, Value * packIndex) const {
     296    Value * const blockIndex = computeBlockIndex(mStreamSetOutputs, name, getProducedItemCount(name));
     297    const StreamSetBuffer * const buf = getStreamSetBuffer(name);
     298    return buf->getStream(getStreamSetBufferPtr(name), streamIndex, blockIndex, packIndex);
     299}
     300
     301Value * KernelBuilder::getRawItemPointer(const std::string & name, Value * streamIndex, Value * absolutePosition) const {
     302    return getStreamSetBuffer(name)->getRawItemPointer(getStreamSetBufferPtr(name), streamIndex, absolutePosition);
    283303}
    284304
     
    286306    const auto f = mStreamSetNameMap.find(name);
    287307    if (LLVM_UNLIKELY(f == mStreamSetNameMap.end())) {
    288         llvm::report_fatal_error("Kernel " + getName() + " does not contain stream set: " + name);
     308        report_fatal_error("Kernel " + getName() + " does not contain stream set: " + name);
    289309    }
    290310    return f->second;
     
    304324}
    305325
    306 inline Value * KernelBuilder::getStreamSetBufferPtr(llvm::Value * index) const {
    307     return getScalarField(getSelf(), index);
    308 }
    309 
    310326Argument * KernelBuilder::getParameter(Function * const f, const std::string & name) const {
    311327    for (auto & arg : f->getArgumentList()) {
     
    314330        }
    315331    }
    316     llvm::report_fatal_error(f->getName() + " does not have parameter " + name);
    317 }
    318 
    319 Value * KernelBuilder::createDoSegmentCall(const std::vector<llvm::Value *> & args) const {
     332    report_fatal_error(f->getName() + " does not have parameter " + name);
     333}
     334
     335Value * KernelBuilder::createDoSegmentCall(const std::vector<Value *> & args) const {
    320336    return iBuilder->CreateCall(getDoSegmentFunction(), args);
    321337}
     
    335351void KernelBuilder::createInstance() {
    336352    if (LLVM_UNLIKELY(mKernelStateType == nullptr)) {
    337         llvm::report_fatal_error("Cannot create kernel instance before calling prepareKernel()");
     353        report_fatal_error("Cannot create kernel instance before calling prepareKernel()");
    338354    }
    339355    mKernelInstance = iBuilder->CreateCacheAlignedAlloca(mKernelStateType);
     
    360376    CreateDoBlockMethodCall();
    361377}
     378
     379//Value * BlockOrientedKernel::loadBlock(const std::string & inputName, Value * const streamIndex) const {
     380
     381//}
     382
     383//Value * BlockOrientedKernel::loadPack(const std::string & inputName, Value * const streamIndex, Value * const packIndex) const {
     384
     385//}
     386
    362387
    363388//  The default doSegment method dispatches to the doBlock routine for
     
    476501    Function * const f = iBuilder->getModule()->getFunction(name);
    477502    if (LLVM_UNLIKELY(f == nullptr)) {
    478         llvm::report_fatal_error("Cannot find " + name);
     503        report_fatal_error("Cannot find " + name);
    479504    }
    480505    return f;
     
    489514    Function * const f = iBuilder->getModule()->getFunction(name);
    490515    if (LLVM_UNLIKELY(f == nullptr)) {
    491         llvm::report_fatal_error("Cannot find " + name);
     516        report_fatal_error("Cannot find " + name);
    492517    }
    493518    return f;
Note: See TracChangeset for help on using the changeset viewer.