[llvm-branch-commits] [WIP] IR: Introduce !elf_section_properties for setting section properties. (PR #149260)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Jul 16 23:11:09 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Peter Collingbourne (pcc)
<details>
<summary>Changes</summary>
This new metadata type may be used to set sh_type and sh_entsize on a
global's section.
TODO:
- Add tests.
---
Full diff: https://github.com/llvm/llvm-project/pull/149260.diff
2 Files Affected:
- (modified) llvm/include/llvm/IR/FixedMetadataKinds.def (+1)
- (modified) llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (+37-23)
``````````diff
diff --git a/llvm/include/llvm/IR/FixedMetadataKinds.def b/llvm/include/llvm/IR/FixedMetadataKinds.def
index df572e8791e13..16fd9f4996176 100644
--- a/llvm/include/llvm/IR/FixedMetadataKinds.def
+++ b/llvm/include/llvm/IR/FixedMetadataKinds.def
@@ -53,3 +53,4 @@ LLVM_FIXED_MD_KIND(MD_DIAssignID, "DIAssignID", 38)
LLVM_FIXED_MD_KIND(MD_coro_outside_frame, "coro.outside.frame", 39)
LLVM_FIXED_MD_KIND(MD_mmra, "mmra", 40)
LLVM_FIXED_MD_KIND(MD_noalias_addrspace, "noalias.addrspace", 41)
+LLVM_FIXED_MD_KIND(MD_elf_section_properties, "elf_section_properties", 42)
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 5454cd475f5ed..2c523b58d0de4 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -653,10 +653,11 @@ static StringRef getSectionPrefixForGlobal(SectionKind Kind, bool IsLarge) {
static SmallString<128>
getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind,
Mangler &Mang, const TargetMachine &TM,
- unsigned EntrySize, bool UniqueSectionName,
+ bool UniqueSectionName,
const MachineJumpTableEntry *JTE) {
SmallString<128> Name =
getSectionPrefixForGlobal(Kind, TM.isLargeGlobalValue(GO));
+ unsigned EntrySize = getEntrySizeForKind(Kind);
if (Kind.isMergeableCString()) {
// We also need alignment here.
// FIXME: this is getting the alignment of the character, not the
@@ -790,8 +791,8 @@ calcUniqueIDUpdateFlagsAndSize(const GlobalObject *GO, StringRef SectionName,
// implicitly for this symbol e.g. .rodata.str1.1, then we don't need
// to unique the section as the entry size for this symbol will be
// compatible with implicitly created sections.
- SmallString<128> ImplicitSectionNameStem = getELFSectionNameForGlobal(
- GO, Kind, Mang, TM, EntrySize, false, /*MJTE=*/nullptr);
+ SmallString<128> ImplicitSectionNameStem =
+ getELFSectionNameForGlobal(GO, Kind, Mang, TM, false, /*MJTE=*/nullptr);
if (SymbolMergeable &&
Ctx.isELFImplicitMergeableSectionNamePrefix(SectionName) &&
SectionName.starts_with(ImplicitSectionNameStem))
@@ -802,8 +803,9 @@ calcUniqueIDUpdateFlagsAndSize(const GlobalObject *GO, StringRef SectionName,
return NextUniqueID++;
}
-static std::tuple<StringRef, bool, unsigned>
-getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
+static std::tuple<StringRef, bool, unsigned, unsigned, unsigned>
+getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM,
+ StringRef SectionName, SectionKind Kind) {
StringRef Group = "";
bool IsComdat = false;
unsigned Flags = 0;
@@ -814,7 +816,23 @@ getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
}
if (TM.isLargeGlobalValue(GO))
Flags |= ELF::SHF_X86_64_LARGE;
- return {Group, IsComdat, Flags};
+
+ unsigned Type, EntrySize;
+ if (MDNode *MD = GO->getMetadata(LLVMContext::MD_elf_section_properties)) {
+ Type = cast<ConstantAsMetadata>(MD->getOperand(0))
+ ->getValue()
+ ->getUniqueInteger()
+ .getZExtValue();
+ EntrySize = cast<ConstantAsMetadata>(MD->getOperand(1))
+ ->getValue()
+ ->getUniqueInteger()
+ .getZExtValue();
+ } else {
+ Type = getELFSectionType(SectionName, Kind);
+ EntrySize = getEntrySizeForKind(Kind);
+ }
+
+ return {Group, IsComdat, Flags, Type, EntrySize};
}
static StringRef handlePragmaClangSection(const GlobalObject *GO,
@@ -850,18 +868,18 @@ static MCSection *selectExplicitSectionGlobal(const GlobalObject *GO,
Kind = getELFKindForNamedSection(SectionName, Kind);
unsigned Flags = getELFSectionFlags(Kind, TM.getTargetTriple());
- auto [Group, IsComdat, ExtraFlags] = getGlobalObjectInfo(GO, TM);
+ auto [Group, IsComdat, ExtraFlags, Type, EntrySize] =
+ getGlobalObjectInfo(GO, TM, SectionName, Kind);
Flags |= ExtraFlags;
- unsigned EntrySize = getEntrySizeForKind(Kind);
const unsigned UniqueID = calcUniqueIDUpdateFlagsAndSize(
GO, SectionName, Kind, TM, Ctx, Mang, Flags, EntrySize, NextUniqueID,
Retain, ForceUnique);
const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM);
- MCSectionELF *Section = Ctx.getELFSection(
- SectionName, getELFSectionType(SectionName, Kind), Flags, EntrySize,
- Group, IsComdat, UniqueID, LinkedToSym);
+ MCSectionELF *Section =
+ Ctx.getELFSection(SectionName, Type, Flags, EntrySize, Group, IsComdat,
+ UniqueID, LinkedToSym);
// Make sure that we did not get some other section with incompatible sh_link.
// This should not be possible due to UniqueID code above.
assert(Section->getLinkedToSymbol() == LinkedToSym &&
@@ -899,13 +917,6 @@ static MCSectionELF *selectELFSectionForGlobal(
const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags,
unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol,
const MachineJumpTableEntry *MJTE = nullptr) {
-
- auto [Group, IsComdat, ExtraFlags] = getGlobalObjectInfo(GO, TM);
- Flags |= ExtraFlags;
-
- // Get the section entry size based on the kind.
- unsigned EntrySize = getEntrySizeForKind(Kind);
-
bool UniqueSectionName = false;
unsigned UniqueID = MCSection::NonUniqueID;
if (EmitUniqueSection) {
@@ -916,15 +927,18 @@ static MCSectionELF *selectELFSectionForGlobal(
(*NextUniqueID)++;
}
}
- SmallString<128> Name = getELFSectionNameForGlobal(
- GO, Kind, Mang, TM, EntrySize, UniqueSectionName, MJTE);
+ SmallString<128> Name =
+ getELFSectionNameForGlobal(GO, Kind, Mang, TM, UniqueSectionName, MJTE);
+
+ auto [Group, IsComdat, ExtraFlags, Type, EntrySize] =
+ getGlobalObjectInfo(GO, TM, Name, Kind);
+ Flags |= ExtraFlags;
// Use 0 as the unique ID for execute-only text.
if (Kind.isExecuteOnly())
UniqueID = 0;
- return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
- EntrySize, Group, IsComdat, UniqueID,
- AssociatedSymbol);
+ return Ctx.getELFSection(Name, Type, Flags, EntrySize, Group, IsComdat,
+ UniqueID, AssociatedSymbol);
}
static MCSection *selectELFSectionForGlobal(
``````````
</details>
https://github.com/llvm/llvm-project/pull/149260
More information about the llvm-branch-commits
mailing list