Add `core::num::Complex`
tracking issue: https://github.com/rust-lang/rust/issues/154023
See also [#t-compiler > representing `_Complex`](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/representing.20.60_Complex.60/with/608308993). The RFC states that the `Complex` type should match the ABI of `_Complex` in C, but provides little detail on how to actually achieve this. We concluded that `repr(complex)` is probably the best approach. Like clang, that means we "desugar" the representation early: `_Complex` is not natively represented in LLVM IR.
I've derived the ABI from `clang` for 28 targets, a decent sample of what we actually support. For now I'm only actually testing `x86_64` because (as you'll see) updating the tests is quite labor-intensive. If we're happy with this direction I can fill more of them in, likely in a separate PR.
The libs side of the type is very bare-bones: I'm not as interested in that part. Others can fill in the various instances, methods, write tests for those, and improve the docs.
r? workingjubilee
Optimize CStr backing slice bounds checks
Hint that Cstr is non empty bcs it always contains the null byte. Byte-zero access reduces from 27 to 4 bytes
Add -Zinstrument-mcount={fentry-nop-record,fentry-record}
The linux kernel still uses fentry for x86 and s390x arches. s390x depends entirely on the compiler to record and nop these sections.
This facilitates support for inserting nop's and/or recording the location of each mcount call in a special section named `__mcount_loc`.
These attributes are currently only supported with fentry on the s390x target, otherwise they are quietly ignored (except on s390x).
Optimize slice::contains for bytewise types
generalize the SliceContains implementation from u8 to all types implementing UnsignedBytewiseOrd since these types have a one byte representation compatible with the
memchr implementation
tests: prefer max-llvm-major-version over open LLVM ranges
Per review, take option 3: replace open `ignore-llvm-version` ranges with `max-llvm-major-version`. Left gdb `X - 99.0` ranges alone so CI's older gdb still runs those tests.
Fixesrust-lang/rust#159338.
The linux kernel still uses fentry for x86 and s390x arches.
s390x depends entirely on the compiler to record and nop
these sections.
This facilitates support for inserting nop's and/or recording the
location of each mcount call in a special section named `__mcount_loc`.
These attributes are currently only supported with fentry on the s390x
target, otherwise they are quietly ignored (except on s390x).
cfi: add diag mode support
Currently a Rust CFI failure only inserts a ud2. However, for clang we have the option for a helpful diagnostic message that explains the violation and is especially helpful for fixing it. This message works through hooking the UBSan runtime and calling into it with the necessary information for a helpful error message. In clang, this is enabled via `-fno-sanitize-trap=cfi`.
This change adds the same behavior to rustc's CFI. Instead of a `no-sanitize-trap` flag, we added `-Z cfi-mode={diag|trap}`, with trap as the default. The diag mode will print the following error message for a violation:
```
tests/ui/sanitizer/cfi/fn-ptr-type-mismatch.rs:1:1: runtime error: control flow integrity check for type fn(i32, i32) -> i32 failed during indirect function call
fn_ptr_type_mismatch.ecd806f409c5c1fc-cgu.0: note: fn_ptr_type_mismatch::add_one defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tests/ui/sanitizer/cfi/fn-ptr-type-mismatch.rs:1:1
```
r? @rcvalle
CFI: Add support for the adt_const_params feature
Adds support for encoding constants of types allowed by the adt_const_params and unsized_const_params features (i.e., char, str, tuple, array, slice, struct, enum, and reference values) as literal arguments, and fixes the encoding of bool values to be 0 and 1, and of negative integer values to be their decimal values preceded by n (fixesrust-lang/rust#159299).
Currently a Rust CFI failure only inserts a ud2. However, for clang
we have the option for a helpful diagnostic message that explains the
violation and is especially helpful for fixing it. This message works
through hooking the UBSan runtime and calling into it with the
necessary information for a helpful error message. In clang, this is
enabled via -fno-sanitize-trap=cfi.
This change adds the same behavior to rustc's CFI. Instead of a
no-sanitize-trap flag, we added -Z cfi-mode={diag|trap}, with trap
as the default. The diag mode will print the following error message
for a violation:
```
tests/ui/sanitizer/cfi/fn-ptr-type-mismatch.rs:1:1: runtime error: control flow integrity check for type fn(i32, i32) -> i32 failed during indirect function call
fn_ptr_type_mismatch.ecd806f409c5c1fc-cgu.0: note: fn_ptr_type_mismatch::add_one defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tests/ui/sanitizer/cfi/fn-ptr-type-mismatch.rs:1:1
```
MIR GVN propagates MaybeUninit::uninit() as `const <uninit>` in aggregate
constructions and codegen emits a memcpy from an `[N x i8] undef` global for
each such field, which LLVM materializes as zero-initialization.
We extract the existing `all_bytes_uninit` skip already present for
`Rvalue::Use` and `Rvalue::Repeat` to a separate helper function, and use
it in the `Rvalue::Aggregate` field loop.
Fix `bool` calling convention for aarch64, etc.
We have been making a broad assumption about the way `bool` should be handled that *may* be correct for Rust calling Rust but is wildly incorrect for cross-language FFI calls. The ABI of `bool` is generally poorly-specified in any C psABI, and often deliberately allows for deviation after the first 8 bits.
This has surfaced as a bug that can corrupt Rust programs on aarch64, but it is a potential latent bug when it comes to every ABI. The simplest solution is to simply stop applying the special case when generating `bool`'s ArgAttributes. This yields control to the later ABI implementations, which may then sign or zero extend the `bool` (or not) according to their needs.
Add CFI tests for return types and never type
In a few different conversations, the CFI types of various functions has come up. First, the CFI type of returning `!` came up in https://github.com/rust-lang/rust/issues/159446#issuecomment-5011154213, where we found that it doesn't have the same CFI type as returning `()`, which it probably should.
Then in rust-lang/rust#159935, I wanted to make sure that this does not change the CFI type of functions when `*mut c_void` appears as an argument. Luckily it does not since it's a lang item, but there's no test for this case.
Thus, add tests for all of these cases and a few others.
AI assistance was involved with writing the test.
When adding `OperandValue::Uninit` to skip stores for entirely-uninit
constants, we missed the `PassMode::Direct | PassMode::Pair` branch of
`codegen_return_terminator`, which called `immediate_or_packed_pair`
unconditionally. A function directly returning an all-uninit value (e.g.
`MaybeUninit::uninit()`) would hit that branch and ICE in
`OperandRef::immediate`, as reported against tokio and reduced by tmiasko.
use `minicore::simd::Simd` more
follow-up to https://github.com/rust-lang/rust/pull/159656.
I've used `grep -rlZ "repr_simd" . | xargs -0 grep -l "use minicore"` to find all test files that used both `repr_simd` and `use minicore`, they now all use `minicore::simd` to `repr_simd` is no longer needed for them.
codegen: skip stores for entirely-uninit constant aggregate fields
MIR GVN (since rust-lang/rust#147827) propagates MaybeUninit::uninit() as `const <uninit>` in aggregate constructions. Without this fix, codegen would emit a memcpy from an `[N x i8] undef` global for each such field, which LLVM materializes as zero-initialization.
This mirrors the existing `all_bytes_uninit` skip already present for `Rvalue::Use` (added in rust-lang/rust#147827) into the `Rvalue::Aggregate` field loop.
Fixes: rust-lang/rust#157743
Apply RemoveNoopLandingPads post-monomorphization
On Cargo this cuts ~5% of the LLVM IR lines we generate (measured with -Cno-prepopulate-passes).
Closes https://github.com/rust-lang/rust/issues/159399.
Adds support for encoding constants of types allowed by the
adt_const_params and unsized_const_params features (i.e., char, str,
tuple, array, slice, struct, enum, and reference values) as literal
arguments, and fixes the encoding of bool values to be 0 and 1, and of
negative integer values to be their decimal values preceded by n.
MIR GVN propagates MaybeUninit::uninit() as `const <uninit>` in aggregate
constructions and codegen emits a memcpy from an `[N x i8] undef` global for
each such field, which LLVM materializes as zero-initialization.
Instead of special-casing `all_bytes_uninit` at each call site, add a new
`OperandValue::Uninit` variant that `eval_mir_constant_to_operand` returns for
any all-bytes-uninit constant. `store_with_flags` is a no-op for this variant,
so the fix applies uniformly across `Rvalue::Use`, `Rvalue::Repeat`, and
`Rvalue::Aggregate` without per-site checks. The variant propagates through
field extraction and transmutes, and is handled appropriately at the remaining
call sites (function arguments, discriminant reads, debug info, etc.).
This speeds up LLVM and improves codegen overall. As an example, for
cargo this cuts ~5% of the LLVM IR lines we generate (measured with
-Cno-prepopulate-passes).