Skip to content

Commit 48eff91

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 1138983 commit 48eff91

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

llvm/lib/Transforms/IPO/Attributor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ Value *AA::getWithType(Value &V, Type &Ty) {
325325
if (isa<UndefValue>(V))
326326
return UndefValue::get(&Ty);
327327
if (auto *C = dyn_cast<Constant>(&V)) {
328-
if (C->isNullValue())
328+
if (C->isNullValue() && !Ty.isPointerTy())
329329
return Constant::getNullValue(&Ty);
330330
if (C->getType()->isPointerTy() && Ty.isPointerTy())
331331
return ConstantExpr::getPointerCast(C, &Ty);

llvm/test/CodeGen/AMDGPU/addrspacecast.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ define amdgpu_kernel void @use_flat_to_constant_addrspacecast(ptr %ptr) #0 {
217217
; HSA-LABEL: {{^}}cast_0_group_to_flat_addrspacecast:
218218

219219
; HSA-DAG: v_mov_b32_e32 v[[LO:[0-9]+]], 0{{$}}
220-
; HSA-DAG: v_mov_b32_e32 v[[HI:[0-9]+]], 0{{$}}
220+
; HSA-DAG: v_mov_b32_e32 v[[HI:[0-9]+]], s{{[0-9]+}}
221221
; HSA-DAG: v_mov_b32_e32 v[[K:[0-9]+]], 7{{$}}
222222
; HSA: flat_store_dword v[[[LO]]:[[HI]]], v[[K]]
223223
define amdgpu_kernel void @cast_0_group_to_flat_addrspacecast() #0 {
@@ -260,7 +260,7 @@ define amdgpu_kernel void @cast_neg1_flat_to_group_addrspacecast() #0 {
260260
; FIXME: Shouldn't need to enable queue ptr
261261
; HSA-LABEL: {{^}}cast_0_private_to_flat_addrspacecast:
262262
; HSA-DAG: v_mov_b32_e32 v[[LO:[0-9]+]], 0{{$}}
263-
; HSA-DAG: v_mov_b32_e32 v[[HI:[0-9]+]], 0{{$}}
263+
; HSA-DAG: v_mov_b32_e32 v[[HI:[0-9]+]], s{{[0-9]+}}
264264
; HSA-DAG: v_mov_b32_e32 v[[K:[0-9]+]], 7{{$}}
265265
; HSA: flat_store_dword v[[[LO]]:[[HI]]], v[[K]]
266266
define amdgpu_kernel void @cast_0_private_to_flat_addrspacecast() #0 {
Lines changed: 18 additions & 0 deletions
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)