[llvm] [SandboxIR][NFC] Move intrinsic code to Utils::isMemIntrinsic() (PR #111019)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 3 09:44:19 PDT 2024


https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/111019

This patch moves the intrinsic specific code from Utils::isMemDepCandidate() to a new function: Utils::isMemIntrinsic().

>From 7153070c5f09b921e8882e463f6101d724063459 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vporpodas at google.com>
Date: Wed, 2 Oct 2024 12:21:36 -0700
Subject: [PATCH] [SandboxIR][NFC] Move intrinsic code to
 Utils::isMemIntrinsic()

This patch moves the intrinsic specific code from Utils::isMemDepCandidate()
to a new function: Utils::isMemIntrinsic().
---
 llvm/include/llvm/SandboxIR/Utils.h           | 28 +++++++++++--------
 .../SandboxVectorizer/DependencyGraph.h       |  1 +
 llvm/unittests/SandboxIR/UtilsTest.cpp        | 28 +++++++++++++++++++
 3 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/llvm/include/llvm/SandboxIR/Utils.h b/llvm/include/llvm/SandboxIR/Utils.h
index 510ae8c10e62f3..781495d9498173 100644
--- a/llvm/include/llvm/SandboxIR/Utils.h
+++ b/llvm/include/llvm/SandboxIR/Utils.h
@@ -17,6 +17,7 @@
 #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 {
@@ -101,24 +102,27 @@ class Utils {
   }
 
   static bool isStackSaveOrRestoreIntrinsic(Instruction *I) {
-    auto *LLVMI = cast<llvm::Instruction>(I->Val);
-    return match(LLVMI,
-                 PatternMatch::m_Intrinsic<llvm::Intrinsic::stackrestore>()) ||
-           match(LLVMI,
-                 PatternMatch::m_Intrinsic<llvm::Intrinsic::stacksave>());
+    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) {
-    auto *LLVMI = cast<llvm::Instruction>(I->Val);
-    return LLVMI->mayReadOrWriteMemory() &&
-           (!isa<llvm::IntrinsicInst>(LLVMI) ||
-            (cast<llvm::IntrinsicInst>(LLVMI)->getIntrinsicID() !=
-                 Intrinsic::sideeffect &&
-             cast<llvm::IntrinsicInst>(LLVMI)->getIntrinsicID() !=
-                 Intrinsic::pseudoprobe));
+    IntrinsicInst *II;
+    return I->mayReadOrWriteMemory() &&
+           (!(II = dyn_cast<IntrinsicInst>(I)) || isMemIntrinsic(II));
   }
 };
 } // namespace llvm::sandboxir
diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
index 5b2089791decb0..0018614fc62367 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h
@@ -61,6 +61,7 @@ class DGNode {
   virtual ~DGNode() = default;
   /// \Returns true if this is before \p Other in program order.
   bool comesBefore(const DGNode *Other) { return I->comesBefore(Other->I); }
+
   /// \Returns true if \p I is a memory dependency candidate instruction.
   static bool isMemDepCandidate(Instruction *I) {
     AllocaInst *Alloca;
diff --git a/llvm/unittests/SandboxIR/UtilsTest.cpp b/llvm/unittests/SandboxIR/UtilsTest.cpp
index 41317e4ab46684..fd7c423fef75a1 100644
--- a/llvm/unittests/SandboxIR/UtilsTest.cpp
+++ b/llvm/unittests/SandboxIR/UtilsTest.cpp
@@ -287,3 +287,31 @@ define void @foo(i8 %v1, ptr %ptr) {
   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));
+}



More information about the llvm-commits mailing list