5847 Commits

Author SHA1 Message Date
Fangrui Song
244e053b6c MC: Remove llvm/MC/MCFixupKindInfo.h
The file used to define `MCFixupKindInfo`, a simple structure,
which is now in MCAsmBackend.h.
2025-07-05 11:24:11 -07:00
Fangrui Song
152c9d577c MC: Sink FKF_IsAlignedDownTo32Bits to needed targets
Utilize the generalized MCAsmBackend::evaluateFixup hook. This reduces
overhead for other targets (e.g., x86).

Now MCAsmBackend::getFixupKindInfo is only used by MCAsmStreamer
-show-encoding in the generic code.
2025-07-05 11:11:07 -07:00
Fangrui Song
fb13be06e1 MC: Generalize evaluateTargetFixup
Generalize evaluateTargetFixup to be called by all targets,
making FKF_IsTarget unneeded.

Next: Update targets that use FKF_IsAlignedDownTo32Bits to define
`evaluateFixup` and remove FKF_IsAlignedDownTo32Bits from the generic
code.
2025-07-05 00:19:48 -07:00
Fangrui Song
32f83d38bd MCFixup: Remove FK_PCRel_
The generic FK_Data_ fixup kinds handle both absolute and PC-relative
fixups. ELFObjectWriter sets IsPCRel to true for `.long foo-.`, so the
backend has to handle PC-relative FK_Data_.

However, the existence of FK_PCRel_ encouraged backends to implement it
as a separate fixup type, leading to redundant and error-prone code.

Removing FK_PCRel_ simplifies the overall fixup mechanism.
2025-07-04 22:57:22 -07:00
Fangrui Song
c20379198c X86: Replace deprecated FK_PCRel_ with FK_Data_ fixup and PCRel flag
We will unify the generic fixup kinds FK_Data_ and FK_PCRel_. A
FK_PCRel_ kind is essentially the corresponding FK_Data_ fixup with the
PCRel flag set.
2025-07-04 22:25:02 -07:00
Fangrui Song
6ec27f1b8a MCFixup: Remove FKF_IsPCRel
PC-relative fixups compute their values as
`sym_a - current_location + offset` (S - P + A).
Now that targets have set PCRel at fixup creation time, we can remove
some overhead from MCAssembler::evaluateFixup.
2025-07-04 21:06:54 -07:00
Fangrui Song
7d500b115d SPARC: Remove unneeded MCFixupKindInfo::FKF_IsPCRel
SPARC now sets PCRel at fixup creation and no longer needs to the
MCAssembler::evaluateFixup workaround that checks
MCFixupKindInfo::FKF_IsPCRel.
2025-07-04 15:45:14 -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
777391a216 MCFixup: Improve location accuracy and remove MCFixup::Loc
Remove the redundant MCFixup::Loc member and instead use MCExpr::Loc to
determine the location for fixups. Previously, many target MCCodeEmitter would
use the beginning of an instruction for fixup locations, which often
resulted in inaccurate column information.

```
// RISCVMCCodeEmitter::getImmOpValue
Fixups.push_back(MCFixup::create(0, Expr, FixupKind, MI.getLoc()));

// X86MCCodeEmitter::emitImmediate
Fixups.push_back(MCFixup::create(static_cast<uint32_t>(CB.size() - StartByte), Expr, FixupKind, Loc));
```

While MCExpr::Loc generally provides more meaningful location data,
tests should avoid over-relying on it. For instance, MCBinaryExpr's
location refers to its operator, and for operands with sigils (like
`$foo`), the location often omits the sigils.

https://llvm-compile-time-tracker.com/compare.php?from=8740ff822d462844506134bb7c425e1778518b95&to=831a11f75d22d64982b13dba132d656ac8567612&stat=instructions%3Au

I've also considered removing MCExpr::Loc (revert
https://reviews.llvm.org/D28861), but we'd lose too much information.
It's also difficult to carry location information to improve location
tracking in target MCCodeEmitter.

This change utilizes previous MCExpr::Loc improvement like
7e3e2e1b8c6ff21e68782a56164139cca334fcf3
7b517cf743f112f980cf6a4d6e6190c2a5b3e451
2025-07-04 12:06:28 -07:00
Fangrui Song
7b517cf743 MCParser: Add SMLoc to expressions
The information helps debugging, and will be used and tested when we
change MCFixup::getLoc to use the MCExpr location and remove
MCFixup::Loc.
2025-07-04 10:24:37 -07:00
Fangrui Song
7e3e2e1b8c MCParser: Add SMLoc to expressions
The information will be used when we change MCFixup::getLoc to use the
MCExpr location, making MCFixup smaller.
2025-07-04 00:58:07 -07:00
Fangrui Song
b478c38c19 MCAsmBackend: Replace FKF_IsPCRel with isPCRel() 2025-07-03 00:51:20 -07:00
Fangrui Song
9234d07752 MCAssembler: Optimize PCRel fixups
* MCAssembler::evaluateFixup sets MCFixup::PCRel.
* ELFObjectWriter retrieves the bit from the MCFixup argument.
2025-07-03 00:01:53 -07:00
Fangrui Song
dd2891535d
MCAsmBackend: Merge addReloc into applyFixup (#146820)
Follow-up to #141333. Relocation generation called both addReloc and
applyFixup, with the default addReloc invoking shouldForceRelocation,
resulting in three virtual calls. This approach was also inflexible, as
targets needing additional data required extending
`shouldForceRelocation` (see #73721, resolved by #141311).

This change integrates relocation handling into applyFixup, eliminating
two virtual calls. The prior default addReloc is renamed to
maybeAddReloc. Targets overriding addReloc now call their customized
addReloc implementation.
2025-07-02 23:14:11 -07:00
Fangrui Song
922dde3c64 MCAssembler: Simplify fixup handling 2025-07-02 20:07:56 -07:00
Fangrui Song
a25887077b ELFObjectWriter: Optimize isInSymtab
Drop `OWriter.Renames.count(&Symbol)` from the fast path
(Used||Signature).
Place the two equated symbol (isVariable()) conditions together.
2025-07-02 19:35:18 -07:00
Rahman Lavaee
6b623a6622
[SHT_LLVM_BB_ADDR_MAP] Remove support for versions 1 and 0 (SHT_LLVM_BB_ADDR_MAP_V0). (#146186)
Version 2 was added more than two years ago
(6015a045d7).
So it should be safe to deprecate older versions.
2025-07-02 10:31:52 -07:00
Fangrui Song
9262ac3ee4 Revert "ELFObjectWriter: Optimize isInSymtab"
This reverts commit 1108cf64196a056aa350baba98e3fab6d7529a59.

Caused a regression for a weird but interesting case (STT_SECTION symbol
as group signature). We no longer define `sec`
```
.section sec,"ax"
.section .foo,"axG",@progbits,sec
nop
```

Fix #146581
2025-07-02 00:08:42 -07:00
Fangrui Song
eac1a1d3a8 MCAssembler: Consistently place MCFragment parameter before MCFixup
... to be consistent with other places, e.g. `recordRelocation`.
While here, use references instead of non-null pointers.
2025-07-01 23:59:35 -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
393aebf5c2
CodeView: Move MCCVDefRangeFragment storage to MCContext/MCFragment. NFC (#146462)
so that ~MCCVInlineLineTableFragment will become trivial when we
make ~MCEncodedFragment trivial (#146307).
2025-06-30 21:49:54 -07:00
Fangrui Song
04395be630
MC: Merge MCFragment.h into MCSection.h
... due to their close relationship. MCSection's inline functions (e.g.
iterator) access MCFragment, and we want MCFragment's inline functions
to access MCSection similarly (#146307).

Pull Request: https://github.com/llvm/llvm-project/pull/146315
2025-06-30 09:41:53 -07:00
David Spickett
9fbde32a8c Revert "CodeView: Move MCCVDefRangeFragment storage to MCContext/MCFragment"
This reverts commit e47d4010d34119c2b4a28e7609fde35449a8b437.

Causing a few tests to crash on Windows buildbots.
2025-06-30 08:09:22 +00:00
Fangrui Song
e47d4010d3 CodeView: Move MCCVDefRangeFragment storage to MCContext/MCFragment
Work toward making ~MCCVInlineLineTableFragment trivial.
2025-06-29 18:56:02 -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
Kazu Hirata
75f359dc56
[MC] Remove an unnecessary cast (NFC) (#146276)
C is already of unsigned char.
2025-06-29 15:41:40 -07:00
Fangrui Song
8d099271c6 MCInt: Change dump functions to accept MCContext instead of MCRegiserInfo
* MCContext is more accessible to callers.
* With MCContext available, printExpr can be used to print an MCExpr (MCOperand::print) seamlessly.
2025-06-29 10:31:29 -07:00
Fangrui Song
dc1a79a978 MC: Make save-temp-labels imply UseNamesOnTempLabels
UseNamesOnTempLabels was false in MCObjectStreamer. `createTempSymbol`
created symbols were unnamed, making debugging difficult.
2025-06-29 00:42:07 -07:00
Fangrui Song
b54337d76c MC: Enhance mc-dump output
* Make pre-layout to -debug-only=mc-dump-pre. This output is not useful
  for most debugging needs.
* Print fragment-associated symbols. Make it easier to locate relevant
  fragments.
* Print the LinkerRelaxable flag.
2025-06-29 00:11:24 -07:00
Fangrui Song
2661d59579 MC: Remove post-relaxation and Symbol printing from mc-dump output
The "Symbol" stanza includes symbol names with all zero indexes. which
are not useful.

The "assembler backend - post-relaxation" part is not useful. Only
Hexagon (and X86 when x86-pad-for-align is set) might change the layout
between "post-relaxation" and "final-layout". From my experience
debugging the two passes requires more dumping code not served by the
output.
2025-06-28 22:47:38 -07:00
Fangrui Song
279e808b75 MC: Make mc-dump output compact
Remove unneeded details like "<" and ">". Reduce indentation.
Omit `this` address to simplify output comparison.
Add a -debug-only=mc-dump test.

While here, add fixup printing for MCRelaxableFragment.
2025-06-28 22:31:38 -07:00
Kazu Hirata
72e24f3022
[MC] Remove an unnecessary cast (NFC) (#146251) 2025-06-28 20:41:32 -07:00
Fangrui Song
6c2aa37fb1 MCAsmStreamer: Print symbol equated to a register
GNU Assembler supports `a = %eax` for x86. We use X86MCExpr to support a
register expression. https://reviews.llvm.org/D47545 added reassignment
support but this code is not necessary.
2025-06-28 18:07:43 -07:00
Fangrui Song
1108cf6419 ELFObjectWriter: Optimize isInSymtab 2025-06-28 17:36:14 -07:00
Fangrui Song
aa8afadd25 MCSymbolRefExpr: Migrate away from deprecated VariantKind 2025-06-28 15:48:43 -07:00
Fangrui Song
e6b25288eb MCExpr: Migrate away from operator<<
Printing an expression is error-prone without a MCAsmInfo argument.
Remove the operator<< overload and replace callers with
MCAsmInfo::printExpr. Some callers are changed to MCExpr::print, with
the goal of eventually making it private.
2025-06-28 14:41:58 -07:00
Fangrui Song
e878b7e349 MCParsedAsmOperand::print: Add MCAsmInfo parameter
so that subclasses can provide the appropriate MCAsmInfo to print
MCExpr objects.

At present, llvm/utils/TableGen/AsmMatcherEmitter.cpp constucts a
generic MCAsmInfo.
2025-06-28 12:05:33 -07:00
Fangrui Song
ff075d0ac2 MCParser: Remove operator<< const MCParsedAsmOperand &MO
Printing an expression is error-prone without a MCAsmInfo argument.
We will add a MCAsmInfo parameter to MCParsedAsmoperand::print.
2025-06-28 11:23:55 -07:00
Fangrui Song
c73906ec69 MCParser: Reduce VK_None uses 2025-06-27 22:02:22 -07:00
Fangrui Song
5aa3e6baa0 MC: Reduce MCSymbolRefExpr::VK_None uses 2025-06-27 21:46:36 -07:00
joaosaffran
171aa34e43
[DirectX] Add static sampler support to root signature (#143422)
Implements static samplers parsing from root signature metadata
representation. This is required to support Root Signatures in HLSL.
Closes: #[126641](https://github.com/llvm/llvm-project/issues/126641)

---------

Co-authored-by: joaosaffran <joao.saffran@microsoft.com>
2025-06-27 14:30:48 -07:00
Fangrui Song
af2bf2e544 MC: Remove unneeded MCSymbolRefExpr::VariantKind calls 2025-06-27 09:47:53 -07:00
Fangrui Song
56b2c7d988 MC: Rename initializeVariantKinds to initializeAtSpecifiers
We introduced VariantKinds after MCSymbolRefExpr::VariantKind and then
deprecated the VariantKind naming in favor of AtSpecifier (#133214).
Rename the function and type to use the recommended convention.
2025-06-26 22:46:20 -07:00
Fangrui Song
7dfcf489fd PowerPC: Separate ELF and XCOFF @ specifiers
`@l` was incorrectly parsed as ELF-specific S_LO. Change it to AIX-specific S_L.
2025-06-26 22:23:51 -07:00
Kai Nacke
655d04859b
[GOFF] Add writing of text records (#137235)
Sections which are not allowed to carry data are marked as virtual. Only
complication when writing out the text is that it must be written in
chunks of 32k-1 bytes, which is done by having a wrapper stream writing
those records.
Data of BSS sections is not written, since the contents is known to be
zero. Instead, the fill byte value is used.
2025-06-26 13:50:40 -04:00
Kai Nacke
33872f1218
[GOFF] Add writing of section symbols (#133799)
Unlike other formats, the GOFF object file format uses a 2 dimensional structure
to define the location of data. For example, the equivalent of the ELF .text
section is made up of a Section Definition (SD) and a class (Element Definition;
ED). The name of the SD symbol depends on the application, while the class has
the predefined name C_CODE/C_CODE64 in AMODE31 and AMODE64 respectively.

Data can be placed into this structure in 2 ways. First, the data (in a text
record) can be associated with an ED symbol. To refer to data, a Label
Definition (LD) is used to give an offset into the data a name. When binding,
the whole data is pulled into the resulting executable, and the addresses
given by the LD symbols are resolved.

The alternative is to use a Part Definition (PR). In this case, the data (in
a text record) is associated with the part. When binding, only the data of
referenced PRs is pulled into the resulting binary.

Both approaches are used. SD, ED, and PR elements are modeled by nested
MCSectionGOFF instances, while LD elements are associated with MCSymbolGOFF
instances.

At the binary level, a record called "External Symbol Definition" (ESD) is used. The
ESD has a type (SD, ED, PR, LD), and depending on the type a different subset of
the fields is used.
2025-06-26 11:52:14 -04:00
Kazu Hirata
3d5903c4d8
[llvm] Use llvm::is_contained (NFC) (#145844)
llvm::is_contained is shorter than llvm::all_of plus a lambda.
2025-06-26 08:41:18 -07:00
Kai Nacke
35a0c18b88
[GOFF] Introduce GOFFWriter class (#131216)
The GOFFWriter has 2 purposes:
- Simplify resource management
- Enable writing of split DWARF files

It follows the design of the other writer classes. No added
functionality at this point.

This changes also makes the GOFFObjectWriter a public class.
2025-06-26 08:24:00 -04:00
Matt Arsenault
fff720d641
Triple: Forward declare Twine and remove include (#145685) 2025-06-26 15:26:04 +09:00