Set target-abi module flag for LoongArch targets
Like the earlier RISC-V fix (rust-lang/rust#123612), emit the "target-abi" module flag for LoongArch so that LTO correctly picks up the ABI.
This follows the recent LLVM change (llvm/llvm-project#223647) that relies on the module flag for proper TargetMachine / data-layout handling during LTO on LoongArch.
`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)
Add missing `#[repr(C)]` in UI, codegen and assembly tests
There are quite a few tests which omit the `#[repr(C)]` on structs that are then used in non-Rustic ABIs. I've gone through the tests in `ui/abi`, `ui/ffi`, `codegen-llvm` and `assembly-llvm` and added `#[repr(C)]` where it was missing. I also converted `#[allow(improper_ctypes{,_definitions})]` to `#[expect(improper_ctypes{,_definitions})]` where possible, and removed it where the lint no longer triggers.
I haven't done it in this PR, but I think it's worth making codegen and assembly tests deny warnings by default. With UI tests `improper_ctypes{,_definitions}` has to be either allowed or will show up in the output, meaning it should be fairly obvious to reviewers, but with codegen and assembly tests warnings just get ignored if the test passes.
Fixesrust-lang/rust#53858
r? @folkertdev
cc @RalfJung
Remove applying inline attributes at the callsite
Following from our discussion and the currently open PR; https://github.com/rust-lang/rust/pull/162460, removing the blanket addition of inline attributes at the callsite, even without the `#[inline(always)]` changes, looks to now have negligible impact.
sparc: make ABI consistent with clang
fixes https://github.com/rust-lang/rust/issues/43894
I've hacked together a version of abi-cafe that can cross-compile. It finds many GCC vs. Clang inconsistencies, but for this target it found 3 ABI bugs that are specific to the rust implementation.
For reference
- [`SparcV8ABIInfo::{classifyArgumentType, classifyReturnType}`](6849789a8f/clang/lib/CodeGen/Targets/Sparc.cpp (L34-L59))
- [`DefaultABIInfo::{classifyArgumentType, classifyReturnType}`](6849789a8f/clang/lib/CodeGen/ABIInfoImpl.cpp (L17-L68))
## Pass ZST arguments
https://godbolt.org/z/P7KoqTWvr
The `sparc` ABI does not skip zero-sized arguments.
## Pass and return `f128` (i.e. `long double`) indirectly
The `long double` type, which corresponds to rust `f128`, is special-cased, it uses `getNaturalAlignIndirect` for both arguments and returns.
## pass aggregate arguments by reference
https://godbolt.org/z/hM8Gh1bfY
The implementation defers to `DefaultABIInfo::{classifyReturnType, classifyArgumentType}`, which use `byval` and `sret` for aggregate types.
fix `is_single_fp_element` for `s390x` and `x86`
In https://github.com/rust-lang/rust/pull/161950#discussion_r3886952310 we discovered that the old `is_single_fp_element` is incorrect in a number of ways.
- it did not consider `f16` or `f128`
- it did not consider transparent wrappers
- on x86, it incorrectly accepted over-aligned types
- on s390x, it incorrectly accepted single-element unions and arrays
So, in practice each target does something slightly different here, and I've split the function into two.
cc @beetrees
wasm: fix ABI for enums with integer layout and ZST fields
fixes https://github.com/rust-lang/rust/issues/121408
follow-up to https://github.com/rust-lang/rust/pull/162806
Clang is doing something that is rather annoying for us to emulate: like with unions it looks at the structure of the type, not the representation. But in rust an `enum` that is represented as an integer is not considered an `aggregate` at all, yet may still need to be passed as one if it has any (even ZST) fields.
r? alexcrichton
cc @hanna-kruppe
implement const Iterator for Range
This allows the use of `for i in i..n` in `const`.
This I believe is interesting enough to be justified under https://github.com/rust-lang/rust/issues/155816
r? @oli-obk
wasm: correctly implement `union` trivial aggregates
A union with two scalar fields, even if they overlap exactly, is not a trivial aggregate, based on the implementation in `clang`:
e78970f80f/clang/lib/CodeGen/ABIInfoImpl.cpp (L338-L402)
It never explicitly considers unions, but essentially views them as records. The `clang` implementation is a plausible interpretation of the specification (though I'd like it to be a bit clearer):
83e5d715d0/BasicCABI.md (function-arguments-and-return-values)
> [2] Any struct or union that recursively (including through nested structs, unions, and arrays) contains just a single scalar value and is not specified to have greater than natural alignment.
We previously got this wrong. I also don't think this was really tested very well, so I added a bunch of test for various cases.
This change moves away from using the `homogeneous_aggregate` method, which is convenient because it is a bit broken and now we don't have to consider wasm when we fix that.
I've validated this against `clang` with abi-cafe.
r? alexcrichton
make `Complex` ABI-compatible on sparc64 and powerpc64
tracking issue: https://github.com/rust-lang/rust/issues/154023
Make the ABI for `Complex` match C for two slightly more involved targets.
For Sparc64 the implementation matches GCC and Clang 24 and up. In https://github.com/llvm/llvm-project/pull/215015 a bug with how small complex numbers are passed was fixed.
For powerpc64 we match Clang exactly (and Clang is compatible with GCC for this target).
cc @beetrees @Gelbpunkt
sanitizers: Implement support for the sanitize ignorelist
The sanitize ignorelist gives central controls over which functions, files, etc. should be ignored and not sanitized. It is a common file format for clang and explained here:
https://clang.llvm.org/docs/SanitizerSpecialCaseList.html. This change adds support for this list in Rust as well.
r? @rcvalle
Pass -Z merge-functions=disabled in tests/codegen-llvm/intrinsics/unchecked_math.rs
Otherwise e.g. `@unchecked_add_unsigned` and `@unchecked_add_signed` get merged after https://github.com/llvm/llvm-project/pull/220015, breaking the test's expectations.
The sanitize ignorelist gives central controls over which functions,
files, etc. should be ignored and not sanitized. It is a common file
format for clang and explained here:
https://clang.llvm.org/docs/SanitizerSpecialCaseList.html. This change
adds support for this list in Rust as well.
Test itanium mangling of `f16` and `f128`
And fix the string that is used for `f128` on powerpc targets, where the standard `"g"` is already taken by `__ibm128`.
In clang
bd5b1f58ae/clang/lib/Basic/Targets/PPC.h (L362-L363)
```c
const char *getFloat128Mangling() const override { return "u9__ieee128"; }
const char *getIbm128Mangling() const override { return "g"; }
```
and GCC
fc54ab94ad/gcc/config/rs6000/rs6000.cc (L20795-L20801)
```c
if (SCALAR_FLOAT_TYPE_P (type) && FLOAT128_IBM_P (TYPE_MODE (type)))
return "g";
if (SCALAR_FLOAT_TYPE_P (type) && FLOAT128_IEEE_P (TYPE_MODE (type)))
return "u9__ieee128";
```
From what i can tell this is not really testable for powerpc at the moment because CFI is not supported on powerpc.
r? tgross35
volatile: allow accesses to non-AM memory to trap
Fixes https://github.com/rust-lang/unsafe-code-guidelines/issues/610 by saying that yes, volatile accesses may trap. This also fixes what I believe to be the last case of "time-traveling UB" we have in Rust: with this change, UB always fully sequenced wrt observable events (I/O and volatile accesses). UB can still travel around unobservable events such as non-volatile memory accesses.
For writes, LLVM has already implemented the new semantics for a long time. For reads, LLVM only recently implemented the "may trap" semantics with LLVM 23. We still allow compiling rustc with older versions of LLVM. To (hopefully) prevent old LLVM from screwing this up with optimizations, this PR makes the backend emit an inline asm block *without* `willreturn`, which should prevent optimizations that break programs where a volatile load traps.
Cc @rust-lang/opsem @rust-lang/wg-llvm
Rework `next_power_of_two` to always be `1 << …`
r? @clarfonthey
Who accidentally nerd-sniped me by mentioning rust-lang/rust#161069
This obviates that PR by reworking the `(checked_)next_power_of_two` logic to calculate the necessary exponent e, then return 2ᵉ, so it's structurally obvious from the IR -- the `shl nuw 1, …` instruction -- that it's always a power of two.
As always, the reason this is tricky is because shifts don't work for `<< Self::BITS`. The previous code handled that by checking `self <= 1` first, and thus doing a `-1 >> n` where `n < BITS`. This code instead flips the check: it looks up-front for a value that will wrap (an input above `1 << (BITS - 1)`) and thus by excluding those cases the calculated `1 << n` always has `n < BITS`. And the codegen tests show that, without needing any `llvm.assume`s, LLVM can take advantage of it to do things like rewriting `%` to masking. Plus the overflow check optimizes out for constrained inputs like slice lengths.