Skip to content

Commit cfc1b92

Browse files
vporpoDanielCChen
authored andcommitted
[SandboxVec][Interval] Implement Interval::comesBefore() (llvm#112026)
This patch implements `Interval::comesBefore(const Interval &Other)` which returns true if this interval is strictly before Other in program order. The function asserts that the intervals are disjoint.
1 parent b7560a0 commit cfc1b92

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Interval.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ template <typename T> class Interval {
127127
}
128128
/// Inequality.
129129
bool operator!=(const Interval &Other) const { return !(*this == Other); }
130+
/// \Returns true if this interval comes before \p Other in program order.
131+
/// This expects disjoint intervals.
132+
bool comesBefore(const Interval &Other) const {
133+
assert(disjoint(Other) && "Expect disjoint intervals!");
134+
return bottom()->comesBefore(Other.top());
135+
}
130136
/// \Returns true if this and \p Other have nothing in common.
131137
bool disjoint(const Interval &Other) const {
132138
if (Other.empty())

llvm/lib/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,7 @@ void DependencyGraph::createNewNodes(const Interval<Instruction> &NewInterval) {
231231
}
232232
// Link new MemDGNode chain with the old one, if any.
233233
if (!DAGInterval.empty()) {
234-
// TODO: Implement Interval::comesBefore() to replace this check.
235-
bool NewIsAbove = NewInterval.bottom()->comesBefore(DAGInterval.top());
236-
assert(
237-
(NewIsAbove || DAGInterval.bottom()->comesBefore(NewInterval.top())) &&
238-
"Expected NewInterval below DAGInterval.");
234+
bool NewIsAbove = NewInterval.comesBefore(DAGInterval);
239235
const auto &TopInterval = NewIsAbove ? NewInterval : DAGInterval;
240236
const auto &BotInterval = NewIsAbove ? DAGInterval : NewInterval;
241237
MemDGNode *LinkTopN =

llvm/unittests/Transforms/Vectorize/SandboxVectorizer/IntervalTest.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ define void @foo(i8 %v0) {
123123
EXPECT_FALSE(Intvl1.disjoint(Intvl3));
124124
EXPECT_TRUE(Intvl1.disjoint(Empty));
125125
}
126+
{
127+
// Check comesBefore().
128+
sandboxir::Interval<sandboxir::Instruction> Intvl1(I0, I0);
129+
sandboxir::Interval<sandboxir::Instruction> Intvl2(I2, I2);
130+
EXPECT_TRUE(Intvl1.comesBefore(Intvl2));
131+
EXPECT_FALSE(Intvl2.comesBefore(Intvl1));
132+
133+
sandboxir::Interval<sandboxir::Instruction> Intvl12(I1, I2);
134+
EXPECT_TRUE(Intvl1.comesBefore(Intvl12));
135+
EXPECT_FALSE(Intvl12.comesBefore(Intvl1));
136+
{
137+
#ifndef NDEBUG
138+
// Check comesBefore() with non-disjoint intervals.
139+
sandboxir::Interval<sandboxir::Instruction> Intvl1(I0, I2);
140+
sandboxir::Interval<sandboxir::Instruction> Intvl2(I2, I2);
141+
EXPECT_DEATH(Intvl1.comesBefore(Intvl2), ".*disjoint.*");
142+
#endif // NDEBUG
143+
}
144+
}
126145
}
127146

128147
// Helper function for returning a vector of instruction pointers from a range

0 commit comments

Comments
 (0)