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

Last change on this file since 4986 was 4974, checked in by nmedfort, 3 years ago

Added the kernel instance class; removed original mmap file access in favour of the boost mmap system. corrected PrintRegister? routine.

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