intrinsics: Rename `abort` to `abort_immediate`
The semantics of `intrinsics::abort()` are closer to what we have unstably as `abort_immediate()` than to `process::abort()` or `libc::abort()`. Rename it to make more clear that the intrinsic is more of an intentional crash with platform-specific behavior than what `libc::abort()` tries to be (i.e. raising `SIGABRT`).
If desired, a new intrinsic like `abort_gracefully` could be introduced that performs platform-specific behavior. This would more cleanly unblock https://github.com/rust-lang/rust/pull/149780.
See also discussion at the tracking issue for `immediate_abort` https://github.com/rust-lang/rust/issues/154601.
The semantics of `intrinsics::abort()` are closer to what we have
unstably as `abort_immediate()` than to `process::abort()` or
`libc::abort()`. Rename it to make more clear that the intrinsic is more
of an intentional crash with platform-specific behavior than what
`libc::abort()` tries to be (i.e. raising `SIGABRT`).
A common definition is like a weak definition except that it must be a
zero-initialized static and when merging two common symbols with the
same name, the size and alignment are set to the higher of both symbols.
This is used for tentative definitions in C and doesn't have any reason
to exist outside of that. This behavior doesn't work across dylibs and
common symbols have inconsistent behavior across linkers [1]. It is also
fragile to rely on getting the largest size of all common symbol
definitions as a (possibly smaller) global definition can override it.
Link: https://maskray.me/blog/all-about-common-symbols [1]
do not complain about unstable target features on nightly
This was brought up in https://github.com/rust-lang/rust/issues/162235#issuecomment-5856608121: we currently print a warning on nightly saying that using an unstable target feature will become a hard error. That's a mistake.
We could either remove the part of the warning that talks about it becoming a hard error, or we could just hide the warning entirely for unstable features on nighty. I went for the latter -- nightly is meant for experimentation with those features so having un-silenceable warnings is not great.
Cc @workingjubilee
Add support for -Zsanitizer-cfi-minimal-runtime
For production use, we should only link in the ubsan_minimal runtime,
instead of the complete ubsan runtime. This adds support for both
cfi-recover and cfi-diag to use the minimal runtime when
`-Zsanitizer-cfi-minimal-runtime` is specified.
This also includes tests, to ensure the flag can only be used if either
cfi-recover or cfi-diag is enabled, it doesn't disrupt the original
behavior, and links in the correct runtime when specified.
cc @1c3t3a
?r rcvalle
Ensure llvm worker threads have sufficient stack space
The rustc driver sets the default stack size to 16MB, however worker threads spawned by the backend will use std's default stack size (usually 2MB).
Pass through the stack size chosen by the driver to the backend, and explicitly request the stack size.
This probably isn't the preferred way to do this, but maybe a starting point to educate me.
This doesn't catch every spawned thread (jobserver and ctrlc also create them through std). Are there other threads which might be sensitive to small stack sizes?
Fixesrust-lang/rust#163272
Less `SpanData` in diagnostics
`SpanData` should really just be an internal detail of `Span`, and its use is worth minimizing. This PR gets rid of two uses.
Details in individual commits.
r? @bjorn3
use is_some_and / is_none_or in a few more places
IMO that makes the code more readable.
Clippy also lints this by default these days, but we don't run that lint on the compiler it seems.
Because they're all just special cases of warnings. This requires
introducing a new `Option<EmissionOverride>` field to `Warning` that
describes the special case behaviour; the `DiagInner::lint_id` field
also gets merged in.
Specific nice things about this:
- Removes some unreachable match arms for `Allow`/`Expect`.
- Removes the hacky upgrading of `Allow`/`Expect` to `Warning` in
`emit_future_breakage_report`.
- The types now have structure that used to be maintained by comments
and assertions. E.g. it's now impossible to not have a `lint_id` for
an `expect` lint. (I always found the `DiagInner::lint_id` field
confusing; it's clearer now.)
- There's a nice comment on `EmissionOverride` summarizing all the
different cases.
- A little less code overall.
The rustc driver sets the default stack size to 16MB, however
worker threads spawned by the backend will use std's default
stack size (usually 2MB).
Pass through the stack size chosen by the driver to the backend,
and explicitly request the stack size.
This is a part of the stack-protector work.
Adding support for merge-functions is not strictly needed, but it does
prevent breakage if the GCC function merging logic changes.
Not adding tests for -Z merge-functions since I don't see any such tests for the
LLVM side of it.
It's a cut down version of `rustc_errors::DiagInner` that avoids `Span`.
It exists because `Span` used to not impl `Send` and so couldn't be sent
from codegen threads to the main thread. But that's no longer true and
we can send `DiagInner`s directly now.
Build a new incr comp session dir from scratch every time
Rather than copying the old incr comp dir and then modifying it. This saves a copy/hardlink for files that are modified. And it removes the need for accurate work product tracking to avoid accumulating cruft, which is non-trivial. We don't accurately track the pre-LTO bitcode files for ThinLTO for example.
Part of https://github.com/rust-lang/compiler-team/issues/908
Rather than copying the old incr comp dir and then modifying it. This
saves a copy/hardlink for files that are modified. And it removes the
need for accurate work product tracking to avoid accumulating cruft,
which is non-trivial. We don't accurately track the pre-LTO bitcode
files for ThinLTO for example.
Adjust for Arm64EC name mangling when checking for exported symbols
After rust-lang/rust#160679 the Arm64EC nightly validation run internally by Microsoft started failing with:
```
=== STDOUT ===
main.c
main.obj : error LNK2019: unresolved external symbol my_add referenced in function #my_add$exit_thunk (EC Symbol)
main.obj : error LNK2019: unresolved external symbol my_hash_lookup referenced in function #my_hash_lookup$exit_thunk (EC Symbol)
main.obj : error LNK2019: unresolved external symbol call_internal referenced in function #call_internal$exit_thunk (EC Symbol)
main.obj : error LNK2019: unresolved external symbol my_safe_div referenced in function #my_safe_div$exit_thunk (EC Symbol)
main.exe : fatal error LNK1120: 4 unresolved externals
```
Root cause is that `symbol_edit` wasn't compensating for Arm64EC's name mangling, and so these symbols were missed.
Only check `SanitizerCfiMinimalRuntimeRequiresCfiRecoverOrDiag` if CFI
is actually enabled.
Use `is_sanitizer_cfi_minimal_runtime_enabled()` helper within
`link.rs`.
Remove `DiagInner::sort_span`
It's a field that can be used for sorting diagnostics. By default it is set to the primary span. It's only used for `BorrowckDiagnosticsBuffer`.
This commit removes it and adds a `sort_span` to each diagnostic recorded in `BorrowckDiagnosticsBuffer`. This avoids various other pieces of code having to deal with it.
r? @lqd
Both will be used by the amdgpu target to implement the `gpu-kernel`
ABI.
`address_space` specifies the address space of an indirect argument.
`AmdgpuKernelArg` translates to LLVM’s byref, which is similar to
on_stack/byval, however, there is no extra copy made, the pointer may
not point to the stack but can point to some other address space, and
the passed argument should not be modified.
byval and byref are mutually exclusive, so change on_stack to an enum
with the new states, Pointer (none), OnStack and AmdgpuKernelArg.
It's a field that can be used for sorting diagnostics. By default it is
set to the primary span. It's only used for `BorrowckDiagnosticsBuffer`.
This commit removes it and adds a `sort_span` to each diagnostic
recorded in `BorrowckDiagnosticsBuffer`. This avoids various other
pieces of code having to deal with it.
`core::num::f16b` Rust's 16bit Brain Float
Implements the [RFC: f16b type](https://github.com/rust-lang/rfcs/pull/3983). Best reviewed commit by commit, happy to split into separate PRs if that is deemed easier to review. However the line count and surface area is, in my opinion, reasonably small.
Adds;
- ABI plumbing for the `f16b` along with `bfloat` lang item to work with LLVM, GCC is explicitly `unimplemented!(...)`
- `f16b` feature gate, page for `f16b` on libruscdoc and a `struct bf16` in `core::num`
- Tests
- Treat `f16b` as a scalar primitive for scalable vectors
Issues;
- [Tracking Issue](https://github.com/rust-lang/rust/issues/160630)
- [RFC](https://github.com/rust-lang/rfcs/pull/3983)
Staticlib rename internal symbols: add COFF support
Follow-up to rust-lang/rust#156950.
`-Zstaticlib-rename-internal-symbols` now also works on COFF targets (Windows). Renaming only rewrites symbol names, so unlike hide it needs no visibility concept.
COFF objects keep their string table at the end of the file, so renames append the new names there and patch the 4-byte length prefix plus each symbol's name offset in place. Both regular and bigobj objects are handled; on i686 a leading underscore is stripped when matching against the exported set. The archive format differs (GNU ar on windows-gnu, COFF on windows-msvc) but the members are always COFF objects, so the existing archive code is unchanged.
Supported on ELF, Apple, and COFF targets. `-Zstaticlib-hide-internal-symbols` remains ELF/Apple-only and still warns on Windows.
A run-make test `staticlib-rename-internal-symbols-coff` mirrors the existing ELF and Mach-O tests.
r? @bjorn3
For production use, we should only link in the ubsan_minimal runtime,
instead of the complete ubsan runtime. This adds support for both
cfi-recover and cfi-diag to use the minimal runtime when
`-Zsanitizer-cfi-minimal-runtime` is specified.
This also includes tests, to ensure the flag can only be used if either
cfi-recover or cfi-diag is enabled, it doesn't disrupt the original
behavior, and links in the correct runtime when specified.
Co-Authored-By: Bastian Kersting <bkersting@google.com>
It encodes the `emit` behaviour, one of:
- `BugAbort`: Abort as a bug.
- `FatalAbort`: Abort as a fatal error.
- `ErrorGuaranteed`: Return an `ErrorGuaranteed` (the default).
- `()`: return `()`.
This is useful but `Diag` is very widely used and it's arguably not
useful enough to be worth the generic parameter. This commit removes it.
Benefits:
- Many fewer generic parameters, including many functions where the
genericity is entirely uninteresting. This includes every
`Diagnostic::into_diag` and `Subdiagnostic::add_to_diag`. Removes the
need for the comment about `<G>` on `trait Diagnostic`.
- No `PhantomData` in `Diag`.
- Makes bootstrapping a bit faster.
The downside is there is no longer a single `emit` method.
- Getting a `!` return type requires calling `emit_bug` or `emit_fatal`.
This only affects a few call sites.
- Getting an `ErrorGuaranteed` return type requires calling `emit_err`.
This affects a lot of call sites. (It arguably makes call sites more
self-documenting.)
- Using the wrong `emit_*` on a diagnostic causes a runtime abort, which
previously couldn't occur. Seems unlikely, but it's not impossible.
- Note: If no particular return type is needed, `emit` can be used for
any diagnostic. (Bug/fatal diagnostics will still trigger abort.)
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.
Simplify the `G` in `Diag<'a, G>`
The `EmissionGuarantee` trait isn't necessary, and just complicates things. Details in individual commits.
r? @oli-obk
The direct dependency from `rustc_codegen_ssa` to `rustc_mir_transform` allowed
codegen to reuse some MIR-transform code for identifying unnecessary landing
pads.
However, we can achieve the same result by calling through a hook on `TyCtxt`,
eliminating the crate dependency.
It's a trait that is used to give `Diag::emit` different return types
depending on the `G` in `Diag<'_, G>`. It works, but it's overkill.
There are only four different `G` types in practice (`BugAbort`,
`FatalAbort`, `ErrorGuaranteed`, and `()`) and having inherent `emit`
methods for each of those four concrete `Diag<'_, G>` types is good
enough.
This commit makes that change. This removes `EmissionGuarantee`
trait bounds from many places, which is nice.
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.
Simplify diagnostic levels
This PR separates diagnostic levels from subdiagnostic levels and does a few preliminary cleanups. Details in individual commits.
r? @oli-obk