And while I'm here, improve the tests to check that the unaligned ones are actually unaligned, since `unaligned_volatile_load::<u8>` doesn't actually test anything.
Rollup of 10 pull requests
Successful merges:
- rust-lang/rust#158410 (Update LLVM for Mach-O __LINKEDIT alignment fix.)
- rust-lang/rust#157397 (cmse: clear padding when crossing the secure boundary)
- rust-lang/rust#158036 (Add -Zinstrument-mcount=fentry to -Zinstrument-mcount)
- rust-lang/rust#158330 (llvm: use intrinsics for f16, f32 minimum/maximum)
- rust-lang/rust#158359 (fix(tests): allow either branch direction in ilog_known_base)
- rust-lang/rust#158067 (LLVM 23: Adapt codegen test to moved assume)
- rust-lang/rust#158261 (Move part of the target checking for `#[may_dangle]` to the parser)
- rust-lang/rust#158358 (Fix invalid E0609 raw pointer deref suggestion inside macros)
- rust-lang/rust#158392 (delegation: add tests for defaults and infers in generics)
- rust-lang/rust#158394 (Generate synthetic generic args only for delegation's child segment)
autodiff - typtree cleanups
In [some recent experiments](https://github.com/rust-lang/rust/pull/157994/changes) I managed to lower more MIR information to LLVM to help Enzyme compile a test case that was previously failing.
That PR became a little bigger, and we might want to add even more code. To get started I split it out of mod.rs, since that file is already 2.4k loc. In the process, I also fix some checks and deleted `typetree_from_ty_impl` and `typetree_from_ty_inner`, since they don't do much.
r? oli-obk
(noticed a regression in a test, marking as draft for now.)
autodiff: stop always needing an alloca
There will still be one for things that aren't just scalars or scalar pairs.
r? @ZuseZ4
cc https://github.com/rust-lang/rust/issues/153250 (the tracking issue for removing intrinsic allocas)
When emitting a `memcpy` for a scalable vector, the size computed by
rustc (`num_vectors * element_count * element_ty`), since
rust-lang/rust#157915, needs to be multiplied by `vscale`.
fentry is essentially a simpler version of mcount where the
counting function is called before any other function prologue
actions happen.
fentry is still used by the linux x86-64 kernel. It's unclear if
or when patchable-function-entry will replace it. It is also used
by clang for some x86-64 mingw toolchains.
This is only supported on some x86, x86-64, and s390x targets.
This attribute enables or disables function instrumentation
when using `-Zinstrument-xray` or `-Zinstrument-mcount`.
It supports the following usage:
`#[instrument_fn = "on|off"]`
For XRay, "on" is equivalent to always instrument, and "off" is
equivalent to never instrumenting.
For mcount, "on" has no effect. "off" disables instrumentation
of the function.
Add unstable loop unrolling hint attributes
Tracking issue: https://github.com/rust-lang/rust/issues/156874
This adds as new attribute `#[unroll]`/`#[unroll(full)]`/`#[unroll(never)]`/`#[unroll(16)]` (or any u32).
`#[unroll]` is behind a new feature gate `#![feature(loop_hints)]` because I intend to add an attribute for loop vectorization as well. If a user wants to turn off loop unrolling to locally minimize code size, LLVM may vectorize the loop even though it isn't unrolled which can produce a similar code size explosion.
debuginfo: slices are DW_TAG_array_type's
Rust debuginfo for unsized structs with embedded slice types is lacking, and it makes it difficult for debuggers to correctly print types like `Rc<str>` (eg rust-lang/rust#114195). Specifically, these three structs would have essentially the same debug info (modulo some names):
```rust
struct Foo { a: u32, s: str } // unsized
struct Bar { a: u32, s: [u8] } // unsized
struct Baz { a: u32, s: u8 }
```
This changes how the compiler generates debuginfo in this case, and I used existing compilers (gcc, clang) handling of C flexible member arrays as a reference.
Add powerpc64-unknown-linux-gnuelfv2 target
This is virtually the same target as the existing -gnu target, but using the ELFv2 ABI instead of the ELFv1 ABI and made possible now that we expose target_abi = "elfv1" or "elfv2" on the 64-bit PowerPC targets.
The ELFv2 ABI is the preferred ABI for powerpc64 when compatibility with ELFv1 is not required and therefore used by e.g. distributions that were bootstrapped after the introduction of ELFv2.
Copying the [target tier policy](https://doc.rust-lang.org/rustc/target-tier-policy.html#tier-3-target-policy):
> A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)
That would be me.
> Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.
This is an interesting one, because I'm not aware of any toolchain that has a target triple for ppc64 + glibc + ELFv2, even though it is a totally valid combination and actively in used by some distributions. Clang and GCC both simply use `-target powerpc64-unknown-linux-gnu -mabi=elfv2` to my knowledge (and cross-toolchains should be using `--with-abi=elfv2` when configuring GCC). However, the chosen name should be somewhat in line with the other existing targets with special ABIs. Renaming `powerpc64-unknown-linux-gnu` to `powerpc64-unknown-linux-gnuelfv1` at the same time to avoid ambiguity would've been an interesting idea, but it'd be a breaking change to a tier 2 target.
> Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.
This target isn't any different than the ELFv1 target in this regard.
> Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.
Nothing to comment on here on my end.
> Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.
This target implements the entire standard library.
> The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.
Documentation can be found in the markdown document added by this PR.
> Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.
Roger that :)
> Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.
This PR doesn't touch any other targets.
> Tier 3 targets must be able to produce assembly using at least one of rustc's supported backends from any host target. (Having support in a fork of the backend is not sufficient, it must be upstream.)
The LLVM backend works.
r? compiler
Add inline asm support for amdgpu
Add support for inline assembly for the amdgpu backend (the
amdgcn-amd-amdhsa target).
Add register classes for `vgpr` (vector general purpose register) and
`sgpr` (scalar general purpose register).
The LLVM backend supports two more classes, `reg`, which is either VGPR
or SGPR, up to the compiler to decide. As instructions often rely on a
register being either a VGPR or SGPR for the assembly to be valid, reg
doesn’t seem that useful (I struggled to write correct tests for it), so
I didn’t end up adding it.
The fourth register class is AGPRs, which only exist on some hardware
versions (not the consumer ones) and they have restricted ways to write
and read from them, which makes it hard to write a Rust variable into
them. They could be used inside assembly blocks, but I didn’t add them
as Rust register class.
There are a few change affecting general inline assembly code, that is
`InlineAsmReg::name()` now returns a `Cow` instead of a `&'static str`.
Because amdgpu has many registers, 256 VGPRs plus combinations of 2 or 4
VGPRs, and I didn’t want to list hundreds of static strings, the amdgpu
reg stores the register number(s) and a non-static String is generated
at runtime for the register name.
Similar for register classes and supported_types.
Vectors of 64-bit types are supported by the LLVM backend, but omitted
here to make the code simpler. There is currently no systematic support
in LLVM of which vectors of 64-bit types are supported. Also, they are
likely seldomly unused, vectors of 16- and 32-bit types are important.
Tracking issue: rust-lang/rust#135024
Set !captures metadata for store of &Freeze
When a `&Freeze` reference is stored (with retag), emit `!captures !{!"address", !"read_provenance"}` metadata to indicate that it's UB to write through the stored pointer.
Related to https://github.com/rust-lang/rust/issues/146844.
r? @ghost
Add support for inline assembly for the amdgpu backend (the
amdgcn-amd-amdhsa target).
Add register classes for `vgpr` (vector general purpose register) and
`sgpr` (scalar general purpose register).
The LLVM backend supports two more classes, `reg`, which is either VGPR
or SGPR, up to the compiler to decide. As instructions often rely on a
register being either a VGPR or SGPR for the assembly to be valid, reg
doesn’t seem that useful (I struggled to write correct tests for it), so
I didn’t end up adding it.
The fourth register class is AGPRs, which only exist on some hardware
versions (not the consumer ones) and they have restricted ways to write
and read from them, which makes it hard to write a Rust variable into
them. They could be used inside assembly blocks, but I didn’t add them
as Rust register class.
There are a few change affecting general inline assembly code, that is
`InlineAsmReg::name()` now returns a `Cow` instead of a `&'static str`.
Because amdgpu has many registers, 256 VGPRs plus combinations of 2 or 4
VGPRs, and I didn’t want to list hundreds of static strings, the amdgpu
reg stores the register number(s) and a non-static String is generated
at runtime for the register name.
Similar for register classes and supported_types.
Vectors of 64-bit types are supported by the LLVM backend, but omitted
here to make the code simpler. There is currently no systematic support
in LLVM of which vectors of 64-bit types are supported. Also, they are
likely seldomly unused, vectors of 16- and 32-bit types are important.
When a `&Freeze` reference is stored (with retag), emit
`!captures !{!"address", !"read_provenance"}` metadata to indicate
that it's UB to write through the stored pointer.
asm! support for the Xtensa architecture
This implements the asm! support for Xtensa. We've been using this code for a few years in [our fork](https://github.com/esp-rs/rust) and it's been working well. I finally found some time to clean it up a bit and start the upstreaming process. This should be one of the final PRs for Xtensa support on the Rust side (minus bug fixes of course). After this, we're mostly just waiting on the LLVM upstreaming which is going well. This PR doesn't cover all possible asm options for Xtensa, but the base ISA plus a few extras that are used in Espressif chips.
r? Amanieu
This is an UnordMap internally. Iteration order for the work product map
should not matter aside from the place it is serialized where sorting by
WorkProductId is sufficient.
add `extern "tail"` calling convention
Maps to LLVM's `tailcc` and hence inherits all its problems.
This calling convention at least compiles on:
- `x86_64`
- `aarch64`
It also compiles on
- `s390x`
- `loongarch64`
But I've been told by target maintainers that this is kind of unintentional, and it's not tested in LLVM.
For most backends LLVM does not have support and fails loudly.
This is virtually the same target as the existing -gnu target, but using
the ELFv2 ABI instead of the ELFv1 ABI and made possible now that we
expose target_abi = "elfv1" or "elfv2" on the 64-bit PowerPC targets.
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
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.
Split coroutine layout computation to its own file
`coroutine.rs` is getting too large, and mixes MIR analyses for trait solving and runtime transformations.
Remove -Zemscripten-wasm-eh
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.
This is the final step of https://github.com/rust-lang/rust/issues/112195.
Use weak linkage for EII defaults
hard to find a test for this, but weak seems more correct (after private discussion with @bjorn3)
Also changes a log statement to make it a lil easier to see what's going on.
r? @bjorn3
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.
Use a `ArrayVec` in `CastTarget`
This commit switches a fixed-size list of `[Option<Reg>; 8]` to instead
holding `ArrayVec<Reg, 8>` in the `CastTarget` type used when
calculating ABIs. This is inspired by [discussion on Zulip][link] where
I'm hoping to in the near future extend the usage of this to possibly
beyond 8 elements for a new WebAssembly ABI taking advantage of
multi-value. For now though this mostly just switches to
array/slice-like idioms of accessors rather than dealing with
`Option<Reg>` as the unit.
[link]: https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Using.20.60ArgAbi.3A.3Amake_direct_deprecated.60/with/598607139
This commit switches a fixed-size list of `[Option<Reg>; 8]` to instead
holding `ArrayVec<Reg, 8>` in the `CastTarget` type used when
calculating ABIs. This is inspired by [discussion on Zulip][link] where
I'm hoping to in the near future extend the usage of this to possibly
beyond 8 elements for a new WebAssembly ABI taking advantage of
multi-value. For now though this mostly just switches to
array/slice-like idioms of accessors rather than dealing with
`Option<Reg>` as the unit.
[link]: https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Using.20.60ArgAbi.3A.3Amake_direct_deprecated.60/with/598607139
compiler: `ops::RangeInclusive` → `range::RangeInclusive`
The type's been stable for over 6 weeks now, so let's use it! It's better for cases like this one where it's stored in a data structure.
Probably won't be materially faster, but does make the variant slightly smaller and lets some more things be `Copy`. And deletes a whole bunch of parens from things being fields instead of methods 😄
r? compiler
The type's been stable for over 6 weeks now, so let's use it! It's better for cases like this one where it's stored in a data structure.
Probably won't be materially faster, but does make the variant slightly smaller and lets some more things be `Copy`.
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!)