1
Fork 0
rust/compiler/rustc_codegen_llvm/src
Michael Goulet 828a3a41b3
Rollup merge of #137417 - taiki-e:riscv-atomic, r=Amanieu
rustc_target: Add more RISC-V atomic-related features

This is a continuation of https://github.com/rust-lang/rust/pull/130877 and adds a few target features, including `zacas`, which was experimental in LLVM 19 and marked non-experimental in LLVM 20.

This adds the following target features to unstable riscv_target_feature:

- `za64rs` (Za64rs Extension 1.0): Reservation Set Size of at Most 64 Bytes
  ([definition in LLVM](https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0-rc2/llvm/lib/Target/RISCV/RISCVFeatures.td#L227-L228), [available since LLVM 18](8649328060))
- `za128rs` (Za128rs Extension 1.0): Reservation Set Size of at Most 128 Bytes
  ([definition in LLVM](https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0-rc2/llvm/lib/Target/RISCV/RISCVFeatures.td#L230-L231), [available since LLVM 18](8649328060))
  - IIUC, `za*rs` can be referenced when implementing helpers to reduce contention in synchronization primitives, like [`crossbeam_utils::CachePadded`](https://docs.rs/crossbeam-utils/latest/crossbeam_utils/struct.CachePadded.html). (relevant discussion: https://github.com/riscv/riscv-profiles/issues/79)
- `zacas` (Zacas Extension 1.0): Atomic Compare-And-Swap Instructions (`amocas.{w,d,q}{,.aq,.rl,.aqrl}` and `amocas.{b,h}{,.aq,.rl,.aqrl}` when `zabha` is also enabled)
  ([definition in LLVM](https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0-rc2/llvm/lib/Target/RISCV/RISCVFeatures.td#L240-L243), [available as non-experimental since LLVM 20](614aeda93b))
  - This implies `zaamo`.
  - This is used to optimize CAS in existing atomics and/or implement 64-bit/128-bit atomics on riscv32/riscv64 (e.g., https://github.com/taiki-e/portable-atomic/pull/173).
  - Note that [LLVM does not automatically use this instruction for 64-bit/128-bit atomics on riscv32/riscv64 even if this feature is enabled, because doing it changes the ABI](876174ffd7/llvm/docs/RISCVUsage.rst (riscv-zacas-note)). (If the ability to do that is provided by LLVM in the future, it should probably be controlled by another ABI feature similar to `forced-atomics`.)
- `zama16b` (Zama16b Extension 1.0): Atomic 16-byte misaligned loads, stores and AMOs
  ([definition in LLVM](https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0-rc2/llvm/lib/Target/RISCV/RISCVFeatures.td#L255-L256), [available since LLVM 19](b090569685))
  - IIUC, unlike AArch64 FEAT_LSE2 which also makes 16-byte aligned ldp ({i,u}128 load) atomic, this extension only affects instructions that already considered atomic if they were naturally aligned. i.e., fld (f64 load) on riscv32 would not be atomic with or without this extension ([relevant QEMU code](b69801dd6b/target/riscv/insn_trans/trans_rvd.c.inc (L50-L62))).
- `zawrs` (Zawrs Extension 1.0): Wait on Reservation Set (`wrs.nto` and `wrs.sto`)
  ([definition in LLVM](https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0-rc2/llvm/lib/Target/RISCV/RISCVFeatures.td#L258), [available as non-experimental since LLVM 17](d41a73aa94))
  - This is used to optimize synchronization primitives (e.g., Linux uses this for spinlocks (b8ddb0df30)).

Btw, the question of whether `zaamo` is implied by `zabha` or not, which was discussed in https://github.com/rust-lang/rust/pull/130877, has been resolved in LLVM 20, since LLVM now treats `zaamo` as implied by `zabha`/`zacas` (https://github.com/llvm/llvm-project/pull/115694), just like GCC and rustc.

r? `@Amanieu`

`@rustbot` label +O-riscv +A-target-feature
2025-02-24 19:21:47 -05:00
..
back update autodiff flags 2025-02-21 21:51:20 -05:00
builder update autodiff flags 2025-02-21 21:51:20 -05:00
coverageinfo coverage: Remove the old code for simplifying counters after MIR opts 2025-02-06 21:44:31 +11:00
debuginfo Rollup merge of #137210 - workingjubilee:fixup-passmode-import, r=RalfJung 2025-02-19 01:30:12 +01:00
llvm Auto merge of #137271 - nikic:gep-nuw-2, r=scottmcm 2025-02-24 03:06:16 +00:00
abi.rs compiler: Stop reexporting stuff in cg_llvm::abi 2025-02-18 00:31:29 -08:00
allocator.rs Document some safety constraints and use more safe wrappers 2025-02-11 09:47:13 +00:00
asm.rs Mark condition/carry bit as clobbered in C-SKY inline assembly 2025-01-29 06:46:05 +09:00
attributes.rs Rename OptimizeAttr::None to Default 2025-01-24 19:34:01 +00:00
base.rs Remove Linkage::Appending 2025-02-07 16:02:19 +00:00
builder.rs Auto merge of #137271 - nikic:gep-nuw-2, r=scottmcm 2025-02-24 03:06:16 +00:00
callee.rs Merge two operations that were always performed together 2025-02-20 11:24:00 +00:00
common.rs Move hashes from rustc_data_structure to rustc_hashes so they can be shared with rust-analyzer 2025-02-16 16:18:30 -05:00
consts.rs Merge two operations that were always performed together 2025-02-20 11:24:00 +00:00
context.rs Rollup merge of #136545 - durin42:nvptx64-align, r=nikic 2025-02-16 00:51:24 -05:00
declare.rs compiler: Stop reexporting stuff in cg_llvm::abi 2025-02-18 00:31:29 -08:00
errors.rs update autodiff flags 2025-02-21 21:51:20 -05:00
intrinsic.rs Rollup merge of #136543 - RalfJung:round-ties-even, r=tgross35 2025-02-23 14:30:25 -05:00
lib.rs Rollup merge of #136858 - safinaskar:parallel-cleanup-2025-02-11-07-54, r=SparrowLii 2025-02-13 03:53:31 -05:00
llvm_util.rs rustc_target: Add more RISC-V atomic-related features 2025-02-22 16:15:14 +09:00
mono_item.rs Merge two operations that were always performed together 2025-02-20 11:24:00 +00:00
type_.rs Rollup merge of #136721 - dpaoliello:cleanllvm2, r=Zalathar 2025-02-11 01:02:40 -05:00
type_of.rs Remove BackendRepr::Uninhabited, replaced with an uninhabited: bool field in LayoutData. 2025-02-20 13:27:32 -06:00
va_arg.rs Teach rust core about Xtensa VaListImpl and add a custom lowering of vaarg for xtensa. 2024-12-03 10:54:08 +00:00
value.rs Add warn(unreachable_pub) to rustc_codegen_llvm. 2024-08-16 08:46:57 +10:00