Files
rust/library/core/src
bors e7d6ce3a6f Auto merge of #114034 - Amanieu:riscv-atomicbool, r=thomcc
Optimize `AtomicBool` for target that don't support byte-sized atomics

`AtomicBool` is defined to have the same layout as `bool`, which means that we guarantee that it has a size of 1 byte. However on certain architectures such as RISC-V, LLVM will emulate byte atomics using a masked CAS loop on an aligned word.

We can take advantage of the fact that `bool` only ever has a value of 0 or 1 to replace `swap` operations with `and`/`or` operations that LLVM can lower to word-sized atomic `and`/`or` operations. This takes advantage of the fact that the incoming value to a `swap` or `compare_exchange` for `AtomicBool` is often a compile-time constant.

### Example

```rust
pub fn swap_true(atomic: &AtomicBool) -> bool {
    atomic.swap(true, Ordering::Relaxed)
}
```

### Old

```asm
	andi	a1, a0, -4
	slli	a0, a0, 3
	li	a2, 255
	sllw	a2, a2, a0
	li	a3, 1
	sllw	a3, a3, a0
	slli	a3, a3, 32
	srli	a3, a3, 32
.LBB1_1:
	lr.w	a4, (a1)
	mv	a5, a3
	xor	a5, a5, a4
	and	a5, a5, a2
	xor	a5, a5, a4
	sc.w	a5, a5, (a1)
	bnez	a5, .LBB1_1
	srlw	a0, a4, a0
	andi	a0, a0, 255
	snez	a0, a0
	ret
```

### New

```asm
	andi	a1, a0, -4
	slli	a0, a0, 3
	li	a2, 1
	sllw	a2, a2, a0
	amoor.w	a1, a2, (a1)
	srlw	a0, a1, a0
	andi	a0, a0, 255
	snez	a0, a0
	ret
```
2023-07-27 01:00:12 +00:00
..
2023-05-03 22:09:33 -07:00
2022-10-29 09:23:12 +02:00
2023-04-28 08:47:55 -07:00
2023-05-12 19:37:02 -07:00
2023-05-15 12:08:16 +02:00
2023-06-14 18:29:08 +03:00
2023-07-12 21:38:55 -04:00
2023-07-18 20:58:35 -04:00
2023-07-12 21:38:55 -04:00
2022-11-20 10:28:14 +01:00
2023-05-12 19:37:02 -07:00
2022-05-22 07:18:32 -03:00
2023-04-16 07:20:26 +00:00
2023-04-16 06:49:27 +00:00
2023-06-18 01:14:45 -07:00
2023-04-16 07:20:26 +00:00
2023-07-08 12:10:12 +09:00
2022-11-04 20:06:18 -07:00
2023-03-15 08:55:22 -04:00
2023-05-12 19:37:02 -07:00
2023-04-16 06:49:27 +00:00
2023-07-12 21:38:55 -04:00
2023-05-18 01:30:12 -04:00
2023-07-16 19:55:03 +02:00
2023-07-12 21:38:55 -04:00