[llvm] [Attributor] Don't replace `AddrSpaceCast` with `ConstantPointerNull` (PR #126779)

via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 11 10:56:04 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Shilei Tian (shiltian)

<details>
<summary>Changes</summary>

`ConstantPointerNull` represents a pointer with value 0, but it doesn’t
necessarily mean a nullptr. `ptr addrspace(x) null` is not the same as
`addrspacecast (ptr null to ptr addrspace(x))` if the nullptr in AS x is not
zero. Therefore, we can't simply replace it.

Fixes #<!-- -->115083.

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


2 Files Affected:

- (modified) llvm/lib/Transforms/IPO/AttributorAttributes.cpp (+9) 
- (added) llvm/test/Transforms/Attributor/do-not-replace-addrspacecast-with-constantpointernull.ll (+39) 


``````````diff
diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
index 17e7fada1082762..143215ec91706b3 100644
--- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
+++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
@@ -10970,6 +10970,15 @@ struct AAPotentialValuesImpl : AAPotentialValues {
       Value *NewV = getSingleValue(A, *this, getIRPosition(), Values);
       if (!NewV || NewV == &OldV)
         continue;
+      // FIXME: ConstantPointerNull represents a pointer with value 0, but it
+      // doesn’t necessarily mean a nullptr. `ptr addrspace(x) null` is not the
+      // same as `addrspacecast (ptr null to ptr addrspace(x))` if the nullptr
+      // in AS x is not zero. Therefore, we can't simply replace it.
+      if (isa<ConstantPointerNull>(NewV) && isa<ConstantExpr>(OldV)) {
+        auto &CE = cast<ConstantExpr>(OldV);
+        if (CE.getOpcode() == Instruction::AddrSpaceCast)
+          continue;
+      }
       if (getCtxI() &&
           !AA::isValidAtPosition({*NewV, *getCtxI()}, A.getInfoCache()))
         continue;
diff --git a/llvm/test/Transforms/Attributor/do-not-replace-addrspacecast-with-constantpointernull.ll b/llvm/test/Transforms/Attributor/do-not-replace-addrspacecast-with-constantpointernull.ll
new file mode 100644
index 000000000000000..77eb5fed81d3cfa
--- /dev/null
+++ b/llvm/test/Transforms/Attributor/do-not-replace-addrspacecast-with-constantpointernull.ll
@@ -0,0 +1,39 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=attributor $s -o - | FileCheck %s
+
+define i32 @bar(ptr %p0, ptr addrspace(5) %p5) {
+; CHECK-LABEL: define i32 @bar(
+; CHECK-SAME: ptr nofree readonly captures(none) [[P0:%.*]], ptr addrspace(5) nofree readonly [[P5:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[TMP0:%.*]] = icmp eq ptr addrspace(5) [[P5]], addrspacecast (ptr null to ptr addrspace(5))
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[TMP0]], ptr [[P0]], ptr null
+; CHECK-NEXT:    switch i8 0, label %[[BB_2:.*]] [
+; CHECK-NEXT:      i8 0, label %[[BB_0:.*]]
+; CHECK-NEXT:      i8 1, label %[[BB_1:.*]]
+; CHECK-NEXT:    ]
+; CHECK:       [[BB_0]]:
+; CHECK-NEXT:    [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 4
+; CHECK-NEXT:    ret i32 [[TMP2]]
+; CHECK:       [[BB_1]]:
+; CHECK-NEXT:    unreachable
+; CHECK:       [[BB_2]]:
+; CHECK-NEXT:    unreachable
+;
+entry:
+  %0 = icmp eq ptr addrspace(5) %p5, addrspacecast (ptr null to ptr addrspace(5))
+  %1 = select i1 %0, ptr %p0, ptr null
+  switch i8 0, label %bb.2 [
+  i8 0, label %bb.0
+  i8 1, label %bb.1
+  ]
+
+bb.0:                                             ; preds = %entry
+  %2 = load i32, ptr %1, align 4
+  ret i32 %2
+
+bb.1:                                             ; preds = %entry
+  ret i32 0
+
+bb.2:                                             ; preds = %entry
+  ret i32 1
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/126779


More information about the llvm-commits mailing list