Ignore:
Timestamp:
May 10, 2017, 4:26:11 PM (2 years ago)
Author:
nmedfort
Message:

Large refactoring step. Removed IR generation code from Kernel (formally KernelBuilder?) and moved it into the new KernelBuilder? class.

File:
1 edited

Legend:

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

    r5436 r5440  
    2828namespace {
    2929
    30 Value * generateBitswap(IDISA::IDISA_Builder * const iBuilder, Value * v) {
     30Value * generateBitswap(const std::unique_ptr<KernelBuilder> & iBuilder, Value * v) {
    3131    Value * bswapFunc = Intrinsic::getDeclaration(iBuilder->getModule(),
    3232            Intrinsic::bswap, v->getType());
     
    3434}
    3535
    36 Value * selectMin(IDISA::IDISA_Builder * const iBuilder, Value * a, Value * b) {
     36Value * selectMin(const std::unique_ptr<KernelBuilder> & iBuilder, Value * a, Value * b) {
    3737    return iBuilder->CreateSelect(iBuilder->CreateICmpULT(a, b), a, b);
    3838}
    3939
    40 Value * createStackVar(IDISA::IDISA_Builder * const iBuilder, Type * type, StringRef name, Value * initializer = nullptr) {
     40Value * createStackVar(const std::unique_ptr<KernelBuilder> & iBuilder, Type * type, StringRef name, Value * initializer = nullptr) {
    4141    Value * var = iBuilder->CreateAlloca(type, nullptr, name);
    4242    if (initializer) {
     
    4848}
    4949
    50 void incStackVar(IDISA::IDISA_Builder * const iBuilder, Value * svar, Value * increment = nullptr) {
     50void incStackVar(const std::unique_ptr<KernelBuilder> & iBuilder, Value * svar, Value * increment = nullptr) {
    5151    Value * value = iBuilder->CreateLoad(svar);
    5252    if (increment) {
     
    5858}
    5959
    60 Value * getOutputPtr(IDISA::IDISA_Builder * const iBuilder, Value * blockStartPtr, Value * offset) {
     60Value * getOutputPtr(const std::unique_ptr<KernelBuilder> & iBuilder, Value * blockStartPtr, Value * offset) {
    6161    return iBuilder->CreateGEP(
    6262            iBuilder->CreatePointerCast(blockStartPtr, iBuilder->getInt32Ty()->getPointerTo()),
     
    6767}       // anonymouse namespace
    6868
    69 
    70 /**
    71  * In order to allow mem2reg to promote the stack variables, alloca's have
    72  * to be in the entry block of a function. Thus, we need to disable indirect
    73  * branching on this kernel to have a standalone DoMethod function.
    74  */
    75 bool LZ4IndexDecoderKernel::useIndirectBr() const {
    76     return false;
    77 }
    78 
    79 
    8069/**
    8170 * Get the offset within the current word.
    8271 */
    83 Value * LZ4IndexDecoderKernel::getWordOffset() {
     72Value * LZ4IndexDecoderKernel::getWordOffset(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
    8473    Value * wordWidthMask = iBuilder->getInt32(wordWidth - 1);
    8574    return iBuilder->CreateAnd(
     
    9382 * Get the offset of the start of the current word.
    9483 */
    95 Value * LZ4IndexDecoderKernel::getWordStartOffset() {
     84Value * LZ4IndexDecoderKernel::getWordStartOffset(const std::unique_ptr<KernelBuilder> & iBuilder) {
    9685    Value * wordWidthMask = iBuilder->getInt32(wordWidth - 1);
    9786    return iBuilder->CreateAnd(
     
    10695 * If offset is not provided, load the current byte by default.
    10796 */
    108 Value * LZ4IndexDecoderKernel::loadRawByte(Value * offset = nullptr) {
     97Value * LZ4IndexDecoderKernel::loadRawByte(const std::unique_ptr<KernelBuilder> & iBuilder, Value * offset) {
    10998    Value * blockStartPtr = iBuilder->CreatePointerCast(
    110             getInputStreamBlockPtr("byteStream", iBuilder->getInt32(0)),
     99            iBuilder->getInputStreamBlockPtr("byteStream", iBuilder->getInt32(0)),
    111100            iBuilder->getInt8PtrTy()
    112101            );
     
    125114 * cleared  = ....111
    126115 */
    127 void LZ4IndexDecoderKernel::setExtenderUntilOffset() {
     116void LZ4IndexDecoderKernel::setExtenderUntilOffset(const std::unique_ptr<KernelBuilder> & iBuilder) {
    128117    // Little-endian, offset counts from LSB
    129118    // extender = extender ^ ~((1 << offset) -1)
    130119    Value * extender = iBuilder->CreateLoad(sExtender);
    131120    Value * wordOffset = iBuilder->CreateZExt(
    132             getWordOffset(),
     121            getWordOffset(iBuilder),
    133122            iBuilder->getSizeTy()
    134123            );
     
    146135 * Called when we potentially reach a new word.  Usually followed by setExtenderUntilOffset.
    147136 */
    148 void LZ4IndexDecoderKernel::loadCurrentExtender() {
     137void LZ4IndexDecoderKernel::loadCurrentExtender(const std::unique_ptr<KernelBuilder> & iBuilder) {
    149138    iBuilder->CreateStore(
    150139            iBuilder->CreateExtractElement(extenders,
     
    158147
    159148
    160 void LZ4IndexDecoderKernel::generateProduceOutput() {
    161     Value * producedItem = getProducedItemCount("literalIndexes");
     149void LZ4IndexDecoderKernel::generateProduceOutput(const std::unique_ptr<KernelBuilder> &iBuilder) {
     150    Value * producedItem = iBuilder->getProducedItemCount("literalIndexes");
    162151
    163152#ifndef NDEBUG
     
    165154    // LiteralStart is adjusted to be relative to the block start, so that
    166155    // the output can be compared against that of the reference implementation.
    167     //iBuilder->CallPrintInt("LiteralStart", getScalarField("LiteralStart"));
    168     iBuilder->CallPrintInt("LiteralStart", iBuilder->CreateSub(
    169                 getScalarField("LiteralStart"), getScalarField("LZ4BlockStart")));
    170     iBuilder->CallPrintInt("LiteralLength", getScalarField("LiteralLength"));
    171     iBuilder->CallPrintInt("MatchOffset", getScalarField("MatchOffset"));
    172     iBuilder->CallPrintInt("MatchLength", getScalarField("MatchLength"));
     156    Value * literalStart = iBuilder->CreateSub(iBuilder->getScalarField("LiteralStart"), iBuilder->getScalarField("LZ4BlockStart"));
     157    iBuilder->CallPrintInt("LiteralStart", literalStart);
     158    iBuilder->CallPrintInt("LiteralLength", iBuilder->getScalarField("LiteralLength"));
     159    iBuilder->CallPrintInt("MatchOffset", iBuilder->getScalarField("MatchOffset"));
     160    iBuilder->CallPrintInt("MatchLength", iBuilder->getScalarField("MatchLength"));
    173161#endif
    174162    printRTDebugMsg("--------------");
     
    179167            );  // producedItem % blockWidth (as blockWidth is always a power of 2)
    180168    Value * literalStartPtr = getOutputPtr(iBuilder,
    181             getOutputStreamBlockPtr("literalIndexes", iBuilder->getInt32(0)), outputOffset);
     169            iBuilder->getOutputStreamBlockPtr("literalIndexes", iBuilder->getInt32(0)), outputOffset);
    182170    Value * literalLengthPtr = getOutputPtr(iBuilder,
    183             getOutputStreamBlockPtr("literalIndexes", iBuilder->getInt32(1)), outputOffset);
     171            iBuilder->getOutputStreamBlockPtr("literalIndexes", iBuilder->getInt32(1)), outputOffset);
    184172    Value * matchOffsetPtr = getOutputPtr(iBuilder,
    185             getOutputStreamBlockPtr("matchIndexes", iBuilder->getInt32(0)), outputOffset);
     173            iBuilder->getOutputStreamBlockPtr("matchIndexes", iBuilder->getInt32(0)), outputOffset);
    186174    Value * matchLengthPtr = getOutputPtr(iBuilder,
    187             getOutputStreamBlockPtr("matchIndexes", iBuilder->getInt32(1)), outputOffset);
    188     iBuilder->CreateStore(getScalarField("LiteralStart"), literalStartPtr);
    189     iBuilder->CreateStore(getScalarField("LiteralLength"), literalLengthPtr);
    190     iBuilder->CreateStore(getScalarField("MatchOffset"), matchOffsetPtr);
    191     iBuilder->CreateStore(getScalarField("MatchLength"), matchLengthPtr);
    192     setProducedItemCount("literalIndexes", iBuilder->CreateAdd(producedItem, iBuilder->getSize(1)));
     175            iBuilder->getOutputStreamBlockPtr("matchIndexes", iBuilder->getInt32(1)), outputOffset);
     176    iBuilder->CreateStore(iBuilder->getScalarField("LiteralStart"), literalStartPtr);
     177    iBuilder->CreateStore(iBuilder->getScalarField("LiteralLength"), literalLengthPtr);
     178    iBuilder->CreateStore(iBuilder->getScalarField("MatchOffset"), matchOffsetPtr);
     179    iBuilder->CreateStore(iBuilder->getScalarField("MatchLength"), matchLengthPtr);
     180    iBuilder->setProducedItemCount("literalIndexes", iBuilder->CreateAdd(producedItem, iBuilder->getSize(1)));
    193181    // matchIndexes has a fixed ratio of 1:1 w.r.t. literalIndexes.
    194182}
    195183
    196184
    197 void LZ4IndexDecoderKernel::generateDoBlockMethod() {
     185void LZ4IndexDecoderKernel::generateDoBlockMethod(const std::unique_ptr<KernelBuilder> & iBuilder) {
    198186    BasicBlock * entry_block = iBuilder->GetInsertBlock();
    199     BasicBlock * exit_block = CreateBasicBlock("exit");
     187    BasicBlock * exit_block = iBuilder->CreateBasicBlock("exit");
    200188
    201189    // %entry
     
    203191    printRTDebugMsg("entry");
    204192    // Global positions in the byte stream.
    205     Value * blockNo = getScalarField("BlockNo");
     193    Value * blockNo = iBuilder->getScalarField("BlockNo");
    206194    blockStartPos = iBuilder->CreateMul(blockNo, iBuilder->getInt32(iBuilder->getBitBlockWidth()), "blockStartPos");
    207195    extenders = iBuilder->CreateBitCast(
    208             loadInputStreamBlock("extenders", iBuilder->getInt32(0)),
     196            iBuilder->loadInputStreamBlock("extenders", iBuilder->getInt32(0)),
    209197            VectorType::get(iBuilder->getSizeTy(), iBuilder->getBitBlockWidth() / wordWidth),
    210198            "extenders");
     
    212200    sOffset = createStackVar(iBuilder, iBuilder->getInt32Ty(), "offset");
    213201    // tempLength has different meanings in different states.
    214     sTempLength = createStackVar(iBuilder, iBuilder->getInt32Ty(), "tempLength", getScalarField("TempLength"));
    215     sTempCount = createStackVar(iBuilder, iBuilder->getInt32Ty(), "tempCount", getScalarField("TempCount"));
    216     sState = createStackVar(iBuilder, iBuilder->getInt8Ty(), "state", getScalarField("State"));
     202    sTempLength = createStackVar(iBuilder, iBuilder->getInt32Ty(), "tempLength", iBuilder->getScalarField("TempLength"));
     203    sTempCount = createStackVar(iBuilder, iBuilder->getInt32Ty(), "tempCount", iBuilder->getScalarField("TempCount"));
     204    sState = createStackVar(iBuilder, iBuilder->getInt8Ty(), "state", iBuilder->getScalarField("State"));
    217205    sExtender = createStackVar(iBuilder, iBuilder->getSizeTy(), "extender",
    218206            iBuilder->CreateExtractElement(extenders, iBuilder->getInt32(0)));
    219207
    220     BasicBlock * skippingBytes = CreateBasicBlock("skipping_bytes");
    221     BasicBlock * dispatch = CreateBasicBlock("dispatch");
     208    BasicBlock * skippingBytes = iBuilder->CreateBasicBlock("skipping_bytes");
     209    BasicBlock * dispatch = iBuilder->CreateBasicBlock("dispatch");
    222210
    223211    iBuilder->CreateCondBr(
    224             iBuilder->CreateICmpUGT(getScalarField("BytesToSkip"), iBuilder->getInt32(0)),
     212            iBuilder->CreateICmpUGT(iBuilder->getScalarField("BytesToSkip"), iBuilder->getInt32(0)),
    225213            skippingBytes, dispatch
    226214            );
    227215
    228216    // %skipping_bytes
    229     generateSkippingBytes(skippingBytes, exit_block);
     217    generateSkippingBytes(iBuilder, skippingBytes, exit_block);
    230218    // Insert point is at the end of skippingBytes.
    231219    iBuilder->CreateBr(dispatch);
     
    235223
    236224    // %at_block_checksum
    237     BasicBlock * atBlockChecksum = CreateBasicBlock("at_block_checksum");
    238     generateAtBlockChecksum(atBlockChecksum, skippingBytes);
     225    BasicBlock * atBlockChecksum = iBuilder->CreateBasicBlock("at_block_checksum");
     226    generateAtBlockChecksum(iBuilder, atBlockChecksum, skippingBytes);
    239227 
    240228    // %at_block_size
    241     BasicBlock * atBlockSize = CreateBasicBlock("at_block_size");
    242     generateAtBlockSize(atBlockSize, skippingBytes, exit_block);
     229    BasicBlock * atBlockSize = iBuilder->CreateBasicBlock("at_block_size");
     230    generateAtBlockSize(iBuilder, atBlockSize, skippingBytes, exit_block);
    243231
    244232    // %at_token
    245     BasicBlock * atToken = CreateBasicBlock("at_token");
    246     generateAtToken(atToken, exit_block);
     233    BasicBlock * atToken = iBuilder->CreateBasicBlock("at_token");
     234    generateAtToken(iBuilder, atToken, exit_block);
    247235
    248236    // %extending_literal_length
    249     BasicBlock * extendingLiteralLen = CreateBasicBlock("extending_literal_length");
    250     generateExtendingLiteralLen(extendingLiteralLen, exit_block);
     237    BasicBlock * extendingLiteralLen = iBuilder->CreateBasicBlock("extending_literal_length");
     238    generateExtendingLiteralLen(iBuilder, extendingLiteralLen, exit_block);
    251239
    252240    // %at_literals
    253     BasicBlock * atLiterals = CreateBasicBlock("at_literals");
    254     generateAtLiterals(atLiterals);
     241    BasicBlock * atLiterals = iBuilder->CreateBasicBlock("at_literals");
     242    generateAtLiterals(iBuilder, atLiterals);
    255243    iBuilder->CreateBr(skippingBytes);
    256244
     
    259247    // If the whole LZ4 block is done, process the (optional) checksum.
    260248    // Otherwise, go around to process the next sequence.
    261     BasicBlock * atOffset1 = CreateBasicBlock("at_first_offset");
     249    BasicBlock * atOffset1 = iBuilder->CreateBasicBlock("at_first_offset");
    262250    iBuilder->SetInsertPoint(atOffset1);
    263251    Value * nowGlobalPos = iBuilder->CreateAdd(blockStartPos, iBuilder->CreateLoad(sOffset));
    264     BasicBlock * blockEnd_else = CreateBasicBlock("block_end_else");
     252    BasicBlock * blockEnd_else = iBuilder->CreateBasicBlock("block_end_else");
    265253    // Conditional branch inserted at the end of the last block.
    266254    iBuilder->CreateUnlikelyCondBr(
    267             iBuilder->CreateICmpEQ(nowGlobalPos, getScalarField("LZ4BlockEnd")),
     255            iBuilder->CreateICmpEQ(nowGlobalPos, iBuilder->getScalarField("LZ4BlockEnd")),
    268256            atBlockChecksum, blockEnd_else
    269257            );
    270     generateAtFirstOffset(blockEnd_else, exit_block);
     258    generateAtFirstOffset(iBuilder, blockEnd_else, exit_block);
    271259
    272260    // %at_second_offset
    273     BasicBlock * atOffset2 = CreateBasicBlock("at_second_offset");
    274     generateAtSecondOffset(atOffset2, exit_block);
     261    BasicBlock * atOffset2 = iBuilder->CreateBasicBlock("at_second_offset");
     262    generateAtSecondOffset(iBuilder, atOffset2, exit_block);
    275263
    276264    // %extending_match_length
    277     BasicBlock * extendingMatchLen = CreateBasicBlock("extending_match_length");
    278     generateExtendingMatchLen(extendingMatchLen, exit_block);
     265    BasicBlock * extendingMatchLen = iBuilder->CreateBasicBlock("extending_match_length");
     266    generateExtendingMatchLen(iBuilder, extendingMatchLen, exit_block);
    279267    iBuilder->CreateBr(atToken);
    280268
     
    301289    iBuilder->SetInsertPoint(exit_block);
    302290    printRTDebugMsg("exit");
    303     setScalarField("State", iBuilder->CreateLoad(sState));
    304     setScalarField("TempLength", iBuilder->CreateLoad(sTempLength));
    305     setScalarField("TempCount", iBuilder->CreateLoad(sTempCount));
    306     setScalarField("BlockNo", iBuilder->CreateAdd(blockNo, iBuilder->getInt32(1)));
     291    iBuilder->setScalarField("State", iBuilder->CreateLoad(sState));
     292    iBuilder->setScalarField("TempLength", iBuilder->CreateLoad(sTempLength));
     293    iBuilder->setScalarField("TempCount", iBuilder->CreateLoad(sTempCount));
     294    iBuilder->setScalarField("BlockNo", iBuilder->CreateAdd(blockNo, iBuilder->getInt32(1)));
    307295    // When the kernel builder uses indirectbr, doBlock is not a separate function.
    308296    // Hence, we branch to a new basic block and fall through instead of returning.
    309     BasicBlock * end_block = CreateBasicBlock("end_of_block");
     297    BasicBlock * end_block = iBuilder->CreateBasicBlock("end_of_block");
    310298    iBuilder->CreateBr(end_block);
    311299    iBuilder->SetInsertPoint(end_block);
     
    313301
    314302
    315 void LZ4IndexDecoderKernel::generateBoundaryDetection(State state, BasicBlock * exit_block, bool updateExtenderWord=false) {
     303void LZ4IndexDecoderKernel::generateBoundaryDetection(const std::unique_ptr<KernelBuilder> & iBuilder, State state, BasicBlock * exit_block, bool updateExtenderWord) {
    316304    if (updateExtenderWord) {
    317         BasicBlock * wordBoundary_then = CreateBasicBlock("word_boundary_then-" + StateLabels.at(state));
    318         BasicBlock * blockBoundary_else = CreateBasicBlock("block_boundary_else-" + StateLabels.at(state));
    319         BasicBlock * wordBoundary_cont = CreateBasicBlock("word_boundary_cont-" + StateLabels.at(state));
     305        BasicBlock * wordBoundary_then = iBuilder->CreateBasicBlock("word_boundary_then-" + StateLabels.at(state));
     306        BasicBlock * blockBoundary_else = iBuilder->CreateBasicBlock("block_boundary_else-" + StateLabels.at(state));
     307        BasicBlock * wordBoundary_cont = iBuilder->CreateBasicBlock("word_boundary_cont-" + StateLabels.at(state));
    320308        iBuilder->CreateUnlikelyCondBr(
    321                 iBuilder->CreateICmpEQ(getWordOffset(), iBuilder->getInt32(0)),
     309                iBuilder->CreateICmpEQ(getWordOffset(iBuilder), iBuilder->getInt32(0)),
    322310                wordBoundary_then, wordBoundary_cont
    323311                );
     
    331319        // Reaching word boundary but not block boundary.  Update the extender word as requested.
    332320        iBuilder->SetInsertPoint(blockBoundary_else);
    333         loadCurrentExtender();
     321        loadCurrentExtender(iBuilder);
    334322        iBuilder->CreateBr(wordBoundary_cont);
    335323
     
    337325        iBuilder->SetInsertPoint(wordBoundary_cont);
    338326    } else {
    339         BasicBlock * blockBoundary_cont = CreateBasicBlock("block_boundary_cont-" + StateLabels.at(state));
     327        BasicBlock * blockBoundary_cont = iBuilder->CreateBasicBlock("block_boundary_cont-" + StateLabels.at(state));
    340328        iBuilder->CreateUnlikelyCondBr(
    341329                iBuilder->CreateICmpEQ(iBuilder->CreateLoad(sOffset), iBuilder->getInt32(iBuilder->getBitBlockWidth())),
     
    348336
    349337
    350 void LZ4IndexDecoderKernel::generateSkippingBytes(BasicBlock * bb, BasicBlock * exit_block) {
     338void LZ4IndexDecoderKernel::generateSkippingBytes(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, BasicBlock * bb, BasicBlock * exit_block) {
    351339    iBuilder->SetInsertPoint(bb);
    352340    printRTDebugMsg("skipping bytes");
     
    355343            iBuilder->getInt32(iBuilder->getBitBlockWidth()), iBuilder->CreateLoad(sOffset)
    356344            );
    357     Value * remainingBytesToSkip = getScalarField("BytesToSkip");
     345    Value * remainingBytesToSkip = iBuilder->getScalarField("BytesToSkip");
    358346    Value * advanceDist = selectMin(iBuilder, remainingBytesInBlock, remainingBytesToSkip);
    359347    remainingBytesToSkip = iBuilder->CreateSub(remainingBytesToSkip, advanceDist);
    360348    incStackVar(iBuilder, sOffset, advanceDist);
    361     setScalarField("BytesToSkip", remainingBytesToSkip);
    362 
    363     generateBoundaryDetection(State::SKIPPING_BYTES, exit_block);
     349    iBuilder->setScalarField("BytesToSkip", remainingBytesToSkip);
     350
     351    generateBoundaryDetection(iBuilder, State::SKIPPING_BYTES, exit_block);
    364352    // Falls through.
    365353}
    366354
    367355
    368 void LZ4IndexDecoderKernel::generateAtBlockSize(BasicBlock * bb, BasicBlock * skippingBytes, BasicBlock * exit_block) {
     356void LZ4IndexDecoderKernel::generateAtBlockSize(const std::unique_ptr<KernelBuilder> &iBuilder, BasicBlock * bb, BasicBlock * skippingBytes, BasicBlock * exit_block) {
    369357    iBuilder->CreateBr(bb);
    370358    iBuilder->SetInsertPoint(bb);
     
    378366
    379367    // A do-while loop.
    380     BasicBlock * loopBody = CreateBasicBlock("blocksize_loop_body");
    381     BasicBlock * loopExit = CreateBasicBlock("blocksize_loop_exit");
     368    BasicBlock * loopBody = iBuilder->CreateBasicBlock("blocksize_loop_body");
     369    BasicBlock * loopExit = iBuilder->CreateBasicBlock("blocksize_loop_exit");
    382370    iBuilder->CreateBr(loopBody);
    383371
    384372    iBuilder->SetInsertPoint(loopBody);
    385     Value * byte = loadRawByte();
     373    Value * byte = loadRawByte(iBuilder);
    386374    Value * newTempLength = iBuilder->CreateAdd(
    387375            iBuilder->CreateShl(iBuilder->CreateLoad(sTempLength), iBuilder->getInt32(8)),
     
    401389
    402390    iBuilder->SetInsertPoint(loopExit);
    403     BasicBlock * blockSizeCompleted_then = CreateBasicBlock("blocksize_completed_then");
    404     BasicBlock * blockSizeCompleted_cont = CreateBasicBlock("blocksize_completed_cont");
     391    BasicBlock * blockSizeCompleted_then = iBuilder->CreateBasicBlock("blocksize_completed_then");
     392    BasicBlock * blockSizeCompleted_cont = iBuilder->CreateBasicBlock("blocksize_completed_cont");
    405393    iBuilder->CreateLikelyCondBr(
    406394            iBuilder->CreateICmpEQ(iBuilder->CreateLoad(sTempCount), iBuilder->getInt32(4)),
     
    413401    Value * blockSize = generateBitswap(iBuilder, iBuilder->CreateLoad(sTempLength));
    414402    Value * currentPos = iBuilder->CreateAdd(blockStartPos, iBuilder->CreateLoad(sOffset));
    415     setScalarField("LZ4BlockStart", currentPos);
    416     setScalarField("LZ4BlockEnd", iBuilder->CreateAdd(currentPos, blockSize));
     403    iBuilder->setScalarField("LZ4BlockStart", currentPos);
     404    iBuilder->setScalarField("LZ4BlockEnd", iBuilder->CreateAdd(currentPos, blockSize));
    417405    printRTDebugInt("blockSize", blockSize);
    418406
    419     BasicBlock * uncompressedBlock_then = CreateBasicBlock("uncompressed_block_then");
    420     BasicBlock * uncompressedBlock_else = CreateBasicBlock("uncompressed_block_cont");
     407    BasicBlock * uncompressedBlock_then = iBuilder->CreateBasicBlock("uncompressed_block_then");
     408    BasicBlock * uncompressedBlock_else = iBuilder->CreateBasicBlock("uncompressed_block_cont");
    421409    iBuilder->CreateUnlikelyCondBr(
    422410            iBuilder->CreateTrunc(
     
    430418    iBuilder->SetInsertPoint(uncompressedBlock_then);
    431419    Value * realBlockSize = iBuilder->CreateXor(blockSize, iBuilder->getInt32(1L << 31));
    432     setScalarField("LZ4BlockEnd", iBuilder->CreateAdd(currentPos, realBlockSize));
    433     setScalarField("BytesToSkip", realBlockSize);
    434     setScalarField("LiteralStart", currentPos);
    435     setScalarField("LiteralLength", realBlockSize);
     420    iBuilder->setScalarField("LZ4BlockEnd", iBuilder->CreateAdd(currentPos, realBlockSize));
     421    iBuilder->setScalarField("BytesToSkip", realBlockSize);
     422    iBuilder->setScalarField("LiteralStart", currentPos);
     423    iBuilder->setScalarField("LiteralLength", realBlockSize);
    436424    // No need to set MatchLength/MatchOffset to 0, nor to produce output,
    437425    // because %atBlockChecksum will do so as the last sequence.
     
    453441    // We could be at the boundary no matter the block size is completed or not.
    454442    iBuilder->SetInsertPoint(blockSizeCompleted_cont);
    455     generateBoundaryDetection(State::AT_BLOCK_SIZE, exit_block);
     443    generateBoundaryDetection(iBuilder, State::AT_BLOCK_SIZE, exit_block);
    456444    // Falls through to %at_token.
    457445}
    458446
    459447
    460 void LZ4IndexDecoderKernel::generateAtToken(BasicBlock * bb, BasicBlock * exit_block) {
     448void LZ4IndexDecoderKernel::generateAtToken(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, BasicBlock * bb, BasicBlock * exit_block) {
    461449    iBuilder->CreateBr(bb);
    462450    iBuilder->SetInsertPoint(bb);
    463451    printRTDebugMsg("reading token");
    464452
    465     Value * token = loadRawByte();
     453    Value * token = loadRawByte(iBuilder);
    466454    Value * literalLen = iBuilder->CreateZExt(
    467455        iBuilder->CreateLShr(token, iBuilder->getInt8(4)),
     
    474462    incStackVar(iBuilder, sOffset);
    475463    // Prepare extender word for scanning.
    476     loadCurrentExtender();
    477     setExtenderUntilOffset();
     464    loadCurrentExtender(iBuilder);
     465    setExtenderUntilOffset(iBuilder);
    478466    // Store the (partial) match length to be extended later.
    479     setScalarField("MatchLength", matchLen);
     467    iBuilder->setScalarField("MatchLength", matchLen);
    480468    // Use tempLength to accumulate extended lengths (until at_literals).
    481469    iBuilder->CreateStore(literalLen, sTempLength);
    482470    iBuilder->CreateStore(iBuilder->getInt8(State::EXTENDING_LITERAL_LENGTH), sState);
    483471
    484     generateBoundaryDetection(State::AT_TOKEN, exit_block);
     472    generateBoundaryDetection(iBuilder, State::AT_TOKEN, exit_block);
    485473    // Falls through to %extending_literal_length.
    486474}
    487475
    488476
    489 void LZ4IndexDecoderKernel::generateExtendingLiteralLen(BasicBlock * bb, BasicBlock * exit_block) {
     477void LZ4IndexDecoderKernel::generateExtendingLiteralLen(const std::unique_ptr<KernelBuilder> & iBuilder, BasicBlock * bb, BasicBlock * exit_block) {
    490478    iBuilder->CreateBr(bb);
    491479    iBuilder->SetInsertPoint(bb);
    492480    printRTDebugMsg("extending literal len");
    493481
    494     Value * wordOffset = getWordOffset();
    495     Value * blockOffset = getWordStartOffset();
     482    Value * wordOffset = getWordOffset(iBuilder);
     483    Value * blockOffset = getWordStartOffset(iBuilder);
    496484    Value * literalLen = iBuilder->CreateLoad(sTempLength);
    497485    Value * literalExtEnd = iBuilder->CreateTrunc(
     
    512500    Value * lastByte = iBuilder->CreateSelect(literalExtReachBoundary,
    513501            iBuilder->getInt8(0),
    514             loadRawByte(iBuilder->CreateAdd(blockOffset, loadOffset)));
     502            loadRawByte(iBuilder, iBuilder->CreateAdd(blockOffset, loadOffset)));
    515503    Value * literalLenExted = iBuilder->CreateICmpUGE(literalLen, iBuilder->getInt32(0xf));
    516504    literalLen = iBuilder->CreateSelect(literalLenExted,
     
    540528    iBuilder->CreateStore(newState, sState);
    541529
    542     generateBoundaryDetection(State::EXTENDING_LITERAL_LENGTH, exit_block, true);
    543     BasicBlock * cont_block = CreateBasicBlock("finished_" + StateLabels.at(State::EXTENDING_LITERAL_LENGTH));
     530    generateBoundaryDetection(iBuilder, State::EXTENDING_LITERAL_LENGTH, exit_block, true);
     531    BasicBlock * cont_block = iBuilder->CreateBasicBlock("finished_" + StateLabels.at(State::EXTENDING_LITERAL_LENGTH));
    544532    // Insert point is still in wordBoundary block now.
    545533    // See if there are still more extenders.
     
    551539
    552540
    553 void LZ4IndexDecoderKernel::generateAtLiterals(BasicBlock * bb) {
     541void LZ4IndexDecoderKernel::generateAtLiterals(const std::unique_ptr<KernelBuilder> & iBuilder, BasicBlock * bb) {
    554542    iBuilder->CreateBr(bb);
    555543    iBuilder->SetInsertPoint(bb);
    556544
    557     setScalarField("LiteralStart", iBuilder->CreateAdd(blockStartPos, iBuilder->CreateLoad(sOffset)));
    558     setScalarField("LiteralLength", iBuilder->CreateLoad(sTempLength));
    559     setScalarField("BytesToSkip", iBuilder->CreateLoad(sTempLength));
     545    iBuilder->setScalarField("LiteralStart", iBuilder->CreateAdd(blockStartPos, iBuilder->CreateLoad(sOffset)));
     546    iBuilder->setScalarField("LiteralLength", iBuilder->CreateLoad(sTempLength));
     547    iBuilder->setScalarField("BytesToSkip", iBuilder->CreateLoad(sTempLength));
    560548    iBuilder->CreateStore(iBuilder->getInt8(State::AT_FIRST_OFFSET), sState);
    561549
     
    565553
    566554
    567 void LZ4IndexDecoderKernel::generateAtFirstOffset(BasicBlock * bb, BasicBlock * exit_block) {
     555void LZ4IndexDecoderKernel::generateAtFirstOffset(const std::unique_ptr<KernelBuilder> &iBuilder, BasicBlock * bb, BasicBlock * exit_block) {
    568556    iBuilder->SetInsertPoint(bb);
    569557    printRTDebugMsg("reading first offset");
    570558
    571     Value * byte = iBuilder->CreateZExt(loadRawByte(), iBuilder->getInt32Ty());
     559    Value * byte = iBuilder->CreateZExt(loadRawByte(iBuilder), iBuilder->getInt32Ty());
    572560    // Use tempLength to store partial offset.
    573561    iBuilder->CreateStore(byte, sTempLength);
     
    575563    iBuilder->CreateStore(iBuilder->getInt8(State::AT_SECOND_OFFSET), sState);
    576564
    577     generateBoundaryDetection(State::AT_FIRST_OFFSET, exit_block);
     565    generateBoundaryDetection(iBuilder, State::AT_FIRST_OFFSET, exit_block);
    578566    // Falls through to %at_second_offset.
    579567}
    580568
    581569
    582 void LZ4IndexDecoderKernel::generateAtSecondOffset(BasicBlock * bb, BasicBlock * exit_block) {
     570void LZ4IndexDecoderKernel::generateAtSecondOffset(const std::unique_ptr<KernelBuilder> & iBuilder, BasicBlock * bb, BasicBlock * exit_block) {
    583571    iBuilder->CreateBr(bb);
    584572    iBuilder->SetInsertPoint(bb);
     
    586574
    587575    Value * byte1 = iBuilder->CreateLoad(sTempLength);
    588     Value * byte2 = iBuilder->CreateZExt(loadRawByte(), iBuilder->getInt32Ty());
     576    Value * byte2 = iBuilder->CreateZExt(loadRawByte(iBuilder), iBuilder->getInt32Ty());
    589577    Value * offset = iBuilder->CreateAdd(
    590578            iBuilder->CreateShl(byte2, iBuilder->getInt32(8)),
    591579            byte1
    592580            );
    593     setScalarField("MatchOffset", offset);
     581    iBuilder->setScalarField("MatchOffset", offset);
    594582    incStackVar(iBuilder, sOffset);
    595583    // Prepare extender word and tempLength for extending.
    596     loadCurrentExtender();
    597     setExtenderUntilOffset();
    598     iBuilder->CreateStore(getScalarField("MatchLength"), sTempLength);
     584    loadCurrentExtender(iBuilder);
     585    setExtenderUntilOffset(iBuilder);
     586    iBuilder->CreateStore(iBuilder->getScalarField("MatchLength"), sTempLength);
    599587    iBuilder->CreateStore(iBuilder->getInt8(State::EXTENDING_MATCH_LENGTH), sState);
    600588
    601     generateBoundaryDetection(State::AT_SECOND_OFFSET, exit_block);
     589    generateBoundaryDetection(iBuilder, State::AT_SECOND_OFFSET, exit_block);
    602590    // Falls through to %extending_match_length.
    603591}
    604592
    605593
    606 void LZ4IndexDecoderKernel::generateExtendingMatchLen(BasicBlock * bb, BasicBlock * exit_block) {
     594void LZ4IndexDecoderKernel::generateExtendingMatchLen(const std::unique_ptr<KernelBuilder> & iBuilder, BasicBlock * bb, BasicBlock * exit_block) {
    607595    iBuilder->CreateBr(bb);
    608596    iBuilder->SetInsertPoint(bb);
    609597    printRTDebugMsg("extending match length");
    610598    printGlobalPos();
    611     printRTDebugInt("rawbyte", loadRawByte());
     599    printRTDebugInt("rawbyte", loadRawByte(iBuilder));
    612600    printRTDebugInt("extword", iBuilder->CreateLoad(sExtender));
    613601
    614     Value * wordOffset = getWordOffset();
    615     Value * blockOffset = getWordStartOffset();
     602    Value * wordOffset = getWordOffset(iBuilder);
     603    Value * blockOffset = getWordStartOffset(iBuilder);
    616604    Value * matchLen = iBuilder->CreateLoad(sTempLength);
    617605    Value * matchExtEnd = iBuilder->CreateTrunc(
     
    633621    Value * lastByte = iBuilder->CreateSelect(matchExtReachBoundary,
    634622            iBuilder->getInt8(0),
    635             loadRawByte(iBuilder->CreateAdd(blockOffset, loadOffset)));
     623            loadRawByte(iBuilder, iBuilder->CreateAdd(blockOffset, loadOffset)));
    636624    Value * matchLenExted = iBuilder->CreateICmpUGE(matchLen, iBuilder->getInt32(0xf));
    637625    matchLen = iBuilder->CreateSelect(matchLenExted,
     
    657645
    658646    Value * unfinished = iBuilder->CreateAnd(matchExtReachBoundary, matchLenExted);
    659     BasicBlock * output_then = CreateBasicBlock("output_then");
    660     BasicBlock * output_cont = CreateBasicBlock("output_cont");
     647    BasicBlock * output_then = iBuilder->CreateBasicBlock("output_then");
     648    BasicBlock * output_cont = iBuilder->CreateBasicBlock("output_cont");
    661649    iBuilder->CreateLikelyCondBr(
    662650            iBuilder->CreateNot(unfinished),
     
    666654    iBuilder->CreateStore(iBuilder->getInt8(State::AT_TOKEN), sState);
    667655    matchLen = iBuilder->CreateAdd(matchLen, iBuilder->getInt32(4));    // Add the constant at the end.
    668     setScalarField("MatchLength", matchLen);
    669     generateProduceOutput();
     656    iBuilder->setScalarField("MatchLength", matchLen);
     657    generateProduceOutput(iBuilder);
    670658    iBuilder->CreateBr(output_cont);
    671659
    672660    iBuilder->SetInsertPoint(output_cont);
    673     generateBoundaryDetection(State::EXTENDING_MATCH_LENGTH, exit_block, true);
    674     BasicBlock * cont_block = CreateBasicBlock("finished_" + StateLabels.at(State::EXTENDING_MATCH_LENGTH));
     661    generateBoundaryDetection(iBuilder, State::EXTENDING_MATCH_LENGTH, exit_block, true);
     662    BasicBlock * cont_block = iBuilder->CreateBasicBlock("finished_" + StateLabels.at(State::EXTENDING_MATCH_LENGTH));
    675663    // Insert point is still in wordBoundary block now.
    676664    // See if there are still more extenders.
     
    681669
    682670
    683 void LZ4IndexDecoderKernel::generateAtBlockChecksum(BasicBlock * bb, BasicBlock * skippingBytes) {
     671void LZ4IndexDecoderKernel::generateAtBlockChecksum(const std::unique_ptr<KernelBuilder> & iBuilder, BasicBlock * bb, BasicBlock * skippingBytes) {
    684672    // No branch here as we have made a conditional branch outside.
    685673    iBuilder->SetInsertPoint(bb);
     
    687675
    688676    // Produce the partial output (fill matchIndexes with 0).
    689     setScalarField("MatchOffset", iBuilder->getInt32(0));
    690     setScalarField("MatchLength", iBuilder->getInt32(0));
    691     generateProduceOutput();
    692 
    693     BasicBlock * hasChecksum_then = CreateBasicBlock("has_checksum_then");
    694     BasicBlock * hasChecksum_cont = CreateBasicBlock("has_checksum_cont");
     677    iBuilder->setScalarField("MatchOffset", iBuilder->getInt32(0));
     678    iBuilder->setScalarField("MatchLength", iBuilder->getInt32(0));
     679    generateProduceOutput(iBuilder);
     680
     681    BasicBlock * hasChecksum_then = iBuilder->CreateBasicBlock("has_checksum_then");
     682    BasicBlock * hasChecksum_cont = iBuilder->CreateBasicBlock("has_checksum_cont");
    695683
    696684    iBuilder->CreateStore(iBuilder->getInt8(State::AT_BLOCK_SIZE), sState);
    697     iBuilder->CreateCondBr(getScalarField("hasBlockChecksum"), hasChecksum_then, hasChecksum_cont);
     685    iBuilder->CreateCondBr(iBuilder->getScalarField("hasBlockChecksum"), hasChecksum_then, hasChecksum_cont);
    698686
    699687    iBuilder->SetInsertPoint(hasChecksum_then);
    700     setScalarField("BytesToSkip", iBuilder->getInt32(4));
     688    iBuilder->setScalarField("BytesToSkip", iBuilder->getInt32(4));
    701689    iBuilder->CreateBr(skippingBytes);
    702690    // Boundary detection will be done in skipping_bytes.
Note: See TracChangeset for help on using the changeset viewer.