Repository navigation
Closed
Labels
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
I tried this code:
pub fn iter_too(v: &[u32]){
for (i, _) in v.iter().enumerate(){
assert!(i <= v.len());
}
}I expected to see this happen: Assertions is removed because it is always false.
Instead, this happened: Generated code contains loop, check and panic.
example::iter_too:
lea rax, [4*rsi + 4]
mov rcx, -1
.LBB0_1:
add rax, -4
je .LBB0_4
add rcx, 1
cmp rcx, rsi
jbe .LBB0_1
push rax
lea rdi, [rip + .L__unnamed_1]
lea rdx, [rip + .L__unnamed_2]
mov esi, 30
call qword ptr [rip + core::panicking::panic@GOTPCREL]
ud2
.LBB0_4:
retSurprisingly, compiler is able to understand that i < v.len()
This code
pub fn iter_too(v: &[u32]){
for (i, _) in v.iter().enumerate(){
assert!(i < v.len());
}
}compiles into
example::iter_too:
retMeta
rustc --version --verbose:
rustc 1.59.0-nightly (91a0600a5 2021-12-18)
binary: rustc
commit-hash: 91a0600a5c22b9d159e3c57526af83e71d1120f8
commit-date: 2021-12-18
host: x86_64-unknown-linux-gnu
release: 1.59.0-nightly
LLVM version: 13.0.0
Compiler returned: 0
stable 1.57 affected too.
Metadata
Metadata
Assignees
Labels
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
AngelicosPhosphoros commentedon Dec 21, 2021
I got this error when tried to work with such code:
I wrote minimized version in the issue.
the8472 commentedon Dec 21, 2021
silly workaround for the moment:
AngelicosPhosphoros commentedon Dec 21, 2021
Another workaround:
BGR360 commentedon Dec 22, 2021
Does this really fall under the category of bug? Seems like it could be considered an enhancement to optimization.
Either way, here's some findings.
The resulting optimized MIR is identical apart from
LtvsLe:With
<With
<=But the resulting LLVM IR is vastly different:
With
<With
<=So I think maybe this points to a problem with constant evaluation.
@rustbot label +A-const-eval +T-compiler
nikic commentedon Dec 22, 2021
The basic problem here is that the loop has two induction variables, a counter and a pointer. The loop condition is on the pointer, and SCEV apparently is unable to determine the correlation between them in this case. Full IR for reference: https://llvm.godbolt.org/z/P8a8nr714
the8472 commentedon Dec 22, 2021
I think that could be improved on the libs side. At least when
iter.for_each()is used instead offor _ inAngelicosPhosphoros commentedon Dec 22, 2021
clang fails to optimize it too.
compiles to
AngelicosPhosphoros commentedon Dec 22, 2021
@the8472
I think, it is not optimal approach. We would need specializations of
Enumerate<I>for all possible cases and still fail in some cases. LLVM-side optimization would be more robust.1 remaining item
AngelicosPhosphoros commentedon Dec 22, 2021
This still would increase complexity and harm maintainability. It is fine to fix the issue that way but I think we should try to fix LLVM first and increase complexity of library only if we fail.
BGR360 commentedon Dec 23, 2021
@nikic or @AngelicosPhosphoros can either of you file the appropriate LLVM issue?
AngelicosPhosphoros commentedon Dec 23, 2021
Done.
llvm/llvm-project#52851
BGR360 commentedon Dec 24, 2021
Interesting @AngelicosPhosphoros in the example you posted on the LLVM issue, you used
<and it still didn't remove the bounds check. In the Rust example, using<resulted in well-optimized code, but<=didn't. What could be causing this difference?the8472 commentedon Apr 27, 2022
The linked LLVM issue was closed and the commit that closed it can be found in the current rust-lang/llvm-project tree, so it should be available on nightly.
But the reproducer still shows the panic on godbolt
AngelicosPhosphoros commentedon Jul 19, 2022
Filed another issue to mainstream: llvm/llvm-project#56612
nikic commentedon Apr 3, 2023
Still a problem with LLVM 16.
LingMan commentedon Jan 28, 2026
Fixed since Rust 1.87, which contained the update to LLVM 20. Interestingly the second LLVM issue still reproduces even with clang 21.1.0.
Rollup merge of rust-lang#165185 - zakrad:codegen-test-92174, r=nneth…
Unrolled build for #165185 in rollup 165375
Rollup merge of #165185 - zakrad:codegen-test-92174, r=nnethercote