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

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

Refactor and encapsulate grep codegen/execution in GrepEngine?

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