Skip to content

Commit 27588fe

Browse files
authored
[MC] Move MCFragment::Atom to MCSectionMachO::Atoms
Mach-O's `.subsections_via_symbols` mechanism associates a fragment with an atom (a non-temporary defined symbol). The current approach (`MCFragment::Atom`) wastes space for other object file formats. After #95077, `MCFragment::LayoutOrder` is only used by `AttemptToFoldSymbolOffsetDifference`. While it could be removed, we might explore future uses for `LayoutOrder`. @aengelke suggests one use case: move `Atom` into MCSection. This works because Mach-O doesn't support `.subsection`, and `LayoutOrder`, as the index into the fragment list, is unchanged. This patch moves MCFragment::Atom to MCSectionMachO::Atoms. `getAtom` may be called at parse time before `Atoms` is initialized, so a bound checking is needed to keep the hack working. Pull Request: #95341
1 parent 597cde1 commit 27588fe

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

llvm/include/llvm/MC/MCFragment.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ class MCFragment {
6060
/// The data for the section this fragment is in.
6161
MCSection *Parent;
6262

63-
/// The atom this fragment is in, as represented by its defining symbol.
64-
const MCSymbol *Atom = nullptr;
65-
6663
/// The offset of this fragment in its section.
6764
uint64_t Offset = 0;
6865

@@ -96,8 +93,7 @@ class MCFragment {
9693
MCSection *getParent() const { return Parent; }
9794
void setParent(MCSection *Value) { Parent = Value; }
9895

99-
const MCSymbol *getAtom() const { return Atom; }
100-
void setAtom(const MCSymbol *Value) { Atom = Value; }
96+
const MCSymbol *getAtom() const;
10197

10298
unsigned getLayoutOrder() const { return LayoutOrder; }
10399
void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }

llvm/include/llvm/MC/MCSectionMachO.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class MCSectionMachO final : public MCSection {
3232
/// for example.
3333
unsigned Reserved2;
3434

35+
// The defining non-temporary symbol for each fragment.
36+
SmallVector<const MCSymbol *, 0> Atoms;
37+
3538
MCSectionMachO(StringRef Segment, StringRef Section, unsigned TAA,
3639
unsigned reserved2, SectionKind K, MCSymbol *Begin);
3740
friend class MCContext;
@@ -74,6 +77,10 @@ class MCSectionMachO final : public MCSection {
7477
bool useCodeAlign() const override;
7578
bool isVirtualSection() const override;
7679

80+
void allocAtoms();
81+
const MCSymbol *getAtom(size_t I) const;
82+
void setAtom(size_t I, const MCSymbol *Sym);
83+
7784
static bool classof(const MCSection *S) {
7885
return S->getVariant() == SV_MachO;
7986
}

llvm/lib/MC/MCFragment.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/MC/MCExpr.h"
1818
#include "llvm/MC/MCFixup.h"
1919
#include "llvm/MC/MCSection.h"
20+
#include "llvm/MC/MCSectionMachO.h"
2021
#include "llvm/MC/MCSymbol.h"
2122
#include "llvm/MC/MCValue.h"
2223
#include "llvm/Support/Casting.h"
@@ -264,6 +265,10 @@ void MCFragment::destroy() {
264265
}
265266
}
266267

268+
const MCSymbol *MCFragment::getAtom() const {
269+
return cast<MCSectionMachO>(Parent)->getAtom(LayoutOrder);
270+
}
271+
267272
// Debugging methods
268273

269274
namespace llvm {

llvm/lib/MC/MCMachOStreamer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,13 @@ void MCMachOStreamer::finishImpl() {
519519
// Set the fragment atom associations by tracking the last seen atom defining
520520
// symbol.
521521
for (MCSection &Sec : getAssembler()) {
522+
cast<MCSectionMachO>(Sec).allocAtoms();
522523
const MCSymbol *CurrentAtom = nullptr;
524+
size_t I = 0;
523525
for (MCFragment &Frag : Sec) {
524526
if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(&Frag))
525527
CurrentAtom = Symbol;
526-
Frag.setAtom(CurrentAtom);
528+
cast<MCSectionMachO>(Sec).setAtom(I++, CurrentAtom);
527529
}
528530
}
529531

llvm/lib/MC/MCSectionMachO.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,15 @@ Error MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
291291

292292
return Error::success();
293293
}
294+
295+
void MCSectionMachO::allocAtoms() {
296+
auto *L = curFragList();
297+
if (L->Tail)
298+
Atoms.resize(L->Tail->getLayoutOrder() + 1);
299+
}
300+
301+
const MCSymbol *MCSectionMachO::getAtom(size_t I) const {
302+
return I < Atoms.size() ? Atoms[I] : nullptr;
303+
}
304+
305+
void MCSectionMachO::setAtom(size_t I, const MCSymbol *Sym) { Atoms[I] = Sym; }

0 commit comments

Comments
 (0)