Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MC] Add MCFragment allocation helpers #95197

Merged
merged 6 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions llvm/include/llvm/MC/MCContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace llvm {

class CodeViewContext;
class MCAsmInfo;
class MCDataFragment;
class MCInst;
class MCLabel;
class MCObjectFileInfo;
Expand Down Expand Up @@ -345,6 +346,8 @@ class MCContext {
void reportCommon(SMLoc Loc,
std::function<void(SMDiagnostic &, const SourceMgr *)>);

MCDataFragment *allocInitialFragment(MCSection &Sec);

MCSymbol *createSymbolImpl(const StringMapEntry<bool> *Name,
bool IsTemporary);
MCSymbol *createSymbol(StringRef Name, bool AlwaysAddSuffix,
Expand Down Expand Up @@ -437,6 +440,10 @@ class MCContext {
/// Create and return a new MC instruction.
MCInst *createMCInst();

template <typename F, typename... Args> F *allocFragment(Args &&...args) {
return new F(std::forward<Args>(args)...);
}

/// \name Symbol Management
/// @{

Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/MC/MCAssembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,8 +820,11 @@ void MCAssembler::layout(MCAsmLayout &Layout) {
for (MCSection &Sec : *this) {
// Create dummy fragments to eliminate any empty sections, this simplifies
// layout.
if (Sec.empty())
new MCDataFragment(&Sec);
if (Sec.empty()) {
auto *F = getContext().allocFragment<MCDataFragment>();
F->setParent(&Sec);
Sec.addFragment(*F);
}

Sec.setOrdinal(SectionIndex++);
}
Expand Down
34 changes: 14 additions & 20 deletions llvm/lib/MC/MCContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ MCInst *MCContext::createMCInst() {
return new (MCInstAllocator.Allocate()) MCInst;
}

// Allocate the initial MCDataFragment for the begin symbol.
MCDataFragment *MCContext::allocInitialFragment(MCSection &Sec) {
assert(!Sec.curFragList()->Head);
auto *F = allocFragment<MCDataFragment>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the plan to remove the Parent parameter from MCFragment? Otherwise, shouldn't allocFragment<MCDataFragment>(&Sec); work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I plan to remove the Parent parameter.

F->setParent(&Sec);
Sec.addFragment(*F);
return F;
}

//===----------------------------------------------------------------------===//
// Symbol Manipulation
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -497,11 +506,8 @@ MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
MCSectionELF(Section, Type, Flags, K, EntrySize, Group, Comdat, UniqueID,
R, LinkedToSym);

auto *F = new MCDataFragment();
Ret->addFragment(*F);
F->setParent(Ret);
auto *F = allocInitialFragment(*Ret);
R->setFragment(F);

return Ret;
}

Expand Down Expand Up @@ -797,11 +803,8 @@ MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
MCSectionWasm(CachedName, Kind, Flags, GroupSym, UniqueID, Begin);
Entry.second = Result;

auto *F = new MCDataFragment();
Result->addFragment(*F);
F->setParent(Result);
auto *F = allocInitialFragment(*Result);
Begin->setFragment(F);

return Result;
}

Expand Down Expand Up @@ -863,10 +866,7 @@ MCSectionXCOFF *MCContext::getXCOFFSection(

Entry.second = Result;

auto *F = new MCDataFragment();
Result->addFragment(*F);
F->setParent(Result);

auto *F = allocInitialFragment(*Result);
if (Begin)
Begin->setFragment(F);

Expand All @@ -886,10 +886,7 @@ MCSectionSPIRV *MCContext::getSPIRVSection() {
MCSectionSPIRV *Result = new (SPIRVAllocator.Allocate())
MCSectionSPIRV(SectionKind::getText(), Begin);

auto *F = new MCDataFragment();
Result->addFragment(*F);
F->setParent(Result);

allocInitialFragment(*Result);
return Result;
}

Expand All @@ -909,10 +906,7 @@ MCSectionDXContainer *MCContext::getDXContainerSection(StringRef Section,
new (DXCAllocator.Allocate()) MCSectionDXContainer(Name, K, nullptr);

// The first fragment will store the header
auto *F = new MCDataFragment();
MapIt->second->addFragment(*F);
F->setParent(MapIt->second);

allocInitialFragment(*MapIt->second);
return MapIt->second;
}

Expand Down
9 changes: 5 additions & 4 deletions llvm/lib/MC/MCELFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ void MCELFStreamer::emitInstToData(const MCInst &Inst,
// When not in a bundle-locked group and the -mc-relax-all flag is used,
// we create a new temporary fragment which will be later merged into
// the current fragment.
DF = new MCDataFragment();
DF = getContext().allocFragment<MCDataFragment>();
else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst()) {
// If we are bundle-locked, we re-use the current fragment.
// The bundle-locking directive ensures this is a new data fragment.
Expand All @@ -596,13 +596,14 @@ void MCELFStreamer::emitInstToData(const MCInst &Inst,
// Optimize memory usage by emitting the instruction to a
// MCCompactEncodedInstFragment when not in a bundle-locked group and
// there are no fixups registered.
MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
MCCompactEncodedInstFragment *CEIF =
getContext().allocFragment<MCCompactEncodedInstFragment>();
insert(CEIF);
CEIF->getContents().append(Code.begin(), Code.end());
CEIF->setHasInstructions(STI);
return;
} else {
DF = new MCDataFragment();
DF = getContext().allocFragment<MCDataFragment>();
insert(DF);
}
if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
Expand Down Expand Up @@ -661,7 +662,7 @@ void MCELFStreamer::emitBundleLock(bool AlignToEnd) {

if (getAssembler().getRelaxAll() && !isBundleLocked()) {
// TODO: drop the lock state and set directly in the fragment
MCDataFragment *DF = new MCDataFragment();
MCDataFragment *DF = getContext().allocFragment<MCDataFragment>();
BundleGroups.push_back(DF);
}

Expand Down
16 changes: 10 additions & 6 deletions llvm/lib/MC/MCMachOStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void MCMachOStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
// We have to create a new fragment if this is an atom defining symbol,
// fragments cannot span atoms.
if (getAssembler().isSymbolLinkerVisible(*Symbol))
insert(new MCDataFragment());
insert(getContext().allocFragment<MCDataFragment>());

MCObjectStreamer::emitLabel(Symbol, Loc);

Expand Down Expand Up @@ -553,7 +553,9 @@ void MCMachOStreamer::finalizeCGProfile() {
MCSection *CGProfileSection = Asm.getContext().getMachOSection(
"__LLVM", "__cg_profile", 0, SectionKind::getMetadata());
Asm.registerSection(*CGProfileSection);
auto *Frag = new MCDataFragment(CGProfileSection);
auto *Frag = getContext().allocFragment<MCDataFragment>();
Frag->setParent(CGProfileSection);
CGProfileSection->addFragment(*Frag);
// For each entry, reserve space for 2 32-bit indices and a 64-bit count.
size_t SectionBytes =
Asm.CGProfile.size() * (2 * sizeof(uint32_t) + sizeof(uint64_t));
Expand All @@ -566,9 +568,9 @@ MCStreamer *llvm::createMachOStreamer(MCContext &Context,
std::unique_ptr<MCCodeEmitter> &&CE,
bool DWARFMustBeAtTheEnd,
bool LabelSections) {
MCMachOStreamer *S =
new MCMachOStreamer(Context, std::move(MAB), std::move(OW), std::move(CE),
DWARFMustBeAtTheEnd, LabelSections);
MCMachOStreamer *S = Context.allocFragment<MCMachOStreamer>(
aengelke marked this conversation as resolved.
Show resolved Hide resolved
Context, std::move(MAB), std::move(OW), std::move(CE),
DWARFMustBeAtTheEnd, LabelSections);
const Triple &Target = Context.getTargetTriple();
S->emitVersionForTarget(
Target, Context.getObjectFileInfo()->getSDKVersion(),
Expand All @@ -593,7 +595,9 @@ void MCMachOStreamer::createAddrSigSection() {
MCSection *AddrSigSection =
Asm.getContext().getObjectFileInfo()->getAddrSigSection();
Asm.registerSection(*AddrSigSection);
auto *Frag = new MCDataFragment(AddrSigSection);
auto *Frag = getContext().allocFragment<MCDataFragment>();
Frag->setParent(AddrSigSection);
AddrSigSection->addFragment(*Frag);
// We will generate a series of pointer-sized symbol relocations at offset
// 0x0. Set the section size to be large enough to contain a single pointer
// (instead of emitting a zero-sized section) so these relocations are
Expand Down
29 changes: 18 additions & 11 deletions llvm/lib/MC/MCObjectStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ MCDataFragment *
MCObjectStreamer::getOrCreateDataFragment(const MCSubtargetInfo *STI) {
MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
if (!F || !canReuseDataFragment(*F, *Assembler, STI)) {
F = new MCDataFragment();
F = getContext().allocFragment<MCDataFragment>();
insert(F);
}
return F;
Expand Down Expand Up @@ -343,7 +343,7 @@ void MCObjectStreamer::emitULEB128Value(const MCExpr *Value) {
emitULEB128IntValue(IntValue);
return;
}
insert(new MCLEBFragment(*Value, false));
insert(getContext().allocFragment<MCLEBFragment>(*Value, false));
}

void MCObjectStreamer::emitSLEB128Value(const MCExpr *Value) {
Expand All @@ -352,7 +352,7 @@ void MCObjectStreamer::emitSLEB128Value(const MCExpr *Value) {
emitSLEB128IntValue(IntValue);
return;
}
insert(new MCLEBFragment(*Value, true));
insert(getContext().allocFragment<MCLEBFragment>(*Value, true));
}

void MCObjectStreamer::emitWeakReference(MCSymbol *Alias,
Expand Down Expand Up @@ -470,7 +470,8 @@ void MCObjectStreamer::emitInstToFragment(const MCInst &Inst,

// Always create a new, separate fragment here, because its size can change
// during relaxation.
MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI);
MCRelaxableFragment *IF =
getContext().allocFragment<MCRelaxableFragment>(Inst, STI);
insert(IF);

SmallString<128> Code;
Expand Down Expand Up @@ -544,7 +545,8 @@ void MCObjectStreamer::emitDwarfAdvanceLineAddr(int64_t LineDelta,
return;
}
const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel, SMLoc());
insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta));
insert(getContext().allocFragment<MCDwarfLineAddrFragment>(LineDelta,
*AddrDelta));
}

void MCObjectStreamer::emitDwarfLineEndEntry(MCSection *Section,
Expand All @@ -569,7 +571,8 @@ void MCObjectStreamer::emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
const MCSymbol *Label,
SMLoc Loc) {
const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel, Loc);
insert(new MCDwarfCallFrameFragment(*AddrDelta, nullptr));
insert(getContext().allocFragment<MCDwarfCallFrameFragment>(*AddrDelta,
nullptr));
}

void MCObjectStreamer::emitCVLocDirective(unsigned FunctionId, unsigned FileNo,
Expand Down Expand Up @@ -640,7 +643,8 @@ void MCObjectStreamer::emitValueToAlignment(Align Alignment, int64_t Value,
unsigned MaxBytesToEmit) {
if (MaxBytesToEmit == 0)
MaxBytesToEmit = Alignment.value();
insert(new MCAlignFragment(Alignment, Value, ValueSize, MaxBytesToEmit));
insert(getContext().allocFragment<MCAlignFragment>(
Alignment, Value, ValueSize, MaxBytesToEmit));

// Update the maximum alignment on the current section if necessary.
MCSection *CurSec = getCurrentSectionOnly();
Expand All @@ -657,7 +661,7 @@ void MCObjectStreamer::emitCodeAlignment(Align Alignment,
void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset,
unsigned char Value,
SMLoc Loc) {
insert(new MCOrgFragment(*Offset, Value, Loc));
insert(getContext().allocFragment<MCOrgFragment>(*Offset, Value, Loc));
}

// Associate DTPRel32 fixup with data and resize data area
Expand Down Expand Up @@ -844,7 +848,8 @@ void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
flushPendingLabels(DF, DF->getContents().size());

assert(getCurrentSectionOnly() && "need a section");
insert(new MCFillFragment(FillValue, 1, NumBytes, Loc));
insert(
getContext().allocFragment<MCFillFragment>(FillValue, 1, NumBytes, Loc));
}

void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
Expand Down Expand Up @@ -874,7 +879,8 @@ void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
flushPendingLabels(DF, DF->getContents().size());

assert(getCurrentSectionOnly() && "need a section");
insert(new MCFillFragment(Expr, Size, NumValues, Loc));
insert(
getContext().allocFragment<MCFillFragment>(Expr, Size, NumValues, Loc));
}

void MCObjectStreamer::emitNops(int64_t NumBytes, int64_t ControlledNopLength,
Expand All @@ -885,7 +891,8 @@ void MCObjectStreamer::emitNops(int64_t NumBytes, int64_t ControlledNopLength,

assert(getCurrentSectionOnly() && "need a section");

insert(new MCNopsFragment(NumBytes, ControlledNopLength, Loc, STI));
insert(getContext().allocFragment<MCNopsFragment>(
NumBytes, ControlledNopLength, Loc, STI));
}

void MCObjectStreamer::emitFileDirective(StringRef Filename) {
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/MC/MCWinCOFFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void MCWinCOFFStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) {
getAssembler().registerSection(*SXData);
SXData->ensureMinAlignment(Align(4));

new MCSymbolIdFragment(Symbol, SXData);
getContext().allocFragment<MCSymbolIdFragment>(Symbol, SXData);

getAssembler().registerSymbol(*Symbol);
CSymbol->setIsSafeSEH();
Expand All @@ -212,7 +212,8 @@ void MCWinCOFFStreamer::emitCOFFSymbolIndex(MCSymbol const *Symbol) {
getAssembler().registerSection(*Sec);
Sec->ensureMinAlignment(Align(4));

new MCSymbolIdFragment(Symbol, getCurrentSectionOnly());
getContext().allocFragment<MCSymbolIdFragment>(Symbol,
getCurrentSectionOnly());

getAssembler().registerSymbol(*Symbol);
}
Expand Down
10 changes: 6 additions & 4 deletions llvm/lib/MC/WinCOFFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,8 +1097,9 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm,

// Create the contents of the .llvm_addrsig section.
if (Mode != DwoOnly && OWriter.EmitAddrsigSection) {
auto Frag = new MCDataFragment(AddrsigSection);
Frag->setLayoutOrder(0);
auto *Frag = Asm.getContext().allocFragment<MCDataFragment>();
Frag->setParent(AddrsigSection);
AddrsigSection->addFragment(*Frag);
raw_svector_ostream OS(Frag->getContents());
for (const MCSymbol *S : OWriter.AddrsigSyms) {
if (!S->isRegistered())
Expand All @@ -1118,8 +1119,9 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm,

// Create the contents of the .llvm.call-graph-profile section.
if (Mode != DwoOnly && CGProfileSection) {
auto *Frag = new MCDataFragment(CGProfileSection);
Frag->setLayoutOrder(0);
auto *Frag = Asm.getContext().allocFragment<MCDataFragment>();
Frag->setParent(CGProfileSection);
CGProfileSection->addFragment(*Frag);
raw_svector_ostream OS(Frag->getContents());
for (const MCAssembler::CGProfileEntry &CGPE : Asm.CGProfile) {
uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
Expand Down
Loading