From 25fe0542cd6d64485e7428456d1dc1176c7bc99e Mon Sep 17 00:00:00 2001 From: Wenju He Date: Thu, 23 Oct 2025 03:18:18 +0200 Subject: [PATCH 1/3] [LLVM->SPIRV] Translate `AddrSpaceCast from generic null` to OpGenericCastToPtr 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. --- lib/SPIRV/SPIRVWriter.cpp | 3 ++- test/SpecConstants/keep-tracked-const.ll | 2 +- test/addrspacecast_generic_null.ll | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 test/addrspacecast_generic_null.ll diff --git a/lib/SPIRV/SPIRVWriter.cpp b/lib/SPIRV/SPIRVWriter.cpp index db2d9fa8a..e059126c1 100644 --- a/lib/SPIRV/SPIRVWriter.cpp +++ b/lib/SPIRV/SPIRVWriter.cpp @@ -1672,7 +1672,8 @@ SPIRVValue *LLVMToSPIRVBase::transUnaryInst(UnaryInstruction *U, } else { BOC = OpCrossWorkgroupCastToPtrINTEL; } - } else if (isa(Cast->getPointerOperand())) { + } else if (isa(Cast->getPointerOperand()) && + SrcAddrSpace != SPIRAS_Generic) { SPIRVType *TransTy = transScavengedType(U); return BM->addNullConstant(bcast(TransTy)); } else { diff --git a/test/SpecConstants/keep-tracked-const.ll b/test/SpecConstants/keep-tracked-const.ll index 04a568187..cbd6bedcf 100644 --- a/test/SpecConstants/keep-tracked-const.ll +++ b/test/SpecConstants/keep-tracked-const.ll @@ -17,8 +17,8 @@ target triple = "spir64-unknown-unknown" ; CHECK: Constant [[#Int8]] [[#]] 1 ; CHECK: Constant [[#Int8]] [[#]] 0 -; CHECK: Constant [[#Int32]] [[#]] 0 ; CHECK: Constant [[#Int32]] [[#]] 1 +; CHECK: Constant [[#Int32]] [[#]] 0 ; CHECK-LLVM: %conv17.i = sext i8 1 to i64 diff --git a/test/addrspacecast_generic_null.ll b/test/addrspacecast_generic_null.ll new file mode 100644 index 000000000..6b10c6b2c --- /dev/null +++ b/test/addrspacecast_generic_null.ll @@ -0,0 +1,19 @@ +; RUN: llvm-spirv %s -o %t.spv +; RUN: spirv-dis %t.spv | FileCheck %s + +target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-G1" +target triple = "spir64-unknown-unknown" + +; Test that addrspacecast of generic null pointer generates GenericCastToPtr +; instruction in SPIR-V. + +; CHECK: %_ptr_Workgroup_uchar = OpTypePointer Workgroup %uchar +; CHECK: %[[NULL:[0-9]+]] = OpConstantNull %_ptr_Generic_uchar +; CHECK: %[[CAST:[0-9]+]] = OpGenericCastToPtr %_ptr_Workgroup_uchar %[[NULL]] +; CHECK: OpPtrEqual %bool %[[CAST]] + +define spir_kernel void @bar_generic_null(ptr addrspace(3) %arg) { +pass26: + %expr = icmp eq ptr addrspace(3) addrspacecast (ptr addrspace(4) null to ptr addrspace(3)), %arg + ret void +} From f0ab70ec747c2f372d54e6565748ea54b7f7d44c Mon Sep 17 00:00:00 2001 From: Wenju He Date: Thu, 23 Oct 2025 04:23:32 +0200 Subject: [PATCH 2/3] CHECK-DAG in keep-tracked-const.ll --- test/SpecConstants/keep-tracked-const.ll | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/SpecConstants/keep-tracked-const.ll b/test/SpecConstants/keep-tracked-const.ll index cbd6bedcf..a00944338 100644 --- a/test/SpecConstants/keep-tracked-const.ll +++ b/test/SpecConstants/keep-tracked-const.ll @@ -17,8 +17,8 @@ target triple = "spir64-unknown-unknown" ; CHECK: Constant [[#Int8]] [[#]] 1 ; CHECK: Constant [[#Int8]] [[#]] 0 -; CHECK: Constant [[#Int32]] [[#]] 1 -; CHECK: Constant [[#Int32]] [[#]] 0 +; CHECK-DAG: Constant [[#Int32]] [[#]] 1 +; CHECK-DAG: Constant [[#Int32]] [[#]] 0 ; CHECK-LLVM: %conv17.i = sext i8 1 to i64 From bd0ea464d65f3f1a12f9b94fbbc2e54673ca390f Mon Sep 17 00:00:00 2001 From: Wenju He Date: Thu, 23 Oct 2025 04:27:05 +0200 Subject: [PATCH 3/3] entry --- test/addrspacecast_generic_null.ll | 1 - 1 file changed, 1 deletion(-) diff --git a/test/addrspacecast_generic_null.ll b/test/addrspacecast_generic_null.ll index 6b10c6b2c..9a90e0a7b 100644 --- a/test/addrspacecast_generic_null.ll +++ b/test/addrspacecast_generic_null.ll @@ -13,7 +13,6 @@ target triple = "spir64-unknown-unknown" ; CHECK: OpPtrEqual %bool %[[CAST]] define spir_kernel void @bar_generic_null(ptr addrspace(3) %arg) { -pass26: %expr = icmp eq ptr addrspace(3) addrspacecast (ptr addrspace(4) null to ptr addrspace(3)), %arg ret void }