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

[mir-opt] Simplify SwitchInts of explictly written ranges to range tests #67488

Open
Centril opened this issue Dec 21, 2019 · 1 comment
Open
Labels
A-mir-opt Area: MIR optimizations C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

@Centril
Copy link
Contributor

Centril commented Dec 21, 2019

Consider the program:

fn program(x: u8) -> u8 {
    match x {
        1 | 2 | 3 | 4 => 0,
        _ => 1,
    }
}

We will generate the following SwitchInt:

switchInt(_1) -> [1u8: bb2, 2u8: bb2, 3u8: bb2, 4u8: bb2, otherwise: bb1];

Ostensibly, something like this would be more profitable:

_2 = Le(const 1u8, _1);
switchInt(move _2) -> [false: bb2, otherwise: bb3];

This is the code you get from the range pattern 0..=4.

cc @wesleywiser @oli-obk

@Centril Centril added I-slow Issue: Problems and improvements with respect to performance of generated code. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 21, 2019
@oli-obk
Copy link
Contributor

oli-obk commented Dec 21, 2019

If there are multiple patterns it's not quite as simple.

fn program(x: u8) -> u8 {
    match x {
        1 | 2 => 2,
		3 | 4 => 0,
        _ => 1,
    }
}

will generate

switchInt(_1) -> [1u8: bb2, 2u8: bb2, 3u8: bb3, 4u8: bb3, otherwise: bb1];

which will need multiple switchInt

Or we teach switchInt to actually understand ranges, not sure that would translate to llvm in any reasonable way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir-opt Area: MIR optimizations C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

No branches or pull requests

3 participants