Skip to content

Commit 6ae31b3

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 9d7177a commit 6ae31b3

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-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) && isa<ConstantExpr>(OldV)) {
10978+
auto &CE = cast<ConstantExpr>(OldV);
10979+
if (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,39 @@
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: switch i8 0, label %[[BB_2:.*]] [
11+
; CHECK-NEXT: i8 0, label %[[BB_0:.*]]
12+
; CHECK-NEXT: i8 1, label %[[BB_1:.*]]
13+
; CHECK-NEXT: ]
14+
; CHECK: [[BB_0]]:
15+
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 4
16+
; CHECK-NEXT: ret i32 [[TMP2]]
17+
; CHECK: [[BB_1]]:
18+
; CHECK-NEXT: unreachable
19+
; CHECK: [[BB_2]]:
20+
; CHECK-NEXT: unreachable
21+
;
22+
entry:
23+
%0 = icmp eq ptr addrspace(5) %p5, addrspacecast (ptr null to ptr addrspace(5))
24+
%1 = select i1 %0, ptr %p0, ptr null
25+
switch i8 0, label %bb.2 [
26+
i8 0, label %bb.0
27+
i8 1, label %bb.1
28+
]
29+
30+
bb.0: ; preds = %entry
31+
%2 = load i32, ptr %1, align 4
32+
ret i32 %2
33+
34+
bb.1: ; preds = %entry
35+
ret i32 0
36+
37+
bb.2: ; preds = %entry
38+
ret i32 1
39+
}

0 commit comments

Comments
 (0)