Commit Graph

1247 Commits

Author SHA1 Message Date
James Barford-Evans
af2f1f375c use ConstExt for methods that do not yet exist in rustc_type_ir 2026-09-22 13:04:44 +01:00
James Barford-Evans
c56a9917d8 Wire up f16b in backends 2026-09-21 08:57:52 +01:00
Nicholas Nethercote
8515e12560 Remove global_backend_features query
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.
2026-09-16 15:29:44 +10:00
Nicholas Nethercote
81210c9d79 Change CodegenBackend::init to take &mut self
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.)
2026-09-16 15:29:44 +10:00
Nicholas Nethercote
51673f89e1 Split build_session
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.
2026-09-16 15:29:42 +10:00
mejrs
6aaabbb863 compiler: fix bug and span_bug imports 2026-09-15 16:20:18 +02:00
Jakub Beránek
8095ae701b Update nightly version 2026-09-11 17:09:03 +02:00
Jakub Beránek
316c827de5 Merge ref 'ca0a6473ffde' from rust-lang/rust
Pull recent changes from https://github.com/rust-lang/rust via Josh.

Upstream ref: rust-lang/rust@ca0a6473ff
Filtered ref: rust-lang/rustc_codegen_cranelift@66664f8bd7
Upstream diff: https://github.com/rust-lang/rust/compare/...ca0a6473ffde01deb7fce24cc04864cf723e14a0

This merge was created using https://github.com/rust-lang/josh-sync.
2026-09-11 15:17:14 +02:00
Jakub Beránek
a6fafea6f1 Prepare for merging from rust-lang/rust
This updates the rust-version file to ca0a6473ff.
2026-09-11 15:17:10 +02:00
Jakub Beránek
fde670d43d Initial josh-sync configuration 2026-09-11 15:17:07 +02:00
Jonathan Brouwer
bd0cf87c2c Rollup merge of #162057 - bjorn3:fatal_cleanup, r=mejrs
Some cleanups around fatal errors and delayed bugs

With the aim of making it easier to move some compiler passes to different locations. And in particular to allow moving them across `has_errors_or_delayed_bugs` calls. This is a prerequisite for a local change that improves the effectiveness of cargo build pipelining.

Fixes https://github.com/rust-lang/rust/issues/152204
2026-09-10 14:22:30 +02:00
bjorn3
49e77c38bd Merge commit '5e120485964f4857f1ad70f7d661fd244d087668' into sync_cg_clif-2026-09-10 2026-09-10 10:18:10 +02:00
bjorn3
052ee70292 Don't rely on codegen emitting fn_abi_of_* errors in abi checks
This avoids a delayed bug if compilation is aborted between checking
function ABIs and codegening all functions.
2026-09-10 09:40:28 +02:00
bjorn3
d64cce9c3b Pass -Zforce-unstable-if-unmarked to cg_clif tests
This makes their test environment consistent between runs inside the
cg_clif and rust repos.
2026-09-09 12:47:02 +02:00
bjorn3
a2b78f28ca Merge commit 'db693f7dbcfab9af2a89b703096f692417e2f756' into sync_cg_clif-2026-09-08 2026-09-08 13:30:06 +02:00
Jonathan Brouwer
f04af2086f Rollup merge of #160197 - susitsm:restrict-inline-asm-cookies, r=petrochenkov
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.

Fixes rust-lang/rust#150451

r? bjorn3
2026-09-07 17:08:07 +02:00
Matyas Susits
970cb98f0e Disable inline asm line info cookies when llvm bitcode is saved or LTO is enabled
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.
2026-09-05 11:52:40 +02:00
Reuben Cruise
cbe88adf39 Creates new InlineAsmSize enum
Facilitates scalable vector support in inline assembly.
2026-09-04 13:29:17 +00:00
Jonathan Brouwer
198bd21de8 Rollup merge of #161971 - hanna-kruppe:rm-flag-satfpcast, r=RalfJung
Remove -Zsaturating-float-casts flag

I added this flag back in 2017 (rust-lang/rust#45205) to enable benchmarking of the saturating semantics when it was newly implemented and still experimental. But saturation has been the official semantics for float<->int `as` casts for years now. A flag for turning it off no longer serves any purpose, it's just `-Zplease-miscompile-casts` now.
2026-09-01 21:26:14 +02:00
Jonathan Brouwer
f9090000f4 Rollup merge of #160989 - N1ark:08-12-generic-float-intrinsincs, r=folkertdev
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
2026-09-01 13:27:38 +02:00
Jonathan Brouwer
1aeb21ba42 Rollup merge of #137720 - folkertdev:naked-function-target-feature, r=Amanieu
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
2026-09-01 13:27:35 +02:00
N1ark
edcae4441d Make sin, cos, exp, exp2, log, log2, log10 generic 2026-08-31 18:57:54 +01:00
N1ark
3ba7833fd1 Cleanup cranelift codegen_float_intrinsic_call 2026-08-31 18:30:59 +01:00
Folkert de Vries
178eb4b50f attach naked function target features to module assembly 2026-08-31 00:25:34 +02:00
Scott McMurray
7519f42401 Add intrinsics for integer minimum and maximum 2026-08-30 00:48:34 -07:00
Hanna Kruppe
c7d949d9ea Remove -Zsaturating-float-casts flag
I added this flag back in 2017 to enable benchmarking of the saturating
semantics when it was newly implemented and still experimental. But
saturation has been the official semantics for float<->int `as` casts
for years now. A flag for turning it off no longer serves any purpose,
it's just `-Zplease-miscompile-casts` now.
2026-08-29 13:50:44 +02:00
Jonathan Brouwer
8e3d555214 Rollup merge of #159103 - aapoalas:reborrow/fix-borrowed-place, r=oli-obk
fix(reborrow): recursive implementation

If Reborrow finds '&'a mut T' fields where 'a is the Reborrowed type's first lifetime parameter (currently the only lifetime that is allowed to reborrow) then it inserts a Deref and borrow of the T, and likewise if it finds a 'T: Reborrow' field then the field type is recursed into.

This makes Reborrow always produce the correct borrow checking logic at the cost of most probably being inconsiderately expensive. The thinking is that performance will be a followup consideration.

r? @oli-obk
2026-08-28 10:15:37 +02:00
Aapo Alasuutari
5686e56377 fix(reborrow): recursive implementation
* Implement Reborrow as a recursive operation

If Reborrow finds '&'a mut T' fields then it inserts
a Deref and borrow of the T, and likewise if it finds a
'T: Reborrow' field then the field type is recursed into.

This makes Reborrow always produce the correct borrow checking logic at
the cost of most probably being inconsiderately expensive. The thinking
is that performance will be a followup consideration.
* PhantomDeref
* Simpler deref test

* Add more PhantomDeref unreachability assertions
* Write out lifetime omission
* Document ProjectionElem::PhantomDeref
* Comment half of reborrow tests
* fix PhantomDeref conflicting with AccessDepth::Shallow
* Recheck CoerceShared in borrowck TypeChecker to ensure its lifetimes make sense
* Fix rebase
* Changes... but where to?
* Typo fix

* Improve comment
* Use fully_perform_op to evaluate CoerceShared trait in borrowck
* More CoerceShared comment
* Comment rest of Reborrow tests

Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de>
2026-08-27 20:03:59 +00:00
Jonathan Brouwer
3cc643bd77 Rollup merge of #160132 - folkertdev:pad-i32-count, r=bjorn3
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).
2026-08-24 15:06:55 +02:00
Folkert de Vries
d5d5ff7edf make pad_i32 of PassMode::cast an integer
so that we can specify more than one i32 of padding.
2026-08-21 21:25:26 +02:00
mejrs
38af4a23c2 move CrateType 2026-08-21 10:35:02 +02:00
bjorn3
588068c141 Merge commit 'bb351cd4ddf59d8a9b5c086a15e41bc6a1c05b5f' into sync_cg_clif-2026-08-19 2026-08-19 19:39:57 +02:00
Jonathan Brouwer
765bc489c8 Rollup merge of #160058 - RalfJung:atomic-volatile, r=JohnTitor
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.
2026-08-17 20:58:24 +02:00
bjorn3
b3e95724e7 Merge commit 'de3cd39a9be127b7c5f9744dfc865d57a9d564d3' into sync_cg_clif-2026-08-17 2026-08-17 13:16:14 +02:00
Ralf Jung
78a3e30143 atomic volatile: add intrinsics 2026-08-16 22:38:03 +02:00
mejrs
196dd2a174 Fix up lang_items import paths 2026-08-09 15:20:29 +02:00
Jonathan Brouwer
6a3b9acf14 Rollup merge of #160530 - RalfJung:internal-target-features, r=nnethercote
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.
2026-08-08 00:38:22 +02:00
Ralf Jung
8bb9f279a9 refactor handling of target features in Session 2026-08-06 14:45:53 +02:00
Jonathan Brouwer
592096bca8 Rollup merge of #160398 - moulins:layout-is-variant-uninhabited, r=oli-obk
rustc_abi: Add `LayoutData::is_variant_uninhabited` method

This is a cheaper alternative to `TyAndLayout::for_variant(_, idx).is_uninhabited()`, which avoids the extra work done by `TyAndLayout::for_variant`.

EDIT: Let me expand on the motivation from this [comment](https://github.com/rust-lang/rust/pull/160398#issuecomment-5167761369):

`TyAndLayout::for_variant` is a curious API: the resulting layout doesn't represent a Rust type in the strict sense (not even `pattern_type!(Enum is Variant { .. })`; instead, it only describes the "variant-specific" fields, and fields shared between variants (e.g. the enum tag or coroutine captures) are entirely missing.

As such, I believe alternative APIs should be used instead to clarify intent, when possible. This PR deals with the simplest cases, and a future PR will add `TyAndLayout::variant_field(self, cx, variant_idx, field_idx)` to deal with the next-most common case of only needing a variant layout for field lookups.
2026-08-06 12:41:07 +02:00
Moulins
fee8922aa3 rustc_abi: Add LayoutData::is_variant_uninhabited method
This is a cheaper alternative to `TyAndLayout::for_variant(_, idx).is_uninhabited()`
2026-08-05 15:38:43 +02:00
Jonathan Brouwer
900f0bcb32 Rollup merge of #159225 - bjorn3:split_incr_comp_session, r=oli-obk
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
2026-08-05 13:36:11 +02:00
Folkert de Vries
6ac1fe6c84 fix f128 libcall abi in the cranelift backend 2026-08-04 10:22:35 -04:00
Jonathan Brouwer
9956011498 Rollup merge of #160012 - RalfJung:deref-validity, r=oli-obk
miri: ensure validity of references and pointers we dereference and cast

Conceptually, when we evaluate the place expression `*place`, there are two steps that happen:
- perform a (typed) load from `place`, which yields a value of pointer/ref type
- construct a new place from that value

Since this is a typed load, we have to ensure that the value satisfies the validity invariant. We forgot to do that, which led to Miri missing a bunch of UB. This PR fixes that by adding the missing checks.

Triggering this UB is a bit non-trivial: you cannot just store an invalid value into `place` using regular assignment (that would already be caught as part of the assignment). However, you can take a raw pointer to `place` and then use that to mutate the contents of `place` in a way that its validity invariant no longer holds. For example:
```rust
fn main() {
    let mut x = &();
    // Overwrite `x` with null.
    unsafe { (&raw mut x).cast::<usize>().write(0) };
    let _val = *x; //~ERROR: null reference
}
```
This program clearly (IMO) should have UB, and has UB in MiniRust, but Miri accepted that program before this PR. The same applies to references that are invalid because they are unaligned (even if we end up only accessing a 1-aligned field, i.e. the rest of the place expression evaluates just fine), and to references that are invalid because they are not dereferenceable (even if we end up projecting to a zero-sized field, i.e., the rest of the place expression evaluates just fine).

Even raw pointers have this problem: a raw pointer can be invalid if its vtable pointer is wrong, and we did not check that.

And finally, this also affects `Box`, and this is where things get tricky. `Box` derefs are not even present any more in the MIR Miri sees; they have been replaced by derefs of the underlying raw pointer. But that means we have no way to know that we should check all those things. So to fix that I adjusted the ElaborateBoxDerefs to emit a new special kind of cast that's almost a transmute but also checks `Box` validity.

Fixes https://github.com/rust-lang/miri/issues/5226
Fixes https://github.com/rust-lang/unsafe-code-guidelines/issues/617
Cc @rust-lang/opsem
2026-08-01 15:56:37 +02:00
Ralf Jung
fcf68f21cf introduce a new CastKind for ElaborateBoxDerefs 2026-08-01 09:27:16 +02:00
Scott McMurray
6da3077db9 Structurally prevent zero-count BackendRepr::SimdVectors
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).
2026-07-30 19:48:24 -07:00
Jacob Pratt
0e238fd6c3 Rollup merge of #138618 - nbdd0121:asm_const_ptr, r=bjorn3
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.
2026-07-26 00:21:39 -04:00
Daniel Paoliello
fe37a8afad Update bootstrap to use -Zembed-metadata=no instead of -Zno-embed-metadata
The recent Cargo submodule update picked up <https://github.com/rust-lang/cargo/pull/17149>

However, bootstrap was still using the old name, resulting in:

```
Building stage1 library artifacts (stage1 -> stage1, arm64ec-pc-windows-msvc)
error: unknown `-Z` flag specified: no-embed-metadata
```

Fix is to switch to the rename.

I also removed the diff in the Cranelift setup script, since they must have already hit this issue and no longer need the workaround.
2026-07-23 11:09:16 -07:00
bjorn3
f9679fa4df 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.
2026-07-23 12:14:56 +02:00
Gary Guo
338d560eaf Handle pointers with offset for asm const 2026-07-22 13:47:31 +01:00
Gary Guo
ca4033987e Unify handling of asm const and sym using CTFE
This gives the asm-const code the basic ability to deal wiht pointer and
provenances, which lays the ground work for asm_const_ptr.

Note that `SymStatic` is not fully removed, a specialized is kept and
renamed as `SymThreadLocalStatic`, for `#[thread_local]` statics where CTFE
does not support naming. The `#[thread_local]` is unstable feature and it's
not clear if we want to support this in `sym`, but removal of it should be
a separate PR.
2026-07-22 13:47:31 +01:00