Skip to content

Codegen forgets array size when used with iterators #63552

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

Closed
orlp opened this issue Aug 14, 2019 · 3 comments
Closed

Codegen forgets array size when used with iterators #63552

orlp opened this issue Aug 14, 2019 · 3 comments
Labels
A-iterators Area: Iterators A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. I-slow Issue: Problems and improvements with respect to performance of generated code. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-llvm Working group: LLVM backend code generation

Comments

@orlp
Copy link
Contributor

orlp commented Aug 14, 2019

Consider these three functions:

pub fn in_range1(x: [usize; 3], m: usize) -> bool {
    (x[0] < m) && (x[1] < m) && (x[2] < m)
}

pub fn in_range2(x: [usize; 3], m: usize) -> bool {
    for k in &x {
        if *k >= m {
            return false;
        }
    }

    true
}

pub fn in_range3(x: [usize; 3], m: usize) -> bool {
    x.iter().cloned().all(|k| k < m)
}

I'd expect all of them to generate roughly the same code since our array x has a fixed compile-time size. With rustc 1.27.1 it does, but after that it seems there was a regression that causes the compiler to forget the size of the array and generate generic code rather than code specific for the array size when an iterator is used.

Particularly damning is that the compiler generates an unrolled loop which is dead code as our array isn't large enough to ever qualify for unrolling.

@jonas-schievink jonas-schievink added A-iterators Area: Iterators 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 14, 2019
@andjo403
Copy link
Contributor

have bisected this regression and it is the PR rust-lang/llvm#115 that causes the regression.
maybe @nikic knows what can cause this regression.

@jonas-schievink jonas-schievink added C-bug Category: This is a bug. I-nominated regression-from-stable-to-stable Performance or correctness regression from one stable version to another. labels Aug 15, 2019
@nikomatsakis
Copy link
Contributor

Going to mark this as P-medium. Would be great however if @nikic, @nagisa, @rkruppe or some other LLVM expert could weigh in on likely causes and whether we can fix it. Thanks to @andjo403 both for isolating the issue and bisecting it!

@nikomatsakis nikomatsakis added the P-medium Medium priority label Aug 29, 2019
@pnkfelix pnkfelix added WG-llvm Working group: LLVM backend code generation and removed I-nominated labels Sep 19, 2019
@nikic
Copy link
Contributor

nikic commented Nov 1, 2019

It looks like this problem is resolved in nightly: https://rust.godbolt.org/z/txaVfL The generated code is quite similar now. Don't know what is responsible for this though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-iterators Area: Iterators A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. I-slow Issue: Problems and improvements with respect to performance of generated code. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-llvm Working group: LLVM backend code generation
Projects
None yet
Development

No branches or pull requests

7 participants