Skip to content

Commit ba20c67

Browse files
MaskRayAlexisPerry
authored andcommitted
[MC] Allocate MCFragment with a bump allocator
llvm#95197 and 7500646 eliminated all raw `new MCXXXFragment`. We can now place fragments in a bump allocator. In addition, remove the dead `Kind == FragmentType(~0)` condition. ~CodeViewContext may call `StrTabFragment->destroy()` and need to be reset before `FragmentAllocator.Reset()`. Tested by llvm/test/MC/COFF/cv-compiler-info.ll using asan. Pull Request: llvm#96402
1 parent d43c71c commit ba20c67

File tree

4 files changed

+28
-27
lines changed

4 files changed

+28
-27
lines changed

llvm/include/llvm/MC/MCContext.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ class MCContext {
136136
/// objects.
137137
BumpPtrAllocator Allocator;
138138

139+
/// For MCFragment instances.
140+
BumpPtrAllocator FragmentAllocator;
141+
139142
SpecificBumpPtrAllocator<MCSectionCOFF> COFFAllocator;
140143
SpecificBumpPtrAllocator<MCSectionDXContainer> DXCAllocator;
141144
SpecificBumpPtrAllocator<MCSectionELF> ELFAllocator;
@@ -432,7 +435,8 @@ class MCContext {
432435
MCInst *createMCInst();
433436

434437
template <typename F, typename... Args> F *allocFragment(Args &&...args) {
435-
return new F(std::forward<Args>(args)...);
438+
return new (FragmentAllocator.Allocate(sizeof(F), alignof(F)))
439+
F(std::forward<Args>(args)...);
436440
}
437441

438442
/// \name Symbol Management

llvm/lib/MC/MCCodeView.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ using namespace llvm::codeview;
2929
CodeViewContext::~CodeViewContext() {
3030
// If someone inserted strings into the string table but never actually
3131
// emitted them somewhere, clean up the fragment.
32-
if (!InsertedStrTabFragment)
33-
delete StrTabFragment;
32+
if (!InsertedStrTabFragment && StrTabFragment)
33+
StrTabFragment->destroy();
3434
}
3535

3636
/// This is a valid number for use with .cv_loc if we've already seen a .cv_file

llvm/lib/MC/MCContext.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,15 @@ void MCContext::reset() {
151151
SPIRVAllocator.DestroyAll();
152152
WasmSignatureAllocator.DestroyAll();
153153

154+
// ~CodeViewContext may destroy a MCFragment outside of sections and need to
155+
// be reset before FragmentAllocator.
156+
CVContext.reset();
157+
154158
MCSubtargetAllocator.DestroyAll();
155159
InlineAsmUsedLabelNames.clear();
156160
Symbols.clear();
157161
Allocator.Reset();
162+
FragmentAllocator.Reset();
158163
Instances.clear();
159164
CompilationDir.clear();
160165
MainFileName.clear();
@@ -165,8 +170,6 @@ void MCContext::reset() {
165170
DwarfCompileUnitID = 0;
166171
CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
167172

168-
CVContext.reset();
169-
170173
MachOUniquingMap.clear();
171174
ELFUniquingMap.clear();
172175
GOFFUniquingMap.clear();

llvm/lib/MC/MCFragment.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -202,60 +202,54 @@ MCFragment::MCFragment(FragmentType Kind, bool HasInstructions)
202202
: Kind(Kind), HasInstructions(HasInstructions), LinkerRelaxable(false) {}
203203

204204
void MCFragment::destroy() {
205-
// First check if we are the sentinel.
206-
if (Kind == FragmentType(~0)) {
207-
delete this;
208-
return;
209-
}
210-
211205
switch (Kind) {
212206
case FT_Align:
213-
delete cast<MCAlignFragment>(this);
207+
cast<MCAlignFragment>(this)->~MCAlignFragment();
214208
return;
215209
case FT_Data:
216-
delete cast<MCDataFragment>(this);
210+
cast<MCDataFragment>(this)->~MCDataFragment();
217211
return;
218212
case FT_CompactEncodedInst:
219-
delete cast<MCCompactEncodedInstFragment>(this);
213+
cast<MCCompactEncodedInstFragment>(this)->~MCCompactEncodedInstFragment();
220214
return;
221215
case FT_Fill:
222-
delete cast<MCFillFragment>(this);
216+
cast<MCFillFragment>(this)->~MCFillFragment();
223217
return;
224218
case FT_Nops:
225-
delete cast<MCNopsFragment>(this);
219+
cast<MCNopsFragment>(this)->~MCNopsFragment();
226220
return;
227221
case FT_Relaxable:
228-
delete cast<MCRelaxableFragment>(this);
222+
cast<MCRelaxableFragment>(this)->~MCRelaxableFragment();
229223
return;
230224
case FT_Org:
231-
delete cast<MCOrgFragment>(this);
225+
cast<MCOrgFragment>(this)->~MCOrgFragment();
232226
return;
233227
case FT_Dwarf:
234-
delete cast<MCDwarfLineAddrFragment>(this);
228+
cast<MCDwarfLineAddrFragment>(this)->~MCDwarfLineAddrFragment();
235229
return;
236230
case FT_DwarfFrame:
237-
delete cast<MCDwarfCallFrameFragment>(this);
231+
cast<MCDwarfCallFrameFragment>(this)->~MCDwarfCallFrameFragment();
238232
return;
239233
case FT_LEB:
240-
delete cast<MCLEBFragment>(this);
234+
cast<MCLEBFragment>(this)->~MCLEBFragment();
241235
return;
242236
case FT_BoundaryAlign:
243-
delete cast<MCBoundaryAlignFragment>(this);
237+
cast<MCBoundaryAlignFragment>(this)->~MCBoundaryAlignFragment();
244238
return;
245239
case FT_SymbolId:
246-
delete cast<MCSymbolIdFragment>(this);
240+
cast<MCSymbolIdFragment>(this)->~MCSymbolIdFragment();
247241
return;
248242
case FT_CVInlineLines:
249-
delete cast<MCCVInlineLineTableFragment>(this);
243+
cast<MCCVInlineLineTableFragment>(this)->~MCCVInlineLineTableFragment();
250244
return;
251245
case FT_CVDefRange:
252-
delete cast<MCCVDefRangeFragment>(this);
246+
cast<MCCVDefRangeFragment>(this)->~MCCVDefRangeFragment();
253247
return;
254248
case FT_PseudoProbe:
255-
delete cast<MCPseudoProbeAddrFragment>(this);
249+
cast<MCPseudoProbeAddrFragment>(this)->~MCPseudoProbeAddrFragment();
256250
return;
257251
case FT_Dummy:
258-
delete cast<MCDummyFragment>(this);
252+
cast<MCDummyFragment>(this)->~MCDummyFragment();
259253
return;
260254
}
261255
}

0 commit comments

Comments
 (0)