Skip to content

Commit cf103ef

Browse files
committed
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=267b136359d8448c73432b4f3ceeefbf4c35e00b&to=ef026be147babfd2b8f4ed4d875deadcbbbb2bbb&stat=instructions:u ``` stage2-O0-g instructins:u geomeon (-0.12%) 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: llvm#148544
1 parent 13d8188 commit cf103ef

28 files changed

+495
-453
lines changed

llvm/include/llvm/MC/MCAsmBackend.h

Lines changed: 6 additions & 10 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
}

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 7 additions & 11 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;
@@ -107,7 +104,7 @@ class MCAssembler {
107104

108105
/// Check whether a fixup can be satisfied, or whether it needs to be relaxed
109106
/// (increased in size, in order to hold its value correctly).
110-
bool fixupNeedsRelaxation(const MCRelaxableFragment &, const MCFixup &) const;
107+
bool fixupNeedsRelaxation(const MCFragment &, const MCFixup &) const;
111108

112109
void layoutSection(MCSection &Sec);
113110
/// Perform one layout iteration and return the index of the first stable
@@ -116,11 +113,11 @@ class MCAssembler {
116113

117114
/// Perform relaxation on a single fragment.
118115
bool relaxFragment(MCFragment &F);
119-
bool relaxInstruction(MCRelaxableFragment &IF);
120-
bool relaxLEB(MCLEBFragment &IF);
116+
bool relaxInstruction(MCFragment &F);
117+
bool relaxLEB(MCFragment &F);
121118
bool relaxBoundaryAlign(MCBoundaryAlignFragment &BF);
122-
bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF);
123-
bool relaxDwarfCallFrameFragment(MCDwarfCallFrameFragment &DF);
119+
bool relaxDwarfLineAddr(MCFragment &F);
120+
bool relaxDwarfCallFrameFragment(MCFragment &F);
124121
bool relaxCVInlineLineTable(MCCVInlineLineTableFragment &DF);
125122
bool relaxCVDefRange(MCCVDefRangeFragment &DF);
126123
bool relaxFill(MCFillFragment &F);
@@ -228,8 +225,7 @@ class MCAssembler {
228225

229226
/// Write the necessary bundle padding to \p OS.
230227
/// Expects a fragment \p F containing instructions and its size \p FSize.
231-
LLVM_ABI void writeFragmentPadding(raw_ostream &OS,
232-
const MCEncodedFragment &F,
228+
LLVM_ABI void writeFragmentPadding(raw_ostream &OS, const MCFragment &F,
233229
uint64_t FSize) const;
234230

235231
LLVM_ABI void reportError(SMLoc L, const Twine &Msg) const;

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: 3 additions & 3 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;
@@ -95,7 +95,7 @@ class MCObjectStreamer : public MCStreamer {
9595
/// fragment is not a data fragment.
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);

0 commit comments

Comments
 (0)