[llvm] [SandboxVec][DAG][NFC] Move functions from Utils to DependencyGraph (PR #111031)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 3 10:45:18 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: vporpo (vporpo)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/111031.diff
4 Files Affected:
- (modified) llvm/include/llvm/SandboxIR/Utils.h (+1-25)
- (modified) llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h (+27-3)
- (modified) llvm/unittests/SandboxIR/UtilsTest.cpp (-100)
- (modified) llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp (+101)
``````````diff
diff --git a/llvm/include/llvm/SandboxIR/Utils.h b/llvm/include/llvm/SandboxIR/Utils.h
index 781495d9498173..e50621b4c1228e 100644
--- a/llvm/include/llvm/SandboxIR/Utils.h
+++ b/llvm/include/llvm/SandboxIR/Utils.h
@@ -17,7 +17,6 @@
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/SandboxIR/Instruction.h"
-#include "llvm/SandboxIR/IntrinsicInst.h"
#include <optional>
namespace llvm::sandboxir {
@@ -100,31 +99,8 @@ class Utils {
return false;
return *Diff > 0;
}
-
- static bool isStackSaveOrRestoreIntrinsic(Instruction *I) {
- if (auto *II = dyn_cast<IntrinsicInst>(I)) {
- auto IID = II->getIntrinsicID();
- return IID == Intrinsic::stackrestore || IID == Intrinsic::stacksave;
- }
- return false;
- }
-
- /// \Returns true if intrinsic \p I touches memory. This is used by the
- /// dependency graph.
- static bool isMemIntrinsic(IntrinsicInst *II) {
- auto IID = II->getIntrinsicID();
- return IID != Intrinsic::sideeffect && IID != Intrinsic::pseudoprobe;
- }
-
- /// We consider \p I as a Memory Dependency Candidate instruction if it
- /// reads/write memory or if it has side-effects. This is used by the
- /// dependency graph.
- static bool isMemDepCandidate(Instruction *I) {
- IntrinsicInst *II;
- return I->mayReadOrWriteMemory() &&
- (!(II = dyn_cast<IntrinsicInst>(I)) || isMemIntrinsic(II));
- }
};
+
} // namespace llvm::sandboxir
#endif // LLVM_SANDBOXIR_UTILS_H
diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
index e67051821e85ce..6333f0b81f9c33 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
@@ -25,7 +25,7 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/SandboxIR/Instruction.h"
-#include "llvm/SandboxIR/Utils.h"
+#include "llvm/SandboxIR/IntrinsicInst.h"
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Interval.h"
namespace llvm::sandboxir {
@@ -62,13 +62,37 @@ class DGNode {
/// \Returns true if this is before \p Other in program order.
bool comesBefore(const DGNode *Other) { return I->comesBefore(Other->I); }
+ static bool isStackSaveOrRestoreIntrinsic(Instruction *I) {
+ if (auto *II = dyn_cast<IntrinsicInst>(I)) {
+ auto IID = II->getIntrinsicID();
+ return IID == Intrinsic::stackrestore || IID == Intrinsic::stacksave;
+ }
+ return false;
+ }
+
+ /// \Returns true if intrinsic \p I touches memory. This is used by the
+ /// dependency graph.
+ static bool isMemIntrinsic(IntrinsicInst *I) {
+ auto IID = I->getIntrinsicID();
+ return IID != Intrinsic::sideeffect && IID != Intrinsic::pseudoprobe;
+ }
+
+ /// We consider \p I as a Memory Dependency Candidate instruction if it
+ /// reads/write memory or if it has side-effects. This is used by the
+ /// dependency graph.
+ static bool isMemDepCandidate(Instruction *I) {
+ IntrinsicInst *II;
+ return I->mayReadOrWriteMemory() &&
+ (!(II = dyn_cast<IntrinsicInst>(I)) || isMemIntrinsic(II));
+ }
+
/// \Returns true if \p I is a memory dependency candidate instruction.
static bool isMemDepNodeCandidate(Instruction *I) {
AllocaInst *Alloca;
- return Utils::isMemDepCandidate(I) ||
+ return isMemDepCandidate(I) ||
((Alloca = dyn_cast<AllocaInst>(I)) &&
Alloca->isUsedWithInAlloca()) ||
- Utils::isStackSaveOrRestoreIntrinsic(I);
+ isStackSaveOrRestoreIntrinsic(I);
}
Instruction *getInstruction() const { return I; }
diff --git a/llvm/unittests/SandboxIR/UtilsTest.cpp b/llvm/unittests/SandboxIR/UtilsTest.cpp
index fd7c423fef75a1..a803c2a1cf977c 100644
--- a/llvm/unittests/SandboxIR/UtilsTest.cpp
+++ b/llvm/unittests/SandboxIR/UtilsTest.cpp
@@ -215,103 +215,3 @@ define void @foo(float %arg0, double %arg1, i8 %arg2, i64 %arg3, ptr %arg4) {
EXPECT_EQ(sandboxir::Utils::getNumBits(L2), 8u);
EXPECT_EQ(sandboxir::Utils::getNumBits(L3), 64u);
}
-
-TEST_F(UtilsTest, Instruction_isStackSaveOrRestoreIntrinsic) {
- parseIR(C, R"IR(
-declare void @llvm.sideeffect()
-define void @foo(i8 %v1, ptr %ptr) {
- %add = add i8 %v1, %v1
- %stacksave = call ptr @llvm.stacksave()
- call void @llvm.stackrestore(ptr %stacksave)
- call void @llvm.sideeffect()
- ret void
-}
-)IR");
- llvm::Function *LLVMF = &*M->getFunction("foo");
- sandboxir::Context Ctx(C);
- sandboxir::Function *F = Ctx.createFunction(LLVMF);
- auto *BB = &*F->begin();
- auto It = BB->begin();
- auto *Add = cast<sandboxir::BinaryOperator>(&*It++);
- auto *StackSave = cast<sandboxir::CallInst>(&*It++);
- auto *StackRestore = cast<sandboxir::CallInst>(&*It++);
- auto *Other = cast<sandboxir::CallInst>(&*It++);
- auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
-
- EXPECT_FALSE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(Add));
- EXPECT_TRUE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(StackSave));
- EXPECT_TRUE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(StackRestore));
- EXPECT_FALSE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(Other));
- EXPECT_FALSE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(Ret));
-}
-
-TEST_F(UtilsTest, Instruction_isMemDepCandidate) {
- parseIR(C, R"IR(
-declare void @llvm.fake.use(...)
-declare void @llvm.sideeffect()
-declare void @llvm.pseudoprobe(i64, i64, i32, i64)
-declare void @bar()
-define void @foo(i8 %v1, ptr %ptr) {
- %add0 = add i8 %v1, %v1
- %ld0 = load i8, ptr %ptr
- store i8 %v1, ptr %ptr
- call void @llvm.sideeffect()
- call void @llvm.pseudoprobe(i64 42, i64 1, i32 0, i64 -1)
- call void @llvm.fake.use(ptr %ptr)
- call void @bar()
- ret void
-}
-)IR");
- llvm::Function *LLVMF = &*M->getFunction("foo");
- sandboxir::Context Ctx(C);
- sandboxir::Function *F = Ctx.createFunction(LLVMF);
- auto *BB = &*F->begin();
- auto It = BB->begin();
- auto *Add0 = cast<sandboxir::BinaryOperator>(&*It++);
- auto *Ld0 = cast<sandboxir::LoadInst>(&*It++);
- auto *St0 = cast<sandboxir::StoreInst>(&*It++);
- auto *SideEffect0 = cast<sandboxir::CallInst>(&*It++);
- auto *PseudoProbe0 = cast<sandboxir::CallInst>(&*It++);
- auto *OtherIntrinsic0 = cast<sandboxir::CallInst>(&*It++);
- auto *CallBar = cast<sandboxir::CallInst>(&*It++);
- auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
-
- using Utils = sandboxir::Utils;
-
- EXPECT_FALSE(Utils::isMemDepCandidate(Add0));
- EXPECT_TRUE(Utils::isMemDepCandidate(Ld0));
- EXPECT_TRUE(Utils::isMemDepCandidate(St0));
- EXPECT_FALSE(Utils::isMemDepCandidate(SideEffect0));
- EXPECT_FALSE(Utils::isMemDepCandidate(PseudoProbe0));
- EXPECT_TRUE(Utils::isMemDepCandidate(OtherIntrinsic0));
- EXPECT_TRUE(Utils::isMemDepCandidate(CallBar));
- EXPECT_FALSE(Utils::isMemDepCandidate(Ret));
-}
-
-TEST_F(UtilsTest, Instruction_isMemIntrinsic) {
- parseIR(C, R"IR(
-declare void @llvm.sideeffect()
-declare void @llvm.pseudoprobe(i64)
-declare void @llvm.assume(i1)
-
-define void @foo(ptr %ptr, i1 %cond) {
- call void @llvm.sideeffect()
- call void @llvm.pseudoprobe(i64 42)
- call void @llvm.assume(i1 %cond)
- ret void
-}
-)IR");
- llvm::Function *LLVMF = &*M->getFunction("foo");
- sandboxir::Context Ctx(C);
- sandboxir::Function *F = Ctx.createFunction(LLVMF);
- auto *BB = &*F->begin();
- auto It = BB->begin();
- auto *SideEffect = cast<sandboxir::IntrinsicInst>(&*It++);
- auto *PseudoProbe = cast<sandboxir::IntrinsicInst>(&*It++);
- auto *OtherIntrinsic = cast<sandboxir::IntrinsicInst>(&*It++);
- using Utils = sandboxir::Utils;
-
- EXPECT_FALSE(Utils::isMemIntrinsic(SideEffect));
- EXPECT_FALSE(Utils::isMemIntrinsic(PseudoProbe));
- EXPECT_TRUE(Utils::isMemIntrinsic(OtherIntrinsic));
-}
diff --git a/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp b/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp
index 28ab38ce3d3536..fb8d3780684f80 100644
--- a/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp
+++ b/llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp
@@ -29,6 +29,107 @@ struct DependencyGraphTest : public testing::Test {
}
};
+TEST_F(DependencyGraphTest, isStackSaveOrRestoreIntrinsic) {
+ parseIR(C, R"IR(
+declare void @llvm.sideeffect()
+define void @foo(i8 %v1, ptr %ptr) {
+ %add = add i8 %v1, %v1
+ %stacksave = call ptr @llvm.stacksave()
+ call void @llvm.stackrestore(ptr %stacksave)
+ call void @llvm.sideeffect()
+ ret void
+}
+)IR");
+ llvm::Function *LLVMF = &*M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+ sandboxir::Function *F = Ctx.createFunction(LLVMF);
+ auto *BB = &*F->begin();
+ auto It = BB->begin();
+ auto *Add = cast<sandboxir::BinaryOperator>(&*It++);
+ auto *StackSave = cast<sandboxir::CallInst>(&*It++);
+ auto *StackRestore = cast<sandboxir::CallInst>(&*It++);
+ auto *Other = cast<sandboxir::CallInst>(&*It++);
+ auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
+
+ using DGNode = sandboxir::DGNode;
+ EXPECT_FALSE(DGNode::isStackSaveOrRestoreIntrinsic(Add));
+ EXPECT_TRUE(DGNode::isStackSaveOrRestoreIntrinsic(StackSave));
+ EXPECT_TRUE(DGNode::isStackSaveOrRestoreIntrinsic(StackRestore));
+ EXPECT_FALSE(DGNode::isStackSaveOrRestoreIntrinsic(Other));
+ EXPECT_FALSE(DGNode::isStackSaveOrRestoreIntrinsic(Ret));
+}
+
+TEST_F(DependencyGraphTest, Instruction_isMemDepCandidate) {
+ parseIR(C, R"IR(
+declare void @llvm.fake.use(...)
+declare void @llvm.sideeffect()
+declare void @llvm.pseudoprobe(i64, i64, i32, i64)
+declare void @bar()
+define void @foo(i8 %v1, ptr %ptr) {
+ %add0 = add i8 %v1, %v1
+ %ld0 = load i8, ptr %ptr
+ store i8 %v1, ptr %ptr
+ call void @llvm.sideeffect()
+ call void @llvm.pseudoprobe(i64 42, i64 1, i32 0, i64 -1)
+ call void @llvm.fake.use(ptr %ptr)
+ call void @bar()
+ ret void
+}
+)IR");
+ llvm::Function *LLVMF = &*M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+ sandboxir::Function *F = Ctx.createFunction(LLVMF);
+ auto *BB = &*F->begin();
+ auto It = BB->begin();
+ auto *Add0 = cast<sandboxir::BinaryOperator>(&*It++);
+ auto *Ld0 = cast<sandboxir::LoadInst>(&*It++);
+ auto *St0 = cast<sandboxir::StoreInst>(&*It++);
+ auto *SideEffect0 = cast<sandboxir::CallInst>(&*It++);
+ auto *PseudoProbe0 = cast<sandboxir::CallInst>(&*It++);
+ auto *OtherIntrinsic0 = cast<sandboxir::CallInst>(&*It++);
+ auto *CallBar = cast<sandboxir::CallInst>(&*It++);
+ auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
+
+ using DGNode = sandboxir::DGNode;
+
+ EXPECT_FALSE(DGNode::isMemDepCandidate(Add0));
+ EXPECT_TRUE(DGNode::isMemDepCandidate(Ld0));
+ EXPECT_TRUE(DGNode::isMemDepCandidate(St0));
+ EXPECT_FALSE(DGNode::isMemDepCandidate(SideEffect0));
+ EXPECT_FALSE(DGNode::isMemDepCandidate(PseudoProbe0));
+ EXPECT_TRUE(DGNode::isMemDepCandidate(OtherIntrinsic0));
+ EXPECT_TRUE(DGNode::isMemDepCandidate(CallBar));
+ EXPECT_FALSE(DGNode::isMemDepCandidate(Ret));
+}
+
+TEST_F(DependencyGraphTest, Instruction_isMemIntrinsic) {
+ parseIR(C, R"IR(
+declare void @llvm.sideeffect()
+declare void @llvm.pseudoprobe(i64)
+declare void @llvm.assume(i1)
+
+define void @foo(ptr %ptr, i1 %cond) {
+ call void @llvm.sideeffect()
+ call void @llvm.pseudoprobe(i64 42)
+ call void @llvm.assume(i1 %cond)
+ ret void
+}
+)IR");
+ llvm::Function *LLVMF = &*M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+ sandboxir::Function *F = Ctx.createFunction(LLVMF);
+ auto *BB = &*F->begin();
+ auto It = BB->begin();
+ auto *SideEffect = cast<sandboxir::IntrinsicInst>(&*It++);
+ auto *PseudoProbe = cast<sandboxir::IntrinsicInst>(&*It++);
+ auto *OtherIntrinsic = cast<sandboxir::IntrinsicInst>(&*It++);
+
+ using DGNode = sandboxir::DGNode;
+ EXPECT_FALSE(DGNode::isMemIntrinsic(SideEffect));
+ EXPECT_FALSE(DGNode::isMemIntrinsic(PseudoProbe));
+ EXPECT_TRUE(DGNode::isMemIntrinsic(OtherIntrinsic));
+}
+
TEST_F(DependencyGraphTest, MemDGNode) {
parseIR(C, R"IR(
declare void @llvm.sideeffect()
``````````
</details>
https://github.com/llvm/llvm-project/pull/111031
More information about the llvm-commits
mailing list