Skip to content

Inconsistent bounds checking optimization in enumerated slice iterators #133979

Closed
@edwloef

Description

@edwloef

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.

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions