5555 Commits

Author SHA1 Message Date
Fangrui Song
b6e2df54c4 [MC] Move some member variables from AsmParser to MCAsmParser
to eliminate some virtual functions and avoid duplication
between AsmParser/MasmParser.
2025-04-02 09:59:18 -07:00
Heejin Ahn
4d1c827423
[WebAssembly] Support parsing .lto_set_conditional (#126546)
In the split-LTO-unit mode in ThinLTO, a compilation module is split
into two and global variables that meet a specific criteria is moved to
the split module.
d21fc58aee/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp (L315-L366)

And if there is an originally local-linkage global value defined in the
original module and referenced in the split module or the vice versa,
that value is _promoted_ by attaching a module ID to their names in
order to prevent name clashes because now they can be referenced from
other modules.
d21fc58aee/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp (L46-L100)

And when that promoted global value is a function, a
`.lto_set_conditional` entry is written to the original module to avoid
breaking references from inline assembly:

d21fc58aee/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp (L84-L91)

The syntax of this is, if the original function name is `symbolA` and
the module ID is `123`,
```ll
module asm ".lto_set_conditional symbolA,symbolA.123"
```
These symbols are parsed here:

648981f913/llvm/lib/MC/MCParser/AsmParser.cpp (L6467)

The first function symbol in this `.lto_set_conditional` do not exist as
a function in the bitcode anymore because it was renamed to the second.
So they are not assigned as function symbols but they are not really
data either, so the object writer crashes here:
5b9e6c7993/llvm/lib/MC/WasmObjectWriter.cpp (L1820)

This PR makes the object writer just skip those symbols.

---

This problem was discovered when I was testing with
`-fwhole-program-vtables`. The reason we didn't have this problem before
with ThinLTO was because `-fsplit-lto-unit`, which splits LTO units when
possible, defaults to false, but it defaults to true when
`-fwhole-program-vtables` is used.
2025-04-02 03:15:29 +09:00
Fangrui Song
36978fadb8 [MC] Add UseAtForSpecifier
Some ELF targets don't use @ for relocation specifiers.
We should not report `error: invalid variant` when @ is used.

Attempt to make expr@specifier parsing less hacky.
2025-04-01 00:06:05 -07:00
Fangrui Song
c6b3fd7999 [MC] maybeParseSectionType: test CommentString instead of AllowAtInIdentifier
Rework https://reviews.llvm.org/D31026
AllowAtInIdentifier is a misnomer: it should be false for ELF targets,
but is currently true as a hack to parse expr@specifier.
2025-03-30 22:27:47 -07:00
Fangrui Song
04a67528d3
[MC] Simplify MCBinaryExpr/MCUnaryExpr printing by reducing parentheses (#133674)
The existing pretty printer generates excessive parentheses for
MCBinaryExpr expressions. This update removes unnecessary parentheses
of MCBinaryExpr with +/- operators and MCUnaryExpr.
Since relocatable expressions only use + and -, this change improves
readability in most cases.

Examples:

- (SymA - SymB) + C now prints as SymA - SymB + C.
  This updates the output of -fexperimental-relative-c++-abi-vtables for
  AArch64 and x86 to `.long _ZN1B3fooEv@PLT-_ZTV1B-8`
- expr + (MCTargetExpr) now prints as expr + MCTargetExpr, with this
  change primarily affecting AMDGPUMCExpr.
2025-03-30 22:03:14 -07:00
Fangrui Song
3acccf042a [MC] Don't print () around $ names
This MIPS behavior from edb9d84dcc4824865e86f963e52d67eb50dde7f5 (2010)
is obsoleted and misleading. This caused confusion in
https://reviews.llvm.org/D123702 ([NVPTX] Disable parens for identifiers
starting with '$')

Note: $tmp was rejected by AsmParser before
https://reviews.llvm.org/D75111 (2020)
2025-03-30 16:38:16 -07:00
Fangrui Song
db603a09da [MC] Move ELF-specific handleAddSubRelocations to ELFObjectWriter::recordRelocation 2025-03-29 19:08:07 -07:00
Fangrui Song
fe6fb910df
[RISCV] Replace @plt/@gotpcrel in data directives with %pltpcrel %gotpcrel
clang -fexperimental-relative-c++-abi-vtables might generate `@plt` and
`@gotpcrel` specifiers in data directives. The syntax is not used in
humand-written assembly code, and is not supported by GNU assembler.
Note: the `@plt` in `.word foo@plt` is different from
the legacy `call func@plt` (where `@plt` is simply ignored).

The `@plt` syntax was selected was simply due to a quirk of AsmParser:
the syntax was supported by all targets until I updated it
to be an opt-in feature in a0671758eb6e52a758bd1b096a9b421eec60204c

RISC-V favors the `%specifier(expr)` syntax following MIPS and Sparc,
and we should follow this convention.

This PR adds support for `.word %pltpcrel(foo+offset)` and
`.word %gotpcrel(foo)`, and drops `@plt` and `@gotpcrel`.

* MCValue::SymA can no longer have a SymbolVariant. Add an assert
  similar to that of AArch64ELFObjectWriter.cpp before
  https://reviews.llvm.org/D81446 (see my analysis at
  https://maskray.me/blog/2025-03-16-relocation-generation-in-assemblers
  if intrigued)
* `jump foo@plt, x31` now has a different diagnostic.

Pull Request: https://github.com/llvm/llvm-project/pull/132569
2025-03-29 11:08:13 -07:00
Fangrui Song
ece72e2731
[MC,COFF] .safeseh: avoid changeSection (#132624)
The directive temporarily switches to the .sxdata section to emit data,
and then calls `insert`, which makes `CurFrag` out of sync of the
current section. Call push/switch/pop instead.

Related to #132464
2025-03-25 21:17:37 -07:00
Julien Villette
f4bb9b53ad
[MCA] Extend -instruction-tables option with verbosity levels (#130574)
Option becomes: -instruction-tables=`<level>`
 
The choice of `<level>` controls number of printed information.
`<level>` may be `none` (default), `normal`, `full`.
Note: If the option is used without `<label>`, default is `normal`
(legacy).

When `<level>` is `full`, additional information are:
- `<Bypass Latency>`: Latency when a bypass is implemented between
operands
  in pipelines (see SchedReadAdvance).
  - `<LLVM Opcode Name>`: mnemonic plus operands identifier.
  - `<Resources units>`: Used resources associated with LLVM Opcode.
- `<instruction comment>`: reports comment if any from source assembly.

Level `full` can be used to better check scheduling info when TableGen
is modified.
LLVM Opcode name help to find right instruction regexp to fix TableGen
Scheduling Info.

-instruction-tables=full option is validated on
AArch64/Neoverse/V1-sve-instructions.s

Follow up of MR #126703

---------

Co-authored-by: Julien Villette <julien.villette@sipearl.com>
2025-03-25 09:19:57 -07:00
Fangrui Song
214fb43cb6 MCExpr: simplify evaluateAsRelocatableImpl after https://reviews.llvm.org/D156505 2025-03-25 09:14:17 -07:00
Fangrui Song
9ee950be95 MCValue: Simplify code with getSubSym
The MCValue::SymB MCSymbolRefExpr member might be replaced with a
MCSymbol in the future. Reduce direct access.
2025-03-24 21:52:40 -07:00
Min-Yih Hsu
77648476f2
[MC] Account for AcquireAtCycle in getReciprocalThroughput (#132653)
Previously `MCSchedModel::getReciprocalThroughput` ignored
`AcquireAtCycle` completey, this patch fixes it by using the largest
`(ReleaseAtCycle - AcquireAtCycle) / NumUnits` as inverse throughput.

Here are some technical explanations:
https://myhsu.xyz/llvm-sched-interval-throughput

---------

Co-authored-by: Julien Villette <julien.villette@sipearl.com>
2025-03-24 10:24:08 -07:00
Fangrui Song
a30f102253 [llvm-ml] Remove unused function after #132750 2025-03-24 09:53:08 -07:00
Eric Astor
e2c5b95da1
[ms] [llvm-ml] Remove space-separated argument support (#132750)
This leads to errors when parsing MASM macro calls, and was retained
from AsmParser by mistake.

Fixes #132074
2025-03-24 12:41:33 -04:00
Oliver Stannard
7ada6f111f
[AsmParser] Correctly handle .ifeqs nested in other conditional directives (#132713)
The parser function used for the .ifeqs and .ifnes directives was
missing the check for whether we are currently in an ignored block of an
outer conditional directive, causing the block to be evaluated when it
should not, for example:

.if 0
  .ifeqs "a", "a"
    // Should not be evaluated, but is
    nop
  .endif
.endif
2025-03-24 14:03:20 +00:00
Fangrui Song
dfae1f968e MCValue: Simplify code with getSubSym 2025-03-23 12:22:44 -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
83c3ec1b07 [MC] Move isMemtag test to AArch64
And introduce MCValue::getAddSym & MCValue::getSubSym to simplify code.

We do not utilize the MCSymbol argument of needsRelocateWithSymbol
as it will go away in the future.
2025-03-23 11:59:21 -07:00
Fangrui Song
c39d393038 ELFObjectWriter: Remove relocation specifier test from shouldRelocateWithSymbol
It's the decision of backend needsRelocateWithSymbol whether the
STT_SECTION adjustment should be suppressed.

test/MC/AArch64/data-directive-specifier.s demonstrates how to test this
property.
2025-03-23 11:50:58 -07:00
Fangrui Song
8553fafff0 [MC] Remove ELFObjectWriter::fixSymbolsInTLSFixups
Finish the migration started by
eea7d32bd262bb5f61790c42ebaa147aa26c3979.
STT_TLS setting has been moved to backend getRelocType.
75f5a4f0dc7d96134cca86543ef3f86ef218ce77 migrated the last target, VE.
2025-03-22 20:10:31 -07:00
Fangrui Song
b19b6d9fab Move SystemZ-specific MCSymbolRefExpr::VariantKind to SystemZMCExpr::Specifier
Similar to previous migration done for other targets (PowerPC, X86, ARM,
etc). Switch from the confusing VariantKind to Specifier, which aligns
with Arm and IBM AIX's documentation.

In addition, rename *MCExpr::getKind, which confusingly shadows the base class getKind.

In the future, relocation specifiers should be encoded as part of
SystemZMCExpr instead of MCSymbolRefExpr.
2025-03-22 18:05:40 -07:00
tltao
f7a32b85b5
[MC][SystemZ] Introduce Target Specific HLASM Streamer for z/OS (#130535)
A more fleshed out version of a previous PR
https://github.com/llvm/llvm-project/pull/107415. The goal is to provide
platforms an alternative to the current MCAsmStreamer which only
supports the GNU Asm syntax.

RFC:
https://discourse.llvm.org/t/rfc-llvm-add-support-for-target-specific-asm-streamer/85095

---------

Co-authored-by: Tony Tao <tonytao@ca.ibm.com>
2025-03-21 11:36:35 -04:00
Fangrui Song
13bb2f450e [MC] Rename some VariantKind functions to use Specifier
Use the more appropriate term "relocation specifier" and avoid the
variable name `Kind`, which conflicts with MCExpr and FixupKind.
2025-03-20 22:06:16 -07:00
Fangrui Song
c177dbe484
Move X86-specific MCSymbolRefExpr::VariantKind to X86MCExpr::Specifier
Move target-specific members outside of MCSymbolRefExpr::VariantKind
(a legacy interface I am eliminating). Most changes are mechanic,
except:

* ELFObjectWriter::shouldRelocateWithSymbol
* The legacy generic code uses `ELFObjectWriter::fixSymbolsInTLSFixups`
  to set `STT_TLS` (and use an unnecessary expression walk). The better
  way is to do this in `getRelocType`, which I have done for
  AArch64, PowerPC, and RISC-V.

In the future, we should encode expressions with a relocation specifier
as X86MCExpr and use MCValue::RefKind to hold the specifier of the
relocatable expression.
https://maskray.me/blog/2025-03-16-relocation-generation-in-assemblers

While here, rename "Modifier' to "Specifier":

> "Relocation modifier", though concise, suggests adjustments happen during the linker's relocation step rather than the assembler's expression evaluation. I landed on "relocation specifier" as the winner. It's clear, aligns with Arm and IBM’s usage, and fits the assembler's role seamlessly.

Pull Request: https://github.com/llvm/llvm-project/pull/132149
2025-03-20 20:28:49 -07:00
Fangrui Song
fa1228552f [X86] Move ABS8 special case to fixupNeedsRelaxationAdvanced
And add a test that X86MCCodeEmitter doesn't utilize a 1-byte
immediate for `cmp (3+$foo)@ABS8, %edi`
2025-03-18 22:12:33 -07:00
Fangrui Song
b9d27ac252 [MC] Fix formatting of a comment 2025-03-17 20:24:08 -07:00
Fangrui Song
8a1b4d0ed2 [MC] Rework AVR #121498 to not add extra argument to shouldForceRelocation
This removes the extra argument from commit 814b34f31e163e76b816194004689985f5b9fd7b.

Also remove unneeded `>= FirstLiteralRelocationKind`.
2025-03-16 23:35:01 -07:00
Fangrui Song
de60c0e034 [MC] .reloc: move FirstLiteralRelocationKind check to evaluateFixup
Target shouldForceRelocation checks `FirstLiteralRelocationKind` to
determine whether a relocation is forced due to the .reloc directive. We
should move the code to evaluateFixup so that many targets don't need to
override shouldForceRelocation.
2025-03-16 22:32:18 -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
Fangrui Song
ff2ed154a8 [MC] evaluateAsAbsolute requires MCValue::RefKind==0
In `.equ a, 3; .if a@plt`, a@plt does not evaluate to an absolute value
(MCExpr::evaluateAsRelocatableImpl disables evaluation when the Kind !=
0 at parse time). Similarly, when using MCTargetValue,
evaluateAsAbsolute should return false when MCValue::RefKind==0.

This allows us to remove `if (!Asm)` check from MipsMCExpr.cpp
(%hi(0xdeadbeef) is not evaluated to a constant without RefKind) and
make targets less error-prone.
2025-03-15 15:33:09 -07:00
Fangrui Song
d6fbffa23c [MC] evaluateAsRelocatable: remove the Fixup argument
Commit 752b91bd821ad8a23e004b6cd631ae4f6984ae8b added the argument for
PowerPC to evaluate @l/@ha as constant in 2014. However, this is not
needed and has been cleaned up by
commit 8560da28c69de481f3ad147722577e87b902facb.

Mips also had an inappropriate use, which has been fixed by
79d84a878e83990c235da8710273a98bf835c915
2025-03-15 14:46:27 -07:00
Fangrui Song
720730041d [MC] Remove empty fixELFSymbolsInTLSFixups overrides
Follow-up to 2ada0c1e6163502a8b3fd6485c290eca26ce8748
2025-03-15 00:24:00 -07:00
Kai Nacke
37d99e9c6e
[GOFF] Refactor GOFFOstream (#131143)
GOFFOstream writes the physical 80 byte records. The records are
connected by flags indicating if there is a successor or a predecessor.
Using the length of the logical record is prone to errors. The new
implementation buffers the last physical record, and writes it out when
new data is written. In this way, the flags can be easily determined.
No obversable change in functionality, therefore no tests.
2025-03-13 16:55:47 -04:00
Fangrui Song
5d5f16204f Move PowerPC-specific MCSymbolRefExpr::VariantKind to PPCMCExpr
Most changes are mechanic, except:

* ELFObjectWriter::shouldRelocateWithSymbol: .TOC.@tocbase does not
  register the undefined symbol.  Move the handling into the
  Sym->isUndefined() code path.
* ELFObjectWriter::fixSymbolsInTLSFixups's VK_PPC* cases are moved to
  PPCELFObjectWriter::getRelocType. We should do similar refactoring
  for other targets and eventually remove fixSymbolsInTLSFixups.

In the future, we should classify PPCMCExpr similar to AArch64MCExpr.
2025-03-12 23:00:03 -07:00
Fangrui Song
eea7d32bd2 [MC] Move fixSymbolsInTLSFixups to ELFObjectWriter
so that we only need to do it once during recordRelocation. In the
future, we should change fixSymbolsInTLSFixups to apply to MCValue
instead of MCExpr, similar to GNU assembler.
2025-03-12 19:49:52 -07:00
Nikita Popov
190063464e
[MC] Speed up checkFeatures() (NFCI) (#130936)
checkFeatures() currently goes through ApplyFeatureFlag(), which will
also handle implied features. This is very slow -- just querying every
feature once takes up 10% of a Rust hello world compile.

However, if we only want to query whether certain features are
set/unset, we can do so directly -- implied features have already been
handled when the FeatureBitset was constructed.
2025-03-12 15:16:51 +01:00
Eric Astor
d48a36f583
[ms] [llvm-ml] Allow optional parenthesized arguments for macros (#129905)
We match ML64.EXE, which allows optional parentheses around a macro's arguments.
2025-03-11 10:32:21 -04: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
Fangrui Song
f120b0d6d2 [MC] Remove MCSymbolRefExpr::VK_Invalid in favor of getVaraintKindForName returning std::optional
so that when the enum members are moved to XXXTargetExpr::VariantKind,,
they do not need to implement an invalid value.
2025-03-11 00:21:31 -07: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
Nikita Popov
979c275097
[IR] Store Triple in Module (NFC) (#129868)
The module currently stores the target triple as a string. This means
that any code that wants to actually use the triple first has to
instantiate a Triple, which is somewhat expensive. The change in #121652
caused a moderate compile-time regression due to this. While it would be
easy enough to work around, I think that architecturally, it makes more
sense to store the parsed Triple in the module, so that it can always be
directly queried.

For this change, I've opted not to add any magic conversions between
std::string and Triple for backwards-compatibilty purses, and instead
write out needed Triple()s or str()s explicitly. This is because I think
a decent number of them should be changed to work on Triple as well, to
avoid unnecessary conversions back and forth.

The only interesting part in this patch is that the default triple is
Triple("") instead of Triple() to preserve existing behavior. The former
defaults to using the ELF object format instead of unknown object
format. We should fix that as well.
2025-03-06 10:27:47 +01:00
Fangrui Song
687854aea8 [MC] Remove unneeded VK_None argument from MCSymbolRefExpr::create. NFC 2025-03-06 00:00:05 -08:00
Fangrui Song
fe56c4c019 [MC] Remove unneeded VK_None argument from MCSymbolRefExpr::create. NFC 2025-03-05 23:14:04 -08:00
Fangrui Song
75f6fe2ee5 [MC] Remove unneeded MCSymbolRefExpr::create overload and add comments
The StringRef overload is often error-prone as users might forget to
register the MCSymbol.

Add comments to MCTargetExpr and MCSymbolRefExpr::VariantKind.
In the distant future the VariantKind parameter might be removed.
2025-03-05 22:10:08 -08:00
Nick Fitzgerald
6018930ef1
[lld][WebAssembly] Support for the custom-page-sizes WebAssembly proposal (#128942)
This commit adds support for WebAssembly's custom-page-sizes proposal to
`wasm-ld`. An overview of the proposal can be found
[here](https://github.com/WebAssembly/custom-page-sizes/blob/main/proposals/custom-page-sizes/Overview.md).
In a sentence, it allows customizing a Wasm memory's page size, enabling
Wasm to target environments with less than 64KiB of memory (the default
Wasm page size) available for Wasm memories.

This commit contains the following:

* Adds a `--page-size=N` CLI flag to `wasm-ld` for configuring the
linked Wasm binary's linear memory's page size.

* When the page size is configured to a non-default value, then the
final Wasm binary will use the encodings defined in the
custom-page-sizes proposal to declare the linear memory's page size.

* Defines a `__wasm_first_page_end` symbol, whose address points to the
first page in the Wasm linear memory, a.k.a. is the Wasm memory's page
size. This allows writing code that is compatible with any page size,
and doesn't require re-compiling its object code. At the same time,
because it just lowers to a constant rather than a memory access or
something, it enables link-time optimization.

* Adds tests for these new features.

r? @sbc100 

cc @sunfishcode
2025-03-04 09:39:30 -08:00
Fangrui Song
a0671758eb Reapply [MCExpr] Remove generic getVariantKindName and getVariantKindForName
They are error-prone as MCParser may parse a variant kind,
which cannot be handled by the target.

The replacement in MCAsmInfo should be used instead.

Follow-up to f244b8eed37a12539fb11b76e19ec7a7eb41dccc
2025-03-03 10:14:10 -08:00
Kazu Hirata
2af4007822 Revert "[MCExpr] Remove generic getVariantKindName and getVariantKindForName"
This reverts commit 04b49b11a8f70424263a3fc1f9c5bc69a9f46844.

This patch breaks ThinLTO/X86/memprof-tailcall-nonunique.ll.

Builtbot failures:
https://lab.llvm.org/buildbot/#/builders/108/builds/9933
https://lab.llvm.org/buildbot/#/builders/25/builds/6868
https://lab.llvm.org/buildbot/#/builders/46/builds/12890
2025-03-03 00:26:19 -08:00
Fangrui Song
0301580580 [PowerPC] Remove VK_PPC_TLSGD and VK_PPC_TLSLD
52cf8e44880bcf614068b66b63393aa8da1edd76 (2013) introduced the
VK_PPC_TLSGD workaround to prevent unconditional reference to
_GLOBAL_OFFSET_TABLE_ in ELFObjectWriter.

e2b355d651ed8f2cbe61672c4c39b6419e471265 (2015) removed the
`_GLOBAL_OFFSET_TABLE_` hack for the generic VK_TLSGD,
making the VK_PPC_TLSGD workaround unneeded.
2025-03-02 22:25:59 -08:00