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

Last change on this file since 5638 was 5630, checked in by nmedfort, 22 months ago

Partial check-in for avoidance of compiling Pablo/LLVM code to determine the Kernel struct type when using a cached object. Inactive RE alternation minimization check in.

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