5839 Commits

Author SHA1 Message Date
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
Fangrui Song
c2faf6a57f MCSectionWasm: 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:30:02 -07:00
Fangrui Song
1ea085be7c MCSectionXCOFF: 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 00:44:05 -07:00
Fangrui Song
b22e22ebfa MC: Allocate initial fragment and define section symbol in changeSection
Reland #150574 with a MCStreamer::changeSection change:
In Mach-O, DWARF sections use Begin as a temporary label, requiring a label
definition, unlike section symbols in other file formats.
(Tested by dec978036ef1037753e7de5b78c978e71c49217b)

---

13a79bbfe583e1d8cc85d241b580907260065eb8 (2017) introduced fragment
creation in MCContext for createELFSectionImpl, which was inappropriate.
Fragments should only be created when using MCSteramer, not during
`MCContext::get*Section` calls.
`initMachOMCObjectFileInfo` defines multiple sections, some of which may
not be used by the code generator. This caused symbol names matching
these sections to be incorrectly marked as undefined (see
https://reviews.llvm.org/D55173).

The fragment code was later replicated in other file formats, such as
WebAssembly (see https://reviews.llvm.org/D46561), XCOFF, and GOFF.

This patch fixes the problem by moving initial fragment allocation from
MCContext::createSection to MCStreamer::changeSection.
While MCContext still creates a section symbol, the symbol is not
attached to the initial fragment. In addition,

* Move `emitLabel`/`setFragment` from `switchSection*` and
  overridden changeSection to `MCObjectStreamer::changeSection` for
  consistency.
* De-virtualize `switchSectionNoPrint`.
* test/CodeGen/XCore/section-name.ll now passes. XCore doesn't support
  MCObjectStreamer. I don't think the MCAsmStreamer output behavior
  change matters.

Pull Request: https://github.com/llvm/llvm-project/pull/150574
2025-07-26 00:05:10 -07:00
Fangrui Song
dd4ebe6514 MCSectionCOFF: 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-25 22:10:48 -07:00
dyung
cb37916a25
Revert "MC: Allocate initial fragment and define section symbol in changeSection" (#150736)
Reverts llvm/llvm-project#150574

This is causing a test failure on AArch64 MacOS bots:
https://lab.llvm.org/buildbot/#/builders/190/builds/24187
2025-07-25 20:21:59 -07:00
Fangrui Song
2571924ad6 MCSectionELF: 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-25 09:50:19 -07:00
Fangrui Song
1955a01d63
MC: Allocate initial fragment and define section symbol in changeSection
13a79bbfe583e1d8cc85d241b580907260065eb8 (2017) introduced fragment
creation in MCContext for createELFSectionImpl, which was inappropriate.
Fragments should only be created when using MCSteramer, not during
`MCContext::get*Section` calls.
`initMachOMCObjectFileInfo` defines multiple sections, some of which may
not be used by the code generator. This caused symbol names matching
these sections to be incorrectly marked as undefined (see
https://reviews.llvm.org/D55173).

The fragment code was later replicated in other file formats, such as
WebAssembly (see https://reviews.llvm.org/D46561), XCOFF, and GOFF.

This patch fixes the problem by moving initial fragment allocation from
MCContext::createSection to MCStreamer::changeSection.
While MCContext still creates a section symbol, the symbol is not
attached to the initial fragment.
In addition, move `emitLabel`/`setFragment` from `switchSection*` and
overridden changeSection to `MCObjectStreamer::changeSection` for
consistency.

* test/CodeGen/XCore/section-name.ll now passes. XCore doesn't support
  MCObjectStreamer. I don't think the MCAsmStreamer output behavior
  change matters.

Pull Request: https://github.com/llvm/llvm-project/pull/150574
2025-07-24 23:37:48 -07:00
Fangrui Song
b0dea47ae6
GOFF: Only register sections within MCObjectStreamer::changeSection
registerSection should only be called by
MCObjectStreamer::changeSection. This will be utilized by a pending
change to move initial fragment allocation from MCContext::createSection
to MCStreamer::changeSection, resolving some issues (Fragments should
only be created when using MCSteramer, not during
`MCContext::getELFSection` calls)

Pull Request: https://github.com/llvm/llvm-project/pull/150183
2025-07-24 20:12:12 -07:00