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

Last change on this file since 5074 was 5074, checked in by cameron, 3 years ago

Kernel infrastructure: move common logic into KernelBuilder? base class; demo linking in wc

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