Skip to content

[MC] Allocate MCFragment with a bump allocator #96402

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

Merged
merged 4 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion llvm/include/llvm/MC/MCContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ class MCContext {
/// objects.
BumpPtrAllocator Allocator;

/// For MCFragment instances.
BumpPtrAllocator FragmentAllocator;

SpecificBumpPtrAllocator<MCSectionCOFF> COFFAllocator;
SpecificBumpPtrAllocator<MCSectionDXContainer> DXCAllocator;
SpecificBumpPtrAllocator<MCSectionELF> ELFAllocator;
Expand Down Expand Up @@ -432,7 +435,8 @@ class MCContext {
MCInst *createMCInst();

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

/// \name Symbol Management
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/MC/MCCodeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ using namespace llvm::codeview;
CodeViewContext::~CodeViewContext() {
// If someone inserted strings into the string table but never actually
// emitted them somewhere, clean up the fragment.
if (!InsertedStrTabFragment)
delete StrTabFragment;
if (!InsertedStrTabFragment && StrTabFragment)
StrTabFragment->destroy();
}

/// This is a valid number for use with .cv_loc if we've already seen a .cv_file
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/MC/MCContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,15 @@ void MCContext::reset() {
SPIRVAllocator.DestroyAll();
WasmSignatureAllocator.DestroyAll();

// ~CodeViewContext may destroy a MCFragment outside of sections and need to
// be reset before FragmentAllocator.
CVContext.reset();

MCSubtargetAllocator.DestroyAll();
InlineAsmUsedLabelNames.clear();
Symbols.clear();
Allocator.Reset();
FragmentAllocator.Reset();
Instances.clear();
CompilationDir.clear();
MainFileName.clear();
Expand All @@ -165,8 +170,6 @@ void MCContext::reset() {
DwarfCompileUnitID = 0;
CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);

CVContext.reset();

MachOUniquingMap.clear();
ELFUniquingMap.clear();
GOFFUniquingMap.clear();
Expand Down
38 changes: 16 additions & 22 deletions llvm/lib/MC/MCFragment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,60 +202,54 @@ MCFragment::MCFragment(FragmentType Kind, bool HasInstructions)
: Kind(Kind), HasInstructions(HasInstructions), LinkerRelaxable(false) {}

void MCFragment::destroy() {
// First check if we are the sentinel.
if (Kind == FragmentType(~0)) {
delete this;
return;
}

switch (Kind) {
case FT_Align:
delete cast<MCAlignFragment>(this);
cast<MCAlignFragment>(this)->~MCAlignFragment();
return;
case FT_Data:
delete cast<MCDataFragment>(this);
cast<MCDataFragment>(this)->~MCDataFragment();
return;
case FT_CompactEncodedInst:
delete cast<MCCompactEncodedInstFragment>(this);
cast<MCCompactEncodedInstFragment>(this)->~MCCompactEncodedInstFragment();
return;
case FT_Fill:
delete cast<MCFillFragment>(this);
cast<MCFillFragment>(this)->~MCFillFragment();
return;
case FT_Nops:
delete cast<MCNopsFragment>(this);
cast<MCNopsFragment>(this)->~MCNopsFragment();
return;
case FT_Relaxable:
delete cast<MCRelaxableFragment>(this);
cast<MCRelaxableFragment>(this)->~MCRelaxableFragment();
return;
case FT_Org:
delete cast<MCOrgFragment>(this);
cast<MCOrgFragment>(this)->~MCOrgFragment();
return;
case FT_Dwarf:
delete cast<MCDwarfLineAddrFragment>(this);
cast<MCDwarfLineAddrFragment>(this)->~MCDwarfLineAddrFragment();
return;
case FT_DwarfFrame:
delete cast<MCDwarfCallFrameFragment>(this);
cast<MCDwarfCallFrameFragment>(this)->~MCDwarfCallFrameFragment();
return;
case FT_LEB:
delete cast<MCLEBFragment>(this);
cast<MCLEBFragment>(this)->~MCLEBFragment();
return;
case FT_BoundaryAlign:
delete cast<MCBoundaryAlignFragment>(this);
cast<MCBoundaryAlignFragment>(this)->~MCBoundaryAlignFragment();
return;
case FT_SymbolId:
delete cast<MCSymbolIdFragment>(this);
cast<MCSymbolIdFragment>(this)->~MCSymbolIdFragment();
return;
case FT_CVInlineLines:
delete cast<MCCVInlineLineTableFragment>(this);
cast<MCCVInlineLineTableFragment>(this)->~MCCVInlineLineTableFragment();
return;
case FT_CVDefRange:
delete cast<MCCVDefRangeFragment>(this);
cast<MCCVDefRangeFragment>(this)->~MCCVDefRangeFragment();
return;
case FT_PseudoProbe:
delete cast<MCPseudoProbeAddrFragment>(this);
cast<MCPseudoProbeAddrFragment>(this)->~MCPseudoProbeAddrFragment();
return;
case FT_Dummy:
delete cast<MCDummyFragment>(this);
cast<MCDummyFragment>(this)->~MCDummyFragment();
return;
}
}
Expand Down
Loading