[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
Mon Oct 27 23:57:12 PDT 2025


https://github.com/wenju-he created https://github.com/llvm/llvm-project/pull/165353

Fix a regression caused by 1ffff05a38c9.
OpenCL spec forbids casting between generic and constant address space pointers.

>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] [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;
+}



More information about the cfe-commits mailing list