[clang] [clang][SPIR][SPIRV] Don't generate constant NULL from addrspacecast generic NULL (PR #165353)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 27 23:57:44 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Wenju He (wenju-he)
<details>
<summary>Changes</summary>
Fix a regression caused by 1ffff05a38c9.
OpenCL spec forbids casting between generic and constant address space pointers.
---
Full diff: https://github.com/llvm/llvm-project/pull/165353.diff
2 Files Affected:
- (modified) clang/lib/CodeGen/Targets/SPIR.cpp (+2-1)
- (added) clang/test/CodeGenOpenCL/nullptr.cl (+14)
``````````diff
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;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/165353
More information about the cfe-commits
mailing list