source: icGREP/icgrep-devel/icgrep/kernels/scanmatchgen.cpp @ 5695

Last change on this file since 5695 was 5695, checked in by cameron, 20 months ago

Complete the scanmatch kernel conversion to use MatchAccumulator? objects

File size: 12.9 KB
Line 
1/*
2 *  Copyright (c) 2015 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5
6#include "scanmatchgen.h"
7#include <llvm/IR/Intrinsics.h>
8#include <llvm/IR/Module.h>
9#include <kernels/kernel_builder.h>
10#include <IR_Gen/FunctionTypeBuilder.h>
11#include <llvm/Support/raw_ostream.h>
12#include <grep_engine.h>
13
14using namespace llvm;
15
16inline static unsigned floor_log2(const unsigned v) {
17    assert ("log2(0) is undefined!" && v != 0);
18    return 31 - __builtin_clz(v);
19}
20
21namespace kernel {
22
23void ScanMatchKernel::generateMultiBlockLogic(const std::unique_ptr<KernelBuilder> &iBuilder) {
24
25    Module * const m = iBuilder->getModule();
26   
27    BasicBlock * const entryBlock = iBuilder->GetInsertBlock();
28    BasicBlock * const initialBlock = iBuilder->CreateBasicBlock("initialBlock");
29    BasicBlock * const scanWordIteration = iBuilder->CreateBasicBlock("ScanWordIteration");
30    BasicBlock * const matches_test_block = iBuilder->CreateBasicBlock("matches_test_block");
31    BasicBlock * const processMatchesEntry = iBuilder->CreateBasicBlock("process_matches_loop");
32    BasicBlock * const prior_breaks_block = iBuilder->CreateBasicBlock("prior_breaks_block");
33    BasicBlock * const loop_final_block = iBuilder->CreateBasicBlock("loop_final_block");
34    BasicBlock * const processMatchesExit = iBuilder->CreateBasicBlock("matches_done_block");
35    BasicBlock * const remaining_breaks_block = iBuilder->CreateBasicBlock("remaining_breaks_block");
36    BasicBlock * const return_block = iBuilder->CreateBasicBlock("return_block");
37    BasicBlock * const scanWordExit = iBuilder->CreateBasicBlock("ScanWordExit");
38    BasicBlock * const blocksExit = iBuilder->CreateBasicBlock("blocksExit");
39    BasicBlock * const callFinalizeScan = iBuilder->CreateBasicBlock("callFinalizeScan");
40    BasicBlock * const scanReturn = iBuilder->CreateBasicBlock("scanReturn");
41    IntegerType * const sizeTy = iBuilder->getSizeTy();
42    const unsigned fieldCount = iBuilder->getBitBlockWidth() / sizeTy->getBitWidth();
43    VectorType * const scanwordVectorType =  VectorType::get(sizeTy, fieldCount);
44    Constant * blockSize = iBuilder->getSize(iBuilder->getBitBlockWidth());
45
46    Function::arg_iterator args = mCurrentMethod->arg_begin();
47    /* self = */ args++;
48    Value * itemsToDo = &*(args++);
49    /* inputStreamAvail = */ args++;
50    Value * match_result = &*(args++);
51    Value * line_break = &*(args++);
52    /* input_stream = */ args++;
53
54    Value * blocksToDo = iBuilder->CreateUDivCeil(itemsToDo, blockSize);
55   
56    Value * match_result_ptr = iBuilder->CreateBitCast(match_result, scanwordVectorType->getPointerTo());
57    Value * line_break_ptr = iBuilder->CreateBitCast(line_break, scanwordVectorType->getPointerTo());
58    Value * accumulator = iBuilder->getScalarField("accumulator_address");
59
60    iBuilder->CreateCondBr(iBuilder->CreateICmpUGT(blocksToDo, iBuilder->getSize(0)), initialBlock, blocksExit);
61
62    iBuilder->SetInsertPoint(initialBlock);
63    PHINode * const blockBase = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2);
64    blockBase->addIncoming(iBuilder->getSize(0), entryBlock);
65    Value * const blockNo = iBuilder->getScalarField("BlockNo");
66    Value * const scanwordPos = iBuilder->CreateShl(blockNo, floor_log2(iBuilder->getBitBlockWidth()));
67    Value * const lastRecordStart = iBuilder->getProcessedItemCount("InputStream");
68    Value * const lastRecordNum = iBuilder->getScalarField("LineNum");
69    iBuilder->CreateBr(scanWordIteration);
70
71    iBuilder->SetInsertPoint(scanWordIteration);
72
73        // while (phiIndex < words per stride)
74        PHINode * const phiIndex = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2, "index");
75        phiIndex->addIncoming(iBuilder->getInt32(0), initialBlock);
76        PHINode * const phiScanwordPos = iBuilder->CreatePHI(scanwordPos->getType(), 2, "pos");
77        phiScanwordPos->addIncoming(scanwordPos, initialBlock);
78        PHINode * const phiLineStart = iBuilder->CreatePHI(lastRecordStart->getType(), 2, "recordstart");
79        phiLineStart->addIncoming(lastRecordStart, initialBlock);
80        PHINode * const phiLineNum = iBuilder->CreatePHI(lastRecordNum->getType(), 2, "recordnum");
81        phiLineNum->addIncoming(lastRecordNum, initialBlock);
82        Value * const matches = iBuilder->CreateLoad(iBuilder->CreateGEP(match_result_ptr, blockBase));
83        Value * const linebreaks = iBuilder->CreateLoad(iBuilder->CreateGEP(line_break_ptr, blockBase));
84        Value * const matchWord = iBuilder->CreateExtractElement(matches, phiIndex);
85        Value * const recordBreaks = iBuilder->CreateExtractElement(linebreaks, phiIndex);
86
87        // The match scanner works with a loop involving four variables:
88        // (a) the bit stream scanword of matches marking the ends of selected records,
89        // (b) the bit stream scanword of record_breaks marking the ends of all records,
90        // (c) the integer lastRecordNum indicating the number of records processed so far,
91        // (d) the index lastRecordStart indicating the file position of the last record.
92        // We set up a loop structure, in which a set of 4 phi nodes initialize these
93        // variables from either the input to the scanner or the computed values within
94        // the loop body.
95
96        iBuilder->CreateBr(matches_test_block);
97
98        // LOOP Test Block
99        iBuilder->SetInsertPoint(matches_test_block);
100        PHINode * phiMatchWord = iBuilder->CreatePHI(sizeTy, 2, "matches");
101        PHINode * phiRecordBreaks = iBuilder->CreatePHI(sizeTy, 2, "recordbreaks");
102        PHINode * phiRecordStart = iBuilder->CreatePHI(sizeTy, 2, "recordstart");
103        PHINode * phiRecordNum = iBuilder->CreatePHI(sizeTy, 2, "recordnum");
104        phiMatchWord->addIncoming(matchWord, scanWordIteration);
105        phiRecordBreaks->addIncoming(recordBreaks, scanWordIteration);
106        phiRecordNum->addIncoming(phiLineNum, scanWordIteration);
107        phiRecordStart->addIncoming(phiLineStart, scanWordIteration);
108        Value * anyMatches = iBuilder->CreateICmpNE(phiMatchWord, ConstantInt::getNullValue(sizeTy));
109        iBuilder->CreateCondBr(anyMatches, processMatchesEntry, processMatchesExit);
110
111            // LOOP BODY
112            // The loop body is entered if we have more matches to process.
113            iBuilder->SetInsertPoint(processMatchesEntry);
114            Value * prior_breaks = iBuilder->CreateAnd(iBuilder->CreateMaskToLowestBitExclusive(phiMatchWord), phiRecordBreaks);
115            // Within the loop we have a conditional block that is executed if there are any prior record breaks.
116            Value * prior_breaks_cond = iBuilder->CreateICmpNE(prior_breaks, ConstantInt::getNullValue(sizeTy));
117            iBuilder->CreateCondBr(prior_breaks_cond, prior_breaks_block, loop_final_block);
118
119                // PRIOR_BREAKS_BLOCK
120                // If there are prior breaks, we count them and compute the record start position.
121                iBuilder->SetInsertPoint(prior_breaks_block);
122                Value * matchedRecordNum = iBuilder->CreateAdd(iBuilder->CreatePopcount(prior_breaks), phiRecordNum);
123                Value * reverseDistance = iBuilder->CreateCountReverseZeroes(prior_breaks);
124                Value * width = ConstantInt::get(sizeTy, sizeTy->getBitWidth());
125                Value * priorRecordStart = iBuilder->CreateAdd(phiScanwordPos, iBuilder->CreateSub(width, reverseDistance));
126                iBuilder->CreateBr(loop_final_block);
127
128            // LOOP FINAL BLOCK
129            // The prior breaks, if any have been counted.  Set up phi nodes for the recordNum
130            // and recortStart depending on whether the conditional execution of prior_breaks_block.
131            iBuilder->SetInsertPoint(loop_final_block);
132            PHINode * matchRecordNum = iBuilder->CreatePHI(sizeTy, 2, "matchRecordNum");
133            matchRecordNum->addIncoming(phiRecordNum, processMatchesEntry);
134            matchRecordNum->addIncoming(matchedRecordNum, prior_breaks_block);
135            phiRecordNum->addIncoming(matchRecordNum, loop_final_block);
136
137            PHINode * matchRecordStart = iBuilder->CreatePHI(sizeTy, 2, "matchRecordStart");
138            matchRecordStart->addIncoming(phiRecordStart, processMatchesEntry);
139            matchRecordStart->addIncoming(priorRecordStart, prior_breaks_block);
140            phiRecordStart->addIncoming(matchRecordStart, loop_final_block);
141            Value * matchRecordEnd = iBuilder->CreateAdd(phiScanwordPos, iBuilder->CreateCountForwardZeroes(phiMatchWord));
142            Value * const inputStream = iBuilder->getRawInputPointer("InputStream", iBuilder->getInt32(0), iBuilder->getInt32(0));
143            Function * dispatcher = m->getFunction("accumulate_match_wrapper"); assert (dispatcher);
144            Value * start_ptr = iBuilder->CreateGEP(inputStream, matchRecordStart);
145            Value * end_ptr = iBuilder->CreateGEP(inputStream, matchRecordEnd);
146            iBuilder->CreateCall(dispatcher, {accumulator, matchRecordNum, start_ptr, end_ptr});
147            Value * remaining_matches = iBuilder->CreateResetLowestBit(phiMatchWord);
148            phiMatchWord->addIncoming(remaining_matches, loop_final_block);
149
150            Value * remaining_breaks = iBuilder->CreateXor(phiRecordBreaks, prior_breaks);
151            phiRecordBreaks->addIncoming(remaining_breaks, loop_final_block);
152
153            iBuilder->CreateBr(matches_test_block);
154
155        // LOOP EXIT/MATCHES_DONE
156        iBuilder->SetInsertPoint(processMatchesExit);
157        // When the matches are done, there may be additional record breaks remaining
158        Value * more_breaks_cond = iBuilder->CreateICmpNE(phiRecordBreaks, ConstantInt::getNullValue(sizeTy));
159        iBuilder->CreateCondBr(more_breaks_cond, remaining_breaks_block, return_block);
160
161            // REMAINING_BREAKS_BLOCK: process remaining record breaks after all matches are processed
162            iBuilder->SetInsertPoint(remaining_breaks_block);
163            Value * break_count = iBuilder->CreatePopcount(phiRecordBreaks);
164            Value * final_record_num = iBuilder->CreateAdd(phiRecordNum, break_count);
165            Value * reverseZeroes = iBuilder->CreateCountReverseZeroes(phiRecordBreaks);
166            Value * pendingLineStart = iBuilder->CreateAdd(phiScanwordPos, iBuilder->CreateSub(width, reverseZeroes));
167            iBuilder->CreateBr(return_block);
168
169        // RETURN block
170        iBuilder->SetInsertPoint(return_block);
171        PHINode * phiFinalRecordNum = iBuilder->CreatePHI(sizeTy, 2, "finalRecordCount");
172        PHINode * phiFinalRecordStart = iBuilder->CreatePHI(sizeTy, 2, "finalRecordStart");
173
174        phiFinalRecordNum->addIncoming(phiRecordNum, processMatchesExit);
175        phiFinalRecordNum->addIncoming(final_record_num, remaining_breaks_block);
176        phiLineNum->addIncoming(phiFinalRecordNum, return_block);
177
178        phiFinalRecordStart->addIncoming(phiRecordStart, processMatchesExit);
179        phiFinalRecordStart->addIncoming(pendingLineStart, remaining_breaks_block);
180        phiLineStart->addIncoming(phiFinalRecordStart, return_block);
181
182        Value * nextScanwordPos = iBuilder->CreateAdd(phiScanwordPos, ConstantInt::get(sizeTy, sizeTy->getBitWidth()));
183        phiScanwordPos->addIncoming(nextScanwordPos, return_block);
184        Value * nextIndex = iBuilder->CreateAdd(phiIndex, iBuilder->getInt32(1));
185        phiIndex->addIncoming(nextIndex, return_block);
186        iBuilder->CreateLikelyCondBr(iBuilder->CreateICmpNE(nextIndex, iBuilder->getInt32(fieldCount)), scanWordIteration, scanWordExit);
187
188    iBuilder->SetInsertPoint(scanWordExit);
189    iBuilder->setScalarField("BlockNo", iBuilder->CreateAdd(blockNo, ConstantInt::get(blockNo->getType(), 1)));
190    iBuilder->setScalarField("LineNum", phiFinalRecordNum);
191    iBuilder->setProcessedItemCount("InputStream", phiFinalRecordStart);
192    Value * blockBaseNext = iBuilder->CreateAdd(blockBase, ConstantInt::get(iBuilder->getSizeTy(), 1));
193    blockBase->addIncoming(blockBaseNext, scanWordExit);
194    iBuilder->CreateLikelyCondBr(iBuilder->CreateICmpNE(blockBaseNext, blocksToDo), initialBlock, blocksExit);
195
196    iBuilder->SetInsertPoint(blocksExit);
197    iBuilder->CreateCondBr(iBuilder->CreateICmpULT(itemsToDo, blockSize), callFinalizeScan, scanReturn);
198    iBuilder->SetInsertPoint(callFinalizeScan);
199    Value * bufSize = iBuilder->getBufferedSize("InputStream");
200    Function * finalizer = m->getFunction("finalize_match_wrapper"); assert (finalizer);
201    Value * const buffer_base = iBuilder->getRawInputPointer("InputStream", iBuilder->getInt32(0), iBuilder->getInt32(0));
202    Value * buffer_end_address = iBuilder->CreateGEP(buffer_base, bufSize);
203    iBuilder->CreateCall(finalizer, {accumulator, buffer_end_address});
204    iBuilder->CreateBr(scanReturn);
205    iBuilder->SetInsertPoint(scanReturn);
206   
207}
208
209ScanMatchKernel::ScanMatchKernel(const std::unique_ptr<kernel::KernelBuilder> & b)
210: MultiBlockKernel("scanMatch",
211    {Binding{b->getStreamSetTy(1, 1), "matchResult"}, Binding{b->getStreamSetTy(1, 1), "lineBreak"}, Binding{b->getStreamSetTy(1, 8), "InputStream", UnknownRate()}},
212    {},
213    {Binding{b->getIntAddrTy(), "accumulator_address"}},
214    {},
215    {Binding{b->getSizeTy(), "BlockNo"}, Binding{b->getSizeTy(), "LineNum"}}) {}
216}
Note: See TracBrowser for help on using the repository browser.