diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 1e5145d0e15b..ea4013cb5bbd 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -1422,7 +1422,8 @@ def GlobalOp : CIR_Op<"global", [Symbol, DeclareOpInterfaceMethods:$initial_value, UnitAttr:$constant, OptionalAttr:$alignment, - OptionalAttr:$ast + OptionalAttr:$ast, + OptionalAttr:$section ); let regions = (region AnyRegion:$ctorRegion, AnyRegion:$dtorRegion); let assemblyFormat = [{ diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 936adfa58593..fe0c16df41d9 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -715,10 +715,8 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty, // Emit section information for extern variables. if (D->hasExternalStorage()) { - if (const SectionAttr *SA = D->getAttr()) { - assert(!UnimplementedFeature::setGlobalVarSection()); - llvm_unreachable("section info for extern vars is NYI"); - } + if (const SectionAttr *SA = D->getAttr()) + GV.setSectionAttr(builder.getStringAttr(SA->getName())); } // Handle XCore specific ABI requirements. @@ -1018,9 +1016,8 @@ void CIRGenModule::buildGlobalVarDefinition(const clang::VarDecl *D, // isTypeConstant(D->getType(), true)); // If it is in a read-only section, mark it 'constant'. - if (const SectionAttr *SA = D->getAttr()) { - assert(0 && "not implemented"); - } + if (const SectionAttr *SA = D->getAttr()) + GV.setSectionAttr(builder.getStringAttr(SA->getName())); // TODO(cir): // GV->setAlignment(getContext().getDeclAlign(D).getAsAlign()); diff --git a/clang/lib/CIR/CodeGen/UnimplementedFeatureGuarding.h b/clang/lib/CIR/CodeGen/UnimplementedFeatureGuarding.h index 89a336146f68..bf894fe53acf 100644 --- a/clang/lib/CIR/CodeGen/UnimplementedFeatureGuarding.h +++ b/clang/lib/CIR/CodeGen/UnimplementedFeatureGuarding.h @@ -41,7 +41,6 @@ struct UnimplementedFeature { // Unhandled global/linkage information. static bool unnamedAddr() { return false; } static bool setComdat() { return false; } - static bool setGlobalVarSection() { return false; } static bool setDSOLocal() { return false; } static bool threadLocal() { return false; } static bool setDLLStorageClass() { return false; } diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 8f07d47684be..12548f8b5af9 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -1505,12 +1505,21 @@ class CIRGlobalOpLowering const auto linkage = convertLinkage(op.getLinkage()); const auto symbol = op.getSymName(); const auto loc = op.getLoc(); + std::optional section = op.getSection(); std::optional init = op.getInitialValue(); + SmallVector attributes; + if (section.has_value()) + attributes.push_back(rewriter.getNamedAttr( + "section", rewriter.getStringAttr(section.value()))); + // Check for missing funcionalities. if (!init.has_value()) { rewriter.replaceOpWithNewOp( - op, llvmType, isConst, linkage, symbol, mlir::Attribute()); + op, llvmType, isConst, linkage, symbol, mlir::Attribute(), + /*alignment*/ 0, /*addrSpace*/ 0, + /*dsoLocal*/ false, /*threadLocal*/ false, + /*comdat*/ mlir::SymbolRefAttr(), attributes); return mlir::success(); } @@ -1584,7 +1593,10 @@ class CIRGlobalOpLowering // Rewrite op. rewriter.replaceOpWithNewOp( - op, llvmType, isConst, linkage, symbol, init.value()); + op, llvmType, isConst, linkage, symbol, init.value(), + /*alignment*/ 0, /*addrSpace*/ 0, + /*dsoLocal*/ false, /*threadLocal*/ false, + /*comdat*/ mlir::SymbolRefAttr(), attributes); return mlir::success(); } }; diff --git a/clang/test/CIR/CodeGen/attributes.c b/clang/test/CIR/CodeGen/attributes.c new file mode 100644 index 000000000000..6c8977c3c41e --- /dev/null +++ b/clang/test/CIR/CodeGen/attributes.c @@ -0,0 +1,13 @@ +// 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 -S -emit-llvm %s -o - | FileCheck %s -check-prefix=LLVM + +extern int __attribute__((section(".shared"))) ext; +int getExt() { + 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"