[llvm] [Attributor] Don't replace `AddrSpaceCast` with `ConstantPointerNull` (PR #126779)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 13 21:36:21 PST 2025
https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/126779
>From 93c20b208695d4d69334c80f2814be7c56213374 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Wed, 12 Feb 2025 12:02:42 -0500
Subject: [PATCH] [Attributor] Don't replace `AddrSpaceCast` with
`ConstantPointerNull`
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
`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.
---
.../Transforms/IPO/AttributorAttributes.cpp | 9 +++++++++
...e-addrspacecast-with-constantpointernull.ll | 18 ++++++++++++++++++
2 files changed, 27 insertions(+)
create mode 100644 llvm/test/Transforms/Attributor/do-not-replace-addrspacecast-with-constantpointernull.ll
diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp
index 17e7fada10827..c067e1e415c63 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)) {
+ if (auto *CE = dyn_cast<ConstantExpr>(&OldV);
+ CE && 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 0000000000000..55b07ae635e68
--- /dev/null
+++ b/llvm/test/Transforms/Attributor/do-not-replace-addrspacecast-with-constantpointernull.ll
@@ -0,0 +1,18 @@
+; 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: [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 4
+; CHECK-NEXT: ret i32 [[TMP2]]
+;
+entry:
+ %icmp = icmp eq ptr addrspace(5) %p5, addrspacecast (ptr null to ptr addrspace(5))
+ %select = select i1 %icmp, ptr %p0, ptr null
+ %load = load i32, ptr %select, align 4
+ ret i32 %load
+}
More information about the llvm-commits
mailing list