2815 Commits

Author SHA1 Message Date
Ralf Jung
a50a3b8e31 various minor target feature cleanups 2025-06-19 10:50:03 +09:00
Ralf Jung
0c4b0f5726 line-wrap and extend comments, typos 2025-06-19 09:45:09 +09:00
Ralf Jung
8bec5bb5ad cg_gcc: properly populate cfg(target_features) with -Ctarget-features 2025-06-19 09:45:07 +09:00
Ralf Jung
cd08652faa move -Ctarget-feature handling into shared code 2025-06-19 09:44:01 +09:00
Ralf Jung
d70ec32ea7 move cfg(target_feature) computation into shared place 2025-06-19 09:42:35 +09:00
bors
6f935a044d Auto merge of #141061 - dpaoliello:shimasfn, r=bjorn3
Change __rust_no_alloc_shim_is_unstable to be a function

This fixes a long sequence of issues:

1. A customer reported that building for Arm64EC was broken: #138541
2. This was caused by a bug in my original implementation of Arm64EC support, namely that only functions on Arm64EC need to be decorated with `#` but Rust was decorating statics as well.
3. Once I corrected Rust to only decorate functions, I started linking failures where the linker couldn't find statics exported by dylib dependencies. This was caused by the compiler not marking exported statics in the generated DEF file with `DATA`, thus they were being exported as functions not data.
4. Once I corrected the way that the DEF files were being emitted, the linker started failing saying that it couldn't find `__rust_no_alloc_shim_is_unstable`. This is because the MSVC linker requires the declarations of statics imported from other dylibs to be marked with `dllimport` (whereas it will happily link to functions imported from other dylibs whether they are marked `dllimport` or not).
5. I then made a change to ensure that `__rust_no_alloc_shim_is_unstable` was marked as `dllimport`, but the MSVC linker started emitting warnings that `__rust_no_alloc_shim_is_unstable` was marked as `dllimport` but was declared in an obj file. This is a harmless warning which is a performance hint: anything that's marked `dllimport` must be indirected via an `__imp` symbol so I added a linker arg in the target to suppress the warning.
6. A customer then reported a similar warning when using `lld-link` (<https://github.com/rust-lang/rust/pull/140176#issuecomment-2872448443>). I don't think it was an implementation difference between the two linkers but rather that, depending on the obj that the declaration versus uses of `__rust_no_alloc_shim_is_unstable` landed in we would get different warnings, so I suppressed that warning as well: #140954.
7. Another customer reported that they weren't using the Rust compiler to invoke the linker, thus these warnings were breaking their build: <https://github.com/rust-lang/rust/pull/140176#issuecomment-2881867433>. At that point, my original change was reverted (#141024) leaving Arm64EC broken yet again.

Taking a step back, a lot of these linker issues arise from the fact that `__rust_no_alloc_shim_is_unstable` is marked as `extern "Rust"` in the standard library and, therefore, assumed to be a foreign item from a different crate BUT the Rust compiler may choose to generate it either in the current crate, some other crate that will be statically linked in OR some other crate that will by dynamically imported.

Worse yet, it is impossible while building a given crate to know if `__rust_no_alloc_shim_is_unstable` will statically linked or dynamically imported: it might be that one of its dependent crates is the one with an allocator kind set and thus that crate (which is compiled later) will decide depending if it has any dylib dependencies or not to import `__rust_no_alloc_shim_is_unstable` or generate it. Thus, there is no way to know if the declaration of `__rust_no_alloc_shim_is_unstable` should be marked with `dllimport` or not.

There is a simple fix for all this: there is no reason `__rust_no_alloc_shim_is_unstable` must be a static. It needs to be some symbol that must be linked in; thus, it could easily be a function instead. As a function, there is no need to mark it as `dllimport` when dynamically imported which avoids the entire mess above.

There may be a perf hit for changing the `volatile load` to be a `tail call`, so I'm happy to change that part back (although I question what the codegen of a `volatile load` would look like, and if the backend is going to try to use load-acquire semantics).

Build with this change applied BEFORE #140176 was reverted to demonstrate that there are no linking issues with either MSVC or MinGW: <https://github.com/rust-lang/rust/actions/runs/15078657205>

Incidentally, I fixed `tests/run-make/no-alloc-shim` to work with MSVC as I needed it to be able to test locally (FYI for #128602)

r? `@bjorn3`
cc `@jieyouxu`
2025-06-18 09:24:40 +00:00
Jacob Pratt
0eb8a66130
Rollup merge of #142588 - ZuseZ4:generic-ctx-imprv, r=oli-obk
Generic ctx imprv

Cleanup work for my gpu pr

r? `@oli-obk`
2025-06-17 23:19:36 +02:00
Manuel Drehwald
6359123d25 add and use generic get_const_int function 2025-06-16 14:23:06 -07:00
Daniel Paoliello
6906b44e1c Change __rust_no_alloc_shim_is_unstable to be a function 2025-06-16 10:54:07 -07:00
Manuel Drehwald
7d3a1d49ab make more CodegenCx function generic 2025-06-16 10:36:15 -07:00
beetrees
5723c9997c
Fix RISC-V C function ABI when passing/returning structs containing floats 2025-06-16 10:14:07 +01:00
bors
68ac5abb06 Auto merge of #142521 - sayantn:simplify-intrinsics, r=nikic,workingjubilee
Use `LLVMIntrinsicGetDeclaration` to completely remove the hardcoded intrinsics list

Follow-up to rust-lang/rust#142259

This also needs a rustc-perf run, because `Intrinsic::getType` can be expensive

`@rustbot` label A-LLVM A-codegen T-compiler
r? `@workingjubilee`
cc `@nikic`
2025-06-16 03:40:18 +00:00
León Orell Valerian Liehr
5cce691c5a
Rollup merge of #142481 - heiher:loong-asm-f16, r=Amanieu
Add `f16` inline asm support for LoongArch

r? `````@Amanieu`````
2025-06-15 23:51:57 +02:00
León Orell Valerian Liehr
0b249d3f85
Rollup merge of #141769 - bjorn3:codegen_metadata_module_rework, r=workingjubilee,saethlin
Move metadata object generation for dylibs to the linker code

This deduplicates some code between codegen backends and may in the future allow adding extra metadata that is only known at link time.

Prerequisite of https://github.com/rust-lang/rust/issues/96708.
2025-06-15 23:51:54 +02:00
sayantn
a9500d6b0b
Correctly account for different address spaces in LLVM intrinsic invocations 2025-06-15 22:45:26 +05:30
sayantn
9415f3d8a6
Use LLVMIntrinsicGetDeclaration to completely remove the hardcoded intrinsics list 2025-06-15 22:15:16 +05:30
sayantn
2038405ff7
Add simd_funnel_sh{l,r} and simd_round_ties_even 2025-06-15 04:33:41 +05:30
bors
cc87afd8c0 Auto merge of #142259 - sayantn:simplify-intrinsics, r=workingjubilee
Simplify implementation of Rust intrinsics by using type parameters in the cache

The current implementation of intrinsics have a lot of duplication to handle different overloads of overloaded LLVM intrinsic. This PR uses the **base name and the type parameters** in the cache instead of the full, overloaded name. This has the benefit that `call_intrinsic` doesn't need to provide the full name, rather the type parameters (which is most of the time more available). This uses `LLVMIntrinsicCopyOverloadedName2` to get the overloaded name from the base name and the type parameters, and only uses it to declare the function.

(originally was part of rust-lang/rust#140763, split off later)

`@rustbot` label A-codegen A-LLVM
r? codegen
2025-06-14 16:43:34 +00:00
WANG Rui
c49ec83868 Add f16 inline asm support for LoongArch 2025-06-14 09:39:30 +08:00
Matthias Krüger
7c3b2e5254
Rollup merge of #140770 - folkertdev:custom-abi, r=tgross35
add `extern "custom"` functions

tracking issue: rust-lang/rust#140829
previous discussion: https://github.com/rust-lang/rust/issues/140566

In short, an `extern "custom"` function is a function with a custom ABI, that rust does not know about. Therefore, such functions can only be defined with `#[unsafe(naked)]` and `naked_asm!`, or via an `extern "C" { /* ... */ }` block. These functions cannot be called using normal rust syntax: calling them can only be done from inline assembly.

The motivation is low-level scenarios where a custom calling convention is used. Currently, we often pick `extern "C"`, but that is a lie because the function does not actually respect the C calling convention.

At the moment `"custom"` seems to be the name with the most support. That name is not final, but we need to pick something to actually implement this.

r? `@traviscross`
cc `@tgross35`

try-job: x86_64-apple-2
2025-06-13 05:19:14 +02:00
Matthias Krüger
1a865fbe4e
Rollup merge of #135927 - azhogin:azhogin/retpoline, r=davidtwco
retpoline and retpoline-external-thunk flags (target modifiers) to enable retpoline-related target features

`-Zretpoline` and `-Zretpoline-external-thunk` flags are target modifiers (tracked to be equal in linked crates).
* Enables target features for `-Zretpoline-external-thunk`:
`+retpoline-external-thunk`, `+retpoline-indirect-branches`, `+retpoline-indirect-calls`.
* Enables target features for `-Zretpoline`:
`+retpoline-indirect-branches`, `+retpoline-indirect-calls`.

It corresponds to clang -mretpoline & -mretpoline-external-thunk flags.

Also this PR forbids to specify those target features manually (warning).

Issue: rust-lang/rust#116852
2025-06-13 05:19:13 +02:00
Folkert de Vries
5f73ce2b7e
add extern "custom" functions 2025-06-12 20:27:10 +02:00
sayantn
d56fcd968d
Simplify implementation of Rust intrinsics by using type parameters in the cache 2025-06-12 00:32:42 +05:30
Folkert de Vries
b6eb4f9c3a
use #[naked] for __rust_probestack 2025-06-10 10:08:57 +02:00
Andrew Zhogin
5601490c9d -Zretpoline and -Zretpoline-external-thunk flags (target modifiers) to enable retpoline-related target features 2025-06-09 21:29:59 +07:00
Jubilee
940a43677a
Rollup merge of #142194 - bjorn3:less_unstable_features, r=jieyouxu
Remove all unused feature gates from the compiler
2025-06-08 17:17:58 -07:00
Jubilee
48667ddd5a
Rollup merge of #142179 - folkertdev:min-global-align-parse, r=workingjubilee
store `target.min_global_align` as an `Align`

Parse the alignment properly when the target is defined/parsed, and error out on invalid alignment values. That means this work doesn't need to happen for every global in each backend.
2025-06-08 17:17:56 -07:00
Jubilee
840baa46ff
Rollup merge of #142053 - heiher:loong32-none, r=wesleywiser
Add new Tier-3 targets: `loongarch32-unknown-none*`

MCP: https://github.com/rust-lang/compiler-team/issues/865

NOTE: LoongArch32 ELF object support is available starting with object v0.37.0.
2025-06-08 17:17:53 -07:00
bjorn3
9223704f4b Remove all unused feature gates from the compiler 2025-06-08 14:50:42 +00:00
Folkert de Vries
a50bd7ca24
store target.min_global_align as an Align 2025-06-07 22:06:42 +02:00
WANG Rui
38d69c3f57 Add new Tier-3 targets: loongarch32-unknown-none*
MCP: https://github.com/rust-lang/compiler-team/issues/865
2025-06-06 08:19:38 +08:00
bors
cf423712b9 Auto merge of #140872 - bjorn3:elf_use_used_linker, r=nikic
Make #[used(linker)] the default on ELF too

`#[used]` currently is an alias for `#[used(linker)]` on all platforms except ELF based ones where it is an alias for `#[used(compiler)]`. The latter has surprising behavior and the LLVM LangRef explicitly states that it "should only be used in rare circumstances, and should not be exposed to source languages." [^2]

The reason `#[used]` still was an alias to `#[used(compiler)]` on ELF is because the gold linker has issues with it. Luckily gold has been deprecated with GCC 15 [^1] and seems to be unable to bootstrap rustc anyway [^3]. As such we shouldn't really care about supporting gold.

This would also allow re-enabling start-stop-gc with lld.

cc https://github.com/rust-lang/rust/issues/93798
Likely fixes https://github.com/rust-lang/rust/issues/85045

[^1]: https://lists.gnu.org/archive/html/info-gnu/2025-02/msg00001.html
[^2]: https://llvm.org/docs/LangRef.html#the-llvm-compiler-used-global-variable
[^3]: https://github.com/rust-lang/rust/issues/139425
2025-06-05 22:52:17 +00:00
bjorn3
f8e9778eb1 Make #[used(linker)] the default on ELF too
#[used] currently is an alias for #[used(linker)] on all platforms
except ELF based ones where it is an alias for #[used(compiler)]. The
latter has surprising behavior and the LLVM LangRef explicitly states
that it "should only be used in rare circumstances, and should not be
exposed to source languages."

The reason #[used] still was an alias to #[used(compiler)] on ELF is
because the gold linker has issues with it. Luckily gold has been
deprecated with GCC 15 and seems to be unable to bootstrap rustc anyway.
As such we shouldn't really care about supporting gold.
2025-06-05 11:35:15 +00:00
WANG Rui
27f8efbae2 Bump object 2025-06-05 07:59:51 +08:00
Matthias Krüger
98da8e60eb
Rollup merge of #142005 - scottmcm:fieldidx-in-variantsmultiple, r=workingjubilee
Change `tag_field` to `FieldIdx` in `Variants::Multiple`

It was already available as a generic parameter anyway, and it's not like we'll ever put a tag in the 5-billionth field.

This is a first part of pulling smaller pieces out of rust-lang/rust#138759, so
r? workingjubilee
2025-06-04 19:50:26 +02:00
Scott McMurray
ee9901e65c Change tag_field to FieldIdx in Variants::Multiple
It was already available as a generic parameter anyway, and it's not like we'll ever put a tag in the 5-billionth field.
2025-06-03 23:42:21 -07:00
Matthias Krüger
59bdb5c3cf
Rollup merge of #141250 - folkertdev:s390x-z17-target-features, r=workingjubilee
add s390x z17 target features

tracking issue: https://github.com/rust-lang/rust/issues/130869

earlier target features were added in https://github.com/rust-lang/rust/pull/135630, and https://github.com/rust-lang/rust/issues/135413#issuecomment-2886439455 has some extra context on these new features.

r? ``@ghost``
cc ``@uweigand``
2025-06-04 07:54:33 +02:00
Matthias Krüger
644f06ec1f
Rollup merge of #141569 - workingjubilee:canonicalize-abi, r=bjorn3
Replace ad-hoc ABI "adjustments" with an `AbiMap` to `CanonAbi`

Our `conv_from_spec_abi`, `adjust_abi`, and `is_abi_supported` combine to give us a very confusing way of reasoning about what _actual_ calling convention we want to lower our code to and whether we want to compile the resulting code at all. Instead of leaving this code as a miniature adventure game in which someone tries to combine stateful mutations into a Rube Goldberg machine that will let them escape the maze and arrive at the promised land of codegen, we let `AbiMap` devour this complexity. Once you have an `AbiMap`, you can answer which `ExternAbi`s will lower to what `CanonAbi`s (and whether they will lower at all).

Removed:
- `conv_from_spec_abi` replaced by `AbiMap::canonize_abi`
- `adjust_abi` replaced by same
- `Conv::PreserveAll` as unused
- `Conv::Cold` as unused
- `enum Conv` replaced by `enum CanonAbi`

target-spec.json changes:
- If you have a target-spec.json then now your "entry-abi" key will be specified in terms of one of the `"{abi}"` strings Rust recognizes, e.g.
```json
    "entry-abi": "C",
    "entry-abi": "win64",
    "entry-abi": "aapcs",
```
2025-06-03 21:53:36 +02:00
Jubilee Young
e0b07a88a3 cg_llvm: convert to CanonAbi 2025-06-03 10:04:19 -07:00
bjorn3
0bd7aa1116 Move metadata object generation for dylibs to the linker code
This deduplicates some code between codegen backends and may in the
future allow adding extra metadata that is only known at link time.
2025-06-03 10:04:34 +00:00
bjorn3
badabab01f Only borrow EncodedMetadata in codegen_crate
And move passing it to the linker to the driver code.
2025-06-03 10:04:34 +00:00
bjorn3
2e8401ae5f Remove type_test from IntrinsicCallBuilderMethods
It is only used within cg_llvm.
2025-06-03 10:00:56 +00:00
bjorn3
00a88b903d Remove get_dbg_loc from DebugInfoBuilderMethods
It is only used within cg_llvm.
2025-06-03 10:00:11 +00:00
bors
2fc3deed9f Auto merge of #141760 - bjorn3:intrinsic_rework_part2, r=fee1-dead
Improve intrinsic handling in cg_ssa (part 2)

* Avoid computing function type and signature for intrinsics where possible
* Nicer handling of bool returning intrinsics

Follow up to https://github.com/rust-lang/rust/pull/141404
2025-06-02 00:57:45 +00:00
Guillaume Gomez
cf4fbc0695
Rollup merge of #141622 - folkertdev:powerpc-va_arg, r=workingjubilee
implement `va_arg` for `powerpc`

tracking issue: https://github.com/rust-lang/rust/issues/44930

The llvm `va_arg` implementation is well-known to have serious limitations. Some planned changes to rust's `VaList` make it much more likely that LLVM miscompiles `va_arg`, so this PR adds support for the various powerpc targets. Now at least the targets that `core` has explicit support for will continue to work.

For `powerpc` (the 32-bit variant) this implementation also fixes a bug where only up to 20 variadic arguments were supported.

Locally (with qemu), these targets now pass the tests in https://github.com/rust-lang/rust/blob/master/tests/run-make/c-link-to-rust-va-list-fn/checkrust.rs. That test does not actually run for the powerpc targets in CI though.

The implementation is based on clang:

- handling of big endian architectures 3c8089d1ea/clang/lib/CodeGen/ABIInfoImpl.cpp (L191-L193)
- 64-bit 3c8089d1ea/clang/lib/CodeGen/Targets/PPC.cpp (L969)
- 32-bit 3c8089d1ea/clang/lib/CodeGen/Targets/PPC.cpp (L430)

cc `@daltenty` (target maintainer)
r? `@workingjubilee`
`@rustbot` label: +F-c_variadic
2025-06-01 19:35:42 +02:00
Folkert de Vries
be13ce341a
implement va_arg for powerpc
This actually fixes a bug where before only 20 arguments could be passed. As far as I can tell, an arbitrary number of arguments is now supported
2025-06-01 00:51:01 +02:00
Folkert de Vries
108a36efe4
implement va_arg for powerpc64 and powerpc64le 2025-05-30 18:02:47 +02:00
bors
15825b7161 Auto merge of #139385 - joboet:threadlocal_address, r=nikic
rustc_codegen_llvm: use `threadlocal.address` intrinsic to access TLS

Fixes #136044
r? `@nikic`
2025-05-30 15:39:56 +00:00
bjorn3
284bec5428 Directly use from_immediate for handling bool 2025-05-30 10:12:57 +00:00
bjorn3
0fcea3db28 Avoid computing function type for intrinsic instances 2025-05-30 10:12:18 +00:00