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

Wildcard pattern not optimized in match expression #108395

Closed
mitiko opened this issue Feb 23, 2023 · 2 comments · Fixed by #131920
Closed

Wildcard pattern not optimized in match expression #108395

mitiko opened this issue Feb 23, 2023 · 2 comments · Fixed by #131920
Assignees
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mitiko
Copy link

mitiko commented Feb 23, 2023

The following snippet generates branches (with -C opt-level=3)

pub fn func1(a: u16, b: u16, v: u16) -> u16 {
    match (a == v, b == v) {
        (true, false) => 0,
        (false, true) => u16::MAX,
        _ => 1 << 15, // half
    }
}

whereas spelling it out to the compiler does not:

pub fn func2(a: u16, b: u16, v: u16) -> u16 {
    match (a == v, b == v) {
        (true, false) => 0,
        (false, true) => u16::MAX,
        (true, true) => 1 << 15, // half
        (false, false) => 1 << 15, // half
    }
}

I believe this breaks the zero cost abstraction promise.
In theory these do mean different things but for constant 16-bit integers on a modern 64-bit system they should be optimized away.

Other quirks:

  • only partially spelling it out to the compiler with (true, true) | (false, false) still generates branches
  • using guard clauses generates branchless code but with more instructions

Godbolt link: https://rust.godbolt.org/z/cWoboKM3d

@clubby789
Copy link
Contributor

@rustbot label +I-slow +A-llvm

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-slow Issue: Problems and improvements with respect to performance of generated code. labels Feb 23, 2023
@Noratrieb Noratrieb changed the title Glob pattern not optimized in match expression Wildcard pattern not optimized in match expression Feb 23, 2023
@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Feb 24, 2023
@veera-sivarajan
Copy link
Contributor

Looks like this is fixed now: https://rust.godbolt.org/z/G93zG8jdb

@clubby789 clubby789 added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Oct 19, 2024
@clubby789 clubby789 self-assigned this Oct 19, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 19, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 19, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 20, 2024
Rollup merge of rust-lang#131920 - clubby789:108395-test, r=jieyouxu

Add codegen test for branchy bool match

Closes rust-lang#108395
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants