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

improve cold_path() #133852

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

improve cold_path() #133852

wants to merge 2 commits into from

Conversation

x17jiri
Copy link
Contributor

@x17jiri x17jiri commented Dec 4, 2024

#120370 added a new instrinsic cold_path() and used it to fix likely and unlikely

However, in order to limit scope, the information about cold code paths is only used in 2-target switch instructions. This is sufficient for likely and unlikely, but limits usefulness of cold_path for idiomatic rust. For example, code like this:

if let Some(x) = y { ... }

may generate 3-target switch:

switch y.discriminator:
0 => true branch
1 = > false branch
_ => unreachable

and therefore marking a branch as cold will have no effect.

This PR improves cold_path() to work with arbitrary switch instructions.

Note that for 2-target switches, we can use llvm.expect, but for multiple targets we need to manually emit branch weights. I checked Clang and it also emits weights in this situation. The Clang's weight calculation is more complex that this PR, which I believe is mainly because switch in C/C++ can have multiple cases going to the same target.

@rustbot
Copy link
Collaborator

rustbot commented Dec 4, 2024

r? @estebank

rustbot has assigned @estebank.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 4, 2024
@x17jiri
Copy link
Contributor Author

x17jiri commented Dec 20, 2024

Just found that this also works:

	let new_buf = Global.allocate(layout).map_err(|_| {
		cold_path();
		Error::new_alloc_failed("Cannot allocate memory.")
	})?;

As long as the closure is inlined, branch weights will be emited.

@estebank
Copy link
Contributor

I'm not the best person to review this, sorry

r? compiler

@rustbot rustbot assigned petrochenkov and unassigned estebank Dec 31, 2024
@bors
Copy link
Contributor

bors commented Jan 2, 2025

☔ The latest upstream changes (presumably #130060) made this pull request unmergeable. Please resolve the merge conflicts.

@petrochenkov
Copy link
Contributor

r? compiler

@rustbot rustbot assigned lcnr and unassigned petrochenkov Jan 2, 2025
@lcnr
Copy link
Contributor

lcnr commented Jan 6, 2025

maybe r? @nikic 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler 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