[clang] [CIR] Convert null pointer constant types in target lowering (PR #228068)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 1 06:08:10 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir
Author: Arseniy Obolenskiy (aobolensk)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/228068.diff
2 Files Affected:
- (modified) clang/lib/CIR/Dialect/Transforms/TargetLowering.cpp (+7-3)
- (modified) clang/test/CIR/CodeGen/spirv-target-lowering-as.cpp (+8)
``````````diff
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering.cpp
index a34b4a00f36c2..769df5572f285 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 c8057dd4a5af1..67b80236d74da 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)>
``````````
</details>
https://github.com/llvm/llvm-project/pull/228068
More information about the cfe-commits
mailing list