Skip to content

Commit f808abf

Browse files
authored
[MC] Add MCFragment allocation helpers
`allocFragment` might be changed to a placement new when the allocation strategy changes. `allocInitialFragment` is to deduplicate the following pattern ``` auto *F = new MCDataFragment(); Result->addFragment(*F); F->setParent(Result); ``` Pull Request: #95197
1 parent 8b9dce3 commit f808abf

File tree

12 files changed

+83
-59
lines changed

12 files changed

+83
-59
lines changed

llvm/include/llvm/MC/MCCodeView.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ struct MCCVFunctionInfo {
143143
/// Holds state from .cv_file and .cv_loc directives for later emission.
144144
class CodeViewContext {
145145
public:
146-
CodeViewContext();
146+
CodeViewContext(MCContext *MCCtx) : MCCtx(MCCtx) {}
147147
~CodeViewContext();
148148

149149
CodeViewContext &operator=(const CodeViewContext &other) = delete;
@@ -223,6 +223,8 @@ class CodeViewContext {
223223
std::pair<StringRef, unsigned> addToStringTable(StringRef S);
224224

225225
private:
226+
MCContext *MCCtx;
227+
226228
/// Map from string to string table offset.
227229
StringMap<unsigned> StringTable;
228230

llvm/include/llvm/MC/MCContext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace llvm {
4444

4545
class CodeViewContext;
4646
class MCAsmInfo;
47+
class MCDataFragment;
4748
class MCInst;
4849
class MCLabel;
4950
class MCObjectFileInfo;
@@ -345,6 +346,8 @@ class MCContext {
345346
void reportCommon(SMLoc Loc,
346347
std::function<void(SMDiagnostic &, const SourceMgr *)>);
347348

349+
MCDataFragment *allocInitialFragment(MCSection &Sec);
350+
348351
MCSymbol *createSymbolImpl(const StringMapEntry<bool> *Name,
349352
bool IsTemporary);
350353
MCSymbol *createSymbol(StringRef Name, bool AlwaysAddSuffix,
@@ -437,6 +440,10 @@ class MCContext {
437440
/// Create and return a new MC instruction.
438441
MCInst *createMCInst();
439442

443+
template <typename F, typename... Args> F *allocFragment(Args &&...args) {
444+
return new F(std::forward<Args>(args)...);
445+
}
446+
440447
/// \name Symbol Management
441448
/// @{
442449

llvm/lib/MC/MCAssembler.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,11 @@ void MCAssembler::layout(MCAsmLayout &Layout) {
820820
for (MCSection &Sec : *this) {
821821
// Create dummy fragments to eliminate any empty sections, this simplifies
822822
// layout.
823-
if (Sec.empty())
824-
new MCDataFragment(&Sec);
823+
if (Sec.empty()) {
824+
auto *F = getContext().allocFragment<MCDataFragment>();
825+
F->setParent(&Sec);
826+
Sec.addFragment(*F);
827+
}
825828

826829
Sec.setOrdinal(SectionIndex++);
827830
}

llvm/lib/MC/MCCodeView.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
using namespace llvm;
2727
using namespace llvm::codeview;
2828

29-
CodeViewContext::CodeViewContext() = default;
30-
3129
CodeViewContext::~CodeViewContext() {
3230
// If someone inserted strings into the string table but never actually
3331
// emitted them somewhere, clean up the fragment.
@@ -138,7 +136,7 @@ void CodeViewContext::recordCVLoc(MCContext &Ctx, const MCSymbol *Label,
138136

139137
MCDataFragment *CodeViewContext::getStringTableFragment() {
140138
if (!StrTabFragment) {
141-
StrTabFragment = new MCDataFragment();
139+
StrTabFragment = MCCtx->allocFragment<MCDataFragment>();
142140
// Start a new string table out with a null byte.
143141
StrTabFragment->getContents().push_back('\0');
144142
}
@@ -450,9 +448,9 @@ void CodeViewContext::emitInlineLineTableForFunction(MCObjectStreamer &OS,
450448
const MCSymbol *FnEndSym) {
451449
// Create and insert a fragment into the current section that will be encoded
452450
// later.
453-
new MCCVInlineLineTableFragment(PrimaryFunctionId, SourceFileId,
454-
SourceLineNum, FnStartSym, FnEndSym,
455-
OS.getCurrentSectionOnly());
451+
auto *F = MCCtx->allocFragment<MCCVInlineLineTableFragment>(
452+
PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
453+
OS.insert(F);
456454
}
457455

458456
MCFragment *CodeViewContext::emitDefRange(
@@ -461,8 +459,10 @@ MCFragment *CodeViewContext::emitDefRange(
461459
StringRef FixedSizePortion) {
462460
// Create and insert a fragment into the current section that will be encoded
463461
// later.
464-
return new MCCVDefRangeFragment(Ranges, FixedSizePortion,
465-
OS.getCurrentSectionOnly());
462+
auto *F =
463+
MCCtx->allocFragment<MCCVDefRangeFragment>(Ranges, FixedSizePortion);
464+
OS.insert(F);
465+
return F;
466466
}
467467

468468
static unsigned computeLabelDiff(MCAsmLayout &Layout, const MCSymbol *Begin,

llvm/lib/MC/MCContext.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ MCInst *MCContext::createMCInst() {
195195
return new (MCInstAllocator.Allocate()) MCInst;
196196
}
197197

198+
// Allocate the initial MCDataFragment for the begin symbol.
199+
MCDataFragment *MCContext::allocInitialFragment(MCSection &Sec) {
200+
assert(!Sec.curFragList()->Head);
201+
auto *F = allocFragment<MCDataFragment>();
202+
F->setParent(&Sec);
203+
Sec.addFragment(*F);
204+
return F;
205+
}
206+
198207
//===----------------------------------------------------------------------===//
199208
// Symbol Manipulation
200209
//===----------------------------------------------------------------------===//
@@ -497,11 +506,8 @@ MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
497506
MCSectionELF(Section, Type, Flags, K, EntrySize, Group, Comdat, UniqueID,
498507
R, LinkedToSym);
499508

500-
auto *F = new MCDataFragment();
501-
Ret->addFragment(*F);
502-
F->setParent(Ret);
509+
auto *F = allocInitialFragment(*Ret);
503510
R->setFragment(F);
504-
505511
return Ret;
506512
}
507513

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

800-
auto *F = new MCDataFragment();
801-
Result->addFragment(*F);
802-
F->setParent(Result);
806+
auto *F = allocInitialFragment(*Result);
803807
Begin->setFragment(F);
804-
805808
return Result;
806809
}
807810

@@ -863,10 +866,7 @@ MCSectionXCOFF *MCContext::getXCOFFSection(
863866

864867
Entry.second = Result;
865868

866-
auto *F = new MCDataFragment();
867-
Result->addFragment(*F);
868-
F->setParent(Result);
869-
869+
auto *F = allocInitialFragment(*Result);
870870
if (Begin)
871871
Begin->setFragment(F);
872872

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

889-
auto *F = new MCDataFragment();
890-
Result->addFragment(*F);
891-
F->setParent(Result);
892-
889+
allocInitialFragment(*Result);
893890
return Result;
894891
}
895892

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

911908
// The first fragment will store the header
912-
auto *F = new MCDataFragment();
913-
MapIt->second->addFragment(*F);
914-
F->setParent(MapIt->second);
915-
909+
allocInitialFragment(*MapIt->second);
916910
return MapIt->second;
917911
}
918912

@@ -1043,7 +1037,7 @@ void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
10431037

10441038
CodeViewContext &MCContext::getCVContext() {
10451039
if (!CVContext)
1046-
CVContext.reset(new CodeViewContext);
1040+
CVContext.reset(new CodeViewContext(this));
10471041
return *CVContext;
10481042
}
10491043

llvm/lib/MC/MCELFStreamer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ void MCELFStreamer::emitInstToData(const MCInst &Inst,
585585
// When not in a bundle-locked group and the -mc-relax-all flag is used,
586586
// we create a new temporary fragment which will be later merged into
587587
// the current fragment.
588-
DF = new MCDataFragment();
588+
DF = getContext().allocFragment<MCDataFragment>();
589589
else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst()) {
590590
// If we are bundle-locked, we re-use the current fragment.
591591
// The bundle-locking directive ensures this is a new data fragment.
@@ -596,13 +596,14 @@ void MCELFStreamer::emitInstToData(const MCInst &Inst,
596596
// Optimize memory usage by emitting the instruction to a
597597
// MCCompactEncodedInstFragment when not in a bundle-locked group and
598598
// there are no fixups registered.
599-
MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
599+
MCCompactEncodedInstFragment *CEIF =
600+
getContext().allocFragment<MCCompactEncodedInstFragment>();
600601
insert(CEIF);
601602
CEIF->getContents().append(Code.begin(), Code.end());
602603
CEIF->setHasInstructions(STI);
603604
return;
604605
} else {
605-
DF = new MCDataFragment();
606+
DF = getContext().allocFragment<MCDataFragment>();
606607
insert(DF);
607608
}
608609
if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
@@ -661,7 +662,7 @@ void MCELFStreamer::emitBundleLock(bool AlignToEnd) {
661662

662663
if (getAssembler().getRelaxAll() && !isBundleLocked()) {
663664
// TODO: drop the lock state and set directly in the fragment
664-
MCDataFragment *DF = new MCDataFragment();
665+
MCDataFragment *DF = getContext().allocFragment<MCDataFragment>();
665666
BundleGroups.push_back(DF);
666667
}
667668

llvm/lib/MC/MCMachOStreamer.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ void MCMachOStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
199199
// We have to create a new fragment if this is an atom defining symbol,
200200
// fragments cannot span atoms.
201201
if (getAssembler().isSymbolLinkerVisible(*Symbol))
202-
insert(new MCDataFragment());
202+
insert(getContext().allocFragment<MCDataFragment>());
203203

204204
MCObjectStreamer::emitLabel(Symbol, Loc);
205205

@@ -555,7 +555,9 @@ void MCMachOStreamer::finalizeCGProfile() {
555555
MCSection *CGProfileSection = Asm.getContext().getMachOSection(
556556
"__LLVM", "__cg_profile", 0, SectionKind::getMetadata());
557557
Asm.registerSection(*CGProfileSection);
558-
auto *Frag = new MCDataFragment(CGProfileSection);
558+
auto *Frag = getContext().allocFragment<MCDataFragment>();
559+
Frag->setParent(CGProfileSection);
560+
CGProfileSection->addFragment(*Frag);
559561
// For each entry, reserve space for 2 32-bit indices and a 64-bit count.
560562
size_t SectionBytes =
561563
Asm.CGProfile.size() * (2 * sizeof(uint32_t) + sizeof(uint64_t));
@@ -595,7 +597,9 @@ void MCMachOStreamer::createAddrSigSection() {
595597
MCSection *AddrSigSection =
596598
Asm.getContext().getObjectFileInfo()->getAddrSigSection();
597599
Asm.registerSection(*AddrSigSection);
598-
auto *Frag = new MCDataFragment(AddrSigSection);
600+
auto *Frag = getContext().allocFragment<MCDataFragment>();
601+
Frag->setParent(AddrSigSection);
602+
AddrSigSection->addFragment(*Frag);
599603
// We will generate a series of pointer-sized symbol relocations at offset
600604
// 0x0. Set the section size to be large enough to contain a single pointer
601605
// (instead of emitting a zero-sized section) so these relocations are

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ MCDataFragment *
225225
MCObjectStreamer::getOrCreateDataFragment(const MCSubtargetInfo *STI) {
226226
MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
227227
if (!F || !canReuseDataFragment(*F, *Assembler, STI)) {
228-
F = new MCDataFragment();
228+
F = getContext().allocFragment<MCDataFragment>();
229229
insert(F);
230230
}
231231
return F;
@@ -343,7 +343,7 @@ void MCObjectStreamer::emitULEB128Value(const MCExpr *Value) {
343343
emitULEB128IntValue(IntValue);
344344
return;
345345
}
346-
insert(new MCLEBFragment(*Value, false));
346+
insert(getContext().allocFragment<MCLEBFragment>(*Value, false));
347347
}
348348

349349
void MCObjectStreamer::emitSLEB128Value(const MCExpr *Value) {
@@ -352,7 +352,7 @@ void MCObjectStreamer::emitSLEB128Value(const MCExpr *Value) {
352352
emitSLEB128IntValue(IntValue);
353353
return;
354354
}
355-
insert(new MCLEBFragment(*Value, true));
355+
insert(getContext().allocFragment<MCLEBFragment>(*Value, true));
356356
}
357357

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

471471
// Always create a new, separate fragment here, because its size can change
472472
// during relaxation.
473-
MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI);
473+
MCRelaxableFragment *IF =
474+
getContext().allocFragment<MCRelaxableFragment>(Inst, STI);
474475
insert(IF);
475476

476477
SmallString<128> Code;
@@ -544,7 +545,8 @@ void MCObjectStreamer::emitDwarfAdvanceLineAddr(int64_t LineDelta,
544545
return;
545546
}
546547
const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel, SMLoc());
547-
insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta));
548+
insert(getContext().allocFragment<MCDwarfLineAddrFragment>(LineDelta,
549+
*AddrDelta));
548550
}
549551

550552
void MCObjectStreamer::emitDwarfLineEndEntry(MCSection *Section,
@@ -569,7 +571,8 @@ void MCObjectStreamer::emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
569571
const MCSymbol *Label,
570572
SMLoc Loc) {
571573
const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel, Loc);
572-
insert(new MCDwarfCallFrameFragment(*AddrDelta, nullptr));
574+
insert(getContext().allocFragment<MCDwarfCallFrameFragment>(*AddrDelta,
575+
nullptr));
573576
}
574577

575578
void MCObjectStreamer::emitCVLocDirective(unsigned FunctionId, unsigned FileNo,
@@ -640,7 +643,8 @@ void MCObjectStreamer::emitValueToAlignment(Align Alignment, int64_t Value,
640643
unsigned MaxBytesToEmit) {
641644
if (MaxBytesToEmit == 0)
642645
MaxBytesToEmit = Alignment.value();
643-
insert(new MCAlignFragment(Alignment, Value, ValueSize, MaxBytesToEmit));
646+
insert(getContext().allocFragment<MCAlignFragment>(
647+
Alignment, Value, ValueSize, MaxBytesToEmit));
644648

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

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

846850
assert(getCurrentSectionOnly() && "need a section");
847-
insert(new MCFillFragment(FillValue, 1, NumBytes, Loc));
851+
insert(
852+
getContext().allocFragment<MCFillFragment>(FillValue, 1, NumBytes, Loc));
848853
}
849854

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

876881
assert(getCurrentSectionOnly() && "need a section");
877-
insert(new MCFillFragment(Expr, Size, NumValues, Loc));
882+
insert(
883+
getContext().allocFragment<MCFillFragment>(Expr, Size, NumValues, Loc));
878884
}
879885

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

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

888-
insert(new MCNopsFragment(NumBytes, ControlledNopLength, Loc, STI));
894+
insert(getContext().allocFragment<MCNopsFragment>(
895+
NumBytes, ControlledNopLength, Loc, STI));
889896
}
890897

891898
void MCObjectStreamer::emitFileDirective(StringRef Filename) {

llvm/lib/MC/MCPseudoProbe.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ void MCPseudoProbe::emit(MCObjectStreamer *MCOS,
8080
if (AddrDelta->evaluateAsAbsolute(Delta, MCOS->getAssemblerPtr())) {
8181
MCOS->emitSLEB128IntValue(Delta);
8282
} else {
83-
MCOS->insert(new MCPseudoProbeAddrFragment(AddrDelta));
83+
MCOS->insert(MCOS->getContext().allocFragment<MCPseudoProbeAddrFragment>(
84+
AddrDelta));
8485
}
8586
} else {
8687
// Emit the GUID of the split function that the sentinel probe represents.

llvm/lib/MC/MCWinCOFFStreamer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void MCWinCOFFStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) {
196196
getAssembler().registerSection(*SXData);
197197
SXData->ensureMinAlignment(Align(4));
198198

199-
new MCSymbolIdFragment(Symbol, SXData);
199+
getContext().allocFragment<MCSymbolIdFragment>(Symbol, SXData);
200200

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

215-
new MCSymbolIdFragment(Symbol, getCurrentSectionOnly());
215+
getContext().allocFragment<MCSymbolIdFragment>(Symbol,
216+
getCurrentSectionOnly());
216217

217218
getAssembler().registerSymbol(*Symbol);
218219
}

0 commit comments

Comments
 (0)