Skip to content

Commit 69c0067

Browse files
authored
[SandboxVec][DAG] Refactoring: Outline code that looks for mem nodes (#111750)
1 parent b77fdf5 commit 69c0067

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ class MemDGNode final : public DGNode {
231231
/// Convenience builders for a MemDGNode interval.
232232
class MemDGNodeIntervalBuilder {
233233
public:
234+
/// Scans the instruction chain in \p Intvl top-down, returning the top-most
235+
/// MemDGNode, or nullptr.
236+
static MemDGNode *getTopMemDGNode(const Interval<Instruction> &Intvl,
237+
const DependencyGraph &DAG);
238+
/// Scans the instruction chain in \p Intvl bottom-up, returning the
239+
/// bottom-most MemDGNode, or nullptr.
240+
static MemDGNode *getBotMemDGNode(const Interval<Instruction> &Intvl,
241+
const DependencyGraph &DAG);
234242
/// Given \p Instrs it finds their closest mem nodes in the interval and
235243
/// returns the corresponding mem range. Note: BotN (or its neighboring mem
236244
/// node) is included in the range.

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,43 @@ void MemDGNode::print(raw_ostream &OS, bool PrintDeps) const {
7878
}
7979
#endif // NDEBUG
8080

81+
MemDGNode *
82+
MemDGNodeIntervalBuilder::getTopMemDGNode(const Interval<Instruction> &Intvl,
83+
const DependencyGraph &DAG) {
84+
Instruction *I = Intvl.top();
85+
Instruction *BeforeI = Intvl.bottom();
86+
// Walk down the chain looking for a mem-dep candidate instruction.
87+
while (!DGNode::isMemDepNodeCandidate(I) && I != BeforeI)
88+
I = I->getNextNode();
89+
if (!DGNode::isMemDepNodeCandidate(I))
90+
return nullptr;
91+
return cast<MemDGNode>(DAG.getNode(I));
92+
}
93+
94+
MemDGNode *
95+
MemDGNodeIntervalBuilder::getBotMemDGNode(const Interval<Instruction> &Intvl,
96+
const DependencyGraph &DAG) {
97+
Instruction *I = Intvl.bottom();
98+
Instruction *AfterI = Intvl.top();
99+
// Walk up the chain looking for a mem-dep candidate instruction.
100+
while (!DGNode::isMemDepNodeCandidate(I) && I != AfterI)
101+
I = I->getPrevNode();
102+
if (!DGNode::isMemDepNodeCandidate(I))
103+
return nullptr;
104+
return cast<MemDGNode>(DAG.getNode(I));
105+
}
106+
81107
Interval<MemDGNode>
82108
MemDGNodeIntervalBuilder::make(const Interval<Instruction> &Instrs,
83109
DependencyGraph &DAG) {
84-
// If top or bottom instructions are not mem-dep candidate nodes we need to
85-
// walk down/up the chain and find the mem-dep ones.
86-
Instruction *MemTopI = Instrs.top();
87-
Instruction *MemBotI = Instrs.bottom();
88-
while (!DGNode::isMemDepNodeCandidate(MemTopI) && MemTopI != MemBotI)
89-
MemTopI = MemTopI->getNextNode();
90-
while (!DGNode::isMemDepNodeCandidate(MemBotI) && MemBotI != MemTopI)
91-
MemBotI = MemBotI->getPrevNode();
110+
auto *TopMemN = getTopMemDGNode(Instrs, DAG);
92111
// If we couldn't find a mem node in range TopN - BotN then it's empty.
93-
if (!DGNode::isMemDepNodeCandidate(MemTopI))
112+
if (TopMemN == nullptr)
94113
return {};
114+
auto *BotMemN = getBotMemDGNode(Instrs, DAG);
115+
assert(BotMemN != nullptr && "TopMemN should be null too!");
95116
// Now that we have the mem-dep nodes, create and return the range.
96-
return Interval<MemDGNode>(cast<MemDGNode>(DAG.getNode(MemTopI)),
97-
cast<MemDGNode>(DAG.getNode(MemBotI)));
117+
return Interval<MemDGNode>(TopMemN, BotMemN);
98118
}
99119

100120
DependencyGraph::DependencyType

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,20 @@ define void @foo(ptr %ptr, i8 %v0, i8 %v1) {
347347
auto *S0N = cast<sandboxir::MemDGNode>(DAG.getNode(S0));
348348
auto *S1N = cast<sandboxir::MemDGNode>(DAG.getNode(S1));
349349

350+
// Check getTopMemDGNode().
351+
using B = sandboxir::MemDGNodeIntervalBuilder;
352+
using InstrInterval = sandboxir::Interval<sandboxir::Instruction>;
353+
EXPECT_EQ(B::getTopMemDGNode(InstrInterval(S0, S0), DAG), S0N);
354+
EXPECT_EQ(B::getTopMemDGNode(InstrInterval(S0, Ret), DAG), S0N);
355+
EXPECT_EQ(B::getTopMemDGNode(InstrInterval(Add0, Add1), DAG), S0N);
356+
EXPECT_EQ(B::getTopMemDGNode(InstrInterval(Add0, Add0), DAG), nullptr);
357+
358+
// Check getBotMemDGNode().
359+
EXPECT_EQ(B::getBotMemDGNode(InstrInterval(S1, S1), DAG), S1N);
360+
EXPECT_EQ(B::getBotMemDGNode(InstrInterval(Add0, S1), DAG), S1N);
361+
EXPECT_EQ(B::getBotMemDGNode(InstrInterval(Add0, Ret), DAG), S1N);
362+
EXPECT_EQ(B::getBotMemDGNode(InstrInterval(Ret, Ret), DAG), nullptr);
363+
350364
// Check empty range.
351365
EXPECT_THAT(sandboxir::MemDGNodeIntervalBuilder::makeEmpty(),
352366
testing::ElementsAre());

0 commit comments

Comments
 (0)