Skip to content

Commit 1d04cf5

Browse files
mshelegoigcbot
authored andcommitted
Do not insert instructions after terminator in GenXPatternMatch
When insertion point for IRBuilder is set to BasicBlock instead of Instruction, new instructions are inserted after BB's terminator
1 parent 415cf16 commit 1d04cf5

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

IGC/VectorCompiler/lib/GenXCodeGen/GenXPatternMatch.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,14 +2118,14 @@ bool MinMaxMatcher::emit() {
21182118

21192119
// For a given instruction, find the insertion position which is the closest
21202120
// to all the similar users to the specified reference user.
2121-
static std::tuple<BasicBlock *, Instruction *>
2121+
static Instruction *
21222122
findOptimalInsertionPos(Instruction *I, Instruction *Ref, DominatorTree *DT,
21232123
std::function<bool(Instruction *)> IsSimilar) {
21242124
IGC_ASSERT_MESSAGE(!isa<PHINode>(Ref), "PHINode is not expected!");
21252125

21262126
// Shortcut case. If it's single-used, insert just before that user.
21272127
if (I->hasOneUse())
2128-
return std::make_tuple(nullptr, Ref);
2128+
return Ref;
21292129

21302130
DenseMap<BasicBlock *, Instruction *> BBs;
21312131
for (auto U : I->users()) {
@@ -2150,7 +2150,7 @@ findOptimalInsertionPos(Instruction *I, Instruction *Ref, DominatorTree *DT,
21502150
auto MI = BBs.begin();
21512151
// Another shortcut case. If it's only used in a single BB,
21522152
if (BBs.size() == 1)
2153-
return std::make_tuple(MI->first, MI->second);
2153+
return MI->second;
21542154

21552155
BasicBlock *BB = MI->first;
21562156
for (++MI; MI != BBs.end(); ++MI)
@@ -2163,7 +2163,9 @@ findOptimalInsertionPos(Instruction *I, Instruction *Ref, DominatorTree *DT,
21632163
Pos = MI->second;
21642164
}
21652165
IGC_ASSERT(BB);
2166-
return std::make_tuple(BB, Pos);
2166+
if (!Pos)
2167+
return BB->getTerminator();
2168+
return Pos;
21672169
}
21682170

21692171
// For the specified constant, calculate its reciprocal if it's safe;
@@ -2279,13 +2281,8 @@ void GenXPatternMatch::visitFDiv(BinaryOperator &I) {
22792281
return User->getOpcode() == Instruction::FDiv && User->hasAllowReciprocal();
22802282
};
22812283

2282-
BasicBlock *BB = nullptr;
2283-
Instruction *Pos = nullptr;
2284-
std::tie(BB, Pos) = findOptimalInsertionPos(Divisor, &I, DT, IsSimilar);
2285-
if (Pos)
2286-
IRB.SetInsertPoint(Pos);
2287-
else
2288-
IRB.SetInsertPoint(BB);
2284+
Instruction *Pos = findOptimalInsertionPos(Divisor, &I, DT, IsSimilar);
2285+
IRB.SetInsertPoint(Pos);
22892286

22902287
// (fdiv 1., (sqrt x)) -> (rsqrt x)
22912288
// TODO: This can be removed if pattern match is applied

0 commit comments

Comments
 (0)