Skip to content

Commit 7b9c6a7

Browse files
authored
[SandboxVec][DAG][NFC] Move functions from Utils to DependencyGraph (llvm#111031)
This patch moves: - Utils::isStackSaveOrRestoreIntrinsic() - Utils::isMemIntrinsic() - Utils::isMemDepCandidate() to DGNode because they no longer require LLVM IR access and are used only by the DAG.
1 parent cdfdc85 commit 7b9c6a7

File tree

4 files changed

+129
-128
lines changed

4 files changed

+129
-128
lines changed

llvm/include/llvm/SandboxIR/Utils.h

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "llvm/Analysis/ScalarEvolution.h"
1818
#include "llvm/Analysis/ValueTracking.h"
1919
#include "llvm/SandboxIR/Instruction.h"
20-
#include "llvm/SandboxIR/IntrinsicInst.h"
2120
#include <optional>
2221

2322
namespace llvm::sandboxir {
@@ -100,31 +99,8 @@ class Utils {
10099
return false;
101100
return *Diff > 0;
102101
}
103-
104-
static bool isStackSaveOrRestoreIntrinsic(Instruction *I) {
105-
if (auto *II = dyn_cast<IntrinsicInst>(I)) {
106-
auto IID = II->getIntrinsicID();
107-
return IID == Intrinsic::stackrestore || IID == Intrinsic::stacksave;
108-
}
109-
return false;
110-
}
111-
112-
/// \Returns true if intrinsic \p I touches memory. This is used by the
113-
/// dependency graph.
114-
static bool isMemIntrinsic(IntrinsicInst *II) {
115-
auto IID = II->getIntrinsicID();
116-
return IID != Intrinsic::sideeffect && IID != Intrinsic::pseudoprobe;
117-
}
118-
119-
/// We consider \p I as a Memory Dependency Candidate instruction if it
120-
/// reads/write memory or if it has side-effects. This is used by the
121-
/// dependency graph.
122-
static bool isMemDepCandidate(Instruction *I) {
123-
IntrinsicInst *II;
124-
return I->mayReadOrWriteMemory() &&
125-
(!(II = dyn_cast<IntrinsicInst>(I)) || isMemIntrinsic(II));
126-
}
127102
};
103+
128104
} // namespace llvm::sandboxir
129105

130106
#endif // LLVM_SANDBOXIR_UTILS_H

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "llvm/ADT/DenseMap.h"
2626
#include "llvm/ADT/iterator_range.h"
2727
#include "llvm/SandboxIR/Instruction.h"
28-
#include "llvm/SandboxIR/Utils.h"
28+
#include "llvm/SandboxIR/IntrinsicInst.h"
2929
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Interval.h"
3030

3131
namespace llvm::sandboxir {
@@ -62,13 +62,37 @@ class DGNode {
6262
/// \Returns true if this is before \p Other in program order.
6363
bool comesBefore(const DGNode *Other) { return I->comesBefore(Other->I); }
6464

65+
static bool isStackSaveOrRestoreIntrinsic(Instruction *I) {
66+
if (auto *II = dyn_cast<IntrinsicInst>(I)) {
67+
auto IID = II->getIntrinsicID();
68+
return IID == Intrinsic::stackrestore || IID == Intrinsic::stacksave;
69+
}
70+
return false;
71+
}
72+
73+
/// \Returns true if intrinsic \p I touches memory. This is used by the
74+
/// dependency graph.
75+
static bool isMemIntrinsic(IntrinsicInst *I) {
76+
auto IID = I->getIntrinsicID();
77+
return IID != Intrinsic::sideeffect && IID != Intrinsic::pseudoprobe;
78+
}
79+
80+
/// We consider \p I as a Memory Dependency Candidate instruction if it
81+
/// reads/write memory or if it has side-effects. This is used by the
82+
/// dependency graph.
83+
static bool isMemDepCandidate(Instruction *I) {
84+
IntrinsicInst *II;
85+
return I->mayReadOrWriteMemory() &&
86+
(!(II = dyn_cast<IntrinsicInst>(I)) || isMemIntrinsic(II));
87+
}
88+
6589
/// \Returns true if \p I is a memory dependency candidate instruction.
6690
static bool isMemDepNodeCandidate(Instruction *I) {
6791
AllocaInst *Alloca;
68-
return Utils::isMemDepCandidate(I) ||
92+
return isMemDepCandidate(I) ||
6993
((Alloca = dyn_cast<AllocaInst>(I)) &&
7094
Alloca->isUsedWithInAlloca()) ||
71-
Utils::isStackSaveOrRestoreIntrinsic(I);
95+
isStackSaveOrRestoreIntrinsic(I);
7296
}
7397

7498
Instruction *getInstruction() const { return I; }

llvm/unittests/SandboxIR/UtilsTest.cpp

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -215,103 +215,3 @@ define void @foo(float %arg0, double %arg1, i8 %arg2, i64 %arg3, ptr %arg4) {
215215
EXPECT_EQ(sandboxir::Utils::getNumBits(L2), 8u);
216216
EXPECT_EQ(sandboxir::Utils::getNumBits(L3), 64u);
217217
}
218-
219-
TEST_F(UtilsTest, Instruction_isStackSaveOrRestoreIntrinsic) {
220-
parseIR(C, R"IR(
221-
declare void @llvm.sideeffect()
222-
define void @foo(i8 %v1, ptr %ptr) {
223-
%add = add i8 %v1, %v1
224-
%stacksave = call ptr @llvm.stacksave()
225-
call void @llvm.stackrestore(ptr %stacksave)
226-
call void @llvm.sideeffect()
227-
ret void
228-
}
229-
)IR");
230-
llvm::Function *LLVMF = &*M->getFunction("foo");
231-
sandboxir::Context Ctx(C);
232-
sandboxir::Function *F = Ctx.createFunction(LLVMF);
233-
auto *BB = &*F->begin();
234-
auto It = BB->begin();
235-
auto *Add = cast<sandboxir::BinaryOperator>(&*It++);
236-
auto *StackSave = cast<sandboxir::CallInst>(&*It++);
237-
auto *StackRestore = cast<sandboxir::CallInst>(&*It++);
238-
auto *Other = cast<sandboxir::CallInst>(&*It++);
239-
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
240-
241-
EXPECT_FALSE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(Add));
242-
EXPECT_TRUE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(StackSave));
243-
EXPECT_TRUE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(StackRestore));
244-
EXPECT_FALSE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(Other));
245-
EXPECT_FALSE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(Ret));
246-
}
247-
248-
TEST_F(UtilsTest, Instruction_isMemDepCandidate) {
249-
parseIR(C, R"IR(
250-
declare void @llvm.fake.use(...)
251-
declare void @llvm.sideeffect()
252-
declare void @llvm.pseudoprobe(i64, i64, i32, i64)
253-
declare void @bar()
254-
define void @foo(i8 %v1, ptr %ptr) {
255-
%add0 = add i8 %v1, %v1
256-
%ld0 = load i8, ptr %ptr
257-
store i8 %v1, ptr %ptr
258-
call void @llvm.sideeffect()
259-
call void @llvm.pseudoprobe(i64 42, i64 1, i32 0, i64 -1)
260-
call void @llvm.fake.use(ptr %ptr)
261-
call void @bar()
262-
ret void
263-
}
264-
)IR");
265-
llvm::Function *LLVMF = &*M->getFunction("foo");
266-
sandboxir::Context Ctx(C);
267-
sandboxir::Function *F = Ctx.createFunction(LLVMF);
268-
auto *BB = &*F->begin();
269-
auto It = BB->begin();
270-
auto *Add0 = cast<sandboxir::BinaryOperator>(&*It++);
271-
auto *Ld0 = cast<sandboxir::LoadInst>(&*It++);
272-
auto *St0 = cast<sandboxir::StoreInst>(&*It++);
273-
auto *SideEffect0 = cast<sandboxir::CallInst>(&*It++);
274-
auto *PseudoProbe0 = cast<sandboxir::CallInst>(&*It++);
275-
auto *OtherIntrinsic0 = cast<sandboxir::CallInst>(&*It++);
276-
auto *CallBar = cast<sandboxir::CallInst>(&*It++);
277-
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
278-
279-
using Utils = sandboxir::Utils;
280-
281-
EXPECT_FALSE(Utils::isMemDepCandidate(Add0));
282-
EXPECT_TRUE(Utils::isMemDepCandidate(Ld0));
283-
EXPECT_TRUE(Utils::isMemDepCandidate(St0));
284-
EXPECT_FALSE(Utils::isMemDepCandidate(SideEffect0));
285-
EXPECT_FALSE(Utils::isMemDepCandidate(PseudoProbe0));
286-
EXPECT_TRUE(Utils::isMemDepCandidate(OtherIntrinsic0));
287-
EXPECT_TRUE(Utils::isMemDepCandidate(CallBar));
288-
EXPECT_FALSE(Utils::isMemDepCandidate(Ret));
289-
}
290-
291-
TEST_F(UtilsTest, Instruction_isMemIntrinsic) {
292-
parseIR(C, R"IR(
293-
declare void @llvm.sideeffect()
294-
declare void @llvm.pseudoprobe(i64)
295-
declare void @llvm.assume(i1)
296-
297-
define void @foo(ptr %ptr, i1 %cond) {
298-
call void @llvm.sideeffect()
299-
call void @llvm.pseudoprobe(i64 42)
300-
call void @llvm.assume(i1 %cond)
301-
ret void
302-
}
303-
)IR");
304-
llvm::Function *LLVMF = &*M->getFunction("foo");
305-
sandboxir::Context Ctx(C);
306-
sandboxir::Function *F = Ctx.createFunction(LLVMF);
307-
auto *BB = &*F->begin();
308-
auto It = BB->begin();
309-
auto *SideEffect = cast<sandboxir::IntrinsicInst>(&*It++);
310-
auto *PseudoProbe = cast<sandboxir::IntrinsicInst>(&*It++);
311-
auto *OtherIntrinsic = cast<sandboxir::IntrinsicInst>(&*It++);
312-
using Utils = sandboxir::Utils;
313-
314-
EXPECT_FALSE(Utils::isMemIntrinsic(SideEffect));
315-
EXPECT_FALSE(Utils::isMemIntrinsic(PseudoProbe));
316-
EXPECT_TRUE(Utils::isMemIntrinsic(OtherIntrinsic));
317-
}

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,107 @@ struct DependencyGraphTest : public testing::Test {
2929
}
3030
};
3131

32+
TEST_F(DependencyGraphTest, isStackSaveOrRestoreIntrinsic) {
33+
parseIR(C, R"IR(
34+
declare void @llvm.sideeffect()
35+
define void @foo(i8 %v1, ptr %ptr) {
36+
%add = add i8 %v1, %v1
37+
%stacksave = call ptr @llvm.stacksave()
38+
call void @llvm.stackrestore(ptr %stacksave)
39+
call void @llvm.sideeffect()
40+
ret void
41+
}
42+
)IR");
43+
llvm::Function *LLVMF = &*M->getFunction("foo");
44+
sandboxir::Context Ctx(C);
45+
sandboxir::Function *F = Ctx.createFunction(LLVMF);
46+
auto *BB = &*F->begin();
47+
auto It = BB->begin();
48+
auto *Add = cast<sandboxir::BinaryOperator>(&*It++);
49+
auto *StackSave = cast<sandboxir::CallInst>(&*It++);
50+
auto *StackRestore = cast<sandboxir::CallInst>(&*It++);
51+
auto *Other = cast<sandboxir::CallInst>(&*It++);
52+
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
53+
54+
using DGNode = sandboxir::DGNode;
55+
EXPECT_FALSE(DGNode::isStackSaveOrRestoreIntrinsic(Add));
56+
EXPECT_TRUE(DGNode::isStackSaveOrRestoreIntrinsic(StackSave));
57+
EXPECT_TRUE(DGNode::isStackSaveOrRestoreIntrinsic(StackRestore));
58+
EXPECT_FALSE(DGNode::isStackSaveOrRestoreIntrinsic(Other));
59+
EXPECT_FALSE(DGNode::isStackSaveOrRestoreIntrinsic(Ret));
60+
}
61+
62+
TEST_F(DependencyGraphTest, Instruction_isMemDepCandidate) {
63+
parseIR(C, R"IR(
64+
declare void @llvm.fake.use(...)
65+
declare void @llvm.sideeffect()
66+
declare void @llvm.pseudoprobe(i64, i64, i32, i64)
67+
declare void @bar()
68+
define void @foo(i8 %v1, ptr %ptr) {
69+
%add0 = add i8 %v1, %v1
70+
%ld0 = load i8, ptr %ptr
71+
store i8 %v1, ptr %ptr
72+
call void @llvm.sideeffect()
73+
call void @llvm.pseudoprobe(i64 42, i64 1, i32 0, i64 -1)
74+
call void @llvm.fake.use(ptr %ptr)
75+
call void @bar()
76+
ret void
77+
}
78+
)IR");
79+
llvm::Function *LLVMF = &*M->getFunction("foo");
80+
sandboxir::Context Ctx(C);
81+
sandboxir::Function *F = Ctx.createFunction(LLVMF);
82+
auto *BB = &*F->begin();
83+
auto It = BB->begin();
84+
auto *Add0 = cast<sandboxir::BinaryOperator>(&*It++);
85+
auto *Ld0 = cast<sandboxir::LoadInst>(&*It++);
86+
auto *St0 = cast<sandboxir::StoreInst>(&*It++);
87+
auto *SideEffect0 = cast<sandboxir::CallInst>(&*It++);
88+
auto *PseudoProbe0 = cast<sandboxir::CallInst>(&*It++);
89+
auto *OtherIntrinsic0 = cast<sandboxir::CallInst>(&*It++);
90+
auto *CallBar = cast<sandboxir::CallInst>(&*It++);
91+
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
92+
93+
using DGNode = sandboxir::DGNode;
94+
95+
EXPECT_FALSE(DGNode::isMemDepCandidate(Add0));
96+
EXPECT_TRUE(DGNode::isMemDepCandidate(Ld0));
97+
EXPECT_TRUE(DGNode::isMemDepCandidate(St0));
98+
EXPECT_FALSE(DGNode::isMemDepCandidate(SideEffect0));
99+
EXPECT_FALSE(DGNode::isMemDepCandidate(PseudoProbe0));
100+
EXPECT_TRUE(DGNode::isMemDepCandidate(OtherIntrinsic0));
101+
EXPECT_TRUE(DGNode::isMemDepCandidate(CallBar));
102+
EXPECT_FALSE(DGNode::isMemDepCandidate(Ret));
103+
}
104+
105+
TEST_F(DependencyGraphTest, Instruction_isMemIntrinsic) {
106+
parseIR(C, R"IR(
107+
declare void @llvm.sideeffect()
108+
declare void @llvm.pseudoprobe(i64)
109+
declare void @llvm.assume(i1)
110+
111+
define void @foo(ptr %ptr, i1 %cond) {
112+
call void @llvm.sideeffect()
113+
call void @llvm.pseudoprobe(i64 42)
114+
call void @llvm.assume(i1 %cond)
115+
ret void
116+
}
117+
)IR");
118+
llvm::Function *LLVMF = &*M->getFunction("foo");
119+
sandboxir::Context Ctx(C);
120+
sandboxir::Function *F = Ctx.createFunction(LLVMF);
121+
auto *BB = &*F->begin();
122+
auto It = BB->begin();
123+
auto *SideEffect = cast<sandboxir::IntrinsicInst>(&*It++);
124+
auto *PseudoProbe = cast<sandboxir::IntrinsicInst>(&*It++);
125+
auto *OtherIntrinsic = cast<sandboxir::IntrinsicInst>(&*It++);
126+
127+
using DGNode = sandboxir::DGNode;
128+
EXPECT_FALSE(DGNode::isMemIntrinsic(SideEffect));
129+
EXPECT_FALSE(DGNode::isMemIntrinsic(PseudoProbe));
130+
EXPECT_TRUE(DGNode::isMemIntrinsic(OtherIntrinsic));
131+
}
132+
32133
TEST_F(DependencyGraphTest, MemDGNode) {
33134
parseIR(C, R"IR(
34135
declare void @llvm.sideeffect()

0 commit comments

Comments
 (0)