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.A-mir-optArea: MIR optimizationsArea: MIR optimizationsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.E-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
rustc 1.50.0-nightly (f76ecd0 2020-12-15) on this code:
pub fn foo(data: &[u8], i: usize, j: usize) -> u8 {
if i < data.len() && j <= i {
data[j]
} else {
0
}
}Can't remove the array bound test (compiling even with aggressive optimizations):
foo:
push rax
cmp rdx, rsi
jae .LBB0_4
cmp rcx, rdx
ja .LBB0_4
cmp rcx, rsi
jae .LBB0_3
mov al, byte ptr [rdi + rcx]
pop rcx
ret
.LBB0_4:
xor eax, eax
pop rcx
ret
.LBB0_3:
lea rdx, [rip + .L__unnamed_1]
mov rdi, rcx
call qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
ud2This shows that value range analysis could be useful.
Godbolt: https://rust.godbolt.org/z/jPoY6Mnx6
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.A-mir-optArea: MIR optimizationsArea: MIR optimizationsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.E-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
camelid commentedon Dec 16, 2020
I relabeled this as an enhancement proposal since to my understanding this isn't a bug.
jrmuizel commentedon Dec 17, 2020
I filed corresponding gcc and llvm bugs:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98357
https://bugs.llvm.org/show_bug.cgi?id=48544
jrmuizel commentedon Dec 17, 2020
It looks like the new ConstraintElimination llvm pass should fix this.
leonardo-m commentedon Dec 17, 2020
That went better than expected. LLVM devs often deliver.
jrmuizel commentedon Mar 1, 2021
I confirmed that:
rustc --emit=asm -C passes=constraint-elimination -O test.rswith a rust built with llvm12 branch gives:leonardo-m commentedon Mar 2, 2021
You could also test issue #80620 :-)
leonardo-m commentedon Mar 2, 2021
Is
-C passes=constraint-eliminationpart of-C opt-level=3?jrmuizel commentedon Mar 2, 2021
I would be surprised if it was, but it's probably worth adding.
veera-sivarajan commentedon Mar 14, 2025
Fixed since
rustc 1.73.0: https://rust.godbolt.org/z/r6rebEhWP@rustbot label +e-needs-test
Rollup merge of rust-lang#145380 - okaneco:add-codegen-tests, r=Mark-…
Rollup merge of rust-lang#145380 - okaneco:add-codegen-tests, r=Mark-…
Unrolled build for #145380
Rollup merge of #145380 - okaneco:add-codegen-tests, r=Mark-Simulacrum
Rollup merge of #145380 - okaneco:add-codegen-tests, r=Mark-Simulacrum