Skip to content

Commit

Permalink
[CIR][CIRGen] Support for section atttribute (llvm#422)
Browse files Browse the repository at this point in the history
This PR adds support for section("$name") attribute
  • Loading branch information
YazZz1k authored and lanza committed Oct 12, 2024
1 parent fdbad24 commit b6df0a6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
3 changes: 2 additions & 1 deletion clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,8 @@ def GlobalOp : CIR_Op<"global", [Symbol, DeclareOpInterfaceMethods<RegionBranchO
OptionalAttr<AnyAttr>:$initial_value,
UnitAttr:$constant,
OptionalAttr<I64Attr>:$alignment,
OptionalAttr<ASTVarDeclInterface>:$ast
OptionalAttr<ASTVarDeclInterface>:$ast,
OptionalAttr<StrAttr>:$section
);
let regions = (region AnyRegion:$ctorRegion, AnyRegion:$dtorRegion);
let assemblyFormat = [{
Expand Down
11 changes: 4 additions & 7 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SectionAttr>()) {
assert(!UnimplementedFeature::setGlobalVarSection());
llvm_unreachable("section info for extern vars is NYI");
}
if (const SectionAttr *SA = D->getAttr<SectionAttr>())
GV.setSectionAttr(builder.getStringAttr(SA->getName()));
}

// Handle XCore specific ABI requirements.
Expand Down Expand Up @@ -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<SectionAttr>()) {
assert(0 && "not implemented");
}
if (const SectionAttr *SA = D->getAttr<SectionAttr>())
GV.setSectionAttr(builder.getStringAttr(SA->getName()));

// TODO(cir):
// GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
Expand Down
1 change: 0 additions & 1 deletion clang/lib/CIR/CodeGen/UnimplementedFeatureGuarding.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
16 changes: 14 additions & 2 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1505,12 +1505,21 @@ class CIRGlobalOpLowering
const auto linkage = convertLinkage(op.getLinkage());
const auto symbol = op.getSymName();
const auto loc = op.getLoc();
std::optional<mlir::StringRef> section = op.getSection();
std::optional<mlir::Attribute> init = op.getInitialValue();

SmallVector<mlir::NamedAttribute> 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<mlir::LLVM::GlobalOp>(
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();
}

Expand Down Expand Up @@ -1584,7 +1593,10 @@ class CIRGlobalOpLowering

// Rewrite op.
rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
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();
}
};
Expand Down
14 changes: 14 additions & 0 deletions clang/test/CIR/CodeGen/attributes.c
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit b6df0a6

Please sign in to comment.