[clang] [CIR][CIRGen] Support for section atttribute on cir.global (PR #188200)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 24 06:35:30 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
@llvm/pr-subscribers-clangir
Author: Chaitanya (skc7)
<details>
<summary>Changes</summary>
Upstreaming clangIR PR: https://github.com/llvm/clangir/pull/422
This PR implement support for `__attribute__((section("name")))` on global variables in ClangIR, matching OGCG behavior.
---
Full diff: https://github.com/llvm/llvm-project/pull/188200.diff
5 Files Affected:
- (modified) clang/include/clang/CIR/Dialect/IR/CIROps.td (+7-1)
- (modified) clang/include/clang/CIR/MissingFeatures.h (-1)
- (modified) clang/lib/CIR/CodeGen/CIRGenModule.cpp (+20-4)
- (modified) clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp (+26-13)
- (added) clang/test/CIR/CodeGen/global-section.c (+14)
``````````diff
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 41858a61480a8..15f98d1d7b9a9 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -2842,7 +2842,9 @@ def CIR_GlobalOp : CIR_Op<"global", [
UnitAttr:$dso_local,
OptionalAttr<CIR_StaticLocalGuardAttr>:$static_local_guard,
OptionalAttr<I64Attr>:$alignment,
- OptionalAttr<ASTVarDeclInterface>:$ast);
+ OptionalAttr<ASTVarDeclInterface>:$ast,
+ OptionalAttr<StrAttr>:$section
+ );
let regions = (region MaxSizedRegion<1>:$ctorRegion,
MaxSizedRegion<1>:$dtorRegion);
@@ -2901,6 +2903,10 @@ def CIR_GlobalOp : CIR_Op<"global", [
void setupRegionInitializedLLVMGlobalOp(
cir::GlobalOp op, mlir::ConversionPatternRewriter &rewriter) const;
+ llvm::SmallVector<mlir::NamedAttribute> lowerGlobalAttributes(
+ cir::GlobalOp op,
+ mlir::ConversionPatternRewriter &rewriter) const;
+
mutable mlir::LLVM::ComdatOp comdatOp = nullptr;
mlir::SymbolRefAttr getComdatAttr(cir::GlobalOp &op,
mlir::OpBuilder &builder) const;
diff --git a/clang/include/clang/CIR/MissingFeatures.h b/clang/include/clang/CIR/MissingFeatures.h
index 68db08a5580ca..740816efc9dc4 100644
--- a/clang/include/clang/CIR/MissingFeatures.h
+++ b/clang/include/clang/CIR/MissingFeatures.h
@@ -31,7 +31,6 @@ struct MissingFeatures {
static bool opGlobalThreadLocal() { return false; }
static bool opGlobalWeakRef() { return false; }
static bool opGlobalUnnamedAddr() { return false; }
- static bool opGlobalSection() { return false; }
static bool opGlobalVisibility() { return false; }
static bool opGlobalDLLImportExport() { return false; }
static bool opGlobalPartition() { return false; }
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index f3ab733bf4c6a..ffa8b35014b79 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -664,11 +664,17 @@ void CIRGenModule::setCommonAttributes(GlobalDecl gd, mlir::Operation *gv) {
void CIRGenModule::setNonAliasAttributes(GlobalDecl gd, mlir::Operation *op) {
setCommonAttributes(gd, op);
+ const Decl *d = gd.getDecl();
+ if (d) {
+ if (auto globalOp = mlir::dyn_cast<cir::GlobalOp>(op)) {
+ if (const auto *sa = d->getAttr<SectionAttr>())
+ globalOp.setSectionAttr(builder.getStringAttr(sa->getName()));
+ }
+ }
+
assert(!cir::MissingFeatures::opGlobalUsedOrCompilerUsed());
- assert(!cir::MissingFeatures::opGlobalSection());
assert(!cir::MissingFeatures::opFuncCPUAndFeaturesAttributes());
assert(!cir::MissingFeatures::opFuncSection());
-
assert(!cir::MissingFeatures::setTargetAttributes());
}
@@ -991,7 +997,11 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
errorNYI(d->getSourceRange(),
"getOrCreateCIRGlobal: MS static data member inline definition");
- assert(!cir::MissingFeatures::opGlobalSection());
+ // Emit section information for extern variables.
+ if (d->hasExternalStorage()) {
+ if (const SectionAttr *sa = d->getAttr<SectionAttr>())
+ gv.setSectionAttr(builder.getStringAttr(sa->getName()));
+ }
gv.setGlobalVisibilityAttr(getGlobalVisibilityAttrFromDecl(d));
// Handle XCore specific ABI requirements.
@@ -1243,7 +1253,13 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
vd->getType().isConstantStorage(astContext,
/*ExcludeCtor=*/true,
/*ExcludeDtor=*/true)));
- assert(!cir::MissingFeatures::opGlobalSection());
+ // If it is in a read-only section, mark it 'constant'.
+ if (const SectionAttr *sa = vd->getAttr<SectionAttr>()) {
+ gv.setSectionAttr(getBuilder().getStringAttr(sa->getName()));
+ const ASTContext::SectionInfo &si = astContext.SectionInfos[sa->getName()];
+ if ((si.SectionFlags & ASTContext::PSF_Write) == 0)
+ gv.setConstant(true);
+ }
// Set CIR linkage and DLL storage class.
gv.setLinkage(linkage);
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 9fa0e720e1591..fd252196c25c3 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -2557,6 +2557,26 @@ mlir::LogicalResult CIRToLLVMGetGlobalOpLowering::matchAndRewrite(
return mlir::success();
}
+llvm::SmallVector<mlir::NamedAttribute>
+CIRToLLVMGlobalOpLowering::lowerGlobalAttributes(
+ cir::GlobalOp op, mlir::ConversionPatternRewriter &rewriter) const {
+ SmallVector<mlir::NamedAttribute> attributes;
+
+ if (auto sectionAttr = op.getSectionAttr())
+ attributes.push_back(rewriter.getNamedAttr("section", sectionAttr));
+
+ mlir::LLVM::VisibilityAttr visibility = mlir::LLVM::VisibilityAttr::get(
+ getContext(), lowerCIRVisibilityToLLVMVisibility(
+ op.getGlobalVisibilityAttr().getValue()));
+ attributes.push_back(rewriter.getNamedAttr("visibility_", visibility));
+
+ if (op->getAttr(CUDAExternallyInitializedAttr::getMnemonic()))
+ attributes.push_back(rewriter.getNamedAttr("externally_initialized",
+ rewriter.getUnitAttr()));
+
+ return attributes;
+}
+
/// Replace CIR global with a region initialized LLVM global and update
/// insertion point to the end of the initializer block.
void CIRToLLVMGlobalOpLowering::setupRegionInitializedLLVMGlobalOp(
@@ -2580,7 +2600,9 @@ void CIRToLLVMGlobalOpLowering::setupRegionInitializedLLVMGlobalOp(
const StringRef symbol = op.getSymName();
mlir::SymbolRefAttr comdatAttr = getComdatAttr(op, rewriter);
- SmallVector<mlir::NamedAttribute> attributes;
+ SmallVector<mlir::NamedAttribute> attributes =
+ lowerGlobalAttributes(op, rewriter);
+
mlir::LLVM::GlobalOp newGlobalOp =
rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
op, llvmType, isConst, linkage, symbol, nullptr, alignment, addrSpace,
@@ -2641,14 +2663,8 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
const uint64_t alignment = op.getAlignment().value_or(0);
const mlir::LLVM::Linkage linkage = convertLinkage(op.getLinkage());
const StringRef symbol = op.getSymName();
- SmallVector<mlir::NamedAttribute> attributes;
-
- // Mark externally_initialized for __device__ and __constant__
- if (auto extInit =
- op->getAttr(CUDAExternallyInitializedAttr::getMnemonic())) {
- attributes.push_back(rewriter.getNamedAttr("externally_initialized",
- rewriter.getUnitAttr()));
- }
+ SmallVector<mlir::NamedAttribute> attributes =
+ lowerGlobalAttributes(op, rewriter);
if (init.has_value()) {
if (mlir::isa<cir::FPAttr, cir::IntAttr, cir::BoolAttr>(init.value())) {
@@ -2679,13 +2695,10 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
}
}
- mlir::LLVM::Visibility visibility =
- lowerCIRVisibilityToLLVMVisibility(op.getGlobalVisibility());
mlir::SymbolRefAttr comdatAttr = getComdatAttr(op, rewriter);
- auto newOp = rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
+ rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
op, llvmType, isConst, linkage, symbol, init.value_or(mlir::Attribute()),
alignment, addrSpace, isDsoLocal, isThreadLocal, comdatAttr, attributes);
- newOp.setVisibility_(visibility);
return mlir::success();
}
diff --git a/clang/test/CIR/CodeGen/global-section.c b/clang/test/CIR/CodeGen/global-section.c
new file mode 100644
index 0000000000000..f82c085b73312
--- /dev/null
+++ b/clang/test/CIR/CodeGen/global-section.c
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVM
+
+extern int __attribute__((section(".shared"))) ext;
+int getExt(void) {
+ return ext;
+}
+// CIR: cir.global "private" external @ext : !s32i {{{.*}}section = ".shared"}
+// LLVM: @ext = external global i32, section ".shared"
+
+int __attribute__((section(".shared"))) glob = 42;
+// CIR: cir.global external @glob = #cir.int<42> : !s32i {{{.*}}section = ".shared"}
+// LLVM: @glob = global i32 42, section ".shared"
``````````
</details>
https://github.com/llvm/llvm-project/pull/188200
More information about the cfe-commits
mailing list