610 Commits

Author SHA1 Message Date
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
Fangrui Song
87424778ef [MC] Remove the Parent parameter from MCFragment ctor callers. NFC 2024-06-21 22:47:55 -07:00
Fangrui Song
b1932b8483
[MC] Aligned bundling: remove special handling for RelaxAll
When both aligned bundling and RelaxAll are enabled, bundle padding is
directly written into fragments (https://reviews.llvm.org/D8072).
(The original motivation was memory usage, which has been achieved from
different angles with recent assembler improvement).

The code presents challenges with the work to replace fragment
representation (e.g. #94950 #95077). This patch removes the special
handling. RelaxAll still works but the behavior seems slightly different
as revealed by 2 changed tests. However, most `-mc-relax-all` tests are
unchanged.

RelaxAll used to be the default for clang -O0. This mode has significant
code size drawbacks and newer Clang doesn't use it (#90013).

---

flushPendingLabels: The FOffset parameter can be removed: pending labels
will be assigned to the incoming fragment at offset 0.

Pull Request: https://github.com/llvm/llvm-project/pull/95188
2024-06-14 10:01:36 -07:00
Fangrui Song
f808abf508
[MC] Add MCFragment allocation helpers
`allocFragment` might be changed to a placement new when the allocation
strategy changes.

`allocInitialFragment` is to deduplicate the following pattern
```
  auto *F = new MCDataFragment();
  Result->addFragment(*F);
  F->setParent(Result);
```

Pull Request: https://github.com/llvm/llvm-project/pull/95197
2024-06-14 09:39:32 -07:00
Fangrui Song
de19f7b6d4
[MC] Replace fragment ilist with singly-linked lists
Fragments are allocated with `operator new` and stored in an ilist with
Prev/Next/Parent pointers. A more efficient representation would be an
array of fragments without the overhead of Prev/Next pointers.

As the first step, replace ilist with singly-linked lists.

* `getPrevNode` uses have been eliminated by previous changes.
* The last use of the `Prev` pointer remains: for each subsection, there is an insertion point and
  the current insertion point is stored at `CurInsertionPoint`.
* `HexagonAsmBackend::finishLayout` needs a backward iterator. Save all
  fragments within `Frags`. Hexagon programs are usually small, and the
  performance does not matter that much.

To eliminate `Prev`, change the subsection representation to
singly-linked lists for subsections and a pointer to the active
singly-linked list. The fragments from all subsections will be chained
together at layout time.

Since fragment lists are disconnected before layout time, we can remove
`MCFragment::SubsectionNumber` (https://reviews.llvm.org/D69411). The
current implementation of `AttemptToFoldSymbolOffsetDifference` requires
future improvement for robustness.

Pull Request: https://github.com/llvm/llvm-project/pull/95077
2024-06-11 09:18:31 -07:00
Fangrui Song
cb63abca27 [MC] Remove getFragmentList uses. NFC 2024-06-10 18:27:34 -07:00
Fangrui Song
bb4ee27a31 [MC] Remove the last MCFragment::getPrevNode use. NFC 2024-06-09 23:14:15 -07:00
Fangrui Song
9d0754ada5 [MC] Relax fragments eagerly
Lazy relaxation caused hash table lookups (`getFragmentOffset`) and
complex use/compute interdependencies. Some expressions involding
forward declared symbols (e.g. `subsection-if.s`) cannot be computed.
Recursion detection requires complex `IsBeingLaidOut`
(https://reviews.llvm.org/D79570).

D76114's `invalidateFragmentsFrom` makes lazy relaxation even less
useful.

Switch to eager relaxation to greatly simplify code and resolve these
issues. This change also removes a `getPrevNode` use, which makes it
more feasible to replace the fragment representation, which might yield
a large peak RSS win.

Minor downsides: The number of section relaxations may increase (offset
by avoiding the hash table lookup). For relax-recompute-align.s, the
computed layout is not optimal.
2024-06-09 23:05:05 -07:00
Amir Ayupov
9fec33aadc Revert "[BOLT] Fix unconditional output of boltedcollection in merge-fdata (#78653)"
This reverts commit 82bc33ea3f1a539be50ed46919dc53fc6b685da9.

Accidentally pushed unrelated changes.
2024-01-18 19:59:09 -08:00
Amir Ayupov
82bc33ea3f
[BOLT] Fix unconditional output of boltedcollection in merge-fdata (#78653)
Fix the bug where merge-fdata unconditionally outputs boltedcollection 
line, regardless of whether input files have it set.

Test Plan:
Added bolt/test/X86/merge-fdata-nobat-mode.test which fails without this
fix.
2024-01-18 19:44:16 -08:00
Jinyang He
b57159cb19
[LoongArch] Support R_LARCH_{ADD,SUB}_ULEB128 for .uleb128 and force relocs when sym is not in section (#76433)
1, Follow RISCV 1df5ea29 to support generates relocs for .uleb128 which
can not be folded. Unlike RISCV, the located content of LoongArch should
be zero. LoongArch fixup uleb128 value by in-place addition and
subtraction reloc types named R_LARCH_{ADD,SUB}_ULEB128. The located
content can affect the result and R_LARCH_ADD_ULEB128 has enough info to
represent the first symbol value, so it needs to be set to zero.
2, Force relocs if sym is not in section so that it can emit relocs for
external symbol.

Fixes:
https://github.com/llvm/llvm-project/pull/72960#issuecomment-1866844679
2024-01-09 15:14:54 +08:00
Craig Topper
e87f33d9ce
[RISCV][MC] Pass MCSubtargetInfo down to shouldForceRelocation and evaluateTargetFixup. (#73721)
Instead of using the STI stored in RISCVAsmBackend, try to get it from
the MCFragment.

This addresses the issue raised here
https://discourse.llvm.org/t/possible-problem-related-to-subtarget-usage/75283
2023-12-07 13:17:58 -08:00