Use `mul nuw nsw` in `intrinsics::copy`
Essentially the same as rust-lang/rust#157560, just for `copy` instead of `copy_nonoverlapping`.
> Yeah, in fact both copy and copy_nonoverlapping could use this since we know the result must be at most isize::MAX else it cannot be inbounds.
> ~ https://github.com/rust-lang/rust/pull/157560#issuecomment-4642072687
r? saethlin
In `copy_nonoverlapping`, use `mul nuw nsw` to compute the byte size
Seems like we might as well? Adding these flags means the optimizer can tell the limited range on the count of items -- like how we use these flags (rust-lang/rust#136575) when calculating `size_of_val` for a slice.
Today we use a wrapping multiplication, which mean that `copy_nonoverlapping::<u32>(src, dst, 0x40000000_00000001)` appears like 4 bytes -- a perfectly reasonable size! -- once it gets to the `memcpy` call.
If I'm understanding <https://doc.rust-lang.org/std/ptr/fn.copy_nonoverlapping.html#safety> properly, this is just exploiting existing UB, since `src` and `dst` must each be inside an allocation, and those allocations can be at most `isize::MAX` bytes. (Plus, fundamentally, to be non-overlapping there's not enough space in the address space to be bigger than `isize::MAX`.)
cc @RalfJung to make sure this is ok, as requested last he found out I was newly exploiting some UB in codegen 🙃
r? codegen
Further optimize `SliceIndex<str>` impl for `Range<usize>`
We can shave a further 2 `icmp`s by inlining `is_char_boundary` and rearranging the checks.
Follow up to https://github.com/rust-lang/rust/pull/145024
Rollup of 8 pull requests
Successful merges:
- rust-lang/rust#156281 (Emit nofree attribute)
- rust-lang/rust#157305 (Eagerly decide whether relaxed bounds are allowed or not)
- rust-lang/rust#148713 (rustc_borrowck: fix async closure error note to report AsyncFn rather than Fn)
- rust-lang/rust#156266 (Don't ICE in has_self_borrows when coroutine captures-by-ref ty is still inferred)
- rust-lang/rust#156417 (Fix an ICE in the vtable iteration for a trait reference in const eval when a supertrait is not implemented)
- rust-lang/rust#156956 (Support generic params in `Lift_Generic`)
- rust-lang/rust#157140 ( rustc_target: Use rustc_abi instead of cfg_abi to detect powerpcspe )
- rust-lang/rust#157423 (Refactor/expand rustc_attr_parsing docs)
Emit nofree attribute
Treat the semantics of pointee.size as "dereferenceable-at-point" and always specify the size. Instead, use a separate NoFree attribute to determine whether dereferenceability extends to the whole function.
Then in the LLVM backend, only actually emit dereferenceable if nofree is also set, as dereferenceable currently implies nofree.
In addition, explicitly emit the nofree attribute, which will help when LLVM switches dereferenceable to be at-point.
Relevant recent LLVM PR: https://github.com/llvm/llvm-project/pull/195658
tests: codegen-llvm: Update bpf-alu32 with the new LLVM attributes
The LLVM backend now emits `noundef zeroext` on `i8` return values and `noundef` on `i8` parameters. Update the FileCheck pattern to match.
r? @nagisa
Treat the semantics of pointee.size as "dereferenceable-at-point"
and always specify the size. Instead, use a separate NoFree attribute
to determine whether dereferenceability extends to the whole function.
Then in the LLVM backend, only actually emit dereferenceable if
nofree is also set, as dereferenceable currently implies nofree.
In addition, explicitly emit the nofree attribute, which will help
when LLVM switches dereferenceable to be at-point.
The LLVM backend now emits `noundef zeroext` on `i8` return values and
`noundef` on `i8` parameters. Update the FileCheck pattern to match
those, and any possible future attributes.
This was necessary when transitioning from JS to wasm exception handling
on Emscripten. Enough time has probably passed that we no longer need to
support JS exception handling on Emscripten. This enables cleaning up a
fair bit of code.
LLVM 23: Run AssignGUIDPass in some places
https://github.com/llvm/llvm-project/pull/184065 introduced the new pass.
I'm not 100% confident that this is the correct patch, but it makes all the previously failing `run-make` "lto" tests pass on my machine.
I added the pass to the `if (NeedThinLTOBufferPasses)` section to match the `addRequiredLTOPreLinkPasses` changes from the LLVM PR, and to the `LLVMRustModuleSerialize` section based on the stack trace of an LLVM assertion.
The IR may now contain an additional `!guid` attribute, requiring some codegen test expectations to be updated. I mostly followed the pattern from the LLVM PR, and removed trailing `{`s. (For both, tests that were failing locally, as well as potentially problematic expectations identified via grep, in case the test wasn't being run locally).
@rustbot label llvm-main
Make hint::cold_path #[cold] so that it works even if the MIR inliner can't inline it
This fixes https://github.com/rust-lang/rust/issues/156859 because the branch weight metadata is actually keyed off seeing a `#[cold]` function call in an arm. We don't need the intrinsic, any `#[cold]` function will do. So if the hint wrapper itself is made `#[cold]`, we will still get the desired effect, and LLVM will clean up the call to an empty function when optimizations are enabled.
std::offload sharedmem
@Sa4dUs @Kevinsala This simply allows requesting shared-memory for gpu kernels at launch time via std::offload. The actual work to access the shared memory inside the kernel has already landed independent of the offload work in rustc, so we're building on that.
cc @Flakebi (thanks for your work!)
Stop needing an alloca for `catch_unwind`
Turns out these were all making `OperandValue::Immediate`s already -- the intrinsic always returns a primitive scalar -- so pretty easy to handle.
cc https://github.com/rust-lang/rust/issues/153250https://github.com/rust-lang/compiler-team/issues/970
While I was looking at it, I also "rustified" the intrinsic signature a bit: returning a `bool` and taking a generic pointee and `unsafe fn`s cleans up the call in `std` a bit without making the implementation in the backend any harder.
r? dianqk
Turns out these were all making `OperandValue::Immediate`s already -- the intrinsic always returns a primitive scalar -- so pretty easy to handle.
While I was looking at it, I also "rustified" the intrinsic signature a bit: returning a `bool` and taking a generic pointee and `unsafe fn`s cleans up the call in `std` a bit without making the implementation in the backend any harder.
Add uwtable annotation to modules when required
When unwind tables are enabled with `-Cforce-unwind-tables=y`, Rust will annotate all functions with the `uwtable` annotation. However, this annotation is missing on modules, which leads to incorrect unwind tables being generated by LLVM for constructors (such as `asan.module_ctor`).
This was discovered because it leads to a crash in Linux when KASAN and dynamic shadow call stack are both enabled. In this scenario, the kernel uses the unwind tables to locate the `paciasp` and `autiasp` instructions in each function and patches the machine code at boot to use the shadow call stack instructions instead. However, LLVM's AArch64PointerAuth pass emits DWARF info for `paciasp` whenever `-g` is passed, but only emits DWARF info for `autiasp` when the `uwtable` attribute is present. Since the `uwtable` annotation is missing for modules, the relevant directives are generated for only the `autiasp` instruction in `asan.module_ctor`, and not for the `paciasp` instruction. This causes the kernel's dynamic SCS logic to patch the prolouge of `asan.module_ctor`, but not the epilogue. This leads to a crash as the shadow call stack becomes unbalanced.
The fact that LLVM doesn't use the same condition for whether to emit DWARF information for both instructions may be a separate bug in LLVM.
Relevant issue: https://github.com/llvm/llvm-project/issues/188234
AI assistance was used to determine the root cause of this crash from the observed symptoms, and to write the tests. Also thanks to @samitolvanen and @maurer for debugging this issue.
Similar to this previous PR of mine: rust-lang/rust#130824
codegen: re-enable FileCheck for scalable-vector tuple intrinsics
The FileCheck directives in `tests/codegen-llvm/scalable-vectors/tuple-intrinsics.rs` were not actually being checked: the test used `//@ build-pass`, and rust-lang/rust#155630 made that explicit by replacing it with `//@ skip-filecheck`.
This fixes the stale checks and re-enables FileCheck for the test. The `get` and `set` cases now use concrete index-0 wrappers, so they have stable `#[no_mangle]` names for FileCheck to match.
Fixesrust-lang/rust#155665.
r? @jieyouxu
The `needs-asm-mnemonic` directive was very general, but in practice was only
being used for `ret`. There are very few other mnemonics that it could
plausibly be useful for (e.g. `nop`), because any instruction that requires
arguments is probably going to be non-portable.
This PR replaces `needs-asm-mnemonic` with a simpler `needs-asm-ret` directive
that uses the same machinery as other simple needs directives.
If we happend to need more mnemonics in the future, we can just add more simple
directives as appropriate (e.g. `needs-asm-nop`).
Re-add call site inlining attributes
It is very odd that we are blindly setting the call site attributes to the same as the definition, because I think the point of the call site attributes is to differentiate between calls. Probably worth looking into later, but for now I think we should just be undoing the change we made by mistake.
This fixes the accidental regression from https://github.com/rust-lang/rust/pull/156242
Remove unsound `target_feature_inline_always` feature
## Summary
- Remove `target_feature_inline_always`
- Update stdarch generators to only use `#[inline]` & regenerate stdarch.
## Why?
Succinctly; the feature relies on LLVMs `AlwaysInlinerPass()` running before LLVMs heuristic based inliner pass. Which is not a basis for sound code.
This has been discussed in [the tracking issue](https://github.com/rust-lang/rust/issues/145574).
If the ordering of the passes were to change, of which they have in the past, it is very possible we could inline functions across callsites with mismatching target features leading to unsound code. Checks proposed in; https://github.com/rust-lang/rust/pull/155426 would only take into account caller -> callee which is not enough to guard against possibly of generating unsound code if the pass ordering were to change.
There doesn't seem to be a way, presently, this this mechanism to provide soundness guarantees nor does it seem like `AlwaysInlinerPass()` is a desired feature of LLVM, which this feature relies on.
r? @RalfJung
LLVM 23: Specify `returnaddress` intrinsic return type
https://github.com/llvm/llvm-project/pull/188464 made the return type of the intrinsic generic to support different pointer address spaces.
@rustbot label llvm-main
Have arrays' `drop_glue` just unsize and call the slice version
It's silly to emit two loops (because of the drop ladder -- just one in panic=abort) for every array length that's dropped when we can just polymorphize to the slice version.
Built atop rust-lang/rust#154327 to avoid conflicts later, so draft for now.
r? @WaffleLapkin