Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,8 @@ bool InferAddressSpacesImpl::updateAddressSpace(
} else {
// Otherwise, infer the address space from its pointer operands.
SmallVector<Constant *, 2> ConstantPtrOps;
for (Value *PtrOperand : getPointerOperands(V, *DL, TTI)) {
SmallVector<Value *, 2> PtrOps = getPointerOperands(V, *DL, TTI);
for (Value *PtrOperand : PtrOps) {
auto I = InferredAddrSpace.find(PtrOperand);
unsigned OperandAS;
if (I == InferredAddrSpace.end()) {
Expand Down Expand Up @@ -1079,12 +1080,18 @@ bool InferAddressSpacesImpl::updateAddressSpace(
if (NewAS == FlatAddrSpace)
break;
}

if (NewAS != FlatAddrSpace && NewAS != UninitializedAddressSpace) {
if (any_of(ConstantPtrOps, [=](Constant *C) {
return !isSafeToCastConstAddrSpace(C, NewAS);
}))
NewAS = FlatAddrSpace;
}

// operator(flat const, flat const, ...) -> flat
if (NewAS == UninitializedAddressSpace &&
PtrOps.size() == ConstantPtrOps.size())
NewAS = FlatAddrSpace;
}

unsigned OldAS = InferredAddrSpace.lookup(&V);
Expand Down
22 changes: 22 additions & 0 deletions llvm/test/Transforms/InferAddressSpaces/NVPTX/nullptr.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=infer-address-spaces %s | FileCheck %s

target triple = "nvptx64-unknown-nvidiacl"

define ptr @pr171890(i1 %c) {
; CHECK-LABEL: define ptr @pr171890(
; CHECK-SAME: i1 [[C:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[STACK:%.*]] = alloca i16, align 2
; CHECK-NEXT: [[TMP0:%.*]] = addrspacecast ptr [[STACK]] to ptr addrspace(5)
; CHECK-NEXT: [[TMP1:%.*]] = addrspacecast ptr addrspace(5) [[TMP0]] to ptr
; CHECK-NEXT: [[CONST:%.*]] = getelementptr i8, ptr null, i64 1
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C]], ptr [[TMP1]], ptr [[CONST]]
; CHECK-NEXT: ret ptr [[SEL]]
;
entry:
%stack = alloca i16, align 2
%const = getelementptr i8, ptr null, i64 1
%sel = select i1 %c, ptr %stack, ptr %const
ret ptr %sel
}