[clang] [clang][SPIR][SPIRV] Don't generate constant NULL from addrspacecast generic NULL (PR #165353)
Wenju He via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 29 17:54:15 PDT 2025
https://github.com/wenju-he updated https://github.com/llvm/llvm-project/pull/165353
>From 283a02452eb7addb8ff2d8953ab70d3e6970e7bc Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Tue, 28 Oct 2025 07:56:25 +0100
Subject: [PATCH 1/2] [clang][SPIR][SPIRV] Don't generate constant NULL from
addrspacecast generic NULL
Fix a regression caused by 1ffff05a38c9.
OpenCL spec forbids casting between generic and constant address space pointers.
---
clang/lib/CodeGen/Targets/SPIR.cpp | 3 ++-
clang/test/CodeGenOpenCL/nullptr.cl | 14 ++++++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CodeGenOpenCL/nullptr.cl
diff --git a/clang/lib/CodeGen/Targets/SPIR.cpp b/clang/lib/CodeGen/Targets/SPIR.cpp
index 15d0b353d748c..abd049aca0ed7 100644
--- a/clang/lib/CodeGen/Targets/SPIR.cpp
+++ b/clang/lib/CodeGen/Targets/SPIR.cpp
@@ -260,7 +260,8 @@ CommonSPIRTargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
LangAS AS = QT->getUnqualifiedDesugaredType()->isNullPtrType()
? LangAS::Default
: QT->getPointeeType().getAddressSpace();
- if (AS == LangAS::Default || AS == LangAS::opencl_generic)
+ if (AS == LangAS::Default || AS == LangAS::opencl_generic ||
+ AS == LangAS::opencl_constant)
return llvm::ConstantPointerNull::get(PT);
auto &Ctx = CGM.getContext();
diff --git a/clang/test/CodeGenOpenCL/nullptr.cl b/clang/test/CodeGenOpenCL/nullptr.cl
new file mode 100644
index 0000000000000..6d203fce91dde
--- /dev/null
+++ b/clang/test/CodeGenOpenCL/nullptr.cl
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -no-enable-noundef-analysis %s -cl-std=CL2.0 -include opencl-c.h -triple spir64 -emit-llvm -o - | FileCheck %s
+
+// CHECK: @constant_p_NULL =
+// CHECK-SAME: addrspace(1) global ptr addrspace(2) null, align 8
+constant char *constant_p_NULL = NULL;
+
+// CHECK-LABEL: cmp_constant
+// CHECK: icmp eq ptr addrspace(2) %p, null
+char cmp_constant(constant char* p) {
+ if (p != 0)
+ return *p;
+ else
+ return 0;
+}
>From b79a173f0b45cee396ba2df46bd161a77d197678 Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Thu, 30 Oct 2025 08:54:08 +0800
Subject: [PATCH 2/2] Update clang/test/CodeGenOpenCL/nullptr.cl
Co-authored-by: Alexey Bader <alexey.bader at intel.com>
---
clang/test/CodeGenOpenCL/nullptr.cl | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/clang/test/CodeGenOpenCL/nullptr.cl b/clang/test/CodeGenOpenCL/nullptr.cl
index 6d203fce91dde..03e08ed87302d 100644
--- a/clang/test/CodeGenOpenCL/nullptr.cl
+++ b/clang/test/CodeGenOpenCL/nullptr.cl
@@ -7,8 +7,7 @@ constant char *constant_p_NULL = NULL;
// CHECK-LABEL: cmp_constant
// CHECK: icmp eq ptr addrspace(2) %p, null
char cmp_constant(constant char* p) {
- if (p != 0)
+ if (p)
return *p;
- else
- return 0;
+ return 0;
}
More information about the cfe-commits
mailing list