Skip to content

Commit 38f7b58

Browse files
committed
[CIR][CIRGen] Support for section atttribute
1 parent d3ef772 commit 38f7b58

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,8 @@ def GlobalOp : CIR_Op<"global", [Symbol, DeclareOpInterfaceMethods<RegionBranchO
14101410
OptionalAttr<AnyAttr>:$initial_value,
14111411
UnitAttr:$constant,
14121412
OptionalAttr<I64Attr>:$alignment,
1413-
OptionalAttr<ASTVarDeclInterface>:$ast
1413+
OptionalAttr<ASTVarDeclInterface>:$ast,
1414+
OptionalAttr<StrAttr>:$section
14141415
);
14151416
let regions = (region AnyRegion:$ctorRegion, AnyRegion:$dtorRegion);
14161417
let assemblyFormat = [{

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -715,10 +715,8 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef MangledName, mlir::Type Ty,
715715

716716
// Emit section information for extern variables.
717717
if (D->hasExternalStorage()) {
718-
if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
719-
assert(!UnimplementedFeature::setGlobalVarSection());
720-
llvm_unreachable("section info for extern vars is NYI");
721-
}
718+
if (const SectionAttr *SA = D->getAttr<SectionAttr>())
719+
GV.setSectionAttr(builder.getStringAttr(SA->getName()));
722720
}
723721

724722
// Handle XCore specific ABI requirements.
@@ -1018,9 +1016,8 @@ void CIRGenModule::buildGlobalVarDefinition(const clang::VarDecl *D,
10181016
// isTypeConstant(D->getType(), true));
10191017

10201018
// If it is in a read-only section, mark it 'constant'.
1021-
if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
1022-
assert(0 && "not implemented");
1023-
}
1019+
if (const SectionAttr *SA = D->getAttr<SectionAttr>())
1020+
GV.setSectionAttr(builder.getStringAttr(SA->getName()));
10241021

10251022
// TODO(cir):
10261023
// GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());

clang/lib/CIR/CodeGen/UnimplementedFeatureGuarding.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ struct UnimplementedFeature {
4141
// Unhandled global/linkage information.
4242
static bool unnamedAddr() { return false; }
4343
static bool setComdat() { return false; }
44-
static bool setGlobalVarSection() { return false; }
4544
static bool setDSOLocal() { return false; }
4645
static bool threadLocal() { return false; }
4746
static bool setDLLStorageClass() { return false; }

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,12 +1423,21 @@ class CIRGlobalOpLowering
14231423
const auto linkage = convertLinkage(op.getLinkage());
14241424
const auto symbol = op.getSymName();
14251425
const auto loc = op.getLoc();
1426+
std::optional<mlir::StringRef> section = op.getSection();
14261427
std::optional<mlir::Attribute> init = op.getInitialValue();
14271428

1429+
SmallVector<mlir::NamedAttribute> attributes;
1430+
if (section.has_value())
1431+
attributes.push_back(rewriter.getNamedAttr(
1432+
"section", rewriter.getStringAttr(section.value())));
1433+
14281434
// Check for missing funcionalities.
14291435
if (!init.has_value()) {
14301436
rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
1431-
op, llvmType, isConst, linkage, symbol, mlir::Attribute());
1437+
op, llvmType, isConst, linkage, symbol, mlir::Attribute(),
1438+
/*alignment*/ 0, /*addrSpace*/ 0,
1439+
/*dsoLocal*/ false, /*threadLocal*/ false,
1440+
/*comdat*/ mlir::SymbolRefAttr(), attributes);
14321441
return mlir::success();
14331442
}
14341443

@@ -1502,7 +1511,10 @@ class CIRGlobalOpLowering
15021511

15031512
// Rewrite op.
15041513
rewriter.replaceOpWithNewOp<mlir::LLVM::GlobalOp>(
1505-
op, llvmType, isConst, linkage, symbol, init.value());
1514+
op, llvmType, isConst, linkage, symbol, init.value(),
1515+
/*alignment*/ 0, /*addrSpace*/ 0,
1516+
/*dsoLocal*/ false, /*threadLocal*/ false,
1517+
/*comdat*/ mlir::SymbolRefAttr(), attributes);
15061518
return mlir::success();
15071519
}
15081520
};

0 commit comments

Comments
 (0)