From b6df0a65f40384e70c805b2b3a62665cd14001c5 Mon Sep 17 00:00:00 2001 From: Kirill Yansitov <36601354+YazZz1k@users.noreply.github.com> Date: Fri, 2 Feb 2024 04:46:51 +0300 Subject: [PATCH] [CIR][CIRGen] Support for section atttribute (#422) This PR adds support for section("$name") attribute --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 3 ++- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 11 ++++------- .../CIR/CodeGen/UnimplementedFeatureGuarding.h | 1 - .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 16 ++++++++++++++-- clang/test/CIR/CodeGen/attributes.c | 14 ++++++++++++++ 5 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 clang/test/CIR/CodeGen/attributes.c 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 59d88eafee21..40de4f53e32e 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -716,10 +716,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. @@ -1019,9 +1017,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..67b625c11520 --- /dev/null +++ b/clang/test/CIR/CodeGen/attributes.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 -S -emit-llvm %s -o - | FileCheck %s -check-prefix=LLVM +// XFAIL: * + +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"