Skip to content

Commit c4fd801

Browse files
authored
[LLVM->SPIRV] Translate AddrSpaceCast from generic null to OpGenericCastToPtr (#3411)
Some GPU backends materialize non-generic nulls via addrspacecast from generic null, see https://github.com/llvm/llvm-project/blob/14ed67411ce1/clang/lib/CodeGen/Targets/SPIR.cpp#L257 llvm-spirv should preserve the addrspacecast.
1 parent 35827fe commit c4fd801

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,8 @@ SPIRVValue *LLVMToSPIRVBase::transUnaryInst(UnaryInstruction *U,
16721672
} else {
16731673
BOC = OpCrossWorkgroupCastToPtrINTEL;
16741674
}
1675-
} else if (isa<ConstantPointerNull>(Cast->getPointerOperand())) {
1675+
} else if (isa<ConstantPointerNull>(Cast->getPointerOperand()) &&
1676+
SrcAddrSpace != SPIRAS_Generic) {
16761677
SPIRVType *TransTy = transScavengedType(U);
16771678
return BM->addNullConstant(bcast<SPIRVTypePointer>(TransTy));
16781679
} else {

test/SpecConstants/keep-tracked-const.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ target triple = "spir64-unknown-unknown"
1717
; CHECK: Constant [[#Int8]] [[#]] 1
1818
; CHECK: Constant [[#Int8]] [[#]] 0
1919

20-
; CHECK: Constant [[#Int32]] [[#]] 0
21-
; CHECK: Constant [[#Int32]] [[#]] 1
20+
; CHECK-DAG: Constant [[#Int32]] [[#]] 1
21+
; CHECK-DAG: Constant [[#Int32]] [[#]] 0
2222

2323
; CHECK-LLVM: %conv17.i = sext i8 1 to i64
2424

test/addrspacecast_generic_null.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: llvm-spirv %s -o %t.spv
2+
; RUN: spirv-dis %t.spv | FileCheck %s
3+
4+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-G1"
5+
target triple = "spir64-unknown-unknown"
6+
7+
; Test that addrspacecast of generic null pointer generates GenericCastToPtr
8+
; instruction in SPIR-V.
9+
10+
; CHECK: %_ptr_Workgroup_uchar = OpTypePointer Workgroup %uchar
11+
; CHECK: %[[NULL:[0-9]+]] = OpConstantNull %_ptr_Generic_uchar
12+
; CHECK: %[[CAST:[0-9]+]] = OpGenericCastToPtr %_ptr_Workgroup_uchar %[[NULL]]
13+
; CHECK: OpPtrEqual %bool %[[CAST]]
14+
15+
define spir_kernel void @bar_generic_null(ptr addrspace(3) %arg) {
16+
%expr = icmp eq ptr addrspace(3) addrspacecast (ptr addrspace(4) null to ptr addrspace(3)), %arg
17+
ret void
18+
}

0 commit comments

Comments
 (0)