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

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

Code clean-up. Removed Pablo Call, SetIthBit? and Prototype.

File size: 13.7 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       
41void ScanMatchKernel::generateDoBlockMethod() const {
42    auto savePoint = iBuilder->saveIP();
43    Module * m = iBuilder->getModule();
44    Function * scanWordFunction = generateScanWordRoutine(m);
45    IntegerType * T = iBuilder->getSizeTy();
46    const unsigned fieldCount = iBuilder->getBitBlockWidth() / T->getBitWidth();
47
48    Type * scanwordVectorType =  VectorType::get(T, fieldCount);
49
50    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
51
52    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
53    Value * kernelStuctParam = getParameter(doBlockFunction, "self");
54    Value * blockNo = getScalarField(kernelStuctParam, blockNoScalar);
55    Value * scanwordPos = iBuilder->CreateMul(blockNo, ConstantInt::get(blockNo->getType(), iBuilder->getBitBlockWidth()));
56   
57    Value * recordStart = getScalarField(kernelStuctParam, "LineStart");
58    Value * recordNum = getScalarField(kernelStuctParam, "LineNum");
59    Value * matches = iBuilder->CreateBlockAlignedLoad(getStream(kernelStuctParam, "matchResults", blockNo, iBuilder->getInt32(0)));
60    Value * linebreaks = iBuilder->CreateBlockAlignedLoad(getStream(kernelStuctParam, "matchResults", blockNo, iBuilder->getInt32(1)));
61    Value * matchWordVector = iBuilder->CreateBitCast(matches, scanwordVectorType);
62    Value * breakWordVector = iBuilder->CreateBitCast(linebreaks, scanwordVectorType);
63    for(unsigned i = 0; i < fieldCount; ++i){
64        Value * matchWord = iBuilder->CreateExtractElement(matchWordVector, ConstantInt::get(T, i));
65        Value * recordBreaksWord = iBuilder->CreateExtractElement(breakWordVector, ConstantInt::get(T, i));
66        Value * wordResult = iBuilder->CreateCall(scanWordFunction, {kernelStuctParam, matchWord, recordBreaksWord, scanwordPos, recordStart, recordNum});
67        scanwordPos = iBuilder->CreateAdd(scanwordPos, ConstantInt::get(T, T->getBitWidth()));
68        recordStart = iBuilder->CreateExtractValue(wordResult, std::vector<unsigned>({0}));
69        recordNum = iBuilder->CreateExtractValue(wordResult, std::vector<unsigned>({1}));
70    }
71    setScalarField(kernelStuctParam, "LineStart", recordStart);
72    setScalarField(kernelStuctParam, "LineNum", recordNum);
73    iBuilder -> CreateRetVoid();
74    iBuilder->restoreIP(savePoint);
75}
76
77   
78Function * ScanMatchKernel::generateScanWordRoutine(Module * m) const {
79    Function * function = m->getFunction("scan_matches_in_scanword");
80    if (LLVM_UNLIKELY(function != nullptr)) {
81        return function;
82    }
83   
84    LLVMContext & ctxt = m->getContext();
85
86    IntegerType * T = iBuilder->getSizeTy();
87    Type * S = PointerType::get(iBuilder->getIntNTy(8), 0);
88    Type * returnType = StructType::get(ctxt, std::vector<Type *>({T, T}));
89    FunctionType * functionType = FunctionType::get(returnType, std::vector<Type *>({PointerType::get(mKernelStateType, 0), T, T, T, T, T}), false);
90   
91    SmallVector<AttributeSet, 6> Attrs;
92    Attrs.push_back(AttributeSet::get(ctxt, ~0U, std::vector<Attribute::AttrKind>({ Attribute::NoUnwind, Attribute::UWTable })));
93    Attrs.push_back(AttributeSet::get(ctxt, 1, std::vector<Attribute::AttrKind>({})));
94    Attrs.push_back(AttributeSet::get(ctxt, 2, std::vector<Attribute::AttrKind>({})));
95    Attrs.push_back(AttributeSet::get(ctxt, 3, std::vector<Attribute::AttrKind>({})));
96    Attrs.push_back(AttributeSet::get(ctxt, 4, std::vector<Attribute::AttrKind>({})));
97    Attrs.push_back(AttributeSet::get(ctxt, 5, std::vector<Attribute::AttrKind>({})));
98    AttributeSet AttrSet = AttributeSet::get(ctxt, Attrs);
99   
100    function = Function::Create(functionType, GlobalValue::ExternalLinkage, "scan_matches_in_scanword", m);
101    function->setCallingConv(CallingConv::C);
102    function->setAttributes(AttrSet);
103    function->addFnAttr(llvm::Attribute::AlwaysInline);
104   
105    Function::arg_iterator args = function->arg_begin();
106    Value * instance = &*(args++);
107    instance->setName("this");
108    Value * matches_input_parm = &*(args++);
109    matches_input_parm->setName("matches");
110    Value * record_breaks_input_parm = &*(args++);
111    record_breaks_input_parm->setName("breaks");
112    Value * scanwordPos = &*(args++);
113    scanwordPos->setName("scanwordPos");
114    Value * recordStart_input_parm = &*(args++);
115    recordStart_input_parm->setName("pendingLineStart");
116    Value * recordNum_input_parm = &*(args++);
117    recordNum_input_parm->setName("lineNum");
118   
119    Constant * matchProcessor = nullptr;
120    switch (mGrepType) {
121        case GrepType::Normal:
122            matchProcessor = m->getOrInsertFunction("wrapped_report_match", iBuilder->getVoidTy(), T, T, T, S, T, T, nullptr);
123            break;
124        case GrepType::NameExpression:
125            matchProcessor = m->getOrInsertFunction("insert_codepoints", iBuilder->getVoidTy(), T, T, T, S, nullptr);
126            break;
127        case GrepType::PropertyValue:
128            matchProcessor = m->getOrInsertFunction("insert_property_values", iBuilder->getVoidTy(), T, T, T, S, nullptr);
129            break;
130        default: llvm_unreachable("unknown grep type");
131    }
132    iBuilder->SetInsertPoint(BasicBlock::Create(ctxt, "entry", function,0));
133   
134    BasicBlock * entry_block = iBuilder->GetInsertBlock();
135    BasicBlock * matches_test_block = BasicBlock::Create(ctxt, "matches_test_block", function, 0);
136    BasicBlock * process_matches_loop_entry = BasicBlock::Create(ctxt, "process_matches_loop", function, 0);
137    BasicBlock * prior_breaks_block = BasicBlock::Create(ctxt, "prior_breaks_block", function, 0);
138    BasicBlock * loop_final_block = BasicBlock::Create(ctxt, "loop_final_block", function, 0);
139    BasicBlock * matches_done_block = BasicBlock::Create(ctxt, "matches_done_block", function, 0);
140    BasicBlock * remaining_breaks_block = BasicBlock::Create(ctxt, "remaining_breaks_block", function, 0);
141    BasicBlock * return_block = BasicBlock::Create(ctxt, "return_block", function, 0);
142   
143   
144    // The match scanner works with a loop involving four variables:
145    // (a) the bit stream scanword of matches marking the ends of selected records,
146    // (b) the bit stream scanword of record_breaks marking the ends of all records,
147    // (c) the integer lastRecordNum indicating the number of records processed so far,
148    // (d) the index lastRecordStart indicating the file position of the last record.
149    // We set up a loop structure, in which a set of 4 phi nodes initialize these
150    // variables from either the input to the scanner or the computed values within
151    // the loop body.
152   
153   
154    iBuilder->CreateBr(matches_test_block);
155   
156    // LOOP Test Block
157    iBuilder->SetInsertPoint(matches_test_block);
158    PHINode * matches_phi = iBuilder->CreatePHI(T, 2, "matches");
159    PHINode * record_breaks_phi = iBuilder->CreatePHI(T, 2, "record_breaks");
160    PHINode * recordNum_phi = iBuilder->CreatePHI(T, 2, "recordNum");
161    PHINode * recordStart_phi = iBuilder->CreatePHI(T, 2, "recordStart");
162    matches_phi->addIncoming(matches_input_parm, entry_block);
163    record_breaks_phi->addIncoming(record_breaks_input_parm, entry_block);
164    recordNum_phi->addIncoming(recordNum_input_parm, entry_block);
165    recordStart_phi->addIncoming(recordStart_input_parm, entry_block);
166    Value * have_matches_cond = iBuilder->CreateICmpNE(matches_phi, ConstantInt::get(T, 0));
167    iBuilder->CreateCondBr(have_matches_cond, process_matches_loop_entry, matches_done_block);
168   
169    // LOOP BODY
170    // The loop body is entered if we have more matches to process.
171    iBuilder->SetInsertPoint(process_matches_loop_entry);
172    Value * prior_breaks = iBuilder->CreateAnd(generateForwardZeroesMask(iBuilder, matches_phi), record_breaks_phi);
173    // Within the loop we have a conditional block that is executed if there are any prior
174    // record breaks.
175    Value * prior_breaks_cond = iBuilder->CreateICmpNE(prior_breaks, ConstantInt::get(T, 0));
176    iBuilder->CreateCondBr(prior_breaks_cond, prior_breaks_block, loop_final_block);
177   
178    // PRIOR_BREAKS_BLOCK
179    // If there are prior breaks, we count them and compute the record start position.
180    iBuilder->SetInsertPoint(prior_breaks_block);
181    Value * matchRecordNum = iBuilder->CreateAdd(generatePopcount(iBuilder, prior_breaks), recordNum_phi);
182    Value * reverseDistance = generateCountReverseZeroes(iBuilder, prior_breaks);
183    Value * width = ConstantInt::get(T, T->getBitWidth());
184    Value * matchRecordStart = iBuilder->CreateAdd(scanwordPos, iBuilder->CreateSub(width, reverseDistance));
185    iBuilder->CreateBr(loop_final_block);
186   
187    // LOOP FINAL BLOCK
188    // The prior breaks, if any have been counted.  Set up phi nodes for the recordNum
189    // and recortStart depending on whether the conditional execution of prior_breaks_block.
190    iBuilder->SetInsertPoint(loop_final_block);
191    PHINode * matchRecordNum_phi = iBuilder->CreatePHI(T, 2, "matchRecordNum");
192    PHINode * matchRecordStart_phi = iBuilder->CreatePHI(T, 2, "matchRecordStart");
193    matchRecordNum_phi->addIncoming(recordNum_phi, process_matches_loop_entry);
194    matchRecordNum_phi->addIncoming(matchRecordNum, prior_breaks_block);
195    matchRecordStart_phi->addIncoming(recordStart_phi, process_matches_loop_entry);
196    matchRecordStart_phi->addIncoming(matchRecordStart, prior_breaks_block);   
197    Value * matchRecordEnd = iBuilder->CreateAdd(scanwordPos, generateCountForwardZeroes(iBuilder, matches_phi));
198   
199
200    Value * fileBuf = getScalarField(instance, "FileBuf");
201    switch (mGrepType) {
202        case GrepType::Normal:
203        {
204            Value * fileSize = getScalarField(instance, "FileSize");
205            Value * fileIdx = getScalarField(instance, "FileIdx");
206            iBuilder->CreateCall(matchProcessor, std::vector<Value *>({matchRecordNum_phi, matchRecordStart_phi, matchRecordEnd, fileBuf, fileSize, fileIdx}));
207            break;
208        }
209        case GrepType::NameExpression:
210        case GrepType::PropertyValue:
211            iBuilder->CreateCall(matchProcessor, std::vector<Value *>({matchRecordNum_phi, matchRecordStart_phi, matchRecordEnd, fileBuf}));
212            break;
213        default: llvm_unreachable("unknown grep type");
214    }
215   
216    Value * remaining_matches = generateResetLowestBit(iBuilder, matches_phi);
217    Value * remaining_breaks = iBuilder->CreateXor(record_breaks_phi, prior_breaks);
218    matches_phi->addIncoming(remaining_matches, loop_final_block);
219    record_breaks_phi->addIncoming(remaining_breaks, loop_final_block);
220    recordNum_phi->addIncoming(matchRecordNum_phi, loop_final_block);
221    recordStart_phi->addIncoming(matchRecordStart_phi, loop_final_block);
222    iBuilder->CreateBr(matches_test_block);
223   
224   
225    // LOOP EXIT/MATCHES_DONE
226    iBuilder->SetInsertPoint(matches_done_block);
227    // When the matches are done, there may be additional record breaks remaining
228    Value * more_breaks_cond = iBuilder->CreateICmpNE(record_breaks_phi, ConstantInt::get(T, 0));
229    iBuilder->CreateCondBr(more_breaks_cond, remaining_breaks_block, return_block);
230   
231    // REMAINING_BREAKS_BLOCK: process remaining record breaks after all matches are processed
232    iBuilder->SetInsertPoint(remaining_breaks_block);
233    Value * break_count = generatePopcount(iBuilder, record_breaks_phi);
234    Value * final_record_num = iBuilder->CreateAdd(recordNum_phi, break_count);
235    Value * reverseZeroes = generateCountReverseZeroes(iBuilder, record_breaks_phi);
236    Value * pendingLineStart = iBuilder->CreateAdd(scanwordPos, iBuilder->CreateSub(width, reverseZeroes));
237    iBuilder->CreateBr(return_block);
238   
239    // RETURN block
240    iBuilder->SetInsertPoint(return_block);
241    PHINode * finalRecordCount_phi = iBuilder->CreatePHI(T, 2, "finalRecordCount");
242    PHINode * finalRecordStart_phi = iBuilder->CreatePHI(T, 2, "finalRecordStart");
243    finalRecordCount_phi->addIncoming(recordNum_phi, matches_done_block);
244    finalRecordCount_phi->addIncoming(final_record_num, remaining_breaks_block);
245    finalRecordStart_phi->addIncoming(recordStart_phi, matches_done_block);
246    finalRecordStart_phi->addIncoming(pendingLineStart, remaining_breaks_block);
247    Value * retVal = UndefValue::get(returnType);
248    retVal = iBuilder->CreateInsertValue(retVal, finalRecordStart_phi, 0);
249    retVal = iBuilder->CreateInsertValue(retVal, finalRecordCount_phi, 1);
250    iBuilder->CreateRet(retVal);
251   
252    return function;
253}
254
255}
Note: See TracBrowser for help on using the repository browser.