Skip to content

Commit dcd5a83

Browse files
committed
[Attributor] Don't replace AddrSpaceCast with ConstantPointerNull
`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.
1 parent e9e717f commit dcd5a83

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -10970,6 +10970,15 @@ struct AAPotentialValuesImpl : AAPotentialValues {
1097010970
Value *NewV = getSingleValue(A, *this, getIRPosition(), Values);
1097110971
if (!NewV || NewV == &OldV)
1097210972
continue;
10973+
// FIXME: ConstantPointerNull represents a pointer with value 0, but it
10974+
// doesn’t necessarily mean a nullptr. `ptr addrspace(x) null` is not the
10975+
// same as `addrspacecast (ptr null to ptr addrspace(x))` if the nullptr
10976+
// in AS x is not zero. Therefore, we can't simply replace it.
10977+
if (isa<ConstantPointerNull>(NewV)) {
10978+
if (auto *CE = dyn_cast<ConstantExpr>(&OldV);
10979+
CE->getOpcode() == Instruction::AddrSpaceCast)
10980+
continue;
10981+
}
1097310982
if (getCtxI() &&
1097410983
!AA::isValidAtPosition({*NewV, *getCtxI()}, A.getInfoCache()))
1097510984
continue;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=attributor %s -o - | FileCheck %s
3+
4+
define i32 @bar(ptr %p0, ptr addrspace(5) %p5) {
5+
; CHECK-LABEL: define i32 @bar(
6+
; CHECK-SAME: ptr nofree readonly captures(none) [[P0:%.*]], ptr addrspace(5) nofree readonly [[P5:%.*]]) #[[ATTR0:[0-9]+]] {
7+
; CHECK-NEXT: [[ENTRY:.*:]]
8+
; CHECK-NEXT: [[TMP0:%.*]] = icmp eq ptr addrspace(5) [[P5]], addrspacecast (ptr null to ptr addrspace(5))
9+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[TMP0]], ptr [[P0]], ptr null
10+
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 4
11+
; CHECK-NEXT: ret i32 [[TMP2]]
12+
;
13+
entry:
14+
%icmp = icmp eq ptr addrspace(5) %p5, addrspacecast (ptr null to ptr addrspace(5))
15+
%select = select i1 %icmp, ptr %p0, ptr null
16+
%load = load i32, ptr %select, align 4
17+
ret i32 %load
18+
}

0 commit comments

Comments
 (0)