Skip to content

Commit dc3a4c0

Browse files
authored
MC: Restructure MCFragment as a fixed part and a variable tail
Refactor the fragment representation of `push rax; jmp foo; nop; jmp foo`, previously encoded as `MCDataFragment(nop); MCRelaxableFragment(jmp foo); MCDataFragment(nop); MCRelaxableFragment(jmp foo)`, to ``` MCFragment(fixed: push rax, variable: jmp foo) MCFragment(fixed: nop, variable: jmp foo) ``` Changes: * Eliminate MCEncodedFragment, moving content and fixup storage to MCFragment. * The new MCFragment contains a fixed-size content (similar to previous MCDataFragment) and an optional variable-size tail. * The variable-size tail supports FT_Relaxable, FT_LEB, FT_Dwarf, and FT_DwarfFrame, with plans to extend to other fragment types. dyn_cast/isa should be avoided for the converted fragment subclasses. * In `setVarFixups`, source fixup offsets are relative to the variable part's start. Stored fixup (in `FixupStorage`) offsets are relative to the fixed part's start. A lot of code does `getFragmentOffset(Frag) + Fixup.getOffset()`, expecting the fixup offset to be relative to the fixed part's start. * HexagonAsmBackend::fixupNeedsRelaxationAdvanced needs to know the associated instruction for a fixup. We have to add a `const MCFragment &` parameter. * In MCObjectStreamer, extend `absoluteSymbolDiff` to apply to FT_Relaxable as otherwise there would be many more FT_DwarfFrame fragments in -g compilations. https://llvm-compile-time-tracker.com/compare.php?from=28e1473e8e523150914e8c7ea50b44fb0d2a8d65&to=778d68ad1d48e7f111ea853dd249912c601bee89&stat=instructions:u ``` stage2-O0-g instructins:u geomeon (-0.07%) stage1-ReleaseLTO-g (link only) max-rss geomean (-0.39%) ``` ``` % /t/clang-old -g -c sqlite3.i -w -mllvm -debug-only=mc-dump &| awk '/^[0-9]+/{s[$2]++;tot++} END{print "Total",tot; n=asorti(s, si); for(i=1;i<=n;i++) print si[i],s[si[i]]}' Total 59675 Align 2215 Data 29700 Dwarf 12044 DwarfCallFrame 4216 Fill 92 LEB 12 Relaxable 11396 % /t/clang-new -g -c sqlite3.i -w -mllvm -debug-only=mc-dump &| awk '/^[0-9]+/{s[$2]++;tot++} END{print "Total",tot; n=asorti(s, si); for(i=1;i<=n;i++) print si[i],s[si[i]]}' Total 32287 Align 2215 Data 2312 Dwarf 12044 DwarfCallFrame 4216 Fill 92 LEB 12 Relaxable 11396 ``` Pull Request: #148544
1 parent 7a9bef0 commit dc3a4c0

39 files changed

+511
-459
lines changed

llvm/include/llvm/MC/MCAsmBackend.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
namespace llvm {
2020

2121
class MCAlignFragment;
22-
class MCDwarfCallFrameFragment;
23-
class MCDwarfLineAddrFragment;
2422
class MCFragment;
2523
class MCLEBFragment;
26-
class MCRelaxableFragment;
2724
class MCSymbol;
2825
class MCAssembler;
2926
class MCContext;
@@ -157,8 +154,9 @@ class LLVM_ABI MCAsmBackend {
157154

158155
/// Target specific predicate for whether a given fixup requires the
159156
/// associated instruction to be relaxed.
160-
virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &, const MCValue &,
161-
uint64_t, bool Resolved) const;
157+
virtual bool fixupNeedsRelaxationAdvanced(const MCFragment &, const MCFixup &,
158+
const MCValue &, uint64_t,
159+
bool Resolved) const;
162160

163161
/// Simple predicate for targets where !Resolved implies requiring relaxation
164162
virtual bool fixupNeedsRelaxation(const MCFixup &Fixup,
@@ -179,18 +177,16 @@ class LLVM_ABI MCAsmBackend {
179177
}
180178

181179
// Defined by linker relaxation targets.
182-
virtual bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF,
183-
bool &WasRelaxed) const {
180+
virtual bool relaxDwarfLineAddr(MCFragment &, bool &WasRelaxed) const {
184181
return false;
185182
}
186-
virtual bool relaxDwarfCFA(MCDwarfCallFrameFragment &DF,
187-
bool &WasRelaxed) const {
183+
virtual bool relaxDwarfCFA(MCFragment &, bool &WasRelaxed) const {
188184
return false;
189185
}
190186

191187
// Defined by linker relaxation targets to possibly emit LEB128 relocations
192188
// and set Value at the relocated location.
193-
virtual std::pair<bool, bool> relaxLEB128(MCLEBFragment &LF,
189+
virtual std::pair<bool, bool> relaxLEB128(MCFragment &,
194190
int64_t &Value) const {
195191
return std::make_pair(false, false);
196192
}
@@ -228,7 +224,7 @@ class LLVM_ABI MCAsmBackend {
228224

229225
bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;
230226

231-
// Return STI for fragments of type MCRelaxableFragment and MCDataFragment
227+
// Return STI for fragments of type MCRelaxableFragment and MCFragment
232228
// with hasInstructions() == true.
233229
static const MCSubtargetInfo *getSubtargetInfo(const MCFragment &F);
234230
};

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@ namespace llvm {
3434
class MCBoundaryAlignFragment;
3535
class MCCVDefRangeFragment;
3636
class MCCVInlineLineTableFragment;
37-
class MCDwarfCallFrameFragment;
38-
class MCDwarfLineAddrFragment;
39-
class MCEncodedFragment;
37+
class MCFragment;
4038
class MCFixup;
4139
class MCLEBFragment;
4240
class MCPseudoProbeAddrFragment;
43-
class MCRelaxableFragment;
4441
class MCSymbolRefExpr;
4542
class raw_ostream;
4643
class MCAsmBackend;
@@ -102,7 +99,7 @@ class MCAssembler {
10299

103100
/// Check whether a fixup can be satisfied, or whether it needs to be relaxed
104101
/// (increased in size, in order to hold its value correctly).
105-
bool fixupNeedsRelaxation(const MCRelaxableFragment &, const MCFixup &) const;
102+
bool fixupNeedsRelaxation(const MCFragment &, const MCFixup &) const;
106103

107104
void layoutSection(MCSection &Sec);
108105
/// Perform one layout iteration and return the index of the first stable
@@ -111,11 +108,11 @@ class MCAssembler {
111108

112109
/// Perform relaxation on a single fragment.
113110
bool relaxFragment(MCFragment &F);
114-
bool relaxInstruction(MCRelaxableFragment &IF);
115-
bool relaxLEB(MCLEBFragment &IF);
111+
bool relaxInstruction(MCFragment &F);
112+
bool relaxLEB(MCFragment &F);
116113
bool relaxBoundaryAlign(MCBoundaryAlignFragment &BF);
117-
bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF);
118-
bool relaxDwarfCallFrameFragment(MCDwarfCallFrameFragment &DF);
114+
bool relaxDwarfLineAddr(MCFragment &F);
115+
bool relaxDwarfCallFrameFragment(MCFragment &F);
119116
bool relaxCVInlineLineTable(MCCVInlineLineTableFragment &DF);
120117
bool relaxCVDefRange(MCCVDefRangeFragment &DF);
121118
bool relaxFill(MCFillFragment &F);

llvm/include/llvm/MC/MCCodeView.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ namespace llvm {
2626
class MCAssembler;
2727
class MCCVDefRangeFragment;
2828
class MCCVInlineLineTableFragment;
29-
class MCDataFragment;
3029
class MCFragment;
3130
class MCSection;
3231
class MCSymbol;
@@ -231,7 +230,7 @@ class CodeViewContext {
231230
StringMap<unsigned> StringTable;
232231

233232
/// The fragment that ultimately holds our strings.
234-
MCDataFragment *StrTabFragment = nullptr;
233+
MCFragment *StrTabFragment = nullptr;
235234
SmallVector<char, 0> StrTab = {'\0'};
236235

237236
/// Get a string table offset.

llvm/include/llvm/MC/MCContext.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ namespace llvm {
4747

4848
class CodeViewContext;
4949
class MCAsmInfo;
50-
class MCDataFragment;
5150
class MCInst;
5251
class MCLabel;
5352
class MCObjectFileInfo;
@@ -334,7 +333,7 @@ class MCContext {
334333
void reportCommon(SMLoc Loc,
335334
std::function<void(SMDiagnostic &, const SourceMgr *)>);
336335

337-
MCDataFragment *allocInitialFragment(MCSection &Sec);
336+
MCFragment *allocInitialFragment(MCSection &Sec);
338337

339338
MCSymbolTableEntry &getSymbolTableEntry(StringRef Name);
340339

llvm/include/llvm/MC/MCELFStreamer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace llvm {
1717

1818
class ELFObjectWriter;
1919
class MCContext;
20-
class MCDataFragment;
2120
class MCFragment;
2221
class MCObjectWriter;
2322
class MCSection;
@@ -51,7 +50,7 @@ class MCELFStreamer : public MCObjectStreamer {
5150
void initSections(bool NoExecStack, const MCSubtargetInfo &STI) override;
5251
void changeSection(MCSection *Section, uint32_t Subsection = 0) override;
5352
void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
54-
void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCDataFragment &F,
53+
void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F,
5554
uint64_t Offset) override;
5655
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Target) override;
5756
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;

llvm/include/llvm/MC/MCObjectStreamer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class MCObjectStreamer : public MCStreamer {
4343
struct PendingMCFixup {
4444
const MCSymbol *Sym;
4545
MCFixup Fixup;
46-
MCDataFragment *DF;
47-
PendingMCFixup(const MCSymbol *McSym, MCDataFragment *F, MCFixup McFixup)
46+
MCFragment *DF;
47+
PendingMCFixup(const MCSymbol *McSym, MCFragment *F, MCFixup McFixup)
4848
: Sym(McSym), Fixup(McFixup), DF(F) {}
4949
};
5050
SmallVector<PendingMCFixup, 2> PendingFixups;
@@ -92,10 +92,10 @@ class MCObjectStreamer : public MCStreamer {
9292
}
9393

9494
/// Get a data fragment to write into, creating a new one if the current
95-
/// fragment is not a data fragment.
95+
/// fragment is not FT_Data.
9696
/// Optionally a \p STI can be passed in so that a new fragment is created
9797
/// if the Subtarget differs from the current fragment.
98-
MCDataFragment *getOrCreateDataFragment(const MCSubtargetInfo* STI = nullptr);
98+
MCFragment *getOrCreateDataFragment(const MCSubtargetInfo *STI = nullptr);
9999

100100
protected:
101101
bool changeSectionImpl(MCSection *Section, uint32_t Subsection);
@@ -109,7 +109,7 @@ class MCObjectStreamer : public MCStreamer {
109109
/// @{
110110

111111
void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
112-
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCDataFragment &F,
112+
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F,
113113
uint64_t Offset);
114114
void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
115115
void emitConditionalAssignment(MCSymbol *Symbol,

0 commit comments

Comments
 (0)