-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Open
Enhancement
Copy link
Labels
A-mir-optArea: MIR optimizationsArea: MIR optimizationsC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Today, this bit of code: https://rust.godbolt.org/z/EfE68Meax
pub fn saturating_sub_at_home(lhs: u32, rhs: u32) -> u32 {
u32::checked_sub(lhs, rhs).unwrap_or(0)
}
Doesn't MIR-opt as well as it ought, because the Option
isn't completely optimized out:
bb2: {
StorageLive(_5);
_5 = SubUnchecked(copy _1, copy _2);
_3 = Option::<u32>::Some(move _5);
StorageDead(_5);
StorageDead(_4);
StorageLive(_6);
_6 = move ((_3 as Some).0: u32);
_0 = move _6;
StorageDead(_6);
goto -> bb3;
}
Even though it's clearly unnecessary to build-then-unwrap the option here.
This isn't "just" a GVN bug, because when GVN sees it it's not all together in one BB like this, and IIRC the Option local is also not SSA at the time, so it's reasonable that GVN doesn't currently address it. Yay phase ordering :/
Once #138545 lands, there will be tests in the repo further demonstrating this, at https://github.com/search?q=repo%3Arust-lang%2Frust%20138544%20%20path%3Atests%2Fmir-opt&type=code
Metadata
Metadata
Assignees
Labels
A-mir-optArea: MIR optimizationsArea: MIR optimizationsC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.