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

[ER] Possible basic panic_bounds_check left after Vec::resize_with #75042

Open
leonardo-m opened this issue Aug 2, 2020 · 1 comment
Open
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-enhancement Category: An issue proposing an enhancement or a PR with one. I-heavy Issue: Problems and improvements with respect to binary size of generated code. 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

@leonardo-m
Copy link

I don't understand this situation much. This code:

pub fn foo() -> Vec<usize> {
    let mut arr: Vec<usize> = vec![];
    arr.resize_with(100, || Default::default());
    for i in 0 .. 100 {
        arr[i] = i;
    }
    arr
}

pub fn foo2() -> Vec<usize> {
    let mut arr: Vec<usize> = vec![];
    arr.resize_with(100, || Default::default());
    arr[0] = 1;
    arr
}

With rustc 1.47.0-nightly cfc572cae 2020-07-30 gives the asm:

foo:
        push    r15
        push    r14
        push    r12
        push    rbx
        push    rax
        mov     r14, rdi
        mov     qword ptr [rdi], 8
        vxorps  xmm0, xmm0, xmm0
        vmovups xmmword ptr [rdi + 8], xmm0
        mov     esi, 100
        call    alloc::vec::Vec<T>::reserve
        mov     r12, qword ptr [r14]
        mov     r15, qword ptr [r14 + 16]
        lea     rdi, [r12 + 8*r15]
        xor     ebx, ebx
        mov     edx, 792
        xor     esi, esi
        call    qword ptr [rip + memset@GOTPCREL]
        mov     qword ptr [r12 + 8*r15 + 792], 0
        add     r15, 100
        mov     qword ptr [r14 + 16], r15
.LBB1_1:
        cmp     r15, rbx
        je      .LBB1_8
        mov     qword ptr [r12 + 8*rbx], rbx
        lea     rax, [rbx + 1]
        cmp     rax, r15
        je      .LBB1_8
        mov     qword ptr [r12 + 8*rbx + 8], rax
        inc     rax
        cmp     rax, r15
        je      .LBB1_8
        mov     qword ptr [r12 + 8*rbx + 16], rax
        inc     rax
        cmp     rax, r15
        je      .LBB1_8
        mov     qword ptr [r12 + 8*rbx + 24], rax
        inc     rax
        cmp     rax, r15
        je      .LBB1_8
        mov     qword ptr [r12 + 8*rbx + 32], rax
        inc     rax
        mov     rbx, rax
        cmp     rax, 100
        jne     .LBB1_1
        mov     rax, r14
        add     rsp, 8
        pop     rbx
        pop     r12
        pop     r14
        pop     r15
        ret
.LBB1_8:
        lea     rdx, [rip + .L__unnamed_1]
        mov     rdi, r15
        mov     rsi, r15
        call    qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
        ud2


foo2:
        push    r15
        push    r14
        push    rbx
        mov     r15, rdi
        mov     qword ptr [rdi], 8
        vxorps  xmm0, xmm0, xmm0
        vmovups xmmword ptr [rdi + 8], xmm0
        mov     esi, 100
        call    alloc::vec::Vec<T>::reserve
        mov     r14, qword ptr [r15]
        mov     rbx, qword ptr [r15 + 16]
        lea     rdi, [r14 + 8*rbx]
        mov     edx, 792
        xor     esi, esi
        call    qword ptr [rip + memset@GOTPCREL]
        mov     qword ptr [r14 + 8*rbx + 792], 0
        add     rbx, 100
        mov     qword ptr [r15 + 16], rbx
        je      .LBB2_1
        mov     qword ptr [r14], 1
        mov     rax, r15
        pop     rbx
        pop     r14
        pop     r15
        ret
.LBB2_1:
        lea     rdx, [rip + .L__unnamed_2]
        xor     edi, edi
        xor     esi, esi
        call    qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
        ud2

That contains panic_bounds_check. If I compile only of those functions the panic_bounds_check go away.

@nbdd0121
Copy link
Contributor

nbdd0121 commented Aug 6, 2020

This generates some more bizarre code:

pub fn foo() -> Vec<usize> {
    let mut arr: Vec<usize> = vec![];
    arr.resize_with(100, || Default::default());
    assert_eq!(arr.len(), 100);
    for i in 0 .. 100 {
        arr[i] = i;
    }
    arr
}

generates code for assert_eq!:

test    r14, r14
jne     .LBB3_2

This is likely a LLVM bug.

@rustbot modify labels: +A-codegen +A-LLVM +C-enhancement +I-slow +I-heavy

@rustbot rustbot added A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-enhancement Category: An issue proposing an enhancement or a PR with one. I-heavy Issue: Problems and improvements with respect to binary size of generated code. I-slow Issue: Problems and improvements with respect to performance of generated code. labels Aug 6, 2020
@JohnTitor JohnTitor added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-enhancement Category: An issue proposing an enhancement or a PR with one. I-heavy Issue: Problems and improvements with respect to binary size of generated code. 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

4 participants