[llvm] 9591f16 - Move FragmentAllocator from MCContext to MCObjectStreamer
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 3 12:26:06 PDT 2025
Author: Fangrui Song
Date: 2025-08-03T12:26:02-07:00
New Revision: 9591f16a5ada294fa825a2581fed568ee1f24597
URL: https://github.com/llvm/llvm-project/commit/9591f16a5ada294fa825a2581fed568ee1f24597
DIFF: https://github.com/llvm/llvm-project/commit/9591f16a5ada294fa825a2581fed568ee1f24597.diff
LOG: Move FragmentAllocator from MCContext to MCObjectStreamer
In MCContext::reset, delete a stale comment as MCCodeView no longer
owns or deallocates MCFragment.
Added:
Modified:
llvm/include/llvm/MC/MCCodeView.h
llvm/include/llvm/MC/MCContext.h
llvm/include/llvm/MC/MCObjectStreamer.h
llvm/lib/MC/MCCodeView.cpp
llvm/lib/MC/MCContext.cpp
llvm/lib/MC/MCObjectStreamer.cpp
llvm/lib/MC/MCWinCOFFStreamer.cpp
llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/MC/MCCodeView.h b/llvm/include/llvm/MC/MCCodeView.h
index 9cde44c71baff..76e15b8518439 100644
--- a/llvm/include/llvm/MC/MCCodeView.h
+++ b/llvm/include/llvm/MC/MCCodeView.h
@@ -203,7 +203,7 @@ class CodeViewContext {
void encodeInlineLineTable(const MCAssembler &Asm,
MCCVInlineLineTableFragment &F);
- MCFragment *
+ void
emitDefRange(MCObjectStreamer &OS,
ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
StringRef FixedSizePortion);
diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h
index ddac161fe0ff9..4bd16cf1505a1 100644
--- a/llvm/include/llvm/MC/MCContext.h
+++ b/llvm/include/llvm/MC/MCContext.h
@@ -434,11 +434,6 @@ class MCContext {
/// Create and return a new MC instruction.
LLVM_ABI MCInst *createMCInst();
- template <typename F, typename... Args> F *allocFragment(Args &&...args) {
- return new (FragmentAllocator.Allocate(sizeof(F), alignof(F)))
- F(std::forward<Args>(args)...);
- }
-
/// \name Symbol Management
/// @{
diff --git a/llvm/include/llvm/MC/MCObjectStreamer.h b/llvm/include/llvm/MC/MCObjectStreamer.h
index eb875a70f8fbe..b9e813b9b0d28 100644
--- a/llvm/include/llvm/MC/MCObjectStreamer.h
+++ b/llvm/include/llvm/MC/MCObjectStreamer.h
@@ -55,7 +55,11 @@ class MCObjectStreamer : public MCStreamer {
SmallVector<std::unique_ptr<uint8_t[]>, 0> FragStorage;
// Available bytes in the current block for trailing data or new fragments.
size_t FragSpace = 0;
+ // Used to allocate special fragments that do not use MCFragment's fixed-size
+ // part.
+ BumpPtrAllocator SpecialFragAllocator;
+ void addSpecialFragment(MCFragment *F);
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &);
void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
@@ -85,9 +89,6 @@ class MCObjectStreamer : public MCStreamer {
/// \name MCStreamer Interface
/// @{
- // Add a fragment with a variable-size tail and start a new empty fragment.
- void insert(MCFragment *F);
-
uint8_t *getCurFragEnd() const {
return reinterpret_cast<uint8_t *>(CurFrag + 1) + CurFrag->getFixedSize();
}
@@ -95,6 +96,16 @@ class MCObjectStreamer : public MCStreamer {
// Add a new fragment to the current section without a variable-size tail.
void newFragment();
+ // Add a new special fragment to the current section and start a new empty
+ // fragment.
+ template <typename FT, typename... Args>
+ FT *newSpecialFragment(Args &&...args) {
+ auto *F = new (SpecialFragAllocator.Allocate(sizeof(FT), alignof(FT)))
+ FT(std::forward<Args>(args)...);
+ addSpecialFragment(F);
+ return F;
+ }
+
void ensureHeadroom(size_t Headroom);
void appendContents(ArrayRef<char> Contents);
void appendContents(size_t Num, uint8_t Elt);
diff --git a/llvm/lib/MC/MCCodeView.cpp b/llvm/lib/MC/MCCodeView.cpp
index 3a5f01c2af607..335934a7793e4 100644
--- a/llvm/lib/MC/MCCodeView.cpp
+++ b/llvm/lib/MC/MCCodeView.cpp
@@ -436,12 +436,11 @@ void CodeViewContext::emitInlineLineTableForFunction(MCObjectStreamer &OS,
const MCSymbol *FnEndSym) {
// Create and insert a fragment into the current section that will be encoded
// later.
- auto *F = MCCtx->allocFragment<MCCVInlineLineTableFragment>(
+ OS.newSpecialFragment<MCCVInlineLineTableFragment>(
PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
- OS.insert(F);
}
-MCFragment *CodeViewContext::emitDefRange(
+void CodeViewContext::emitDefRange(
MCObjectStreamer &OS,
ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
StringRef FixedSizePortion) {
@@ -451,9 +450,7 @@ MCFragment *CodeViewContext::emitDefRange(
auto &Saved = DefRangeStorage.emplace_back(Ranges.begin(), Ranges.end());
// Create and insert a fragment into the current section that will be encoded
// later.
- auto *F = MCCtx->allocFragment<MCCVDefRangeFragment>(Saved, FixedSizePortion);
- OS.insert(F);
- return F;
+ OS.newSpecialFragment<MCCVDefRangeFragment>(Saved, FixedSizePortion);
}
static unsigned computeLabelDiff(const MCAssembler &Asm, const MCSymbol *Begin,
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index 39bf62832e56b..4d89a2d73e4dd 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -153,15 +153,12 @@ 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();
diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index 200e29a987ad9..bcc77c06d1fa1 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -84,7 +84,7 @@ void MCObjectStreamer::ensureHeadroom(size_t Headroom) {
addFragment(F);
}
-void MCObjectStreamer::insert(MCFragment *Frag) {
+void MCObjectStreamer::addSpecialFragment(MCFragment *Frag) {
assert(Frag->getKind() != MCFragment::FT_Data &&
"F should have a variable-size tail");
// Frag is not connected to FragSpace. Before modifying CurFrag with
@@ -173,6 +173,7 @@ void MCObjectStreamer::reset() {
EmitDebugFrame = false;
FragStorage.clear();
FragSpace = 0;
+ SpecialFragAllocator.Reset();
MCStreamer::reset();
}
@@ -649,7 +650,7 @@ void MCObjectStreamer::emitCodeAlignment(Align Alignment,
void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset,
unsigned char Value,
SMLoc Loc) {
- insert(getContext().allocFragment<MCOrgFragment>(*Offset, Value, Loc));
+ newSpecialFragment<MCOrgFragment>(*Offset, Value, Loc);
}
void MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name,
@@ -681,8 +682,7 @@ void MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name,
void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
SMLoc Loc) {
assert(getCurrentSectionOnly() && "need a section");
- insert(
- getContext().allocFragment<MCFillFragment>(FillValue, 1, NumBytes, Loc));
+ newSpecialFragment<MCFillFragment>(FillValue, 1, NumBytes, Loc);
}
void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
@@ -709,15 +709,13 @@ void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
// Otherwise emit as fragment.
assert(getCurrentSectionOnly() && "need a section");
- insert(
- getContext().allocFragment<MCFillFragment>(Expr, Size, NumValues, Loc));
+ newSpecialFragment<MCFillFragment>(Expr, Size, NumValues, Loc);
}
void MCObjectStreamer::emitNops(int64_t NumBytes, int64_t ControlledNopLength,
SMLoc Loc, const MCSubtargetInfo &STI) {
assert(getCurrentSectionOnly() && "need a section");
- insert(getContext().allocFragment<MCNopsFragment>(
- NumBytes, ControlledNopLength, Loc, STI));
+ newSpecialFragment<MCNopsFragment>(NumBytes, ControlledNopLength, Loc, STI);
}
void MCObjectStreamer::emitFileDirective(StringRef Filename) {
diff --git a/llvm/lib/MC/MCWinCOFFStreamer.cpp b/llvm/lib/MC/MCWinCOFFStreamer.cpp
index 8be5054ad25e2..b69c2103d059f 100644
--- a/llvm/lib/MC/MCWinCOFFStreamer.cpp
+++ b/llvm/lib/MC/MCWinCOFFStreamer.cpp
@@ -258,7 +258,7 @@ void MCWinCOFFStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) {
switchSection(SXData);
SXData->ensureMinAlignment(Align(4));
- insert(getContext().allocFragment<MCSymbolIdFragment>(Symbol));
+ newSpecialFragment<MCSymbolIdFragment>(Symbol);
getAssembler().registerSymbol(*Symbol);
CSymbol->setIsSafeSEH();
@@ -273,7 +273,7 @@ void MCWinCOFFStreamer::emitCOFFSymbolIndex(MCSymbol const *Symbol) {
MCSection *Sec = getCurrentSectionOnly();
Sec->ensureMinAlignment(Align(4));
- insert(getContext().allocFragment<MCSymbolIdFragment>(Symbol));
+ newSpecialFragment<MCSymbolIdFragment>(Symbol);
getAssembler().registerSymbol(*Symbol);
}
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
index 1f02e56ea0d11..56a4cc3d65c2e 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
@@ -511,9 +511,8 @@ void X86AsmBackend::emitInstructionBegin(MCObjectStreamer &OS,
isFirstMacroFusibleInst(Inst, *MCII))) {
// If we meet a unfused branch or the first instuction in a fusiable pair,
// insert a BoundaryAlign fragment.
- PendingBA = OS.getContext().allocFragment<MCBoundaryAlignFragment>(
- AlignBoundary, STI);
- OS.insert(PendingBA);
+ PendingBA =
+ OS.newSpecialFragment<MCBoundaryAlignFragment>(AlignBoundary, STI);
}
}
More information about the llvm-commits
mailing list