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

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

New kernel infrastructure

File size: 13.6 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::generateKernel() {
43    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
44    if (mKernelStateType == nullptr) finalizeKernelStateType();
45    KernelBuilder::generateKernel();
46    Module * m = iBuilder->getModule();
47   
48    Function * scanWordFunction = generateScanWordRoutine(m);
49    const unsigned fieldCount = iBuilder->getBitBlockWidth() / mScanwordBitWidth;
50    Type * T = iBuilder->getIntNTy(mScanwordBitWidth);
51    Type * scanwordVectorType =  VectorType::get(T, fieldCount);
52
53    Function * doBlockFunction = m->getFunction(mKernelName + "_DoBlock");
54
55    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
56    Value * kernelStuctParam = getParameter(doBlockFunction, "self");
57    Value * scanwordPos = getScalarField(kernelStuctParam, "BlockNo");
58    scanwordPos = iBuilder->CreateMul(scanwordPos, ConstantInt::get(scanwordPos->getType(), iBuilder->getBitBlockWidth()));
59   
60    Value * recordStart = getScalarField(kernelStuctParam, "LineStart");
61    Value * recordNum = getScalarField(kernelStuctParam, "LineNum");
62    Value * matchResultsPtr = getParameter(doBlockFunction, "matchResults");
63    Value * matches = iBuilder->CreateBlockAlignedLoad(matchResultsPtr, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
64    Value * linebreaks = iBuilder->CreateBlockAlignedLoad(matchResultsPtr, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
65    Value * matchWordVector = iBuilder->CreateBitCast(matches, scanwordVectorType);
66    Value * breakWordVector = iBuilder->CreateBitCast(linebreaks, scanwordVectorType);
67    for(unsigned i = 0; i < fieldCount; ++i){
68        Value * matchWord = iBuilder->CreateExtractElement(matchWordVector, ConstantInt::get(T, i));
69        Value * recordBreaksWord = iBuilder->CreateExtractElement(breakWordVector, ConstantInt::get(T, i));
70        Value * wordResult = iBuilder->CreateCall(scanWordFunction, {kernelStuctParam, matchWord, recordBreaksWord, scanwordPos, recordStart, recordNum});
71        scanwordPos = iBuilder->CreateAdd(scanwordPos, ConstantInt::get(T, mScanwordBitWidth));
72        recordStart = iBuilder->CreateExtractValue(wordResult, std::vector<unsigned>({0}));
73        recordNum = iBuilder->CreateExtractValue(wordResult, std::vector<unsigned>({1}));
74    }
75    setScalarField(kernelStuctParam, "LineStart", recordStart);
76    setScalarField(kernelStuctParam, "LineNum", recordNum);
77    setScalarField(kernelStuctParam, "BlockNo", iBuilder->CreateAdd(getScalarField(kernelStuctParam, "BlockNo"), iBuilder->getInt64(1)));
78    iBuilder -> CreateRetVoid();
79   
80    // scanMatch FinalBlock function simply dispatches to the DoBlock function
81    addTrivialFinalBlockMethod(m);
82    iBuilder->restoreIP(savePoint);
83}
84
85   
86Function * scanMatchKernel::generateScanWordRoutine(Module * m) {
87    Function * function = m->getFunction("scan_matches_in_scanword");
88    if (LLVM_UNLIKELY(function != nullptr)) {
89        return function;
90    }
91   
92    LLVMContext & ctxt = m->getContext();
93    Type * T = iBuilder->getIntNTy(mScanwordBitWidth);
94    Type * S = PointerType::get(iBuilder->getIntNTy(8), 0);
95    Type * returnType = StructType::get(ctxt, std::vector<Type *>({T, T}));
96    FunctionType * functionType = FunctionType::get(returnType, std::vector<Type *>({PointerType::get(mKernelStateType, 0), T, T, T, T, T}), false);
97   
98    SmallVector<AttributeSet, 6> Attrs;
99    Attrs.push_back(AttributeSet::get(ctxt, ~0U, std::vector<Attribute::AttrKind>({ Attribute::NoUnwind, Attribute::UWTable })));
100    Attrs.push_back(AttributeSet::get(ctxt, 1, std::vector<Attribute::AttrKind>({})));
101    Attrs.push_back(AttributeSet::get(ctxt, 2, std::vector<Attribute::AttrKind>({})));
102    Attrs.push_back(AttributeSet::get(ctxt, 3, std::vector<Attribute::AttrKind>({})));
103    Attrs.push_back(AttributeSet::get(ctxt, 4, std::vector<Attribute::AttrKind>({})));
104    Attrs.push_back(AttributeSet::get(ctxt, 5, std::vector<Attribute::AttrKind>({})));
105    AttributeSet AttrSet = AttributeSet::get(ctxt, Attrs);
106   
107    function = Function::Create(functionType, GlobalValue::ExternalLinkage, "scan_matches_in_scanword", m);
108    function->setCallingConv(CallingConv::C);
109    function->setAttributes(AttrSet);
110    function->addFnAttr(llvm::Attribute::AlwaysInline);
111   
112    Function::arg_iterator args = function->arg_begin();
113    Value * instance = &*(args++);
114    instance->setName("this");
115    Value * matches_input_parm = &*(args++);
116    matches_input_parm->setName("matches");
117    Value * record_breaks_input_parm = &*(args++);
118    record_breaks_input_parm->setName("breaks");
119    Value * scanwordPos = &*(args++);
120    scanwordPos->setName("scanwordPos");
121    Value * recordStart_input_parm = &*(args++);
122    recordStart_input_parm->setName("pendingLineStart");
123    Value * recordNum_input_parm = &*(args++);
124    recordNum_input_parm->setName("lineNum");
125   
126    Constant * matchProcessor;
127    if (mIsNameExpression) {
128        matchProcessor = m->getOrInsertFunction("insert_codepoints", Type::getVoidTy(ctxt), T, T, T, S, nullptr);
129    } else {
130        matchProcessor = m->getOrInsertFunction("wrapped_report_match", Type::getVoidTy(ctxt), T, T, T, S, T, T, nullptr);
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, mScanwordBitWidth);
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    if (mIsNameExpression) {
202        iBuilder->CreateCall(matchProcessor, std::vector<Value *>({matchRecordNum_phi, matchRecordStart_phi, matchRecordEnd, fileBuf}));
203    } else {
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    }
208   
209    Value * remaining_matches = generateResetLowestBit(iBuilder, matches_phi);
210    Value * remaining_breaks = iBuilder->CreateXor(record_breaks_phi, prior_breaks);
211    matches_phi->addIncoming(remaining_matches, loop_final_block);
212    record_breaks_phi->addIncoming(remaining_breaks, loop_final_block);
213    recordNum_phi->addIncoming(matchRecordNum_phi, loop_final_block);
214    recordStart_phi->addIncoming(matchRecordStart_phi, loop_final_block);
215    iBuilder->CreateBr(matches_test_block);
216   
217   
218    // LOOP EXIT/MATCHES_DONE
219    iBuilder->SetInsertPoint(matches_done_block);
220    // When the matches are done, there may be additional record breaks remaining
221    Value * more_breaks_cond = iBuilder->CreateICmpNE(record_breaks_phi, ConstantInt::get(T, 0));
222    iBuilder->CreateCondBr(more_breaks_cond, remaining_breaks_block, return_block);
223   
224    // REMAINING_BREAKS_BLOCK: process remaining record breaks after all matches are processed
225    iBuilder->SetInsertPoint(remaining_breaks_block);
226    Value * break_count = generatePopcount(iBuilder, record_breaks_phi);
227    Value * final_record_num = iBuilder->CreateAdd(recordNum_phi, break_count);
228    Value * reverseZeroes = generateCountReverseZeroes(iBuilder, record_breaks_phi);
229    Value * pendingLineStart = iBuilder->CreateAdd(scanwordPos, iBuilder->CreateSub(width, reverseZeroes));
230    iBuilder->CreateBr(return_block);
231   
232    // RETURN block
233    iBuilder->SetInsertPoint(return_block);
234    PHINode * finalRecordCount_phi = iBuilder->CreatePHI(T, 2, "finalRecordCount");
235    PHINode * finalRecordStart_phi = iBuilder->CreatePHI(T, 2, "finalRecordStart");
236    finalRecordCount_phi->addIncoming(recordNum_phi, matches_done_block);
237    finalRecordCount_phi->addIncoming(final_record_num, remaining_breaks_block);
238    finalRecordStart_phi->addIncoming(recordStart_phi, matches_done_block);
239    finalRecordStart_phi->addIncoming(pendingLineStart, remaining_breaks_block);
240    Value * retVal = UndefValue::get(returnType);
241    retVal = iBuilder->CreateInsertValue(retVal, finalRecordStart_phi, 0);
242    retVal = iBuilder->CreateInsertValue(retVal, finalRecordCount_phi, 1);
243    iBuilder->CreateRet(retVal);
244   
245    return function;
246}
247
248}
Note: See TracBrowser for help on using the repository browser.