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

Last change on this file since 5379 was 5379, checked in by nmedfort, 2 years ago

Bug fixes for last check in

File size: 13.4 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 <IR_Gen/idisa_builder.h>
9#include <llvm/IR/Module.h>
10
11using namespace llvm;
12
13namespace kernel {
14
15Value * generateForwardZeroesMask(IDISA::IDISA_Builder * iBuilder, Value * bits) {
16    Value * bits_minus1 = iBuilder->CreateSub(bits, ConstantInt::get(bits->getType(), 1));
17    return iBuilder->CreateAnd(bits_minus1, iBuilder->CreateNot(bits));
18}
19
20Value * generatePopcount(IDISA::IDISA_Builder * iBuilder, Value * bits) {
21    Value * ctpopFunc = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::ctpop, bits->getType());
22    return iBuilder->CreateCall(ctpopFunc, std::vector<Value *>({bits}));
23}
24
25Value * generateCountForwardZeroes(IDISA::IDISA_Builder * iBuilder, Value * bits) {
26    Value * cttzFunc = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::cttz, bits->getType());
27    return iBuilder->CreateCall(cttzFunc, std::vector<Value *>({bits, ConstantInt::get(iBuilder->getInt1Ty(), 0)}));
28}
29
30Value * generateCountReverseZeroes(IDISA::IDISA_Builder * iBuilder, Value * bits) {
31    Value * ctlzFunc = Intrinsic::getDeclaration(iBuilder->getModule(), Intrinsic::ctlz, bits->getType());
32    return iBuilder->CreateCall(ctlzFunc, std::vector<Value *>({bits, ConstantInt::get(iBuilder->getInt1Ty(), 0)}));
33}
34
35Value * generateResetLowestBit(IDISA::IDISA_Builder * iBuilder, Value * bits) {
36    Value * bits_minus1 = iBuilder->CreateSub(bits, ConstantInt::get(bits->getType(), 1));
37    return iBuilder->CreateAnd(bits_minus1, bits);
38}
39       
40
41
42void ScanMatchKernel::generateDoBlockMethod() {
43
44    Module * const m = iBuilder->getModule();
45    BasicBlock * const entryBlock = iBuilder->GetInsertBlock();
46    BasicBlock * const scanWordIteration = CreateBasicBlock("ScanWordIteration");
47    BasicBlock * const matches_test_block = CreateBasicBlock("matches_test_block");
48    BasicBlock * const processMatchesEntry = CreateBasicBlock("process_matches_loop");
49    BasicBlock * const prior_breaks_block = CreateBasicBlock("prior_breaks_block");
50    BasicBlock * const loop_final_block = CreateBasicBlock("loop_final_block");
51    BasicBlock * const processMatchesExit = CreateBasicBlock("matches_done_block");
52    BasicBlock * const remaining_breaks_block = CreateBasicBlock("remaining_breaks_block");
53    BasicBlock * const return_block = CreateBasicBlock("return_block");
54    BasicBlock * const scanWordExit = CreateBasicBlock("ScanWordExit");
55    IntegerType * const sizeTy = iBuilder->getSizeTy();
56    PointerType * const codeUnitTy = iBuilder->getIntNTy(mCodeUnitWidth)->getPointerTo();
57    const unsigned fieldCount = iBuilder->getBitBlockWidth() / sizeTy->getBitWidth();
58    VectorType * const scanwordVectorType =  VectorType::get(sizeTy, fieldCount);
59    Value * const blockNo = getScalarField("BlockNo");
60    Value * const scanwordPos = iBuilder->CreateMul(blockNo, ConstantInt::get(blockNo->getType(), iBuilder->getBitBlockWidth()));
61    Value * const lastRecordStart = getScalarField("LineStart");
62    Value * const lastRecordNum = getScalarField("LineNum");
63    Value * const inputStream = iBuilder->CreatePointerCast(getRawInputPointer("InputStream", iBuilder->getInt32(0), iBuilder->getInt32(0)), codeUnitTy);
64
65    Value * const fileSize = iBuilder->CreateAdd(getProcessedItemCount("InputStream"), getScalarField("PendingBytes"));
66
67    Constant * matchProcessor = nullptr;
68    Value * fileIdx = nullptr;
69    switch (mGrepType) {
70        case GrepType::Normal:
71            fileIdx = getScalarField("FileIdx");
72            matchProcessor = m->getOrInsertFunction("wrapped_report_match" + std::to_string(mCodeUnitWidth), iBuilder->getVoidTy(), sizeTy, sizeTy, sizeTy, codeUnitTy, sizeTy, sizeTy, nullptr);
73            break;
74        case GrepType::NameExpression:
75            matchProcessor = m->getOrInsertFunction("insert_codepoints", iBuilder->getVoidTy(), sizeTy, sizeTy, sizeTy, codeUnitTy, nullptr);
76            break;
77        case GrepType::PropertyValue:
78            matchProcessor = m->getOrInsertFunction("insert_property_values", iBuilder->getVoidTy(), sizeTy, sizeTy, sizeTy, codeUnitTy, nullptr);
79            break;
80        default: llvm_unreachable("unknown grep type");
81    }
82
83    Value * const matches = iBuilder->CreateBitCast(loadInputStreamBlock("matchResult", iBuilder->getInt32(0)), scanwordVectorType);
84    Value * const linebreaks = iBuilder->CreateBitCast(loadInputStreamBlock("lineBreak", iBuilder->getInt32(0)), scanwordVectorType);
85
86    iBuilder->CreateBr(scanWordIteration);
87
88    iBuilder->SetInsertPoint(scanWordIteration);
89
90        // while (phiIndex < words per stride)
91        PHINode * const phiIndex = iBuilder->CreatePHI(iBuilder->getInt32Ty(), 2, "index");
92        phiIndex->addIncoming(iBuilder->getInt32(0), entryBlock);
93        PHINode * const phiScanwordPos = iBuilder->CreatePHI(scanwordPos->getType(), 2, "pos");
94        phiScanwordPos->addIncoming(scanwordPos, entryBlock);
95        PHINode * const phiLineStart = iBuilder->CreatePHI(lastRecordStart->getType(), 2, "recordstart");
96        phiLineStart->addIncoming(lastRecordStart, entryBlock);
97        PHINode * const phiLineNum = iBuilder->CreatePHI(lastRecordNum->getType(), 2, "recordnum");
98        phiLineNum->addIncoming(lastRecordNum, entryBlock);
99        Value * const matchWord = iBuilder->CreateExtractElement(matches, phiIndex);
100        Value * const recordBreaks = iBuilder->CreateExtractElement(linebreaks, phiIndex);
101
102        // The match scanner works with a loop involving four variables:
103        // (a) the bit stream scanword of matches marking the ends of selected records,
104        // (b) the bit stream scanword of record_breaks marking the ends of all records,
105        // (c) the integer lastRecordNum indicating the number of records processed so far,
106        // (d) the index lastRecordStart indicating the file position of the last record.
107        // We set up a loop structure, in which a set of 4 phi nodes initialize these
108        // variables from either the input to the scanner or the computed values within
109        // the loop body.
110
111        iBuilder->CreateBr(matches_test_block);
112
113        // LOOP Test Block
114        iBuilder->SetInsertPoint(matches_test_block);
115        PHINode * phiMatchWord = iBuilder->CreatePHI(sizeTy, 2, "matches");
116        PHINode * phiRecordBreaks = iBuilder->CreatePHI(sizeTy, 2, "recordbreaks");
117        PHINode * phiRecordStart = iBuilder->CreatePHI(sizeTy, 2, "recordstart");
118        PHINode * phiRecordNum = iBuilder->CreatePHI(sizeTy, 2, "recordnum");
119        phiMatchWord->addIncoming(matchWord, scanWordIteration);
120        phiRecordBreaks->addIncoming(recordBreaks, scanWordIteration);
121        phiRecordNum->addIncoming(phiLineNum, scanWordIteration);
122        phiRecordStart->addIncoming(phiLineStart, scanWordIteration);
123        Value * anyMatches = iBuilder->CreateICmpNE(phiMatchWord, ConstantInt::getNullValue(sizeTy));
124        iBuilder->CreateCondBr(anyMatches, processMatchesEntry, processMatchesExit);
125
126            // LOOP BODY
127            // The loop body is entered if we have more matches to process.
128            iBuilder->SetInsertPoint(processMatchesEntry);
129            Value * prior_breaks = iBuilder->CreateAnd(generateForwardZeroesMask(iBuilder, phiMatchWord), phiRecordBreaks);
130            // Within the loop we have a conditional block that is executed if there are any prior record breaks.
131            Value * prior_breaks_cond = iBuilder->CreateICmpNE(prior_breaks, ConstantInt::getNullValue(sizeTy));
132            iBuilder->CreateCondBr(prior_breaks_cond, prior_breaks_block, loop_final_block);
133
134                // PRIOR_BREAKS_BLOCK
135                // If there are prior breaks, we count them and compute the record start position.
136                iBuilder->SetInsertPoint(prior_breaks_block);
137                Value * matchedRecordNum = iBuilder->CreateAdd(generatePopcount(iBuilder, prior_breaks), phiRecordNum);
138                Value * reverseDistance = generateCountReverseZeroes(iBuilder, prior_breaks);
139                Value * width = ConstantInt::get(sizeTy, sizeTy->getBitWidth());
140                Value * priorRecordStart = iBuilder->CreateAdd(phiScanwordPos, iBuilder->CreateSub(width, reverseDistance));
141                iBuilder->CreateBr(loop_final_block);
142
143            // LOOP FINAL BLOCK
144            // The prior breaks, if any have been counted.  Set up phi nodes for the recordNum
145            // and recortStart depending on whether the conditional execution of prior_breaks_block.
146            iBuilder->SetInsertPoint(loop_final_block);
147            PHINode * matchRecordNum = iBuilder->CreatePHI(sizeTy, 2, "matchRecordNum");
148            matchRecordNum->addIncoming(phiRecordNum, processMatchesEntry);
149            matchRecordNum->addIncoming(matchedRecordNum, prior_breaks_block);
150            phiRecordNum->addIncoming(matchRecordNum, loop_final_block);
151
152            PHINode * matchRecordStart = iBuilder->CreatePHI(sizeTy, 2, "matchRecordStart");
153            matchRecordStart->addIncoming(phiRecordStart, processMatchesEntry);
154            matchRecordStart->addIncoming(priorRecordStart, prior_breaks_block);
155            phiRecordStart->addIncoming(matchRecordStart, loop_final_block);
156
157            Value * matchRecordEnd = iBuilder->CreateAdd(phiScanwordPos, generateCountForwardZeroes(iBuilder, phiMatchWord));
158            switch (mGrepType) {
159                case GrepType::Normal:
160                    iBuilder->CreateCall(matchProcessor, {matchRecordNum, matchRecordStart, matchRecordEnd, inputStream, fileSize, fileIdx});
161                    break;
162                case GrepType::NameExpression:
163                case GrepType::PropertyValue:
164                    iBuilder->CreateCall(matchProcessor, {matchRecordNum, matchRecordStart, matchRecordEnd, inputStream});
165                    break;
166                default: break;
167            }
168
169            Value * remaining_matches = generateResetLowestBit(iBuilder, phiMatchWord);
170            phiMatchWord->addIncoming(remaining_matches, loop_final_block);
171
172            Value * remaining_breaks = iBuilder->CreateXor(phiRecordBreaks, prior_breaks);
173            phiRecordBreaks->addIncoming(remaining_breaks, loop_final_block);
174
175            iBuilder->CreateBr(matches_test_block);
176
177        // LOOP EXIT/MATCHES_DONE
178        iBuilder->SetInsertPoint(processMatchesExit);
179        // When the matches are done, there may be additional record breaks remaining
180        Value * more_breaks_cond = iBuilder->CreateICmpNE(phiRecordBreaks, ConstantInt::getNullValue(sizeTy));
181        iBuilder->CreateCondBr(more_breaks_cond, remaining_breaks_block, return_block);
182
183            // REMAINING_BREAKS_BLOCK: process remaining record breaks after all matches are processed
184            iBuilder->SetInsertPoint(remaining_breaks_block);
185            Value * break_count = generatePopcount(iBuilder, phiRecordBreaks);
186            Value * final_record_num = iBuilder->CreateAdd(phiRecordNum, break_count);
187            Value * reverseZeroes = generateCountReverseZeroes(iBuilder, phiRecordBreaks);
188            Value * pendingLineStart = iBuilder->CreateAdd(phiScanwordPos, iBuilder->CreateSub(width, reverseZeroes));
189            iBuilder->CreateBr(return_block);
190
191        // RETURN block
192        iBuilder->SetInsertPoint(return_block);
193        PHINode * phiFinalRecordNum = iBuilder->CreatePHI(sizeTy, 2, "finalRecordCount");
194        PHINode * phiFinalRecordStart = iBuilder->CreatePHI(sizeTy, 2, "finalRecordStart");
195
196        phiFinalRecordNum->addIncoming(phiRecordNum, processMatchesExit);
197        phiFinalRecordNum->addIncoming(final_record_num, remaining_breaks_block);
198        phiLineNum->addIncoming(phiFinalRecordNum, return_block);
199
200        phiFinalRecordStart->addIncoming(phiRecordStart, processMatchesExit);
201        phiFinalRecordStart->addIncoming(pendingLineStart, remaining_breaks_block);
202        phiLineStart->addIncoming(phiFinalRecordStart, return_block);
203
204        Value * nextScanwordPos = iBuilder->CreateAdd(phiScanwordPos, ConstantInt::get(sizeTy, sizeTy->getBitWidth()));
205        phiScanwordPos->addIncoming(nextScanwordPos, return_block);
206        Value * nextIndex = iBuilder->CreateAdd(phiIndex, iBuilder->getInt32(1));
207        phiIndex->addIncoming(nextIndex, return_block);
208        iBuilder->CreateLikelyCondBr(iBuilder->CreateICmpNE(nextIndex, iBuilder->getInt32(fieldCount)), scanWordIteration, scanWordExit);
209
210    iBuilder->SetInsertPoint(scanWordExit);
211    setScalarField("BlockNo", iBuilder->CreateAdd(blockNo, ConstantInt::get(blockNo->getType(), 1)));
212    setScalarField("LineStart", phiFinalRecordStart);
213    setScalarField("LineNum", phiFinalRecordNum);
214}
215
216void ScanMatchKernel::generateInitMethod() {
217    setScalarField("PendingBytes", iBuilder->getSize(iBuilder->getBitBlockWidth() + 2));
218}
219
220void ScanMatchKernel::generateFinalBlockMethod(llvm::Value * remainingItems) {
221    setScalarField("PendingBytes", remainingItems);
222    CreateDoBlockMethodCall();
223}
224
225ScanMatchKernel::ScanMatchKernel(IDISA::IDISA_Builder * iBuilder, GrepType grepType, const unsigned codeUnitWidth)
226: BlockOrientedKernel(iBuilder, "scanMatch" + std::to_string(codeUnitWidth),
227    {Binding{iBuilder->getStreamSetTy(1, 8), "InputStream"}, Binding{iBuilder->getStreamSetTy(1, 1), "matchResult"}, Binding{iBuilder->getStreamSetTy(1, 1), "lineBreak"}},
228    {},
229    {Binding{iBuilder->getSizeTy(), "FileIdx"}},
230    {},
231    {Binding{iBuilder->getSizeTy(), "BlockNo"}, Binding{iBuilder->getSizeTy(), "LineStart"}, Binding{iBuilder->getSizeTy(), "LineNum"}, Binding{iBuilder->getSizeTy(), "PendingBytes"}})
232, mGrepType(grepType)
233, mCodeUnitWidth(codeUnitWidth) {
234}
235
236}
Note: See TracBrowser for help on using the repository browser.