Skip to content

vec macro into_boxed codegen regression #71861

Closed
@RustyYato

Description

@RustyYato

This is a regression from stable 1.42.0 to the latest beta & nightly

found because of this: https://internals.rust-lang.org/t/more-efficient-boxed-slice-creation/12271

pub fn bar2(n: usize) -> Box<[u32]> {
    vec![0; n].into_boxed_slice()
}

I think the regression happened because of a missed optimization to inline alloc::raw_vec::RawVec<T,A>::allocate_in::{{closure}}, so it generates unnecessary panic handlers

stable

alloc::raw_vec::RawVec<T,A>::allocate_in::{{closure}}:
        push    rax
        call    qword ptr [rip + alloc::raw_vec::capacity_overflow@GOTPCREL]
        ud2

example::bar2:
        push    r14
        push    rbx
        push    rax
        mov     ecx, 4
        mov     rax, rdi
        mul     rcx
        jo      .LBB1_5
        mov     rbx, rdi
        mov     r14, rax
        test    rax, rax
        je      .LBB1_4
        mov     esi, 4
        mov     rdi, r14
        call    qword ptr [rip + __rust_alloc_zeroed@GOTPCREL]
        test    rax, rax
        je      .LBB1_6
        mov     rcx, rax
.LBB1_4:
        mov     rax, rcx
        mov     rdx, rbx
        add     rsp, 8
        pop     rbx
        pop     r14
        ret
.LBB1_5:
        call    alloc::raw_vec::RawVec<T,A>::allocate_in::{{closure}}
        ud2
.LBB1_6:
        mov     esi, 4
        mov     rdi, r14
        call    qword ptr [rip + alloc::alloc::handle_alloc_error@GOTPCREL]
        ud2

nightly

core::ptr::drop_in_place:
        mov     rsi, qword ptr [rdi + 8]
        test    rsi, rsi
        je      .LBB0_2
        shl     rsi, 2
        test    rsi, rsi
        je      .LBB0_2
        mov     rdi, qword ptr [rdi]
        mov     edx, 4
        jmp     qword ptr [rip + __rust_dealloc@GOTPCREL]
.LBB0_2:
        ret

alloc::raw_vec::RawVec<T,A>::allocate_in::{{closure}}:
        push    rax
        call    qword ptr [rip + alloc::raw_vec::capacity_overflow@GOTPCREL]
        ud2

alloc::raw_vec::RawVec<T,A>::allocate_in::{{closure}}:
        push    rax
        mov     rax, qword ptr [rdi]
        mov     rsi, qword ptr [rdi + 8]
        mov     rdi, rax
        call    qword ptr [rip + alloc::alloc::handle_alloc_error@GOTPCREL]
        ud2

example::bar2:
        push    r15
        push    r14
        push    rbx
        sub     rsp, 32
        mov     ecx, 4
        xor     esi, esi
        mov     rax, rdi
        mul     rcx
        mov     rbx, rax
        setno   al
        jo      .LBB3_18
        mov     r14, rdi
        mov     sil, al
        shl     rsi, 2
        mov     qword ptr [rsp + 8], rbx
        mov     qword ptr [rsp + 16], rsi
        test    rbx, rbx
        je      .LBB3_3
        mov     rdi, rbx
        call    qword ptr [rip + __rust_alloc_zeroed@GOTPCREL]
        mov     rsi, rax
.LBB3_3:
        test    rsi, rsi
        je      .LBB3_19
        mov     rax, rbx
        shr     rax, 2
        mov     qword ptr [rsp + 8], rsi
        mov     qword ptr [rsp + 16], rax
        mov     qword ptr [rsp + 24], r14
        cmp     rax, r14
        je      .LBB3_15
        jb      .LBB3_10
        test    rbx, rbx
        je      .LBB3_15
        lea     r15, [4*r14]
        cmp     rbx, r15
        je      .LBB3_14
        mov     edx, 4
        mov     rdi, rsi
        mov     rsi, rbx
        test    r15, r15
        je      .LBB3_9
        mov     rcx, r15
        call    qword ptr [rip + __rust_realloc@GOTPCREL]
        test    rax, rax
        je      .LBB3_16
        mov     rsi, rax
        mov     rbx, r15
        jmp     .LBB3_14
.LBB3_9:
        call    qword ptr [rip + __rust_dealloc@GOTPCREL]
        xor     ebx, ebx
        mov     esi, 4
.LBB3_14:
        mov     qword ptr [rsp + 8], rsi
        shr     rbx, 2
        mov     qword ptr [rsp + 16], rbx
.LBB3_15:
        mov     rax, rsi
        mov     rdx, r14
        add     rsp, 32
        pop     rbx
        pop     r14
        pop     r15
        ret
.LBB3_18:
        call    alloc::raw_vec::RawVec<T,A>::allocate_in::{{closure}}
        ud2
.LBB3_19:
        lea     rdi, [rsp + 8]
        call    alloc::raw_vec::RawVec<T,A>::allocate_in::{{closure}}
        ud2
.LBB3_10:
        lea     rdi, [rip + .L__unnamed_1]
        lea     rdx, [rip + .L__unnamed_2]
        mov     esi, 36
        call    qword ptr [rip + core::panicking::panic@GOTPCREL]
        ud2
.LBB3_16:
        mov     esi, 4
        mov     rdi, r15
        call    qword ptr [rip + alloc::alloc::handle_alloc_error@GOTPCREL]
        ud2
        mov     rbx, rax
        lea     rdi, [rsp + 8]
        call    core::ptr::drop_in_place
        mov     rdi, rbx
        call    _Unwind_Resume@PLT
        ud2

.L__unnamed_1:
        .ascii  "Tried to shrink to a larger capacity"

.L__unnamed_3:
        .ascii  "/rustc/f05a5240440b3eaef1684a7965860fab40301947/src/libcore/macros/mod.rs"

.L__unnamed_2:
        .quad   .L__unnamed_3
        .asciz  "I\000\000\000\000\000\000\000\n\000\000\000\t\000\000"

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationC-bugCategory: This is a bug.I-slowIssue: Problems and improvements with respect to performance of generated code.ICEBreaker-Cleanup-CrewHelping to "clean up" bugs with minimal examples and bisectionsP-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions