[clang] [CIR] Reject unlowered language address spaces on CIR globals (PR #223660)
Akimasa Watanuki via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 15 04:30:05 PDT 2026
https://github.com/Men-cotton created https://github.com/llvm/llvm-project/pull/223660
Diagnose a `LangAddressSpaceAttr` on `cir.global` before selecting the LLVM global address space. This keeps standalone global attributes from bypassing pointer conversion and silently falling back to address space zero.
Assisted-by: Codex / GPT-5.6 Sol
>From 6985f0c16e6acfead69885049e5967bee2fb90d8 Mon Sep 17 00:00:00 2001
From: mencotton <mencotton0410 at gmail.com>
Date: Sat, 29 Aug 2026 00:18:19 +0900
Subject: [PATCH] [CIR] Reject unlowered language address spaces on CIR globals
Diagnose a LangAddressSpaceAttr on cir.global before selecting the LLVM global address space. This keeps standalone global attributes from bypassing pointer conversion and silently falling back to address space zero.
Assisted-by: Codex / GPT-5.6 Sol
---
clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 4 ++++
clang/test/CIR/Lowering/unlowered-address-spaces.cir | 9 +++++++++
2 files changed, 13 insertions(+)
create mode 100644 clang/test/CIR/Lowering/unlowered-address-spaces.cir
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 81a1bb2576f0a..e725b18f3ff81 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -3000,6 +3000,10 @@ CIRToLLVMGlobalOpLowering::matchAndRewriteRegionInitializedGlobal(
mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
cir::GlobalOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
+ if (mlir::isa_and_present<cir::LangAddressSpaceAttr>(op.getAddrSpaceAttr()))
+ return op.emitError()
+ << "cannot lower a global with a language address space";
+
// If this global requires non-trivial initialization or destruction,
// that needs to be moved to runtime handlers during LoweringPrepare.
if (!op.getCtorRegion().empty() || !op.getDtorRegion().empty())
diff --git a/clang/test/CIR/Lowering/unlowered-address-spaces.cir b/clang/test/CIR/Lowering/unlowered-address-spaces.cir
new file mode 100644
index 0000000000000..0faf18e5d0067
--- /dev/null
+++ b/clang/test/CIR/Lowering/unlowered-address-spaces.cir
@@ -0,0 +1,9 @@
+// RUN: cir-opt %s --split-input-file --cir-to-llvm -verify-diagnostics
+
+!s32i = !cir.int<s, 32>
+
+module attributes {cir.triple = "spirv64-unknown-unknown"} {
+ // expected-error @below {{cannot lower a global with a language address space}}
+ // expected-error @below {{failed to legalize operation 'cir.global'}}
+ cir.global external lang_address_space(offload_global) @global = #cir.int<1> : !s32i
+}
More information about the cfe-commits
mailing list