[llvm] InferAddressSpaces: Add llvm.fake.use baseline tests (PR #109567)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 22 00:02:35 PDT 2024


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/109567

InferAddressSpaces: Add llvm.fake.use baseline tests

InferAddressSpaces: Handle llvm.fake.use

>From a68d948fcd3947a791356b0059da1f34d2da49e9 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Sun, 22 Sep 2024 09:58:16 +0400
Subject: [PATCH 1/2] InferAddressSpaces: Add llvm.fake.use baseline tests

---
 .../Transforms/InferAddressSpaces/fake-use.ll | 105 ++++++++++++++++++
 1 file changed, 105 insertions(+)
 create mode 100644 llvm/test/Transforms/InferAddressSpaces/fake-use.ll

diff --git a/llvm/test/Transforms/InferAddressSpaces/fake-use.ll b/llvm/test/Transforms/InferAddressSpaces/fake-use.ll
new file mode 100644
index 00000000000000..c4bfd161f1c2e5
--- /dev/null
+++ b/llvm/test/Transforms/InferAddressSpaces/fake-use.ll
@@ -0,0 +1,105 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=infer-address-spaces -assume-default-is-flat-addrspace %s | FileCheck %s
+
+declare void @llvm.fake.use(...)
+
+ at gv = internal addrspace(3) global float 0.0, align 4
+
+define void @one_fake_use(ptr addrspace(1) %global.ptr) {
+; CHECK-LABEL: define void @one_fake_use(
+; CHECK-SAME: ptr addrspace(1) [[GLOBAL_PTR:%.*]]) {
+; CHECK-NEXT:    [[CAST0:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR]] to ptr
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[CAST0]])
+; CHECK-NEXT:    ret void
+;
+  %cast0 = addrspacecast ptr addrspace(1) %global.ptr to ptr
+  call void (...) @llvm.fake.use(ptr %cast0)
+  ret void
+}
+
+define void @one_fake_use_repeat_operands(ptr addrspace(1) %global.ptr) {
+; CHECK-LABEL: define void @one_fake_use_repeat_operands(
+; CHECK-SAME: ptr addrspace(1) [[GLOBAL_PTR:%.*]]) {
+; CHECK-NEXT:    [[CAST0:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR]] to ptr
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[CAST0]], ptr [[CAST0]])
+; CHECK-NEXT:    ret void
+;
+  %cast0 = addrspacecast ptr addrspace(1) %global.ptr to ptr
+  call void (...) @llvm.fake.use(ptr %cast0, ptr %cast0)
+  ret void
+}
+
+define void @one_fake_use_refers_original_ptr(ptr addrspace(1) %global.ptr) {
+; CHECK-LABEL: define void @one_fake_use_refers_original_ptr(
+; CHECK-SAME: ptr addrspace(1) [[GLOBAL_PTR:%.*]]) {
+; CHECK-NEXT:    [[CAST0:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR]] to ptr
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[CAST0]], ptr addrspace(1) [[GLOBAL_PTR]])
+; CHECK-NEXT:    ret void
+;
+  %cast0 = addrspacecast ptr addrspace(1) %global.ptr to ptr
+  call void (...) @llvm.fake.use(ptr %cast0, ptr addrspace(1) %global.ptr)
+  ret void
+}
+
+define void @multiple_inferrable_fake_use(ptr addrspace(1) %global.ptr0, ptr addrspace(1) %global.ptr1) {
+; CHECK-LABEL: define void @multiple_inferrable_fake_use(
+; CHECK-SAME: ptr addrspace(1) [[GLOBAL_PTR0:%.*]], ptr addrspace(1) [[GLOBAL_PTR1:%.*]]) {
+; CHECK-NEXT:    [[CAST0:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR0]] to ptr
+; CHECK-NEXT:    [[CAST1:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR1]] to ptr
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[CAST0]], ptr [[CAST1]])
+; CHECK-NEXT:    ret void
+;
+  %cast0 = addrspacecast ptr addrspace(1) %global.ptr0 to ptr
+  %cast1 = addrspacecast ptr addrspace(1) %global.ptr1 to ptr
+  call void (...) @llvm.fake.use(ptr %cast0, ptr %cast1)
+  ret void
+}
+
+define void @multiple_fake_use_one_inferrable(ptr %flat.ptr0, ptr addrspace(1) %global.ptr1) {
+; CHECK-LABEL: define void @multiple_fake_use_one_inferrable(
+; CHECK-SAME: ptr [[FLAT_PTR0:%.*]], ptr addrspace(1) [[GLOBAL_PTR1:%.*]]) {
+; CHECK-NEXT:    [[CAST1:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR1]] to ptr
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[FLAT_PTR0]], ptr [[CAST1]])
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[CAST1]], ptr [[FLAT_PTR0]])
+; CHECK-NEXT:    ret void
+;
+  %cast1 = addrspacecast ptr addrspace(1) %global.ptr1 to ptr
+  call void (...) @llvm.fake.use(ptr %flat.ptr0, ptr %cast1)
+  call void (...) @llvm.fake.use(ptr %cast1, ptr %flat.ptr0)
+  ret void
+}
+
+define void @vector_of_pointers(<2 x ptr addrspace(1)> %global.ptr) {
+; CHECK-LABEL: define void @vector_of_pointers(
+; CHECK-SAME: <2 x ptr addrspace(1)> [[GLOBAL_PTR:%.*]]) {
+; CHECK-NEXT:    [[CAST0:%.*]] = addrspacecast <2 x ptr addrspace(1)> [[GLOBAL_PTR]] to <2 x ptr>
+; CHECK-NEXT:    call void (...) @llvm.fake.use(<2 x ptr> [[CAST0]])
+; CHECK-NEXT:    ret void
+;
+  %cast0 = addrspacecast <2 x ptr addrspace(1)> %global.ptr to <2 x ptr>
+  call void (...) @llvm.fake.use(<2 x ptr> %cast0)
+  ret void
+}
+
+define void @use_global_var() {
+; CHECK-LABEL: define void @use_global_var() {
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr addrspacecast (ptr addrspace(3) @gv to ptr))
+; CHECK-NEXT:    ret void
+;
+  call void (...) @llvm.fake.use(ptr addrspacecast (ptr addrspace(3) @gv to ptr))
+  ret void
+}
+
+define void @use_gep_cast(ptr addrspace(1) %global.ptr) {
+; CHECK-LABEL: define void @use_gep_cast(
+; CHECK-SAME: ptr addrspace(1) [[GLOBAL_PTR:%.*]]) {
+; CHECK-NEXT:    [[CAST0:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR]] to ptr
+; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i8, ptr [[CAST0]], i64 16
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[GEP]], ptr [[CAST0]])
+; CHECK-NEXT:    ret void
+;
+  %cast0 = addrspacecast ptr addrspace(1) %global.ptr to ptr
+  %gep = getelementptr i8, ptr %cast0, i64 16
+  call void (...) @llvm.fake.use(ptr %gep, ptr %cast0)
+  ret void
+}

>From cb2ce0d7dba76f302978ddca555ae43b3e482be8 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Sun, 22 Sep 2024 09:58:50 +0400
Subject: [PATCH 2/2] InferAddressSpaces: Handle llvm.fake.use

---
 .../Transforms/Scalar/InferAddressSpaces.cpp  | 14 ++++++++++
 .../Transforms/InferAddressSpaces/fake-use.ll | 28 +++++++------------
 2 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
index 566cdc51f6e74a..60fd2a286119b3 100644
--- a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
+++ b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
@@ -414,6 +414,10 @@ bool InferAddressSpacesImpl::rewriteIntrinsicOperands(IntrinsicInst *II,
     II->setCalledFunction(NewDecl);
     return true;
   }
+  case Intrinsic::fake_use: {
+    II->replaceUsesOfWith(OldV, NewV);
+    return true;
+  }
   default: {
     Value *Rewrite = TTI->rewriteIntrinsicWithAddressSpace(II, OldV, NewV);
     if (!Rewrite)
@@ -455,6 +459,16 @@ void InferAddressSpacesImpl::collectRewritableIntrinsicOperands(
     appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(1),
                                                  PostorderStack, Visited);
     break;
+  case Intrinsic::fake_use: {
+    for (Value *Op : II->operands()) {
+      if (Op->getType()->isPtrOrPtrVectorTy()) {
+        appendsFlatAddressExpressionToPostorderStack(Op, PostorderStack,
+                                                     Visited);
+      }
+    }
+
+    break;
+  }
   default:
     SmallVector<int, 2> OpIndexes;
     if (TTI->collectFlatAddressOperands(OpIndexes, IID)) {
diff --git a/llvm/test/Transforms/InferAddressSpaces/fake-use.ll b/llvm/test/Transforms/InferAddressSpaces/fake-use.ll
index c4bfd161f1c2e5..ad7f621dc40e85 100644
--- a/llvm/test/Transforms/InferAddressSpaces/fake-use.ll
+++ b/llvm/test/Transforms/InferAddressSpaces/fake-use.ll
@@ -8,8 +8,7 @@ declare void @llvm.fake.use(...)
 define void @one_fake_use(ptr addrspace(1) %global.ptr) {
 ; CHECK-LABEL: define void @one_fake_use(
 ; CHECK-SAME: ptr addrspace(1) [[GLOBAL_PTR:%.*]]) {
-; CHECK-NEXT:    [[CAST0:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR]] to ptr
-; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[CAST0]])
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr addrspace(1) [[GLOBAL_PTR]])
 ; CHECK-NEXT:    ret void
 ;
   %cast0 = addrspacecast ptr addrspace(1) %global.ptr to ptr
@@ -20,8 +19,7 @@ define void @one_fake_use(ptr addrspace(1) %global.ptr) {
 define void @one_fake_use_repeat_operands(ptr addrspace(1) %global.ptr) {
 ; CHECK-LABEL: define void @one_fake_use_repeat_operands(
 ; CHECK-SAME: ptr addrspace(1) [[GLOBAL_PTR:%.*]]) {
-; CHECK-NEXT:    [[CAST0:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR]] to ptr
-; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[CAST0]], ptr [[CAST0]])
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr addrspace(1) [[GLOBAL_PTR]], ptr addrspace(1) [[GLOBAL_PTR]])
 ; CHECK-NEXT:    ret void
 ;
   %cast0 = addrspacecast ptr addrspace(1) %global.ptr to ptr
@@ -32,8 +30,7 @@ define void @one_fake_use_repeat_operands(ptr addrspace(1) %global.ptr) {
 define void @one_fake_use_refers_original_ptr(ptr addrspace(1) %global.ptr) {
 ; CHECK-LABEL: define void @one_fake_use_refers_original_ptr(
 ; CHECK-SAME: ptr addrspace(1) [[GLOBAL_PTR:%.*]]) {
-; CHECK-NEXT:    [[CAST0:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR]] to ptr
-; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[CAST0]], ptr addrspace(1) [[GLOBAL_PTR]])
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr addrspace(1) [[GLOBAL_PTR]], ptr addrspace(1) [[GLOBAL_PTR]])
 ; CHECK-NEXT:    ret void
 ;
   %cast0 = addrspacecast ptr addrspace(1) %global.ptr to ptr
@@ -44,9 +41,7 @@ define void @one_fake_use_refers_original_ptr(ptr addrspace(1) %global.ptr) {
 define void @multiple_inferrable_fake_use(ptr addrspace(1) %global.ptr0, ptr addrspace(1) %global.ptr1) {
 ; CHECK-LABEL: define void @multiple_inferrable_fake_use(
 ; CHECK-SAME: ptr addrspace(1) [[GLOBAL_PTR0:%.*]], ptr addrspace(1) [[GLOBAL_PTR1:%.*]]) {
-; CHECK-NEXT:    [[CAST0:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR0]] to ptr
-; CHECK-NEXT:    [[CAST1:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR1]] to ptr
-; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[CAST0]], ptr [[CAST1]])
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr addrspace(1) [[GLOBAL_PTR0]], ptr addrspace(1) [[GLOBAL_PTR1]])
 ; CHECK-NEXT:    ret void
 ;
   %cast0 = addrspacecast ptr addrspace(1) %global.ptr0 to ptr
@@ -58,9 +53,8 @@ define void @multiple_inferrable_fake_use(ptr addrspace(1) %global.ptr0, ptr add
 define void @multiple_fake_use_one_inferrable(ptr %flat.ptr0, ptr addrspace(1) %global.ptr1) {
 ; CHECK-LABEL: define void @multiple_fake_use_one_inferrable(
 ; CHECK-SAME: ptr [[FLAT_PTR0:%.*]], ptr addrspace(1) [[GLOBAL_PTR1:%.*]]) {
-; CHECK-NEXT:    [[CAST1:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR1]] to ptr
-; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[FLAT_PTR0]], ptr [[CAST1]])
-; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[CAST1]], ptr [[FLAT_PTR0]])
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[FLAT_PTR0]], ptr addrspace(1) [[GLOBAL_PTR1]])
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr addrspace(1) [[GLOBAL_PTR1]], ptr [[FLAT_PTR0]])
 ; CHECK-NEXT:    ret void
 ;
   %cast1 = addrspacecast ptr addrspace(1) %global.ptr1 to ptr
@@ -72,8 +66,7 @@ define void @multiple_fake_use_one_inferrable(ptr %flat.ptr0, ptr addrspace(1) %
 define void @vector_of_pointers(<2 x ptr addrspace(1)> %global.ptr) {
 ; CHECK-LABEL: define void @vector_of_pointers(
 ; CHECK-SAME: <2 x ptr addrspace(1)> [[GLOBAL_PTR:%.*]]) {
-; CHECK-NEXT:    [[CAST0:%.*]] = addrspacecast <2 x ptr addrspace(1)> [[GLOBAL_PTR]] to <2 x ptr>
-; CHECK-NEXT:    call void (...) @llvm.fake.use(<2 x ptr> [[CAST0]])
+; CHECK-NEXT:    call void (...) @llvm.fake.use(<2 x ptr addrspace(1)> [[GLOBAL_PTR]])
 ; CHECK-NEXT:    ret void
 ;
   %cast0 = addrspacecast <2 x ptr addrspace(1)> %global.ptr to <2 x ptr>
@@ -83,7 +76,7 @@ define void @vector_of_pointers(<2 x ptr addrspace(1)> %global.ptr) {
 
 define void @use_global_var() {
 ; CHECK-LABEL: define void @use_global_var() {
-; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr addrspacecast (ptr addrspace(3) @gv to ptr))
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr addrspace(3) @gv)
 ; CHECK-NEXT:    ret void
 ;
   call void (...) @llvm.fake.use(ptr addrspacecast (ptr addrspace(3) @gv to ptr))
@@ -93,9 +86,8 @@ define void @use_global_var() {
 define void @use_gep_cast(ptr addrspace(1) %global.ptr) {
 ; CHECK-LABEL: define void @use_gep_cast(
 ; CHECK-SAME: ptr addrspace(1) [[GLOBAL_PTR:%.*]]) {
-; CHECK-NEXT:    [[CAST0:%.*]] = addrspacecast ptr addrspace(1) [[GLOBAL_PTR]] to ptr
-; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i8, ptr [[CAST0]], i64 16
-; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr [[GEP]], ptr [[CAST0]])
+; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i8, ptr addrspace(1) [[GLOBAL_PTR]], i64 16
+; CHECK-NEXT:    call void (...) @llvm.fake.use(ptr addrspace(1) [[GEP]], ptr addrspace(1) [[GLOBAL_PTR]])
 ; CHECK-NEXT:    ret void
 ;
   %cast0 = addrspacecast ptr addrspace(1) %global.ptr to ptr



More information about the llvm-commits mailing list