362 Commits

Author SHA1 Message Date
Sam Elliott
08289ad13a
[RISCV] Track Linker Relaxable through Assembly Relaxation (#152602)
Span-dependent instructions on RISC-V interact in a complex manner with
linker relaxation. The span-dependent assembler algorithm implemented in
LLVM has to start with the smallest version of an instruction and then
only make it larger, so we compress instructions before emitting them to
the streamer.

When the instruction is streamed, the information that the instruction
(or rather, the fixup on the instruction) is linker relaxable must be
accurate, even though the assembler relaxation process may transform a
not-linker-relaxable instruction/fixup into one that that is linker
relaxable, for instance `c.jal` becoming `qc.e.jal`, or `bne` getting
turned into `beq; jal` (the `jal` is linker relaxable).

In order for this to work, the following things have to happen:
- Any instruction/fixup which might be relaxed to a linker-relaxable
instruction/fixup, gets marked as `RelaxCandidate = true` in
RISCVMCCodeEmitter.
- In RISCVAsmBackend, when emitting the `R_RISCV_RELAX` relocation, we
have to check that the relocation/fixup kind is one that may need a
relax relocation, as well as that it is marked as linker relaxable (the
latter will not be set if relaxation is disabled).
- Linker Relaxable instructions streamed to a Relaxable fragment need to
mark the fragment and its section as linker relaxable.

I also added more debug output for Sections/Fixups which are marked
Linker Relaxable.

This results in more relocations, when these PC-relative fixups cross an
instruction with a fixup that is resolved as not linker-relaxable but
caused the fragment to be marked linker relaxable at streaming time
(i.e. `c.j`).

(cherry picked from commit 9e8f7acd2b3a71dad473565a6a6f3ba51a3e6bca)

Fixes: #150071
2025-08-18 07:51:49 +02:00
Fangrui Song
1fbfa333f6 MCAlignFragment: Rename fields and use uint8_t FillLen
* Rename the vague `Value` to `Fill`.
* FillLen is at most 8. Making the field smaller to facilitate encoding
  MCAlignFragment as a MCFragment union member.
* Replace an unreachable report_fatal_error with assert.
2025-07-13 14:07:10 -07:00
Fangrui Song
267b136359 MCFragment: Refactor code for MCFragment
To prepare for moving content and fixup member variables from
MCEncodedFragment to MCFragment and removing
MCDataFragment/MCRelaxableFragment classes, replace dyn_cast with
getKind() tests.
2025-07-12 20:29:34 -07:00
Fangrui Song
0393084adc
MC: Store MCRelaxableFragment MCInst out-of-line
Follow-up to #146307

Moved MCInst storage to MCSection, enabling trivial ~MCRelaxableFragment
and eliminating the need for a fragment walk in ~MCSection.

Updated MCRelaxableFragment::getInst to construct an MCInst on demand.
Modified MCAssembler::relaxInstruction's mayNeedRelaxation to accept
opcode and operands instead of an MCInst, avoiding redundant MCInst
creation. Note that MCObjectStreamer::emitInstructionImpl calls
mayNeedRelaxation before determining the target fragment for the MCInst.

Unfortunately, we also have to encode `MCInst::Flags` to support
the EVEX prefix, e.g. `{evex} xorw $foo, %ax`

There is a small decrease in max-rss (stage1-ReleaseLTO-g (link only))
with negligible instructions:u change.
https://llvm-compile-time-tracker.com/compare.php?from=0b533f2d9f0551aaffb13dcac8e0fd0a952185b5&to=f26b57f33bc7ccae749a57dfc841de7ce2acc2ef&stat=max-rss&linkStats=on

Next: Enable MCFragment to store fixed-size data (was MCDataFragment's job)
and optional Opcode/Operands data (was MCRelaxableFragment's job),
and delete MCDataFragment/MCRelaxableFragment.
This will allow re-encoding of Data+Relax+Data+Relax sequences as
Frag+Frag. The saving should outweigh the downside of larger
MCFragment.

Pull Request: https://github.com/llvm/llvm-project/pull/147229
2025-07-08 09:44:27 -07:00
Fangrui Song
20b3ab5683 MCFixup: Remove unused Loc argument
MCFixup::Loc has been removed in favor of MCExpr::Loc through
`const MCExpr *Value` (commit 777391a2164b89d2030ca013562151ca3c3676d1).
2025-07-04 12:23:04 -07:00
Fangrui Song
9beb467d92
MC: Store fragment content and fixups out-of-line
Moved `Contents` and `Fixups` SmallVector storage to MCSection, enabling
trivial destructors for most fragment subclasses and eliminating the need
for MCFragment::destroy in ~MCSection.

For appending content to the current section, use
getContentsForAppending. During assembler relaxation, prefer
setContents/setFixups, which may involve copying and reduce the benefits
of https://reviews.llvm.org/D145791.

Moving only Contents out-of-line caused a slight performance regression
(Alexis Engelke's 2024 prototype). By also moving Fragments out-of-line,
fragment destructors become trivial, resulting in
neglgible instructions:u increase for "stage2-O0-g" and [large max-rss decrease](https://llvm-compile-time-tracker.com/compare.php?from=84e82746c3ff63ec23a8b85e9efd4f7fccf92590&to=555a28c0b2f8250a9cf86fd267a04b0460283e15&stat=max-rss&linkStats=on)
for the "stage1-ReleaseLTO-g (link only)" benchmark.
(
An older version using fewer inline functions: https://llvm-compile-time-tracker.com/compare.php?from=bb982e733cfcda7e4cfb0583544f68af65211ed1&to=f12d55f97c47717d438951ecddecf8ebd28c296b&linkStats=on
)

Now using plain SmallVector in MCSection for storage, with potential for
future allocator optimizations, such as allocating `Contents` as the
trailing object of MCDataFragment. (GNU Assembler uses gnulib's obstack
for fragment management.)

Co-authored-by: Alexis Engelke <engelke@in.tum.de>

Pull Request: https://github.com/llvm/llvm-project/pull/146307
2025-07-01 00:21:12 -07:00
Fangrui Song
2de51345fb MCFragment: Add addFixup to replace getFixups().push_back()
to not expose SmallVector to the callers. We will make fixup storage out
of line.
2025-06-29 16:26:00 -07:00
Fangrui Song
cd075a4013 MCObjectStreamer: Deduplicate emitInstToData 2025-06-29 16:12:10 -07:00
Fangrui Song
af2bf2e544 MC: Remove unneeded MCSymbolRefExpr::VariantKind calls 2025-06-27 09:47:53 -07:00
Fangrui Song
290fc1ea11
MC,AsmPrinter: Report redefinition error instead of crashing in more cases
* Fix the crash for `.equiv b, undef; b:` (.equiv equates a symbol to an expression and reports an error if the symbol was already defined).
* Remove redundant isVariable check from emitFunctionEntryLabel

Pull Request: https://github.com/llvm/llvm-project/pull/145460
2025-06-23 23:00:03 -07:00
Fangrui Song
d4c7d0be1f MCObjectStreamer: Replace getAssemblerPtr with getAssembler
In general getAssemblerPtr should only be called by MCParse.
Revert some changes from https://reviews.llvm.org/D45164?id=143128
2025-06-13 19:12:12 -07:00
Fangrui Song
ab0931b638 MC: Set MCSection::LinkerRelaxable for linker-relaxable MCAlignFragment
Commit bb03cdcb441fd68da9d1ebb7d5f39f73667cd39c caused a Linux kernel
regression https://github.com/ClangBuiltLinux/linux/issues/2091

When a section contains linker-relaxable MCAlignmentFragment but no
linker-relaxable instructions, the RISCVAsmBackend::isPCRelFixupResolved
code path should be taken as well. The #76552 condition in the fragment
walk code will make the fixup unresolvable, leading to a relocation.
2025-05-28 22:46:37 -07:00
Fangrui Song
95756e67c2 MC: Rework .weakref
Use a variable symbol without any specifier instead of VK_WEAKREF.
Add code in ELFObjectWriter::executePostLayoutBinding to check
whether the target should be made an undefined weak symbol.

This change fixes several issues:

* Unreferenced `.weakref alias, target` no longer creates an undefined `target`.
* When `alias` is already defined, report an error instead of crashing.

.weakref is specific to ELF. llvm-ml has reused the VK_WEAKREF name for
a different concept. wasm incorrectly copied the ELF implementation.
Remove it.
2025-05-25 21:09:55 -07:00
Fangrui Song
64390b9181 MC: Remove MCAssembler argument from addFileName 2025-05-25 12:52:35 -07:00
Fangrui Song
7d71a35658 MCFixup: Remove FK_PCRel_ from getKindForSize
Remove FK_PCRel_* kinds from the generic fixup list, as they are not
generic like FK_Data_*. In getRelocType, FK_PCRel_* can be replaced with
FK_Data_* by leveraging the IsPCRel argument. Their inclusion in the
generic kind list caused confusion for PowerPC, RISCV, and VE targets.

The X86/M68k uses can be implemented as target-specific fixups.
2025-05-24 12:02:18 -07:00
Jeremy Morse
26d9cb17a6
[MC][DebugInfo] Emit linetable entries with known offsets immediately (#134677)
DWARF linetable entries are usually emitted as a sequence of
MCDwarfLineAddrFragment fragments containing the line-number difference
and an MCExpr describing the instruction-range the linetable entry
covers. These then get relaxed during assembly emission.

However, a large number of these instruction-range expressions are
ranges within a fixed MCDataFragment, i.e. a range over fixed-size
instructions that are not subject to relaxation at a later stage. Thus,
we can compute the address-delta immediately, and not spend time and
memory describing that computation so it can be deferred.
2025-05-20 21:26:56 +01:00
Fangrui Song
39662922e1 MCObjectStreamer: Refine absoluteSymbolDiff condition
The function is called to test the fast path - when Lo/Hi are within the
same fragment. This is unsafe - Lo/Hi at the begin and end of a
relaxable fragment should not evaluate to a constant. However, we don't
have tests that exercise the code path.

Nevertheless, make the check safer and remove the now unnecessary
isRISCV check (from https://reviews.llvm.org/D103539).
2025-04-12 19:02:07 -07:00
Fangrui Song
b1cd3cb3f4 [MC] Replace getSymA()->getSymbol() with getAddSym. NFC
We will replace the MCSymbolRefExpr member in MCValue with MCSymbol.
This change reduces dependence on MCSymbolRefExpr.
2025-04-05 13:34:24 -07:00
Fangrui Song
086af83688 [MC] Replace getSymA()->getSymbol() with getAddSym. NFC
We will replace the MCSymbolRefExpr member in MCValue with MCSymbol.
This change reduces dependence on MCSymbolRefExpr.
2025-04-05 13:23:14 -07:00
Fangrui Song
b73e144bdf MCValue: Simplify code with getSubSym
MCValue::SymB is a MCSymbolRefExpr *, which might become MCSymbol * in
the future. Simplify some code that uses MCValue::SymB.
2025-03-23 12:13:13 -07:00
Fangrui Song
7722d7519c [MC] evaluateAsRelocatableImpl: remove the Fixup argument
Follow-up to d6fbffa23c84e622735b3e880fd800985c1c0072 . This commit
updates all call sites and removes the argument from the function.
2025-03-15 16:10:19 -07:00
Yaxun (Sam) Liu
da0f9e75d8
Reland: [MC] output inlined-at debug info (#106230) (#130306)
Reland https://github.com/llvm/llvm-project/pull/106230

The original PR was reverted due to compilation time regression.

This PR fixed that by adding a condition OutStreamer->isVerboseAsm() to
the generation of extra inlined-at debug info, so that it does not
affect normal compilation time.

Currently MC print source location of instructions in comments in
assembly when debug info is available, however, it does not include
inlined-at locations when a function is inlined.

For example, function foo is defined in header file a.h and is called
multiple times in b.cpp. If foo is inlined, current assembly will only
show its instructions with their line numbers in a.h. With inlined-at
locations, the assembly will also show where foo is called in b.cpp.

This patch adds inlined-at locations to the comments by using
DebugLoc::print. It makes the printed source location info consistent
with those printed by machine passes.
2025-03-11 09:43:14 -04:00
Nikita Popov
aa1d2cc5d7 Revert "[MC] output inlined-at debug info (#106230)"
This reverts commit f3dc358953a13caf7521fc615a08f6317930351c.

This causes a large compile-time regression:
https://llvm-compile-time-tracker.com/compare.php?from=267403442264959f6b06e227ff450c385f4b3ef2&to=f3dc358953a13caf7521fc615a08f6317930351c&stat=instructions:u
2025-03-07 09:45:15 +01:00
Yaxun (Sam) Liu
f3dc358953
[MC] output inlined-at debug info (#106230)
Currently MC print source location of instructions in comments in
assembly when debug info is available, however, it does not include
inlined-at locations when a function is inlined.

For example, function foo is defined in header file a.h and is called
multiple times in b.cpp. If foo is inlined, current assembly will only
show its instructions with their line numbers in a.h. With inlined-at
locations, the assembly will also show where foo is called in b.cpp.

This patch adds inlined-at locations to the comments by using
DebugLoc::print. It makes the printed source location info consistent
with those printed by machine passes.
2025-03-06 22:47:11 -05:00
Fangrui Song
60486292b7 [MC] Move MIPS-specific gprel/tprel/dtprel from MCStreamer to MipsTargetStreamer
https://reviews.llvm.org/D23669 inappropriately added MIPS-specific
dtprel/tprel directives to MCStreamer. In addition,
llvm-mc -filetype=null parsing these directives will crash.
This patch moves these functions to MipsTargetStreamer and fixes
-filetype=null.

gprel32 and gprel64, called by AsmPrinter, are moved to
MCTargetStreamer.
2025-03-02 14:59:21 -08:00
Fangrui Song
3fa5b52714 [MC] Decrease direct access to getContents()
to make it easier to store fragment contents out-of-line. Loosely based
on Alexis Engelke's prototype.
2024-12-21 17:14:26 -08:00
alx32
28646d0cc1
[MC] Add .loc_label instruction (#99710)
As discussed in [the
RFC](https://discourse.llvm.org/t/rfc-extending-llvm-mc-loc-directive-with-labeling-support/79608)
we need a way to create labels in the assembler-generated line section
in order to support the future addition of the
[DW_AT_LLVM_stmt_sequence](https://discourse.llvm.org/t/rfc-new-dwarf-attribute-for-symbolication-of-merged-functions/79434)
attribute.

We have a similar precedent for such behavior with the
[.cfi_label](https://github.com/llvm/llvm-project/pull/97922)
instruction - so we add the `.loc_label THE_LABEL_NAME` instruction
which:
- Terminates the current line sequence in the line section
- Creates a new label with the specified label name in the `.debug_line`
section
2024-09-20 06:32:34 -07:00
Fangrui Song
211494705b MCObjectStreamer: Remove an unneeded getBackendPtr test
All of `MCAsmBackend`, `MCCodeEmitter`, and `MCObjectWriter` must be
non-null.
2024-07-22 23:03:43 -07:00
Fangrui Song
219d80bcb7 MCAssembler: Move FileNames and CompilerVersion to MCObjectWriter 2024-07-22 20:20:32 -07:00
Alexis Engelke
f15266e9d5
[MC][ELF] Emit instructions directly into fragment (#94950)
Avoid needless copying of instructions and fixups and directly emit into
the fragment small vectors.

This (optionally, second commit) also removes the single use of the
MCCompactEncodedInstFragment to simplify code.
2024-07-04 16:46:31 +02:00
Fangrui Song
db48f1a176 [MC] Remove nullable getCurrentSectionOnly use from AsmParser
We will implement getCurrentSectionOnly with `CurFrag->getParent()`,
which is non-null. Eliminate a nullable use.
2024-06-27 22:37:37 -07:00
Fangrui Song
fcffb2c024 [MC] Replace one nullable getCurrentSectionOnly with CurFrag
We will implement getCurrentSectionOnly with `CurFrag->getParent()`,
which is non-null. Eliminate a nullable use.
2024-06-27 22:27:14 -07:00
Fangrui Song
fadc72235e [MC] switchSectionNoPrint: update CurFrag
To ensure that CurFrag->Parent and SectionStack.back() are in sync.
2024-06-27 22:00:40 -07:00
Fangrui Song
eaf816f6cb [MC] Remove unneeded nullness check from getOrCreateDataFragment. NFC
`getCurrentFragment()` no longer returns null after
7423bf78eb53d81ce0c7b3a38e39a56341ca2a89.
2024-06-27 20:13:10 -07:00
Fangrui Song
e48c4011ca [MC] Cache current fragment in MCStreamer
This eliminates indirection through `getCurrentSectionOnly()->curFragList()->Tail`.
2024-06-27 19:44:37 -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
aa3589f0cc
[MC,X86] emitInstruction: remove virtual function calls due to Intel JCC Erratum
https://reviews.llvm.org/D70157 (for Intel Jump Conditional Code
Erratum) introduced two virtual function calls in the generic
MCObjectStreamer::emitInstruction, which added some overhead.

This patch removes the virtual function overhead:

* Define `llvm::X86_MC::emitInstruction` that calls `emitInstruction{Begin,End}`.
* Define {X86ELFStreamer,X86WinCOFFStreamer}::emitInstruction to call `llvm::X86_MC::emitInstruction`

Pull Request: https://github.com/llvm/llvm-project/pull/96835
2024-06-27 09:43:32 -07:00
Fangrui Song
e7622ab472 [MC] Remove unused MCObjectStreamer::CurSubsectionIdx. NFC 2024-06-22 23:23:47 -07:00
Fangrui Song
95f983f823 [MC] Change Subsection parameters from const MCExpr * to uint32_t
Follow-up to 05ba5c0648ae5e80d5afce270495bf3b1eef9af4. uint32_t is
preferred over const MCExpr * in the section stack uses because it
should only be evaluated once. Change the paramter type to match.
2024-06-22 21:48:11 -07:00
Fangrui Song
05ba5c0648 [MC] MCSectionSubPair: replace const MCExpr * with uint32_t 2024-06-22 19:44:52 -07:00
Fangrui Song
485d7eaefd [MC] Remove remnant code related to pending labels 2024-06-22 09:41:34 -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
4684d0c007 [MC] emitLabelAtPos: change parameter to MCDataFragment &. NFC
emitLabelAtPos is only called by ARMELFStreamer with MCDataFragment.
2024-06-21 23:46:58 -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
90a23d3a5a [MC] Restore setOffset(0) in emitLabel
For bolt/test/runtime/X86/exceptions-pic.test, llvm-bolt seems to call
emitLabel twice and the assert will fail. Work around it after
2cc4bc132cbcc76c5552cbc128830943ea596b3e
2024-06-12 19:53:43 -07:00
Fangrui Song
2cc4bc132c MCFragment: Initialize Offset to 0
After 9d0754ada5dbbc0c009bcc2f7824488419cc5530 ("[MC] Relax fragments
eagerly") removes the assert of Offset, it is no longer useful to
initialize the member to -1.

Now the symbol value estimate is more precise, which leads to slight
behavior change to layout-interdependency.s.
2024-06-12 14:13:20 -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