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

Last change on this file since 4945 was 4945, checked in by lindanl, 3 years ago

Use kernel state methods.

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