[clang] [CIR] Convert null pointer constant types in target lowering (PR #228068)

Arseniy Obolenskiy via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 2 04:02:27 PDT 2026


https://github.com/aobolensk updated https://github.com/llvm/llvm-project/pull/228068

>From c2c09185d02f53c3baa2a779271da285004215f0 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Thu, 1 Oct 2026 15:06:12 +0200
Subject: [PATCH 1/2] [CIR] Convert null pointer constant types in target
 lowering

The pass converted the cir.const result type but not the type inside its `#cir.ptr` attribute, so the verifier rejected address-space null constants
---
 clang/lib/CIR/Dialect/Transforms/TargetLowering.cpp | 10 +++++++---
 clang/test/CIR/CodeGen/spirv-target-lowering-as.cpp |  8 ++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering.cpp
index a34b4a00f36c2c..769df5572f285e 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering.cpp
@@ -73,9 +73,9 @@ class CIRGenericTargetLoweringPattern : public mlir::ConversionPattern {
     mlir::OperationState loweredOpState(op->getLoc(), op->getName());
     loweredOpState.addOperands(operands);
 
-    // Preserve auxiliary metadata verbatim. Convert only inherent TypeAttrs so
-    // address-space-bearing operation semantics (e.g. AllocaOp's allocaType)
-    // stay in sync with the converted result types.
+    // Preserve auxiliary metadata verbatim. Convert only inherent TypeAttrs and
+    // pointer constants so address-space-bearing operation semantics (e.g.
+    // AllocaOp's allocaType) stay in sync with the converted result types.
     loweredOpState.propertiesAttr = op->getPropertiesAsAttribute();
     loweredOpState.addAttributes(op->getDiscardableAttrDictionary().getValue());
 
@@ -101,6 +101,10 @@ class CIRGenericTargetLoweringPattern : public mlir::ConversionPattern {
           if (auto typeAttr = mlir::dyn_cast<mlir::TypeAttr>(attr))
             attr = mlir::TypeAttr::get(
                 typeConverter->convertType(typeAttr.getValue()));
+          else if (auto ptrAttr = mlir::dyn_cast<cir::ConstPtrAttr>(attr))
+            attr = cir::ConstPtrAttr::get(
+                typeConverter->convertType(ptrAttr.getType()),
+                ptrAttr.getValue());
         });
     rewriter.replaceOp(op, loweredOp);
     return mlir::success();
diff --git a/clang/test/CIR/CodeGen/spirv-target-lowering-as.cpp b/clang/test/CIR/CodeGen/spirv-target-lowering-as.cpp
index c8057dd4a5af1e..67b80236d74da4 100644
--- a/clang/test/CIR/CodeGen/spirv-target-lowering-as.cpp
+++ b/clang/test/CIR/CodeGen/spirv-target-lowering-as.cpp
@@ -71,3 +71,11 @@ generic_int *cast_and_global(global_int *ptr) {
 // POST: cir.cast address_space
 // POST-SAME: !cir.ptr<!s32i, target_address_space(1)>
 // POST-SAME: !cir.ptr<!s32i, target_address_space(4)>
+
+generic_int *null_generic() { return nullptr; }
+
+// PRE-LABEL: cir.func {{.*}} @_Z12null_genericv
+// PRE: cir.const #cir.ptr<null> : !cir.ptr<!s32i, lang_address_space(offload_generic)>
+
+// POST-LABEL: cir.func {{.*}} @_Z12null_genericv
+// POST: cir.const #cir.ptr<null> : !cir.ptr<!s32i, target_address_space(4)>

>From 0cd2630fb748d39fac0ca5a15f59078fbedb7cea Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Fri, 2 Oct 2026 09:35:26 +0200
Subject: [PATCH 2/2] add test

---
 clang/test/CIR/CodeGen/amdgpu-target-lowering-as.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/clang/test/CIR/CodeGen/amdgpu-target-lowering-as.cpp b/clang/test/CIR/CodeGen/amdgpu-target-lowering-as.cpp
index 631cd2819c3bd2..9be771e1621a86 100644
--- a/clang/test/CIR/CodeGen/amdgpu-target-lowering-as.cpp
+++ b/clang/test/CIR/CodeGen/amdgpu-target-lowering-as.cpp
@@ -64,3 +64,13 @@ void get_globals() {
 // POST: cir.func {{.*}} @_Z11get_globalsv
 // POST:   cir.get_global @g_global   : !cir.ptr<!s32i, target_address_space(1)>
 // POST:   cir.get_global @g_constant : !cir.ptr<!s32i, target_address_space(4)>
+
+// ---- cir.const: null pointer constant type with lang AS -> target AS ----
+
+int [[clang::opencl_global]] *null_global() { return nullptr; }
+
+// PRE:  cir.func {{.*}} @_Z11null_globalv
+// PRE:    cir.const #cir.ptr<null> : !cir.ptr<!s32i, lang_address_space(offload_global)>
+
+// POST: cir.func {{.*}} @_Z11null_globalv
+// POST:   cir.const #cir.ptr<null> : !cir.ptr<!s32i, target_address_space(1)>



More information about the cfe-commits mailing list