5847 Commits

Author SHA1 Message Date
Kazu Hirata
cbf5af9668
[llvm] Remove unused includes (NFC) (#154051)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-08-17 23:46:35 -07:00
Fangrui Song
34c7b7ccae MCSymbol: Remove setUndefined
The name is misleading, as setting Fragment to nullptr does not
necessarily make it undefined - common and equated symbols have
a nullptr fragment as well.
2025-08-17 15:57:27 -07:00
Fangrui Song
aa96e20dce MCSymbol: Remove AMDGPU-specific Kind::TargetCommon
The SymContentsTargetCommon kind introduced by
https://reviews.llvm.org/D61493 lackes significant and should be treated
as a regular common symbol with a different section index.

Update ELFObjectWriter to respect the specified section index.
The new representation also works with Hexagon's SHN_HEXAGON_SCOMMON.
2025-08-16 15:39:33 -07:00
Fangrui Song
190778a8ba MCSymbol: Rename SymContents to kind
The names "SymbolContents" and "SymContents*" members are confusing.
Rename to kind and Kind::XXX similar to lld/ELF/Symbols.h

Rename SymContentsVariable to Kind::Equated as the former term is
"equated symbol", not "variable".
2025-08-16 15:10:35 -07:00
Fangrui Song
1893caa9bc MCSymbol: Decrease the bitfield size of SymbolContents
Follow-up to 57b0843f68f5f349c73d1bf54e321a1a6d1800bf

The size of MCSymbol has been reduced to 24 bytes on 64-bit systems.
2025-08-16 10:43:05 -07:00
Abhishek Kaushik
2415e3b3bf
[NFC][MC][GOFF] Use llvm_unreachable for unreachable case (#152930) 2025-08-13 12:56:12 +05:30
Sterling-Augustine
2e9944a03e
Generate an .sframe section with a skeleton header (#151223)
This continues the sframe implementation discussed previously.

Of note, this also adds some target dependent functions to the object
file. Additional fields will be needed later. It would be possible to do
all of this inside the sframe implementation itself if it feels a little
messy and specialized, but generally I think that target info goes with
target info.

Another question is if we want a sentinel value for unimplemented sframe
abi arches, or a std::optional. Both work.
2025-08-12 12:57:31 -07:00
Sam Elliott
9e8f7acd2b
[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`).

Fixes: #150071
2025-08-12 09:02:48 +01:00
Abhishek Kaushik
61842ac379
Revert "[NFC][MC] Removed unused switch case in emitCATTR" (#152929)
Reverts llvm/llvm-project#152907

This is causing some build failures.
2025-08-10 22:20:07 +05:30
Abhishek Kaushik
9247b51a02
[NFC][MC] Removed unused switch case in emitCATTR (#152907)
The switch statement `switch (Rmode)` is in an if-block that checks if
`Rmode != GOFF::ESD_RMODE_None` making the `case GOFF::ESD_RMODE_None:`
unnecessary.
2025-08-10 21:46:17 +05:30
Fangrui Song
3769ce013b
MC: Refine ALIGN relocation conditions
Each section now tracks the index of the first linker-relaxable
fragment, enabling two changes:

* Delete redundant ALIGN relocations before the first linker-relaxable
  instruction in a section. The primary example is the offset 0
  R_RISCV_ALIGN relocation for a text section aligned by 4.
* For alignments larger than the NOP size after the first
  linker-relaxable instruction, ALIGN relocations are now generated, even in
  norelax regions. This fixes the issue #150159.

The new test llvm/test/MC/RISCV/Relocations/align-after-relax.s
verifies the required ALIGN in a norelax region following
linker-relaxable instructions.
By using a fragment index within the subsection (which is less than or
equal to the section's index), the implementation may generate redundant
ALIGN relocations in lower-numbered subsections before the first
linker-relaxable instruction.

align-option-relax.s demonstrates the ALIGN optimization.
Add an initial `call` to a few tests to prevent the ALIGN optimization.

---

When the alignment exceeds 2, we insert $alignment-2 bytes of NOPs, even
in non-RVC code. This enables non-RVC code following RVC code to handle
a 2-byte adjustment without requiring an additional state in MCSection
or AsmParser.

```
.globl _start
_start:
// GNU ld can relax this to  6505          lui     a0, 0x1
// LLD hasn't implemented this transformation.
  lui a0, %hi(foo)

.option push
.option norelax
.option norvc
// Now we generate R_RISCV_ALIGN with addend 2, even if this is a norvc region.
.balign 4
b0:
  .word 0x3a393837
.option pop
foo:
```

Pull Request: https://github.com/llvm/llvm-project/pull/150816
2025-08-07 19:16:58 -07:00
Tim Renouf
e99c565cd2
MC,AMDGPU: Don't pad .text with s_code_end if it would otherwise be empty (#147980)
We don't want that padding in a module that only contains data, not
code.

Also fix MCSection::hasInstructions() so it works with the asm streamer
too.
2025-08-06 13:25:45 +01:00
Fangrui Song
e8fc808bf8 Reapply "MCFragment: Use trailing data for fixed-size part" (#150846)
The fixed-size content of the MCFragment object is now stored as
trailing data, replacing ContentStart/ContentEnd with ContentSize. The
available space for trailing data is tracked using `FragSpace`. If the
available space is insufficient, a new block is allocated within the
bump allocator `MCObjectStreamer::FragStorage`.

FragList::Tail cannot be reused when switching sections or subsections,
as it is not associated with the fragment space tracked by `FragSpace`.
Instead, allocate a new fragment, which becomes less expensive after #150574.

Data can only be appended to the tail fragment of a subsection, not to
fragments in the middle. Post-assembler-layout adjustments (such as
.llvm_addrsig and .llvm.call-graph-profile) have been updated to use the
variable-size part instead.

---

This reverts commit a2fef664c29a53bfa8a66927fcf8b2e5c9da4876,
which reverted the innocent f1aa6050bd90f8ec4273da55d362e23905ad3a81 .
Commit df71243fa885cd3db701dc35a0c8d157adaf93b3, the MCOrgFragment fix,
has fixed the root cause of https://github.com/ClangBuiltLinux/linux/issues/2116
2025-08-04 09:10:42 -07:00
Fangrui Song
df71243fa8 MC: Evaluate .org during fragment relaxation
Similar to 742ecfc13e8aa34cfff2900e31838f657fcafe30 for MCFillFragment,
ensure `.org` directives with expressions are re-evaluated during
fragment relaxation, as their sizes may change. Continue iteration to
prevent stale, incorrect sizes. While I knew MCOrgFragment likely needed
to be re-evaluated at all, I did not have a motivation to add it;-)

This fixes the root cause of
https://github.com/ClangBuiltLinux/linux/issues/2116
(writeSectionData assertion failure when building the Linux kernel for arm64)

The issue cannot be reliably replicated. The specific test case would
not replicate if any of the following condition was not satisfied:

* .org was not re-evaluated. Fixed by this commit.
* clang -cc1as has a redundant `initSections` call, leading to a
  redundant initial FT_Align fragment. llvm-mc -filetype=obj, lacking
  the redundant `initSections`, doesn't replicate.
* faa931b717c02d57f0814caa9133219040e6a85b decreased sizeof(MCFragment).
* f1aa6050bd90f8ec4273da55d362e23905ad3a81 added more fragments
2025-08-04 00:29:14 -07:00
Fangrui Song
a2fef664c2 Revert "MCFragment: Use trailing data for fixed-size part"
This reverts commit f1aa6050bd90f8ec4273da55d362e23905ad3a81 (reland of #150846),
fixing conflicts.

It caused https://github.com/ClangBuiltLinux/linux/issues/2116 ,
which surfaced after a subsequent commit faa931b717c02d57f0814caa9133219040e6a85b decreased sizeof(MCFragment).

```
% /tmp/Debug/bin/clang "-cc1as" "-triple" "aarch64" "-filetype" "obj" "-main-file-name" "a.s" "-o" "a.o" "a.s"
clang: /home/ray/llvm/llvm/lib/MC/MCAssembler.cpp:615: void llvm::MCAssembler::writeSectionData(raw_ostream &, const MCSection *) const: Assertion `getContext().hadError() || OS.tell() - Start == getSectionAddressSize(*Sec)' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /tmp/Debug/bin/clang -cc1as -triple aarch64 -filetype obj -main-file-name a.s -o a.o a.s
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  libLLVMSupport.so.22.0git 0x00007cf91eb753cd llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 61
fish: Job 1, '/tmp/Debug/bin/clang "-cc1as" "…' terminated by signal SIGABRT (Abort)
```

The test is sensitive to precise fragment offsets. Using llvm-mc
-filetype=obj -triple=aarch64 a.s does not replicate the issue. However,
clang -cc1as includes an unnecessary `initSection` (adding an extra
FT_Align), which causes the problem.
2025-08-03 23:18:51 -07:00
Fangrui Song
57b0843f68 MCSymbol: Remove isUnset
The isUnset state lacks significance and should be treated as equivalent
to an undefined symbol.

Equated and common symbols seem to have subtle semantic distinctions in
GAS, justifying the use of distinct `SymContents*` values.

TODO: Ensure common symbols have a fragment, so that `isDefined()`
returns true, rejecting `.comm c, 4, 4; .set c, 4`
2025-08-03 21:31:11 -07:00
Fangrui Song
d1ee636b98 MCSymbol: Remove Kind
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to discourage dynamic dispatching on the type.
2025-08-03 19:17:05 -07:00
Fangrui Song
2d4ecba957 MCSymbolMachO: Remove classof
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to eliminate MCSymbol::Kind in the base class.
2025-08-03 18:59:35 -07:00
Fangrui Song
b77f51f3f1 MCSymbolXOFF: Remove classof
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to eliminate MCSymbol::Kind in the base class.
2025-08-03 18:49:36 -07:00
Fangrui Song
888d3cde3a MCSymbolGOFF: Remove classof
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to eliminate MCSymbol::Kind in the base class.
2025-08-03 18:31:39 -07:00
Fangrui Song
d6c2e53151 MCSymbolXCOFF: Migrate away from classof
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to eliminate MCSymbol::Kind in the base class.
2025-08-03 18:18:44 -07:00
Fangrui Song
85f00707dd MCSymbolMachO: Migrate away from classof
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to eliminate MCSymbol::Kind in the base class.
2025-08-03 17:48:50 -07:00
Fangrui Song
570e09047c MCSymbolWasm: Remove classof
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to eliminate MCSymbol::Kind in the base class.
2025-08-03 17:28:33 -07:00
Fangrui Song
45a061b8c8 MCSymbolCOFF: Remove classof
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to eliminate MCSymbol::Kind in the base class.
2025-08-03 16:45:31 -07:00
Fangrui Song
5d5ce06cae MCSymbolELF: Migrate away from classof
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to eliminate MCSymbol::Kind in the base class.
2025-08-03 16:21:19 -07:00
Fangrui Song
b51ff2705f MCSymbolELF: Migrate away from classof
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to eliminate MCSymbol::Kind in the base class.
2025-08-03 16:05:35 -07:00
Fangrui Song
e640ca8b9a MCSymbolELF: Migrate away from classof
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to eliminate MCSymbol::Kind in the base class.
2025-08-03 15:45:36 -07:00
Fangrui Song
e2d761df32 MCSymbolELF: Migrate away from classof
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to eliminate MCSymbol::Kind in the base class.
2025-08-03 15:05:22 -07:00
Fangrui Song
bc0f696b1f MCSymbol: Replace isELF with MCContext::isELF 2025-08-03 12:40:55 -07:00
Fangrui Song
9591f16a5a Move FragmentAllocator from MCContext to MCObjectStreamer
In MCContext::reset, delete a stale comment as MCCodeView no longer
owns or deallocates MCFragment.
2025-08-03 12:26:02 -07:00
Fangrui Song
faa931b717 MCFragment: Store the number of variable-size tail fixups as uint8_t
Decrease sizeof(MCFragment) by 8 on 64-bit machines.
2025-08-02 15:17:00 -07:00
Fangrui Song
f1aa6050bd MCFragment: Use trailing data for fixed-size part
Reapply after #151724 switched to `char *Data`, fixing a
-fsanitize=pointer-overflow issue in MCAssembler::layout.

---

The fixed-size content of the MCFragment object is now stored as
trailing data, replacing ContentStart/ContentEnd with ContentSize. The
available space for trailing data is tracked using `FragSpace`. If the
available space is insufficient, a new block is allocated within the
bump allocator `MCObjectStreamer::FragStorage`.

FragList::Tail cannot be reused when switching sections or subsections,
as it is not associated with the fragment space tracked by `FragSpace`.
Instead, allocate a new fragment, which becomes less expensive after #150574.

Data can only be appended to the tail fragment of a subsection, not to
fragments in the middle. Post-assembler-layout adjustments (such as
.llvm_addrsig and .llvm.call-graph-profile) have been updated to use the
variable-size part instead.

Pull Request: https://github.com/llvm/llvm-project/pull/150846
2025-08-02 10:29:33 -07:00
Fangrui Song
d3589edafc MCAsmBackend::applyFixup: Change Data to indicate the relocated location
`Data` now references the first byte of the fixup offset within the current fragment.

MCAssembler::layout asserts that the fixup offset is within either the
fixed-size content or the optional variable-size tail, as this is the
most the generic code can validate without knowing the target-specific
fixup size.

Many backends applyFixup assert
```
assert(Offset + Size <= F.getSize() && "Invalid fixup offset!");
```

This refactoring allows a subsequent change to move the fixed-size
content outside of MCSection::ContentStorage, fixing the
-fsanitize=pointer-overflow issue of #150846

Pull Request: https://github.com/llvm/llvm-project/pull/151724
2025-08-02 09:27:06 -07:00
Fangrui Song
1db2879a36 MCAssembler: Split evaluateFixup to Number of fixups and Number of fixup evaluations for relaxation
The number of fixups is a more important metric, guiding the data
structure optimization.
2025-08-01 20:47:53 -07:00
Prabhu Rajasekaran
7c6a1c3d15
[llvm][AsmPrinter] Emit call graph section
Collect the necessary information for constructing the call graph
section, and emit to .callgraph section of the binary.

MD5 hash of the callee_type metadata string is used as the numerical
type id emitted.

Reviewers: ilovepi

Reviewed By: ilovepi

Pull Request: https://github.com/llvm/llvm-project/pull/87576
2025-07-31 13:38:20 -07:00
Nikolay Panchenko
f5d49c71b4
Revert "MCFragment: Use trailing data for fixed-size part" (#151383)
Reverts llvm/llvm-project#150846 due to unsigned underflow identifier by
UBSan in several tests
2025-07-30 16:48:53 -04:00
Fangrui Song
3ecefba52d MC: Restore emitInstToData optimization
Accidentally dropped by f36ce53adf9294556c5d5f5f55c484c923f0c286
2025-07-28 23:31:39 -07:00
Fangrui Song
f36ce53adf
MCFragment: Use trailing data for fixed-size part
The fixed-size content of the MCFragment object is now stored as
trailing data, replacing ContentStart/ContentEnd with ContentSize. The
available space for trailing data is tracked using `FragSpace`. If the
available space is insufficient, a new block is allocated within the
bump allocator `MCObjectStreamer::FragStorage`.

FragList::Tail cannot be reused when switching sections or subsections,
as it is not associated with the fragment space tracked by `FragSpace`.
Instead, allocate a new fragment, which becomes less expensive after #150574.

Data can only be appended to the tail fragment of a subsection, not to
fragments in the middle. Post-assembler-layout adjustments (such as
.llvm_addrsig and .llvm.call-graph-profile) have been updated to use the
variable-size part instead.

Pull Request: https://github.com/llvm/llvm-project/pull/150846
2025-07-28 22:12:15 -07:00
Fangrui Song
769b0e6f5c XCOFF: Simplify R_REF and remove a MCFragment::addFixup use
Simplify the code added by https://reviews.llvm.org/D144356
2025-07-28 21:40:08 -07:00
Fangrui Song
2bebbe166b MCFragment: Migrate away from appendContents
The fixed-size content of the MCFragment object will be stored as
trailing data (#150846). Any post-assembler-layout adjustments must
target the variable-size tail.
2025-07-28 20:35:35 -07:00
Fangrui Song
f3bcaeac94 Optimize MCObjectStreamer::emitInstToData 2025-07-27 09:31:38 -07:00
Fangrui Song
69d0078f16 MC: Generate relocation for a branch crossing linker-relaxable FT_Align fragment
"Encode FT_Align in fragment's variable-size tail" or a neighbor change
caused a regression that was similar to the root cause of
ab0931b6389838cb5d7d11914063a1ddd84102f0
(See the new test (.text3 with a call at the start"))

For a FT_Align fragment, the offset between location A (with offset <=
FixedSize) and B (offset == FixedSize+VarSize) cannot be resolved.

In addition, delete unneeded condition `F->isLinkerRelaxable()`.

LoongArch linker relaxation is largely under-tested, but update it as well.
2025-07-26 21:19:48 -07:00
Fangrui Song
87c73f498d Move MCSection::printSwitchToSection to MCAsmInfo
This removes the only virtual function of MCSection.

NVPTXTargetStreamer::changeSection uses the MCSectionELF print method.
Change it to just print the section name.
2025-07-26 15:42:05 -07:00
Fangrui Song
9b41b62c73 MC: Merge MCSection*.cpp into MCAsmInfo*.cpp
To centralize assembly-related functions to MCAsmInfo and move toward
making MCSection non-virtual. MCSection*.cpp files primarily define
printSwitchToSection, which is tighly related to MCAsmInfo.
2025-07-26 13:22:38 -07:00
Fangrui Song
1c31c6801a MCSection: Remove empty printSwitchToSection overrides 2025-07-26 12:20:27 -07:00
Fangrui Song
e9de1ee9f5 MC: Move useCodeAlign from MCSection to MCAsmInfo
To centralize assembly-related virtual functions to MCAsmInfo and move
toward making MCSection non-virtual.
2025-07-26 11:34:10 -07:00
Fangrui Song
3aeab925db MCSection: Remove SectionVariant
The object file format specific derived classes are used in context like
MCStreamer and MCObjectTargetWriter where the type is statically known.
We don't use isa/dyn_cast.
2025-07-26 10:24:06 -07:00
Fangrui Song
190fcc28af MC: Replace MCSection's SV_MachO check with MCContext::IsMachO 2025-07-26 10:09:45 -07:00
Fangrui Song
f517ac2083 MCSectionCOFF: Avoid cast
The object file format specific derived classes are used in context like
MCStreamer and MCObjectTargetWriter where the type is statically known.
We don't use isa/dyn_cast and we want to eliminate
MCSection::SectionVariant in the base class.
2025-07-26 10:04:04 -07:00
Fangrui Song
1e56686f6d MCSectionGOFF: Remove classof
The object file format specific derived classes are used in context like
MCStreamer and MCObjectTargetWriter where the type is statically known.
We don't use isa/dyn_cast and we want to eliminate
MCSection::SectionVariant in the base class.
2025-07-26 09:42:37 -07:00