Skip to content

Commit 9451dbe

Browse files
use dominfo
1 parent 2d42476 commit 9451dbe

File tree

2 files changed

+42
-51
lines changed

2 files changed

+42
-51
lines changed

mlir/include/mlir/IR/Dominance.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ class DominanceInfo : public detail::DominanceInfoBase</*IsPostDom=*/false> {
187187
/// dominance" of ops, the single block is considered to properly dominate
188188
/// itself in a graph region.
189189
bool properlyDominates(Block *a, Block *b) const;
190+
191+
bool properlyDominates(Block *aBlock, Block::iterator aIt, Block *bBlock,
192+
Block::iterator bIt, bool enclosingOk = true) const {
193+
return super::properlyDominatesImpl(aBlock, aIt, bBlock, bIt, enclosingOk);
194+
}
195+
196+
bool dominates(Block *aBlock, Block::iterator aIt, Block *bBlock,
197+
Block::iterator bIt, bool enclosingOk = true) const {
198+
return (aBlock == bBlock && aIt == bIt) ||
199+
super::properlyDominatesImpl(aBlock, aIt, bBlock, bIt, enclosingOk);
200+
}
190201
};
191202

192203
/// A class for computing basic postdominance information.
@@ -210,6 +221,18 @@ class PostDominanceInfo : public detail::DominanceInfoBase</*IsPostDom=*/true> {
210221
bool postDominates(Block *a, Block *b) const {
211222
return a == b || properlyPostDominates(a, b);
212223
}
224+
225+
bool properlyPostDominates(Block *aBlock, Block::iterator aIt, Block *bBlock,
226+
Block::iterator bIt,
227+
bool enclosingOk = true) const {
228+
return super::properlyDominatesImpl(aBlock, aIt, bBlock, bIt, enclosingOk);
229+
}
230+
231+
bool postDominates(Block *aBlock, Block::iterator aIt, Block *bBlock,
232+
Block::iterator bIt, bool enclosingOk = true) const {
233+
return (aBlock == bBlock && aIt == bIt) ||
234+
super::properlyDominatesImpl(aBlock, aIt, bBlock, bIt, enclosingOk);
235+
}
213236
};
214237

215238
} // namespace mlir

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -54,55 +54,6 @@ static void logFailure(llvm::ScopedPrinter &os, StringRef fmt, Args &&...args) {
5454
});
5555
}
5656

57-
/// Given two insertion points in the same block, choose the later one.
58-
static OpBuilder::InsertPoint
59-
chooseLaterInsertPointInBlock(OpBuilder::InsertPoint a,
60-
OpBuilder::InsertPoint b) {
61-
assert(a.getBlock() == b.getBlock() && "expected same block");
62-
Block *block = a.getBlock();
63-
if (a.getPoint() == block->begin())
64-
return b;
65-
if (b.getPoint() == block->begin())
66-
return a;
67-
if (a.getPoint()->isBeforeInBlock(&*b.getPoint()))
68-
return b;
69-
return a;
70-
}
71-
72-
/// Helper function that chooses the insertion point among the two given ones
73-
/// that is later.
74-
// TODO: Extend DominanceInfo API to work with block iterators.
75-
static OpBuilder::InsertPoint chooseLaterInsertPoint(OpBuilder::InsertPoint a,
76-
OpBuilder::InsertPoint b) {
77-
// Case 1: Fast path: Same block. This is the most common case.
78-
if (LLVM_LIKELY(a.getBlock() == b.getBlock()))
79-
return chooseLaterInsertPointInBlock(a, b);
80-
81-
// Case 2: Different block, but same region.
82-
if (a.getBlock()->getParent() == b.getBlock()->getParent()) {
83-
DominanceInfo domInfo;
84-
if (domInfo.properlyDominates(a.getBlock(), b.getBlock()))
85-
return b;
86-
if (domInfo.properlyDominates(b.getBlock(), a.getBlock()))
87-
return a;
88-
// Neither of the two blocks dominante each other.
89-
llvm_unreachable("unable to find valid insertion point");
90-
}
91-
92-
// Case 3: b's region contains a: choose a.
93-
if (b.getBlock()->getParent()->findAncestorOpInRegion(
94-
*a.getPoint()->getParentOp()))
95-
return a;
96-
97-
// Case 4: a's region contains b: choose b.
98-
if (a.getBlock()->getParent()->findAncestorOpInRegion(
99-
*b.getPoint()->getParentOp()))
100-
return b;
101-
102-
// Neither of the two operations contain each other.
103-
llvm_unreachable("unable to find valid insertion point");
104-
}
105-
10657
/// Helper function that computes an insertion point where the given value is
10758
/// defined and can be used without a dominance violation.
10859
static OpBuilder::InsertPoint computeInsertPoint(Value value) {
@@ -117,9 +68,26 @@ static OpBuilder::InsertPoint computeInsertPoint(Value value) {
11768
/// defined and can be used without a dominance violation.
11869
static OpBuilder::InsertPoint computeInsertPoint(ArrayRef<Value> vals) {
11970
assert(!vals.empty() && "expected at least one value");
71+
DominanceInfo domInfo;
12072
OpBuilder::InsertPoint pt = computeInsertPoint(vals.front());
121-
for (Value v : vals.drop_front())
122-
pt = chooseLaterInsertPoint(pt, computeInsertPoint(v));
73+
for (Value v : vals.drop_front()) {
74+
// Choose the "later" insertion point.
75+
OpBuilder::InsertPoint nextPt = computeInsertPoint(v);
76+
if (domInfo.dominates(pt.getBlock(), pt.getPoint(), nextPt.getBlock(),
77+
nextPt.getPoint())) {
78+
// pt is before nextPt => choose nextPt.
79+
pt = nextPt;
80+
} else {
81+
#ifndef NDEBUG
82+
// nextPt should be before pt => choose pt.
83+
// If pt, nextPt are no dominance relationship, then there is no valid
84+
// insertion point at which all given values are defined.
85+
bool dom = domInfo.dominates(nextPt.getBlock(), nextPt.getPoint(),
86+
pt.getBlock(), pt.getPoint());
87+
assert(dom && "unable to find valid insertion point");
88+
#endif // NDEBUG
89+
}
90+
}
12391
return pt;
12492
}
12593

0 commit comments

Comments
 (0)