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

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

Bug fix; simplification

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