It's now possible to get the backend features (a `Vec<String>`) when the
codegen backend is started, pass it back through `CodegenBackendInit`,
and just store it in the `Session`. This removes the need for the query.
Also:
- `WriteBackendMethods::target_machine_factory` no longer needs the
`target_features` parameter, because it's now available through the
`sess` parameter.
- `CodegenContext` no longer needs the `backend_features` field because
we can use `sess.global_backend_features` instead.
- `CodegenBackend::provide` is now a no-op for all the in-tree backends.
I haven't removed it because out-of-tree backends still rely on it.
It currently takes `&self`, which is a bit strange for an `init` method.
As a result, the Cranelift and GCC backends have to use types with
interior mutability.
This commit changes it to `&mut self`. Benefits:
- The Cranelift backend can use `Option` instead of `OnceCell` to
indicate uninit vs. init.
- The GCC backend can avoid `Mutex`, and use `bool` instead of
`AtomicBool`, which makes things much simpler. The commit also
restructures `GccCodegenBackend` to mirror `CraneliftCodegenBackend`:
just contain an `Option<BackendConfig>`, which makes the uninit vs.
init distinction foolproof. (E.g. no need to set `lto_supported` to
false and then later overwrite it with the real value.) As part of
this the `LockedTargetInfo` type is renamed `SharedTargetInfo` because
that better matches its new internals. (All this compiles both with
and without the "master" feature set.)
Session creation is currently awkward: we build a mostly-initialized
session, then use it to initialize a codegen backend, and then use the
codegen backend to finish initializing the session.
And it's not just awkward: within the Cranelift backend's `init` method
`sess.lto()` is called, which consults `sess.thin_lto_supported`,
*before* that field has been properly set! In practice it had no effect
but it's worth fixing.
This commit cleans up this mess. It introduces `EarlySession`, which
contains just four `Session` fields, the ones that are needed for
codegen backend initialization. It is now a field within `Session`, and
`Session` derefs to `EarlySession` to avoid changing a zillion
`sess.target`/`sess.opts`/etc. occurrences. `EarlySession` is passed to
`init`, which returns a `CodegenBackendInit` that contains the
backend-specific information needed to build a `Session`. (It replaces
the `replaced_intrinsics`, `fallback_intrinsics`, and
`thin_lto_supported` methods.) The `Session` can then be built in a
single step. No more `Session`/`CodegenBackend` initialization
intermingling.
A few functions that previously took a `Session` now take something
else, e.g. a `Target`. Some `Session` methods are now `EarlySession`
methods. And a new `early_lto` method is used for Cranelift's LTO check.
Refactor the way cg_ssa handles indirect returns (returns via `sret`) — Take 2
This continues the work that @FractalFir done in rust-lang/rust#144976.
**I managed to make this work, so this confirms the original approach works and is ready to merge.**
r? workingjubilee
(assigning the same reviewer as the original PR)
# The problem
The current way `cg_ssa` handles `PassMode::Indirect` is fundamentally incompatible with the requirements of the GCC backend. This is currently worked around in a very brittle way, that breaks on ARM.
In order to function correctly, `cg_gcc` requires the `sret`(indirect return) pointer to be treated in a special way.
# The solution
This PR separates the `sret` pointer from all the other arguments, allowing each backend to decide how it wants to handle that pointer. This will allow GCC to do it's own thing, while changing nothing on the LLVM side. Currently, the PR just makes both backends preappend the sret arg, mimicking the previous behaviour.
The PR is based on this [zulip suggestion](https://rust-lang.zulipchat.com/#narrow/channel/421156-gsoc/topic/Project.3A.20Bootstrap.20of.20rustc.20with.20rustc_codegen_gcc/near/526487725).
# Assumptions I made.
The PR makes certain assumptions about the ABI handling.
1. No LLVM intrinsic returns via sret. [According to my sources](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/Can.20LLVM.20intrinsics.20return.20via.20PassMode.3A.3AIndirect.3F), this is always true.
2. Tail calls don't currently return via `sret`. From [asking about](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/Can.20tail.20calls.20return.20indirectly.28via.20a.20pointer.29.3F/near/532984507), it seems like the tail call handling does not currently support indirect returns. So, I did not add support for that.
3. TLS shims don't perform indirect returns, and asserts and drops also don't perform indirect returns(since they return nothing) - those assumptions seemed reasonable.
4. Inline assembly calls don't use indirect returns. I *assumed* functions like `inline_asm_call` will not need the `sret` handling code. I am not 100% sure about this, tough.
Restrict LLVM inline asm location cookie usage. Fixes#150451
The parallel frontend makes the cookies nondeterministic in their current form, resulting in nondeterministic outputs when bitcode is emitted or LTO is used.
Causes minor diagnostic regression for inline asm in release builds. See rust-lang/rust#150451 for details.
Fixesrust-lang/rust#150451
r? bjorn3
The parallel frontend makes the cookies nondeterministic in their
current form, resulting in nondeterministic outputs when bitcode is
emitted or LTO is used.
Causes minor diagnostic regression for inline asm in release builds.
Make sin, cos, exp, exp2, log, log2, log10 generic
Rebased and smaller version of rust-lang/rust#153934
Following `fabs`, make the `sin`, `cos`, `exp`, `exp2`, `log`, `log2` and `log10` intrinsics generic over the float type, rather than having four variants per float type.
The first two commits are purely stylistic:
- reorganised Cranelift code to make following changes simpler
- moved a misplaced comment in `compiler/rustc_codegen_llvm/src/intrinsic.rs` that caused `x fmt` to give up
The last commit actually makes them generic! Most code is a bit simpler, ~~and this will also hopefully simplify adding support for these intrinsics for the future [`bf16` type](https://github.com/rust-lang/rust/pull/160859)~~ :)
Unfortunately both GCC and Cranelift backend changes are a bit churny. Their code is a bit, opaque, to put it kindly, and I didn't want to refactor those here.
r? @folkertdev
cc @RalfJung
support `#[target_feature(enable = ...)]` on `#[naked]` functions
fixes https://github.com/rust-lang/rust/issues/136280
Instructions that are part of a target feature require a special directive on some targets. This PR adds those for the most common targets.
This is very WIP, but I'm hoping to collect some feedback on what is (not) supported and how to report that to users.
r? @ghost
cc @taiki-e @Amanieu
test `f16::mul_add` not double-rounding the result
test the precision of `f16::mul_add`. The semantics of `mul_add` are that there should only be one rounding of the final result back into the storage type, i.e. the intermediate result of the multiplication should not be rounded.
A naive implementation of `f16::mul_add` as `f32::mul_add(a as f32, b as f32, c as f32) as f16` has insufficient precision, see https://github.com/llvm/llvm-project/issues/98389.
Our `rustc_codegen_gcc` backend still used the `f32` approach, this PR changes it to instead (implicitly) use the fallback body from `core`, which uses `f64::mul_add` so that the result is correctly rounded.
make `pad_i32` of `PassMode::cast` an integer
so that we can specify more than one i32 of padding. This PR only adds the functionality but does not yet use it: there should be no functional changes.
This is needed for the ABI of `Complex<{ float }>` on 32-bit powerpc. Other mechanisms, e.g. using `PassMode::prefixed` don't appear to work.
More discussion is in [#t-compiler/help > power complex abi](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/power.20complex.20abi/with/613294420).
Add floating point inline ASM support for SPARC
This PR adds support for floating point registers to SPARC inline ASM.
Ping target maintainers: @psumbera @kulikjak @jonathanpallant @mvolfik @he32 @0323pin @semarie
Tracking issues:
f16 inline ASM: rust-lang/rust#125398 (part of rust-lang/rust#116909)
SPARC inline ASM: rust-lang/rust#93335
atomic volatile: add intrinsics
Extend the atomic load/store intrinsics to also support volatile atomic load/store.
Tracking issue: https://github.com/rust-lang/rust/issues/158947.
Library APIs that use these intrinsics will be added inn a future PR.
The GCC and cranelift implementations ignore the volatile flag, not sure how that should be implemented for them.
- Upgrade from `getrandom v0.4.2` to `v0.4.3` to drop its `wasip2` and
`wasip3` dependencies and many transitives.
- Upgrade from `gimli v0.33` to `v0.34` as a direct dependency and
through a `thorin-dwp` upgrade.
- Upgrade from `object v0.37` and `v0.38` to `v0.39` as a direct
dependency and via `ar_archive_writer` and `thorin-dwp` upgrades.
- Upgrade `libloading` and `wasmparser` to match other dependencies.
This also consolidates from `hashbrown v0.15`, `v0.16`, and `v0.17` to
just `v0.17.1`, which is the same that `std` currently uses.
refactor handling of target features in Session
`Session` currently contains two lists of target features: `target_features`, which is also exposed in `cfg`, and `unstable_target_features`, which is used internally to communicate between various parts of the compiler which target features are *actually* available, including some that we don't have plans to put in `cfg`, namely "forbidden" target features. The `unstable_target_features` list is *not* equivalent to what nightly code sees in `cfg(target_features)` as the latter excludes "forbidden" target features. Both lists are computed by `fn cfg_target_features` even though one of them is never used for `cfg`. It's all kind of messy.
This PR refactors that: `fn cfg_target_features` is replaced by `fn internal_target_features` which computes all enabled Rust target features (including "forbidden" ones -- which are really more like "internal-only" ones so the 2nd commit renames them). We then compute `cfg(target_features)` from that. The session only stores one list, `internal_target_features`, which corresponds to the previous `unstable_target_features`.
To simplify computing `internal_target_features` I also refactored `parse_rust_feature_list` to better distinguish actual Rust target features from unknown target features that we are just grandfathering in. I also made `implied_target_features` not rebuild the same hash map over and over again. And I got rid of a bunch of silly temporary vectors and iterations over all Rust target features.
Split IncrCompSession out of Session
This will allow introducing a separate incr comp session dir for the post LTO artifacts in the future. In addition it statically encodes the lifetime of the incr comp session rather than requiring an enum behind a mutex stored in the Session.
Based on https://github.com/rust-lang/rust/pull/159000
Part of https://github.com/rust-lang/compiler-team/issues/908
Structurally prevent zero-count `BackendRepr::SimdVector`s
This is already *supposed* to be impossible in layout, but this emphasizes that better. This PR makes no behaviour changes; just `BackendLaneCount` instead of `u64` in https://doc.rust-lang.org/nightly/nightly-rustc/rustc_abi/enum.BackendRepr.html
Ironically I was inspired to do this as part of [looking at making `Simd<T, 0>` *work*](https://rust-lang.zulipchat.com/#narrow/channel/257879-project-portable-simd/topic/Allow.20N.3D.3D0.3F/with/613345298), but importantly if that's going to happen I think it should be `BackendRepr::Memory` like other ZSTs, *not* a `BackendRepr::ScalarVector` that would need to carry around a useless LLVM value in `OperandValue::Immediate` (where it's not even clear what the LLVM type of that value would be).
This is already *supposed* to be impossible in layout, but this emphasizes that better.
Ironically I was inspired to do this as part of looking at making `Simd<T, 0>` *work*, but importantly if that's going to happen I think it should be `BackendRepr::Memory` like other ZSTs, *not* a `BackendRepr::ScalarVector` that would need to carry around a useless LLVM value in `OperandValue::Immediate` (where it's not even clear what the LLVM type of that value would be).
Support using const pointers in asm `const` operand
Implements [RFC#3848](https://github.com/rust-lang/rfcs/pull/3848) with tracking issue rust-lang/rust#128464
This adds support of const pointers for asm `const` in addition to plain integers.
The inline `asm!` support is implemented using `i` constraint, and the `global_asm!` and `naked_asm!` support is implemented by inserting `symbol + offset` and make `symbol` compiler-used. For unnamed consts, it will create additional internal & hidden symbols so that they can be referenced by global_asm.
~~The feature is also implemented for GCC backend but it's untested.~~ Tested now.
This will allow introducing a separate incr comp session dir for the
post LTO artifacts in the future. In addition it statically encodes the
lifetime of the incr comp session rather than requiring an enum behind a
mutex stored in the Session.