Skip to content

Inconsistent bounds checking optimization in enumerated slice iterators #133979

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
edwloef opened this issue Dec 6, 2024 · 1 comment · Fixed by #134117
Closed

Inconsistent bounds checking optimization in enumerated slice iterators #133979

edwloef opened this issue Dec 6, 2024 · 1 comment · Fixed by #134117
Assignees
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@edwloef
Copy link
Contributor

edwloef commented Dec 6, 2024

I tried this code:

fn test(a: &[&[u8]]) -> u32 {
    a
        .iter()
        .enumerate()
        .map(|(y, b)| {
            b.iter()
                .enumerate()
                .filter(|(_, c)| **c == b'A')
                .map(|(x, _)| {
                    a[y][x] as u32
                })
                .sum::<u32>()
        })
        .sum()
}

I expected to see this happen: bounds checks are optimized out

Instead, this happened: bounds checks are not optimized out

https://godbolt.org/z/jsxvaeez7

Meta

rustc --version --verbose:

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1
Additional info

  • This occurs from rust versions 1.56.0 to current nightly.
  • Consistently utilizing either a[y] or b (not both as seen above) removes the produced bounds checks.
  • Removing the filter also removes the bounds checks.

@edwloef edwloef added the C-bug Category: This is a bug. label Dec 6, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 6, 2024
@fmease fmease added C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 6, 2024
@nikic nikic added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Dec 6, 2024
@dianqk
Copy link
Member

dianqk commented Dec 9, 2024

Canonicalizing GEPs should help this.

@rustbot claim

bors added a commit to rust-lang-ci/rust that referenced this issue Dec 10, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 11, 2024
@bors bors closed this as completed in a611773 Dec 15, 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. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such 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