521 Commits

Author SHA1 Message Date
Fangrui Song
be5a845e4c [MC] Compute fragment offsets eagerly
This builds on top of commit 9d0754ada5dbbc0c009bcc2f7824488419cc5530
("[MC] Relax fragments eagerly") and relaxes fragments eagerly to
eliminate MCSection::HasLayout and `getFragmentOffset` overhead. The
approach is slightly different from
1a47f3f3db66589c11f8ddacfeaecc03fb80c510 and has less performance
benefit.

The new layout algorithm also addresses the following problems:

* Size change of MCFillFragment/MCOrgFragment did not influence the
  fixed-point iteration, which could be problematic for contrived cases.
* The `invalid number of bytes` error was reported too early. Since
  `.zero A-B` might have temporary negative values in the first few
  iterations.
* X86AsmBackend::finishLayout performed only one iteration, which might
  not converge. In addition, the removed `#ifndef NDEBUG` code (disabled
  by default) in X86AsmBackend::finishLayout was problematic, as !NDEBUG
  and NDEBUG builds evaluated fragment offsets at different times before
  this patch.
* The computed layout for relax-recompute-align.s is optimal now.

Builds with many text sections (e.g. full LTO) shall observe a decrease
in compile time while the new algorithm could be slightly slower for
some -O0 -g projects.

Aligned bundling from the deprecated PNaCl placed constraints how we can
perform iteration.
2024-07-30 18:38:03 -07:00
Fangrui Song
4eb5450f63 Revert "[MC] Compute fragment offsets eagerly"
This reverts commit 1a47f3f3db66589c11f8ddacfeaecc03fb80c510.

Fix #100283

This commit is actually a trigger of other preexisting problems:

* Size change of fill fragments does not influence the fixed-point iteration.
* The `invalid number of bytes` error is reported too early. Since
  `.zero A-B` might have temporary negative values in the first few
  iterations.

However, the problems appeared at least "benign" (did not affect the
Linux kernel builds) before this commit.
2024-07-30 14:52:29 -07:00
Fangrui Song
de5aa8d006 [MC] Remove unused MCCompactEncodedInstFragment
This has been used after #94950.
2024-07-28 22:35:56 -07:00
Fangrui Song
b2f5ac678f MCAssembler: Remove unused functions 2024-07-22 23:34:19 -07:00
Fangrui Song
f017d89b22 MCAssembler: Move SubsectionsViaSymbols; to MCObjectWriter 2024-07-22 23:31:01 -07:00
Fangrui Song
99e6631e30 MCAssembler: Remove getWriterPtr
Commit 8728e097dfbec3630a1dd907431c0f14274a1ae8 (2018) introduced
getWriterPtr, which might be nullptr for MCAsmStreamer. This lured call
sites to use getWriterPtr when it's guaranteed to be non-null for
MCObjectstreamer. Remove this misleading API.
2024-07-22 22:31:08 -07:00
Fangrui Song
eb2239299e MCAssembler: Move LinkerOptions to MachObjectWriter 2024-07-22 22:02:51 -07:00
Fangrui Song
219d80bcb7 MCAssembler: Move FileNames and CompilerVersion to MCObjectWriter 2024-07-22 20:20:32 -07:00
Fangrui Song
c473e75ade MCAssmembler: Move ELFHeaderEFlags to ELFObjectWriter
Now that MCELFStreamer can access ELFObjectWriter (commit
70c52b62c5669993e341664a63bfbe5245e32884), we can move ELFHeaderEFlags
there.
2024-07-22 18:20:18 -07:00
Fangrui Song
1a47f3f3db [MC] Compute fragment offsets eagerly
This builds on top of commit 9d0754ada5dbbc0c009bcc2f7824488419cc5530
("[MC] Relax fragments eagerly") and relaxes fragments eagerly to
eliminate MCSection::HasLayout and `getFragmentOffset` overhead.

Note: The removed `#ifndef NDEBUG` code (disabled by default) in
X86AsmBackend::finishLayout was problematic, as (a) !NDEBUG and NDEBUG
builds evaluated fragment offsets at different times before this patch
(b) one iteration might not be sufficient to converge. There might be
some edge cases that it did not handle. Anyhow, this patch probably
makes it work for more cases.
2024-07-21 15:42:27 -07:00
Fangrui Song
09a399a1dd [MC] Move VersionInfo to MachObjectWriter 2024-07-21 13:03:21 -07:00
Fangrui Song
9539a77960 [MC] Export llvm::WinCOFFObjectWriter and access it from MCWinCOFFStreamer
Similar to commit 28fcafb50274be2520117eacb0a886adafefe59d (2011) for
MachObjectWriter. MCWinCOFFStreamer can now access WinCOFFObjectWriter
directly without holding object file format specific inforamtion in
MCAssembler (e.g. IncrementalLinkerCompatible).
2024-07-21 12:04:47 -07:00
Fangrui Song
a2af375556 [MC] Move LOHContainer to MachObjectwriter 2024-07-21 11:19:52 -07:00
Fangrui Song
b75453bc07 MCAssembler: Remove unneeded non-const iterators for Sections and misleading size()
The pointers cannot be mutated even if the dereferenced MCSection can.
2024-07-05 15:42:38 -07:00
Fangrui Song
4a0aff199b MCAssembler: Clean up iterator types for Symbols 2024-07-05 15:15:01 -07:00
Fangrui Song
009082aa4b [MC] Move MCAssembler::DataRegions to MachObjectWriter
and make some cleanup.
2024-07-04 23:34:54 -07:00
Fangrui Song
1d4d92d1cc [MC] Move MCAssembler::IndirectSymbols to MachObjectWriter 2024-07-04 22:56:03 -07:00
Fangrui Song
94471e73fe [MC] Move MCAssembler::isSymbolLinkerVisible to MCSymbolMachO 2024-07-03 17:25:10 -07:00
Fangrui Song
35668e2c9c
Remove llvm/MC/MCAsmLayout.h and the unused parameter in MCAssembler::layout
This restores 63ec52f867ada8d841dd872acf3d0cb62e2a99e8 and
46f7929879a59ec72dc75679b4201e2d314efba9, NFC changes that were
unnecessarily reverted.

This completes the work that merges MCAsmLayout into MCAssembler.

Pull Request: https://github.com/llvm/llvm-project/pull/97449
2024-07-02 16:56:35 -07:00
Fangrui Song
e94a00c3b8 [MC] Use a stub ctor for MCAsmLayout
and replace MCAssembler::Layout with a bool.
This mostly completes "[MC] Start merging MCAsmLayout into MCAssembler".

Note: BOLT used a dummy `MCAsmLayout` to call `getSymbolOffset`, which
is technically not supported. There is some discussion in
https://reviews.llvm.org/D154604 .

The revert f80a4072ced41b52363c63df28fea9a649f7f89e is incorrect and
actually broke bots.
2024-07-02 10:10:31 -07:00
Davide Italiano
f80a4072ce Revert "[MC] Use a stub ctor for MCAsmLayout"
This reverts commit bbb50369a149d9a7d1f91efaaabf75c260a220c7.

This breaks BOLT.
2024-07-02 08:54:18 -07:00
Davide Italiano
ac0b48a0db Revert "MCAssembler::layout: remove the unused MCAsmLayout parameter"
This reverts commit 63ec52f867ada8d841dd872acf3d0cb62e2a99e8.
2024-07-02 08:54:05 -07:00
Davide Italiano
48263bd7e7 Revert "[MC] Remove assert to work around BOLT"
This reverts commit c72cb2766cec0ac519a051780ae5aed42485e012.
The patch was not reviewed, and this assertion guards against incorrect
values for symbols if the linker decides to modify the layout as it
happens on some platforms. getSymbolOffset() is used for code size
estimation, among others.

Reverting for now, until we get proper code review and undersatnding of
the implications.
2024-07-02 08:41:24 -07:00
Fangrui Song
c72cb2766c [MC] Remove assert to work around BOLT
BOLT used a dummy `MCAsmLayout` to call `getSymbolOffset`, which is
technically not supported. There is some discussion in
https://reviews.llvm.org/D154604 that this is not ideal.
For now, remove the assert.

The assert was added by bbb50369a149d9a7d1f91efaaabf75c260a220c7.
2024-07-01 20:41:15 -07:00
Fangrui Song
63ec52f867 MCAssembler::layout: remove the unused MCAsmLayout parameter
Almost complete the MCAsmLayout removal work started by 67957a45ee1ec42ae1671cdbfa0d73127346cc95.
2024-07-01 18:17:05 -07:00
Fangrui Song
bbb50369a1 [MC] Use a stub ctor for MCAsmLayout
and replace MCAssembler::Layout with a bool.
This mostly completes "[MC] Start merging MCAsmLayout into MCAssembler".
2024-07-01 18:04:27 -07:00
Fangrui Song
e8e05c3192 [MC] Remove the MCAsmLayout parameter from MCAsmBackend::finishLayout
And remove unnecessary MCAssembler::finishLayout.
2024-07-01 17:36:16 -07:00
Fangrui Song
66cd8ec4c0 MCCodeView: replace the MCAsmLayout parameter with MCAssembler 2024-07-01 17:08:38 -07:00
Fangrui Song
bd3215149a MCExpr::evaluateKnownAbsolute: replace the MCAsmLayout parameter with MCAssembler
and add a comment.
2024-07-01 16:45:57 -07:00
Fangrui Song
e25e8003ca MCExpr::evaluateAsRelocatable: replace the MCAsmLayout parameter with MCAssembler
Continue the MCAsmLayout removal work started by 67957a45ee1ec42ae1671cdbfa0d73127346cc95.
2024-07-01 16:23:43 -07:00
Fangrui Song
78804f891c [MC] Remove the evaluateAsAbsolute overload that takes a MCAsmLayout parameter
Continue the MCAsmLayout removal work started by 67957a45ee1ec42ae1671cdbfa0d73127346cc95.
2024-07-01 15:38:18 -07:00
Fangrui Song
8f7887ad53 [MC] Remove the MCAsmLayout parameter from MCAsmBackend::relax*
finishLayout is now the only member function that has a MCAsmLayout
parameter.
2024-07-01 14:36:25 -07:00
Fangrui Song
7840c00668 [MC] Move MCAsmLayout::SectionOrder to MachObjectWriter::SectionOrder
Follow-up to 2c1fb411ce3aed148a278660d215e0f88ff9b9be.

SectionOrder is Mach-O specific to place zerofill sections after
non-zerofill sections in the object writer.
2024-07-01 13:17:53 -07:00
Fangrui Song
dbf12b2f77 [MC] Remove MCAsmLayout::{getSymbolOffset,getBaseSymbol}
The MCAsmLayout::* forwarders added by
67957a45ee1ec42ae1671cdbfa0d73127346cc95 have all been removed.
2024-07-01 11:51:26 -07:00
Fangrui Song
a40ca78bb9 [MC] Remove MCAsmLayout::{getSectionFileSize,getSectionAddressSize} 2024-07-01 11:27:32 -07:00
Fangrui Song
658c03d587 [MC] Remove the MCAsmLayout parameter from evaluateTargetFixup 2024-07-01 11:10:51 -07:00
Fangrui Song
a5e905b73d [MC] Remove MCAsmLayout::getFragmentAddress 2024-07-01 11:02:15 -07:00
Fangrui Song
6b707a8cc1 [MC] Remove the MCAsmLayout parameter from MCObjectWriter::executePostLayoutBinding 2024-07-01 10:47:46 -07:00
Fangrui Song
23e6224374 [MC] Remove the MCAsmLayout parameter from MCObjectWriter::{writeObject,writeSectionData} 2024-07-01 10:04:59 -07:00
Fangrui Song
22c7317f1e [MC] Remove the MCAsmLayout parameter from relocation related functions 2024-07-01 00:16:48 -07:00
Fangrui Song
4289c422a8 [MC] Remove the MCAsmLayout parameter from MCObjectWriter::recordRelocation 2024-06-30 22:13:54 -07:00
Fangrui Song
67957a45ee [MC] Start merging MCAsmLayout into MCAssembler
Follow-up to 10c894cffd0f4bef21b54a43b5780240532e44cf.

MCAsmLayout, introduced by ac8a95498a99eb16dff9d3d0186616645d200b6e
(2010), provides APIs to compute fragment/symbol/section offsets.
The separate class is cumbersome and passing it around has overhead.
Let's remove it as the underlying implementation is tightly coupled with
MCAsmLayout anyway.

Some forwarders are added to ease migration.
2024-06-30 16:10:27 -07:00
Fangrui Song
10c894cffd [MC] Move MCAsmLayout from MCFragment.cpp to MCAssembler.cpp. NFC
8d736236d36ca5c98832b7631aea2e538f6a54aa (2015) moved these MCAsmLayout
functions to MCFragment.cpp, but the original placement is better as
these functions are tightly coupled with MCAssembler.cpp.
2024-06-30 14:22:25 -07:00
Fangrui Song
41a08e764a [MC] Move Mach-O specific getAtom and isSectionAtomizableBySymbols to Mach-O files
and devirtualize isSectionAtomizableBySymbols.
2024-06-30 10:23:13 -07:00
Fangrui Song
6cb69d6c1a [MC] Move bool members earlier. NFC
RelaxAll/SubsectionsViaSyms are used more frequently then vector members
like Sections/Symbols. The size of MCAssembler doesn't particularly
matter, so avoid bit fields.
2024-06-30 09:49:32 -07:00
Fangrui Song
7423bf78eb [MC] Ensure subsections have a MCDataFragment
Similar to 21fac2d1d060b0f9b11a746718e58d4cd1ee97e5 for sections. This
makes it feasible to cache the current fragment in MCStreamer.
2024-06-27 19:12:35 -07:00
Fangrui Song
abfff89b74 [MC] Chain together fragments only if Subsections.size() > 1
and delete an unneeded setParent call.
2024-06-27 10:35:45 -07:00
Fangrui Song
21fac2d1d0 [MC] Ensure all new sections have a MCDataFragment
MCAssembler::layout ensures that every section has at least one
fragment, which simplifies MCAsmLayout::getSectionAddressSize (see
e73353c7201a3080851d99a16f5fe2c17f7697c6 from 2010). It's better to
ensure the condition is satisfied at create time (COFF, GOFF, Mach-O) to
simplify more fragment processing.
2024-06-23 10:08:52 -07:00
Fangrui Song
c9f6a5e495 [MC] Move computeBundlePadding closer to its only caller. NFC
There is only one caller after #95188.
2024-06-22 13:28:07 -07:00
Fangrui Song
7500646629 [MC] Remove pending labels
This commit removes the complexity introduced by pending labels in
https://reviews.llvm.org/D5915 by using a simpler approach. D5915 aimed
to ensure padding placement before `.Ltmp0` for the following code, but
at the cost of expensive per-instruction `flushPendingLabels`.

```
// similar to llvm/test/MC/X86/AlignedBundling/labeloffset.s
.bundle_lock align_to_end
  calll   .L0$pb
.bundle_unlock
.L0$pb:
  popl    %eax
.Ltmp0:   //// padding should be inserted before this label instead of after
  addl    $_GLOBAL_OFFSET_TABLE_+(.Ltmp0-.L0$pb), %eax
```

(D5915 was adjusted by https://reviews.llvm.org/D8072 and
https://reviews.llvm.org/D71368)

This patch achieves the same goal by setting the offset of the empty
MCDataFragment (`Prev`) in `layoutBundle`. This eliminates the need for
pending labels and simplifies the code.

llvm/test/MC/MachO/pending-labels.s (D71368): relocation symbols are
changed, but the result is still supported by linkers.
2024-06-22 00:34:16 -07:00