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

Last change on this file since 6173 was 6104, checked in by cameron, 15 months ago

getArgumentList() deprecated

File size: 12.3 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/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> & b, Value * const numOfStrides) {
24
25    Module * const m = b->getModule();
26   
27    BasicBlock * const entryBlock = b->GetInsertBlock();
28    BasicBlock * const initialBlock = b->CreateBasicBlock("initialBlock");
29    BasicBlock * const scanWordIteration = b->CreateBasicBlock("ScanWordIteration");
30    BasicBlock * const matches_test_block = b->CreateBasicBlock("matches_test_block");
31    BasicBlock * const processMatchesEntry = b->CreateBasicBlock("process_matches_loop");
32    BasicBlock * const prior_breaks_block = b->CreateBasicBlock("prior_breaks_block");
33    BasicBlock * const loop_final_block = b->CreateBasicBlock("loop_final_block");
34    BasicBlock * const processMatchesExit = b->CreateBasicBlock("matches_done_block");
35    BasicBlock * const remaining_breaks_block = b->CreateBasicBlock("remaining_breaks_block");
36    BasicBlock * const return_block = b->CreateBasicBlock("return_block");
37    BasicBlock * const scanWordExit = b->CreateBasicBlock("ScanWordExit");
38    BasicBlock * const blocksExit = b->CreateBasicBlock("blocksExit");
39    BasicBlock * const callFinalizeScan = b->CreateBasicBlock("callFinalizeScan");
40    BasicBlock * const scanReturn = b->CreateBasicBlock("scanReturn");
41    IntegerType * const sizeTy = b->getSizeTy();
42    const unsigned fieldCount = b->getBitBlockWidth() / sizeTy->getBitWidth();
43    VectorType * const scanwordVectorType =  VectorType::get(sizeTy, fieldCount);
44    Constant * const ZERO = b->getSize(0);
45    Constant * const ONE = b->getSize(1);
46    Value * const blocksToDo = b->CreateMul(numOfStrides, b->getSize(mStride / b->getBitBlockWidth()));
47    Value * accumulator = b->getScalarField("accumulator_address");
48
49    b->CreateBr(initialBlock);
50
51    b->SetInsertPoint(initialBlock);
52    PHINode * const blockOffset = b->CreatePHI(sizeTy, 2);
53    blockOffset->addIncoming(ZERO, entryBlock);
54
55    Value * matches = b->loadInputStreamBlock("matchResult", ZERO, blockOffset);
56    matches = b->CreateBitCast(matches, scanwordVectorType);
57    Value * linebreaks = b->loadInputStreamBlock("lineBreak", ZERO, blockOffset);
58    linebreaks = b->CreateBitCast(linebreaks, scanwordVectorType);
59
60    Value * const blockNo = b->getScalarField("BlockNo");
61    Value * const scanwordPos = b->CreateShl(blockNo, floor_log2(b->getBitBlockWidth()));
62    Value * const consumed = b->getProcessedItemCount("InputStream");
63    Value * const consumedLines = b->getScalarField("LineNum");
64    b->CreateBr(scanWordIteration);
65
66    b->SetInsertPoint(scanWordIteration);
67
68        // while (phiIndex < words per stride)
69        PHINode * const phiIndex = b->CreatePHI(sizeTy, 2, "index");
70        phiIndex->addIncoming(ZERO, initialBlock);
71        PHINode * const phiScanwordPos = b->CreatePHI(scanwordPos->getType(), 2, "pos");
72        phiScanwordPos->addIncoming(scanwordPos, initialBlock);
73        PHINode * const phiLineStart = b->CreatePHI(consumed->getType(), 2, "recordstart");
74        phiLineStart->addIncoming(consumed, initialBlock);
75        PHINode * const phiLineNum = b->CreatePHI(consumedLines->getType(), 2, "recordnum");
76        phiLineNum->addIncoming(consumedLines, initialBlock);
77
78        Value * const matchWord = b->CreateExtractElement(matches, phiIndex);
79        Value * const recordBreaks = b->CreateExtractElement(linebreaks, phiIndex);
80
81        // The match scanner works with a loop involving four variables:
82        // (a) the bit stream scanword of matches marking the ends of selected records,
83        // (b) the bit stream scanword of record_breaks marking the ends of all records,
84        // (c) the integer lastRecordNum indicating the number of records processed so far,
85        // (d) the index lastRecordStart indicating the file position of the last record.
86        // We set up a loop structure, in which a set of 4 phi nodes initialize these
87        // variables from either the input to the scanner or the computed values within
88        // the loop body.
89
90        b->CreateBr(matches_test_block);
91
92        // LOOP Test Block
93        b->SetInsertPoint(matches_test_block);
94        PHINode * const phiMatchWord = b->CreatePHI(sizeTy, 2, "matches");
95        PHINode * const phiRecordBreaks = b->CreatePHI(sizeTy, 2, "recordbreaks");
96        PHINode * const phiRecordStart = b->CreatePHI(sizeTy, 2, "recordstart");
97        PHINode * const phiRecordNum = b->CreatePHI(sizeTy, 2, "recordnum");
98        phiMatchWord->addIncoming(matchWord, scanWordIteration);
99        phiRecordBreaks->addIncoming(recordBreaks, scanWordIteration);
100        phiRecordStart->addIncoming(phiLineStart, scanWordIteration);
101        phiRecordNum->addIncoming(phiLineNum, scanWordIteration);
102        Value * const anyMatches = b->CreateICmpNE(phiMatchWord, ZERO);
103        b->CreateCondBr(anyMatches, processMatchesEntry, processMatchesExit);
104
105            // LOOP BODY
106            // The loop body is entered if we have more matches to process.
107            b->SetInsertPoint(processMatchesEntry);
108            Value * prior_breaks = b->CreateAnd(b->CreateMaskToLowestBitExclusive(phiMatchWord), phiRecordBreaks);
109            // Within the loop we have a conditional block that is executed if there are any prior record breaks.
110            Value * prior_breaks_cond = b->CreateICmpNE(prior_breaks, ZERO);
111            b->CreateCondBr(prior_breaks_cond, prior_breaks_block, loop_final_block);
112
113                // PRIOR_BREAKS_BLOCK
114                // If there are prior breaks, we count them and compute the record start position.
115                b->SetInsertPoint(prior_breaks_block);
116                Value * matchedRecordNum = b->CreateAdd(b->CreatePopcount(prior_breaks), phiRecordNum);
117                Value * reverseDistance = b->CreateCountReverseZeroes(prior_breaks, true);
118                Value * width = ConstantInt::get(sizeTy, sizeTy->getBitWidth());
119                Value * priorRecordStart = b->CreateAdd(phiScanwordPos, b->CreateSub(width, reverseDistance));
120                b->CreateBr(loop_final_block);
121
122            // LOOP FINAL BLOCK
123            // The prior breaks, if any have been counted.  Set up phi nodes for the recordNum
124            // and recortStart depending on whether the conditional execution of prior_breaks_block.
125            b->SetInsertPoint(loop_final_block);
126            PHINode * matchRecordNum = b->CreatePHI(sizeTy, 2, "matchRecordNum");
127            matchRecordNum->addIncoming(phiRecordNum, processMatchesEntry);
128            matchRecordNum->addIncoming(matchedRecordNum, prior_breaks_block);
129            phiRecordNum->addIncoming(matchRecordNum, loop_final_block);
130
131            PHINode * const matchRecordStart = b->CreatePHI(sizeTy, 2, "matchRecordStart");
132            matchRecordStart->addIncoming(phiRecordStart, processMatchesEntry);
133            matchRecordStart->addIncoming(priorRecordStart, prior_breaks_block);
134            phiRecordStart->addIncoming(matchRecordStart, loop_final_block);
135            Value * matchRecordEnd = b->CreateAdd(phiScanwordPos, b->CreateCountForwardZeroes(phiMatchWord, true));
136            // It is possible that the matchRecordEnd position is one past EOF.  Make sure not
137            // to access past EOF.
138            Value * const bufLimit = b->CreateSub(b->CreateAdd(scanwordPos, b->getAvailableItemCount("InputStream")), ONE);
139            matchRecordEnd = b->CreateUMin(matchRecordEnd, bufLimit);
140            Function * const dispatcher = m->getFunction("accumulate_match_wrapper"); assert (dispatcher);
141            Value * const startPtr = b->getRawInputPointer("InputStream", matchRecordStart);
142            Value * const endPtr = b->getRawInputPointer("InputStream", matchRecordEnd);
143            Function::arg_iterator argi = dispatcher->arg_begin();
144            const auto matchRecNumArg = &*(argi++);
145            Value * const matchRecNum = b->CreateZExtOrTrunc(matchRecordNum, matchRecNumArg->getType());
146            b->CreateCall(dispatcher, {accumulator, matchRecNum, startPtr, endPtr});
147            Value * remaining_matches = b->CreateResetLowestBit(phiMatchWord);
148            phiMatchWord->addIncoming(remaining_matches, loop_final_block);
149
150            Value * remaining_breaks = b->CreateXor(phiRecordBreaks, prior_breaks);
151            phiRecordBreaks->addIncoming(remaining_breaks, loop_final_block);
152
153            b->CreateBr(matches_test_block);
154
155        // LOOP EXIT/MATCHES_DONE
156        b->SetInsertPoint(processMatchesExit);
157        // When the matches are done, there may be additional record breaks remaining
158        Value * more_breaks_cond = b->CreateICmpNE(phiRecordBreaks, ZERO);
159        b->CreateCondBr(more_breaks_cond, remaining_breaks_block, return_block);
160
161            // REMAINING_BREAKS_BLOCK: process remaining record breaks after all matches are processed
162            b->SetInsertPoint(remaining_breaks_block);
163            Value * break_count = b->CreatePopcount(phiRecordBreaks);
164            Value * final_record_num = b->CreateAdd(phiRecordNum, break_count);
165            Value * reverseZeroes = b->CreateCountReverseZeroes(phiRecordBreaks);
166            Value * pendingLineStart = b->CreateAdd(phiScanwordPos, b->CreateSub(width, reverseZeroes));
167            b->CreateBr(return_block);
168
169        // RETURN block
170        b->SetInsertPoint(return_block);
171        PHINode * phiFinalRecordNum = b->CreatePHI(sizeTy, 2, "finalRecordCount");
172        PHINode * phiFinalRecordStart = b->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 = b->CreateAdd(phiScanwordPos, ConstantInt::get(sizeTy, sizeTy->getBitWidth()));
183        phiScanwordPos->addIncoming(nextScanwordPos, return_block);
184        Value * nextIndex = b->CreateAdd(phiIndex, ONE);
185        phiIndex->addIncoming(nextIndex, return_block);
186        b->CreateLikelyCondBr(b->CreateICmpNE(nextIndex, b->getSize(fieldCount)), scanWordIteration, scanWordExit);
187
188    b->SetInsertPoint(scanWordExit);
189    b->setScalarField("BlockNo", b->CreateAdd(blockNo, ConstantInt::get(blockNo->getType(), 1)));
190    b->setScalarField("LineNum", phiFinalRecordNum);
191    b->setProcessedItemCount("InputStream", phiFinalRecordStart);
192    Value * const nextBlockOffset = b->CreateAdd(blockOffset, ONE);
193    blockOffset->addIncoming(nextBlockOffset, scanWordExit);
194    b->CreateLikelyCondBr(b->CreateICmpNE(nextBlockOffset, blocksToDo), initialBlock, blocksExit);
195
196    b->SetInsertPoint(blocksExit);
197    b->CreateCondBr(mIsFinal, callFinalizeScan, scanReturn);
198
199    b->SetInsertPoint(callFinalizeScan);
200    Value * avail = b->getAvailableItemCount("InputStream");
201    b->setProcessedItemCount("InputStream", b->CreateAdd(avail, scanwordPos));
202    Function * finalizer = m->getFunction("finalize_match_wrapper"); assert (finalizer);
203    Value * const buffer_base = b->getRawInputPointer("InputStream", ZERO);
204    Value * buffer_end_address = b->CreateGEP(buffer_base, avail);
205    b->CreateCall(finalizer, {accumulator, buffer_end_address});
206    b->CreateBr(scanReturn);
207
208    b->SetInsertPoint(scanReturn);
209}
210
211ScanMatchKernel::ScanMatchKernel(const std::unique_ptr<kernel::KernelBuilder> & b)
212: MultiBlockKernel("scanMatch",
213// inputs
214{Binding{b->getStreamSetTy(1, 1), "matchResult", FixedRate(), Principal()}
215,Binding{b->getStreamSetTy(1, 1), "lineBreak"}
216,Binding{b->getStreamSetTy(1, 8), "InputStream", FixedRate(), { Deferred(), RequiresLinearAccess() }}},
217// outputs
218{},
219// input scalars
220{Binding{b->getIntAddrTy(), "accumulator_address"}},
221// output scalars
222{},
223// kernel state
224{Binding{b->getSizeTy(), "BlockNo"}
225,Binding{b->getSizeTy(), "LineNum"}}) {
226
227}
228
229}
Note: See TracBrowser for help on using the repository browser.