Skip to content

Commit 9591f16

Browse files
committed
Move FragmentAllocator from MCContext to MCObjectStreamer
In MCContext::reset, delete a stale comment as MCCodeView no longer owns or deallocates MCFragment.
1 parent 95e96b9 commit 9591f16

File tree

8 files changed

+28
-31
lines changed

8 files changed

+28
-31
lines changed

llvm/include/llvm/MC/MCCodeView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class CodeViewContext {
203203
void encodeInlineLineTable(const MCAssembler &Asm,
204204
MCCVInlineLineTableFragment &F);
205205

206-
MCFragment *
206+
void
207207
emitDefRange(MCObjectStreamer &OS,
208208
ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
209209
StringRef FixedSizePortion);

llvm/include/llvm/MC/MCContext.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,6 @@ class MCContext {
434434
/// Create and return a new MC instruction.
435435
LLVM_ABI MCInst *createMCInst();
436436

437-
template <typename F, typename... Args> F *allocFragment(Args &&...args) {
438-
return new (FragmentAllocator.Allocate(sizeof(F), alignof(F)))
439-
F(std::forward<Args>(args)...);
440-
}
441-
442437
/// \name Symbol Management
443438
/// @{
444439

llvm/include/llvm/MC/MCObjectStreamer.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ class MCObjectStreamer : public MCStreamer {
5555
SmallVector<std::unique_ptr<uint8_t[]>, 0> FragStorage;
5656
// Available bytes in the current block for trailing data or new fragments.
5757
size_t FragSpace = 0;
58+
// Used to allocate special fragments that do not use MCFragment's fixed-size
59+
// part.
60+
BumpPtrAllocator SpecialFragAllocator;
5861

62+
void addSpecialFragment(MCFragment *F);
5963
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &);
6064
void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
6165
void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
@@ -85,16 +89,23 @@ class MCObjectStreamer : public MCStreamer {
8589
/// \name MCStreamer Interface
8690
/// @{
8791

88-
// Add a fragment with a variable-size tail and start a new empty fragment.
89-
void insert(MCFragment *F);
90-
9192
uint8_t *getCurFragEnd() const {
9293
return reinterpret_cast<uint8_t *>(CurFrag + 1) + CurFrag->getFixedSize();
9394
}
9495
MCFragment *allocFragSpace(size_t Headroom);
9596
// Add a new fragment to the current section without a variable-size tail.
9697
void newFragment();
9798

99+
// Add a new special fragment to the current section and start a new empty
100+
// fragment.
101+
template <typename FT, typename... Args>
102+
FT *newSpecialFragment(Args &&...args) {
103+
auto *F = new (SpecialFragAllocator.Allocate(sizeof(FT), alignof(FT)))
104+
FT(std::forward<Args>(args)...);
105+
addSpecialFragment(F);
106+
return F;
107+
}
108+
98109
void ensureHeadroom(size_t Headroom);
99110
void appendContents(ArrayRef<char> Contents);
100111
void appendContents(size_t Num, uint8_t Elt);

llvm/lib/MC/MCCodeView.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,11 @@ void CodeViewContext::emitInlineLineTableForFunction(MCObjectStreamer &OS,
436436
const MCSymbol *FnEndSym) {
437437
// Create and insert a fragment into the current section that will be encoded
438438
// later.
439-
auto *F = MCCtx->allocFragment<MCCVInlineLineTableFragment>(
439+
OS.newSpecialFragment<MCCVInlineLineTableFragment>(
440440
PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
441-
OS.insert(F);
442441
}
443442

444-
MCFragment *CodeViewContext::emitDefRange(
443+
void CodeViewContext::emitDefRange(
445444
MCObjectStreamer &OS,
446445
ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
447446
StringRef FixedSizePortion) {
@@ -451,9 +450,7 @@ MCFragment *CodeViewContext::emitDefRange(
451450
auto &Saved = DefRangeStorage.emplace_back(Ranges.begin(), Ranges.end());
452451
// Create and insert a fragment into the current section that will be encoded
453452
// later.
454-
auto *F = MCCtx->allocFragment<MCCVDefRangeFragment>(Saved, FixedSizePortion);
455-
OS.insert(F);
456-
return F;
453+
OS.newSpecialFragment<MCCVDefRangeFragment>(Saved, FixedSizePortion);
457454
}
458455

459456
static unsigned computeLabelDiff(const MCAssembler &Asm, const MCSymbol *Begin,

llvm/lib/MC/MCContext.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,12 @@ void MCContext::reset() {
153153
SPIRVAllocator.DestroyAll();
154154
WasmSignatureAllocator.DestroyAll();
155155

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

160158
MCSubtargetAllocator.DestroyAll();
161159
InlineAsmUsedLabelNames.clear();
162160
Symbols.clear();
163161
Allocator.Reset();
164-
FragmentAllocator.Reset();
165162
Instances.clear();
166163
CompilationDir.clear();
167164
MainFileName.clear();

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void MCObjectStreamer::ensureHeadroom(size_t Headroom) {
8484
addFragment(F);
8585
}
8686

87-
void MCObjectStreamer::insert(MCFragment *Frag) {
87+
void MCObjectStreamer::addSpecialFragment(MCFragment *Frag) {
8888
assert(Frag->getKind() != MCFragment::FT_Data &&
8989
"F should have a variable-size tail");
9090
// Frag is not connected to FragSpace. Before modifying CurFrag with
@@ -173,6 +173,7 @@ void MCObjectStreamer::reset() {
173173
EmitDebugFrame = false;
174174
FragStorage.clear();
175175
FragSpace = 0;
176+
SpecialFragAllocator.Reset();
176177
MCStreamer::reset();
177178
}
178179

@@ -649,7 +650,7 @@ void MCObjectStreamer::emitCodeAlignment(Align Alignment,
649650
void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset,
650651
unsigned char Value,
651652
SMLoc Loc) {
652-
insert(getContext().allocFragment<MCOrgFragment>(*Offset, Value, Loc));
653+
newSpecialFragment<MCOrgFragment>(*Offset, Value, Loc);
653654
}
654655

655656
void MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name,
@@ -681,8 +682,7 @@ void MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name,
681682
void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
682683
SMLoc Loc) {
683684
assert(getCurrentSectionOnly() && "need a section");
684-
insert(
685-
getContext().allocFragment<MCFillFragment>(FillValue, 1, NumBytes, Loc));
685+
newSpecialFragment<MCFillFragment>(FillValue, 1, NumBytes, Loc);
686686
}
687687

688688
void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
@@ -709,15 +709,13 @@ void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
709709

710710
// Otherwise emit as fragment.
711711
assert(getCurrentSectionOnly() && "need a section");
712-
insert(
713-
getContext().allocFragment<MCFillFragment>(Expr, Size, NumValues, Loc));
712+
newSpecialFragment<MCFillFragment>(Expr, Size, NumValues, Loc);
714713
}
715714

716715
void MCObjectStreamer::emitNops(int64_t NumBytes, int64_t ControlledNopLength,
717716
SMLoc Loc, const MCSubtargetInfo &STI) {
718717
assert(getCurrentSectionOnly() && "need a section");
719-
insert(getContext().allocFragment<MCNopsFragment>(
720-
NumBytes, ControlledNopLength, Loc, STI));
718+
newSpecialFragment<MCNopsFragment>(NumBytes, ControlledNopLength, Loc, STI);
721719
}
722720

723721
void MCObjectStreamer::emitFileDirective(StringRef Filename) {

llvm/lib/MC/MCWinCOFFStreamer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ void MCWinCOFFStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) {
258258
switchSection(SXData);
259259
SXData->ensureMinAlignment(Align(4));
260260

261-
insert(getContext().allocFragment<MCSymbolIdFragment>(Symbol));
261+
newSpecialFragment<MCSymbolIdFragment>(Symbol);
262262
getAssembler().registerSymbol(*Symbol);
263263
CSymbol->setIsSafeSEH();
264264

@@ -273,7 +273,7 @@ void MCWinCOFFStreamer::emitCOFFSymbolIndex(MCSymbol const *Symbol) {
273273
MCSection *Sec = getCurrentSectionOnly();
274274
Sec->ensureMinAlignment(Align(4));
275275

276-
insert(getContext().allocFragment<MCSymbolIdFragment>(Symbol));
276+
newSpecialFragment<MCSymbolIdFragment>(Symbol);
277277
getAssembler().registerSymbol(*Symbol);
278278
}
279279

llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,8 @@ void X86AsmBackend::emitInstructionBegin(MCObjectStreamer &OS,
511511
isFirstMacroFusibleInst(Inst, *MCII))) {
512512
// If we meet a unfused branch or the first instuction in a fusiable pair,
513513
// insert a BoundaryAlign fragment.
514-
PendingBA = OS.getContext().allocFragment<MCBoundaryAlignFragment>(
515-
AlignBoundary, STI);
516-
OS.insert(PendingBA);
514+
PendingBA =
515+
OS.newSpecialFragment<MCBoundaryAlignFragment>(AlignBoundary, STI);
517516
}
518517
}
519518

0 commit comments

Comments
 (0)