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

rustc fails to detect variable bounds #100308

Open
dragostis opened this issue Aug 9, 2022 · 0 comments
Open

rustc fails to detect variable bounds #100308

dragostis opened this issue Aug 9, 2022 · 0 comments
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-mir-opt Area: MIR optimizations C-bug Category: This is a bug. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such 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

@dragostis
Copy link

This is probably an LLVM issue, but I decided to start my journey here. rustc/LLVM seems to be unable to detect bounds of a variable inside of this simple loop:

pub fn walk(slice: &mut [u64]) -> bool {
    let mut cursor = 0;

    for i in 0..slice.len() {
        cursor += (slice[i] != 0) as usize;
    }
    
    cursor <= slice.len()
}

The code generated for the function above is not a simple constant return, but it actually processes the whole slice.

The reason why this is an interesting use case is because you cannot seem to write a fully optimized compact function without unsafe code:

pub fn compact_slow(slice: &mut [u64]) {
    let mut cursor = 0;

    for i in 0..slice.len() {
        let val = slice[i];

        slice[cursor] = val;

        cursor += (val != 0) as usize;
    }
}

godbolt (with bounds check)

pub fn compact_fast(slice: &mut [u64]) {
    let mut cursor = 0;

    for i in 0..slice.len() {
        let val = unsafe {
            let val = *slice.get_unchecked(i);

            *slice.get_unchecked_mut(cursor) = val;

            val
        };

        cursor += (val != 0) as usize;
    }
}

godbolt (without bounds check)

@dragostis dragostis added the C-bug Category: This is a bug. label Aug 9, 2022
@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@fmease fmease 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-mir-opt Area: MIR optimizations C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such and removed needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. labels Jan 26, 2024
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. A-mir-opt Area: MIR optimizations C-bug Category: This is a bug. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such 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