Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize AtomicBool for target that don't support byte-sized atomics #114034

Merged
merged 1 commit into from
Jul 27, 2023

Conversation

Amanieu
Copy link
Member

@Amanieu Amanieu commented Jul 24, 2023

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

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

Old

	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

	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

@rustbot
Copy link
Collaborator

rustbot commented Jul 24, 2023

r? @cuviper

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 24, 2023
@thomcc
Copy link
Member

thomcc commented Jul 24, 2023

This looks fine to me, you can r=me when you're happy with the set of platforms.

@Amanieu
Copy link
Member Author

Amanieu commented Jul 25, 2023

As far as I can tell LoongArch and RISC-V are the main ones that people care about. There might be other but I will leave those to follow-up PRs.

@bors r=thomcc

@bors
Copy link
Contributor

bors commented Jul 25, 2023

📌 Commit 9e5077dce91db0f4761aba04567b42fa6884f789 has been approved by thomcc

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 25, 2023
@bors
Copy link
Contributor

bors commented Jul 25, 2023

⌛ Testing commit 9e5077dce91db0f4761aba04567b42fa6884f789 with merge 8bd1c3eef862bd2ecdc2774529142004de3febf5...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Jul 25, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 25, 2023
@Amanieu Amanieu force-pushed the riscv-atomicbool branch from 9e5077d to 3282132 Compare July 25, 2023 23:09
@Amanieu
Copy link
Member Author

Amanieu commented Jul 25, 2023

@bors r=thomcc

@bors
Copy link
Contributor

bors commented Jul 25, 2023

📌 Commit 3282132c7c428b01821d3d7454c5e015deb7a860 has been approved by thomcc

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 25, 2023
`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.
@Amanieu Amanieu force-pushed the riscv-atomicbool branch from 3282132 to 3dbee5b Compare July 25, 2023 23:09
@Amanieu
Copy link
Member Author

Amanieu commented Jul 25, 2023

@bors r=thomcc

@bors
Copy link
Contributor

bors commented Jul 25, 2023

📌 Commit 3dbee5b has been approved by thomcc

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jul 27, 2023

⌛ Testing commit 3dbee5b with merge e7d6ce3...

@bors
Copy link
Contributor

bors commented Jul 27, 2023

☀️ Test successful - checks-actions
Approved by: thomcc
Pushing e7d6ce3 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jul 27, 2023
@bors bors merged commit e7d6ce3 into rust-lang:master Jul 27, 2023
@rustbot rustbot added this to the 1.73.0 milestone Jul 27, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (e7d6ce3): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.5% [-0.8%, -0.3%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.5% [-0.8%, -0.3%] 2

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.0% [0.0%, 0.0%] 4
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [0.0%, 0.0%] 4

Bootstrap: 652.686s -> 656.285s (0.55%)

SixWeining pushed a commit to llvm/llvm-project that referenced this pull request Sep 26, 2023
Similar to D156801 for RISCV.

Link: rust-lang/rust#114034
Link: #64090

Reviewed By: SixWeining, xen0n

Differential Revision: https://reviews.llvm.org/D159252
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants