Skip to content

Commit 719e9d3

Browse files
committed
[SandboxVec][DAG] Refactoring: Outline code that looks for mem nodes
1 parent 102c384 commit 719e9d3

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
@@ -154,6 +154,14 @@ class MemDGNode final : public DGNode {
154154
/// Convenience builders for a MemDGNode interval.
155155
class MemDGNodeIntervalBuilder {
156156
public:
157+
/// Scans the instruction chain in \p Intvl top-down, returning the top-most
158+
/// MemDGNode, or nullptr.
159+
static MemDGNode *getTopMemDGNode(const Interval<Instruction> &Intvl,
160+
const DependencyGraph &DAG);
161+
/// Scans the instruction chain in \p Intvl bottom-up, returning the
162+
/// bottom-most MemDGNode, or nullptr.
163+
static MemDGNode *getBotMemDGNode(const Interval<Instruction> &Intvl,
164+
const DependencyGraph &DAG);
157165
/// Given \p Instrs it finds their closest mem nodes in the interval and
158166
/// returns the corresponding mem range. Note: BotN (or its neighboring mem
159167
/// 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
@@ -32,23 +32,43 @@ void DGNode::dump() const {
3232
}
3333
#endif // NDEBUG
3434

35+
MemDGNode *
36+
MemDGNodeIntervalBuilder::getTopMemDGNode(const Interval<Instruction> &Intvl,
37+
const DependencyGraph &DAG) {
38+
Instruction *I = Intvl.top();
39+
Instruction *BeforeI = Intvl.bottom();
40+
// Walk down the chain looking for a mem-dep candidate instruction.
41+
while (!DGNode::isMemDepNodeCandidate(I) && I != BeforeI)
42+
I = I->getNextNode();
43+
if (!DGNode::isMemDepNodeCandidate(I))
44+
return nullptr;
45+
return cast<MemDGNode>(DAG.getNode(I));
46+
}
47+
48+
MemDGNode *
49+
MemDGNodeIntervalBuilder::getBotMemDGNode(const Interval<Instruction> &Intvl,
50+
const DependencyGraph &DAG) {
51+
Instruction *I = Intvl.bottom();
52+
Instruction *AfterI = Intvl.top();
53+
// Walk up the chain looking for a mem-dep candidate instruction.
54+
while (!DGNode::isMemDepNodeCandidate(I) && I != AfterI)
55+
I = I->getPrevNode();
56+
if (!DGNode::isMemDepNodeCandidate(I))
57+
return nullptr;
58+
return cast<MemDGNode>(DAG.getNode(I));
59+
}
60+
3561
Interval<MemDGNode>
3662
MemDGNodeIntervalBuilder::make(const Interval<Instruction> &Instrs,
3763
DependencyGraph &DAG) {
38-
// If top or bottom instructions are not mem-dep candidate nodes we need to
39-
// walk down/up the chain and find the mem-dep ones.
40-
Instruction *MemTopI = Instrs.top();
41-
Instruction *MemBotI = Instrs.bottom();
42-
while (!DGNode::isMemDepNodeCandidate(MemTopI) && MemTopI != MemBotI)
43-
MemTopI = MemTopI->getNextNode();
44-
while (!DGNode::isMemDepNodeCandidate(MemBotI) && MemBotI != MemTopI)
45-
MemBotI = MemBotI->getPrevNode();
64+
auto *TopMemN = getTopMemDGNode(Instrs, DAG);
4665
// If we couldn't find a mem node in range TopN - BotN then it's empty.
47-
if (!DGNode::isMemDepNodeCandidate(MemTopI))
66+
if (TopMemN == nullptr)
4867
return {};
68+
auto *BotMemN = getBotMemDGNode(Instrs, DAG);
69+
assert(BotMemN != nullptr && "TopMemN should be null too!");
4970
// Now that we have the mem-dep nodes, create and return the range.
50-
return Interval<MemDGNode>(cast<MemDGNode>(DAG.getNode(MemTopI)),
51-
cast<MemDGNode>(DAG.getNode(MemBotI)));
71+
return Interval<MemDGNode>(TopMemN, BotMemN);
5272
}
5373

5474
DependencyGraph::DependencyType

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

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

308+
// Check getTopMemDGNode().
309+
using B = sandboxir::MemDGNodeIntervalBuilder;
310+
using InstrInterval = sandboxir::Interval<sandboxir::Instruction>;
311+
EXPECT_EQ(B::getTopMemDGNode(InstrInterval(S0, S0), DAG), S0N);
312+
EXPECT_EQ(B::getTopMemDGNode(InstrInterval(S0, Ret), DAG), S0N);
313+
EXPECT_EQ(B::getTopMemDGNode(InstrInterval(Add0, Add1), DAG), S0N);
314+
EXPECT_EQ(B::getTopMemDGNode(InstrInterval(Add0, Add0), DAG), nullptr);
315+
316+
// Check getBotMemDGNode().
317+
EXPECT_EQ(B::getBotMemDGNode(InstrInterval(S1, S1), DAG), S1N);
318+
EXPECT_EQ(B::getBotMemDGNode(InstrInterval(Add0, S1), DAG), S1N);
319+
EXPECT_EQ(B::getBotMemDGNode(InstrInterval(Add0, Ret), DAG), S1N);
320+
EXPECT_EQ(B::getBotMemDGNode(InstrInterval(Ret, Ret), DAG), nullptr);
321+
308322
// Check empty range.
309323
EXPECT_THAT(sandboxir::MemDGNodeIntervalBuilder::makeEmpty(),
310324
testing::ElementsAre());

0 commit comments

Comments
 (0)