Ignore:
Timestamp:
Feb 21, 2017, 2:22:21 PM (2 years ago)
Author:
nmedfort
Message:

Work on parenthesis matching and expandable buffers. Changed CBuilder CreateMemZero? to zero n bytes rather than n units to conform to the built-in CreateMemSet? and CreateMemCpy? methods.

Location:
icGREP/icgrep-devel/icgrep/kernels
Files:
4 edited

Legend:

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

    r5329 r5337  
    66#include "alignedprint.h"
    77#include <IR_Gen/idisa_builder.h>  // for IDISA_Builder
     8#include <llvm/IR/Module.h>
    89
    910using namespace llvm;
     
    9495}
    9596
    96 void PrintableStreamSet::generateDoBlockMethod() {
     97void PrintStreamSet::generateDoBlockMethod() {
    9798
    9899    /*
     
    118119    */
    119120
    120     BasicBlock * entry = iBuilder->GetInsertBlock();
    121     BasicBlock * cond = CreateBasicBlock("cond");
    122     BasicBlock * body = CreateBasicBlock("body");
    123     BasicBlock * exit = CreateBasicBlock("exit");
    124 
    125     Value * count = getInputStreamSetCount("bitStream");
    126     iBuilder->CreateBr(cond);
    127     iBuilder->SetInsertPoint(cond);
    128     PHINode * i = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2, "i");
    129     i->addIncoming(iBuilder->getSize(0), entry);
    130 
    131     iBuilder->CreateCondBr(iBuilder->CreateICmpNE(i, count), body, exit);
    132     iBuilder->SetInsertPoint(body);
    133     // Load current block
    134     Value * bitStrmVal = loadInputStreamBlock("bitStream", i);
    135 
    136     Value * bits[8];
    137 
    138     bits[0] = ConstantInt::getNullValue(iBuilder->getBitBlockType());
    139     bits[1] = ConstantInt::getNullValue(iBuilder->getBitBlockType());
    140     bits[2] = ConstantInt::getAllOnesValue(iBuilder->getBitBlockType());
    141     bits[3] = bitStrmVal;
    142     Value * negBitStrmVal = iBuilder->simd_not(bitStrmVal);
    143     bits[4] = negBitStrmVal;
    144     bits[5] = negBitStrmVal;
    145     bits[6] = negBitStrmVal;
    146     bits[7] = bitStrmVal;
    147 
    148     // Reassemble the paralell bit streams into a byte stream
    149     Value * printableBytes[8];
    150     p2s(iBuilder, bits, printableBytes);
    151     for (unsigned j = 0; j < 8; ++j) {
    152         storeOutputStreamPack("byteStream", i, iBuilder->getInt32(j), iBuilder->bitCast(printableBytes[j]));
    153     }
    154 
    155     i->addIncoming(iBuilder->CreateAdd(i, iBuilder->getSize(1)), iBuilder->GetInsertBlock());
    156     iBuilder->CreateBr(cond);
    157 
    158     iBuilder->SetInsertPoint(exit);
     121    for (const std::string & name : mNames) {
     122
     123        BasicBlock * entry = iBuilder->GetInsertBlock();
     124
     125        Value * count = getInputStreamSetCount(name);
     126        ConstantInt * const streamLength = iBuilder->getSize(iBuilder->getBitBlockWidth() + mNameWidth + 1);
     127        Value * output = iBuilder->CreateAlloca(iBuilder->getInt8Ty(), streamLength);
     128
     129        Value * outputName = iBuilder->CreateGlobalStringPtr(name.c_str());
     130        ConstantInt * const length = iBuilder->getInt32(name.length());
     131        iBuilder->CreateMemCpy(output, outputName, length, 1);
     132        iBuilder->CreateMemSet(iBuilder->CreateGEP(output, iBuilder->getInt32(name.length())), iBuilder->getInt8(' '), iBuilder->getInt32(mNameWidth - name.length()), 1);
     133        iBuilder->CreateStore(iBuilder->getInt8(10), iBuilder->CreateGEP(output, iBuilder->getInt32(iBuilder->getBitBlockWidth() + mNameWidth)));
     134
     135        if (isa<ConstantInt>(count) && cast<ConstantInt>(count)->isOne()) {
     136
     137            // Load current block
     138            Value * const input = loadInputStreamBlock(name, iBuilder->getInt32(0));
     139
     140            Value * bits[8];
     141            bits[0] = ConstantInt::getNullValue(iBuilder->getBitBlockType());
     142            bits[1] = ConstantInt::getNullValue(iBuilder->getBitBlockType());
     143            bits[2] = ConstantInt::getAllOnesValue(iBuilder->getBitBlockType());
     144            bits[3] = input;
     145            Value * const negated = iBuilder->simd_not(input);
     146            bits[4] = negated;
     147            bits[5] = negated;
     148            bits[6] = negated;
     149            bits[7] = input;
     150
     151            // Reassemble the paralell bit streams into a byte stream
     152            Value * printableBytes[8];
     153            p2s(iBuilder, bits, printableBytes);
     154            for (unsigned k = 0; k < 8; ++k) {
     155                const auto offset = mNameWidth + (k * (iBuilder->getBitBlockWidth() / 8));
     156                for (unsigned t = 0; t < (iBuilder->getBitBlockWidth() / 8); ++t) {
     157                    iBuilder->CreateStore(iBuilder->CreateExtractElement(printableBytes[k], iBuilder->getInt32(t)), iBuilder->CreateGEP(output, iBuilder->getInt32(offset + t)));
     158                }
     159            }
     160
     161            iBuilder->CreateWriteCall(iBuilder->getInt32(1), output, streamLength);
     162
     163        } else {
     164
     165            iBuilder->CreateStore(iBuilder->getInt8('['), iBuilder->CreateGEP(output, length));
     166
     167            BasicBlock * cond = CreateBasicBlock("cond");
     168
     169            BasicBlock * getIntLength = CreateBasicBlock("getIntLength");
     170
     171            BasicBlock * writeInt = CreateBasicBlock("writeInt");
     172            BasicBlock * writeVector = CreateBasicBlock("writeVector");
     173
     174            BasicBlock * exit = CreateBasicBlock("exit");
     175
     176            ConstantInt * TEN = iBuilder->getSize(10);
     177            ConstantInt * ONE = iBuilder->getSize(1);
     178
     179            iBuilder->CreateBr(cond);
     180            iBuilder->SetInsertPoint(cond);
     181            PHINode * i = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2, "i");
     182            i->addIncoming(iBuilder->getSize(0), entry);
     183
     184            iBuilder->CreateCondBr(iBuilder->CreateICmpNE(i, count), getIntLength, exit);
     185            // -------------------------------------------------------------------------
     186            iBuilder->SetInsertPoint(getIntLength);
     187
     188            PHINode * l = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2, "l");
     189            l->addIncoming(iBuilder->getSize(name.length() + 1), cond);
     190            PHINode * temp = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2, "temp");
     191            temp->addIncoming(i, cond);
     192
     193            l->addIncoming(iBuilder->CreateAdd(l, ONE), getIntLength);
     194
     195            temp->addIncoming(iBuilder->CreateUDiv(temp, TEN), getIntLength);
     196
     197            iBuilder->CreateCondBr(iBuilder->CreateICmpUGE(temp, TEN), getIntLength, writeInt);
     198            // -------------------------------------------------------------------------
     199            iBuilder->SetInsertPoint(writeInt);
     200            PHINode * value = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
     201            value->addIncoming(i, getIntLength);
     202
     203            PHINode * j = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2, "j");
     204            j->addIncoming(l, getIntLength);
     205            Value * ch = iBuilder->CreateURem(value, TEN);
     206            ch = iBuilder->CreateTrunc(ch, iBuilder->getInt8Ty());
     207            ch = iBuilder->CreateAdd(ch, iBuilder->getInt8('0'));
     208
     209            value->addIncoming(iBuilder->CreateUDiv(value, TEN), writeInt);
     210            iBuilder->CreateStore(ch, iBuilder->CreateGEP(output, j));
     211            j->addIncoming(iBuilder->CreateSub(j, ONE), writeInt);
     212
     213            iBuilder->CreateCondBr(iBuilder->CreateICmpUGE(value, TEN), writeInt, writeVector);
     214            // -------------------------------------------------------------------------
     215            iBuilder->SetInsertPoint(writeVector);
     216
     217            iBuilder->CreateStore(iBuilder->getInt8(']'), iBuilder->CreateGEP(output, iBuilder->CreateAdd(l, iBuilder->getSize(1))));
     218
     219            // Load current block
     220            Value * const input = loadInputStreamBlock(name, i);
     221
     222            Value * bits[8];
     223            bits[0] = ConstantInt::getNullValue(iBuilder->getBitBlockType());
     224            bits[1] = ConstantInt::getNullValue(iBuilder->getBitBlockType());
     225            bits[2] = ConstantInt::getAllOnesValue(iBuilder->getBitBlockType());
     226            bits[3] = input;
     227            Value * const negated = iBuilder->simd_not(input);
     228            bits[4] = negated;
     229            bits[5] = negated;
     230            bits[6] = negated;
     231            bits[7] = input;
     232
     233            // Reassemble the paralell bit streams into a byte stream
     234            Value * printableBytes[8];
     235            p2s(iBuilder, bits, printableBytes);
     236            for (unsigned k = 0; k < 8; ++k) {
     237                const auto offset = mNameWidth + (k * (iBuilder->getBitBlockWidth() / 8));
     238                for (unsigned t = 0; t < (iBuilder->getBitBlockWidth() / 8); ++t) {
     239                    iBuilder->CreateStore(iBuilder->CreateExtractElement(printableBytes[k], iBuilder->getInt32(t)), iBuilder->CreateGEP(output, iBuilder->getInt32(offset + t)));
     240                }
     241            }
     242
     243            iBuilder->CreateWriteCall(iBuilder->getInt32(1), output, streamLength);
     244
     245            i->addIncoming(iBuilder->CreateAdd(i, ONE), iBuilder->GetInsertBlock());
     246            iBuilder->CreateBr(cond);
     247            // -------------------------------------------------------------------------
     248            iBuilder->SetInsertPoint(exit);
     249
     250        }
     251    }
    159252}
    160253
    161254PrintableBits::PrintableBits(IDISA::IDISA_Builder * builder)
    162 : BlockOrientedKernel(builder, "PrintableBits", {Binding{builder->getStreamSetTy(1), "bitStream"}}, {Binding{builder->getStreamSetTy(1, 8), "byteStream"}}, {}, {}, {})
    163 {
     255: BlockOrientedKernel(builder, "PrintableBits", {Binding{builder->getStreamSetTy(1), "bitStream"}}, {Binding{builder->getStreamSetTy(1, 8), "byteStream"}}, {}, {}, {}) {
    164256    setNoTerminateAttribute(true);
    165257    setDoBlockUpdatesProducedItemCountsAttribute(false);
     
    167259
    168260SelectStream::SelectStream(IDISA::IDISA_Builder * builder, unsigned sizeInputStreamSet, unsigned streamIndex)
    169 : BlockOrientedKernel(builder, "SelectStream", {Binding{builder->getStreamSetTy(sizeInputStreamSet), "bitStreams"}}, {Binding{builder->getStreamSetTy(1, 1), "bitStream"}}, {}, {}, {}), mSizeInputStreamSet(sizeInputStreamSet), mStreamIndex(streamIndex)
    170 {
     261: BlockOrientedKernel(builder, "SelectStream", {Binding{builder->getStreamSetTy(sizeInputStreamSet), "bitStreams"}}, {Binding{builder->getStreamSetTy(1, 1), "bitStream"}}, {}, {}, {}), mSizeInputStreamSet(sizeInputStreamSet), mStreamIndex(streamIndex) {
    171262    setNoTerminateAttribute(true);
    172263    setDoBlockUpdatesProducedItemCountsAttribute(false);
     
    174265}
    175266
    176 PrintableStreamSet::PrintableStreamSet(IDISA::IDISA_Builder * builder)
    177 : BlockOrientedKernel(builder, "PrintableStreamSet", {Binding{builder->getStreamSetTy(0), "bitStream"}}, {Binding{builder->getStreamSetTy(0, 8), "byteStream"}}, {}, {}, {}) {
    178     setNoTerminateAttribute(true);
    179     setDoBlockUpdatesProducedItemCountsAttribute(false);
    180 }
    181 
    182 
    183 
    184 }
     267PrintStreamSet::PrintStreamSet(IDISA::IDISA_Builder * builder, std::vector<std::string> && names, const unsigned minWidth)
     268: BlockOrientedKernel(builder, "PrintableStreamSet", {}, {}, {}, {}, {})
     269, mNames(names)
     270, mNameWidth() {
     271    auto width = minWidth;
     272    for (const std::string & name : mNames) {
     273        mStreamSetInputs.emplace_back(builder->getStreamSetTy(0), name);
     274        width = std::max<unsigned>(name.length() + 5, width);
     275    }
     276    mNameWidth = width;
     277}
     278
     279}
  • icGREP/icgrep-devel/icgrep/kernels/alignedprint.h

    r5329 r5337  
    3030};
    3131
    32 class PrintableStreamSet : public BlockOrientedKernel {
     32class PrintStreamSet : public BlockOrientedKernel {
    3333public:
    34     PrintableStreamSet(IDISA::IDISA_Builder * builder);
    35     virtual ~PrintableStreamSet() {}
     34    PrintStreamSet(IDISA::IDISA_Builder * builder, std::vector<std::string> && names, const unsigned minWidth = 16);
     35    virtual ~PrintStreamSet() {}
    3636private:
    3737    void generateDoBlockMethod() override;
     38private:
     39    const std::vector<std::string> mNames;
     40    unsigned mNameWidth;
    3841};
    3942
  • icGREP/icgrep-devel/icgrep/kernels/kernel.cpp

    r5330 r5337  
    196196void KernelBuilder::callGenerateInitMethod() {
    197197    mCurrentFunction = getInitFunction();
    198     iBuilder->SetInsertPoint(CreateBasicBlock("Init_entry"));
     198    iBuilder->SetInsertPoint(CreateBasicBlock("entry"));
    199199    Function::arg_iterator args = mCurrentFunction->arg_begin();
    200200    mSelf = &*(args++);
     
    303303    report_fatal_error("Error: no binding in " + getName() + " for " + name);
    304304}
    305 
    306 
    307305
    308306Value * KernelBuilder::getInputStreamBlockPtr(const std::string & name, Value * streamIndex) const {
  • icGREP/icgrep-devel/icgrep/kernels/streamset.cpp

    r5329 r5337  
    4242        count = mBaseType->getArrayNumElements();
    4343    }
    44     return iBuilder->getInt32(count);
     44    return iBuilder->getSize(count);
    4545}
    4646
     
    193193    Value * const ptr = iBuilder->CreateAlignedMalloc(bufferType, size, iBuilder->getCacheAlignment());
    194194    const auto alignment = bufferType->getPrimitiveSizeInBits() / 8;
    195     iBuilder->CreateMemZero(ptr, size, alignment);
     195    Constant * bufferWidth = ConstantExpr::getIntegerCast(ConstantExpr::getSizeOf(bufferType), size->getType(), false);
     196    iBuilder->CreateMemZero(ptr, iBuilder->CreateMul(size, bufferWidth), alignment);
    196197    Value * const streamSetPtr = iBuilder->CreateGEP(mStreamSetBufferPtr, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
    197198    iBuilder->CreateStore(ptr, streamSetPtr);
     
    201202
    202203    // MDNode *Weights = MDBuilder(Ctx).createBranchWeights(42, 13);
     204
     205    /// TODO: Check whether a dominating test with the same streamIndex exists or whether streamIndex is guaranteed to be < capacity
    203206
    204207    // ENTRY
     
    225228    iBuilder->SetInsertPoint(expand);
    226229    /// TODO: this should call a function rather than be inlined into the block. REVISIT once tested.
     230    Constant * vectorWidth = ConstantExpr::getIntegerCast(ConstantExpr::getSizeOf(streamSet->getType()->getPointerElementType()), capacity->getType(), false);
    227231    Value * newCapacity = iBuilder->CreateMul(streamIndex, iBuilder->getSize(2));
    228232    iBuilder->CreateStore(newCapacity, capacityPtr);
     
    231235    Value * newStreamSet = iBuilder->CreateAlignedMalloc(bufferType, size, iBuilder->getCacheAlignment());
    232236    iBuilder->CreateStore(newStreamSet, streamSetPtr);
    233     Value * const diffCapacity = iBuilder->CreateSub(newCapacity, capacity);
     237    Value * const diffCapacity = iBuilder->CreateMul(iBuilder->CreateSub(newCapacity, capacity), vectorWidth);
    234238    const auto alignment = bufferType->getPrimitiveSizeInBits() / 8;
    235239    for (unsigned i = 0; i < mBufferBlocks; ++i) {
     
    239243        Value * destOffset = iBuilder->CreateMul(newCapacity, offset);
    240244        Value * destPtr = iBuilder->CreateGEP(newStreamSet, destOffset);
    241         iBuilder->CreateMemCpy(destPtr, srcPtr, capacity, alignment);
     245        iBuilder->CreateMemCpy(destPtr, srcPtr, iBuilder->CreateMul(capacity, vectorWidth), alignment);
    242246        Value * destZeroOffset = iBuilder->CreateAdd(destOffset, capacity);
    243247        Value * destZeroPtr = iBuilder->CreateGEP(newStreamSet, destZeroOffset);
Note: See TracChangeset for help on using the changeset viewer.