Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2024

Conversation

vporpo
Copy link
Contributor

@vporpo vporpo commented Oct 3, 2024

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

This patch moves the intrinsic specific code from Utils::isMemDepCandidate()
to a new function: Utils::isMemIntrinsic().
@llvmbot
Copy link
Member

llvmbot commented Oct 3, 2024

@llvm/pr-subscribers-llvm-transforms

Author: vporpo (vporpo)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/111019.diff

3 Files Affected:

  • (modified) llvm/include/llvm/SandboxIR/Utils.h (+16-12)
  • (modified) llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h (+1)
  • (modified) llvm/unittests/SandboxIR/UtilsTest.cpp (+28)
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));
+}

@vporpo vporpo merged commit a1ff427 into llvm:main Oct 3, 2024
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants