Skip to content

Boxing zeroed arrays and slices uses rust_alloc + memset instead of rust_alloc_zeroed #80752

Open
@leonardo-m

Description

@leonardo-m
#![feature(box_syntax)]

use std::convert::TryInto;

const N: usize = 100_000;

pub fn foo1() -> Vec<u32> {
    vec![0; N]
}

pub fn foo2() -> Box<[u32]> {
    box [0; N]
}

pub fn foo3() -> Box<[u32; N]> {
    box [0; N]
}

pub fn foo4() -> Box<[u32; N]> {
    vec![0; N].into_boxed_slice().try_into().unwrap()
}

Gives (rustc 1.51.0-nightly 8018418 2021-01-03):

.LCPI0_0:
    .quad   100000
    .quad   100000
foo1:
    push    rbx
    mov     rbx, rdi
    mov     edi, 400000
    mov     esi, 4
    call    qword ptr [rip + __rust_alloc_zeroed@GOTPCREL]
    test    rax, rax
    je      .LBB0_1
    mov     qword ptr [rbx], rax
    vmovaps xmm0, xmmword ptr [rip + .LCPI0_0]
    vmovups xmmword ptr [rbx + 8], xmm0
    mov     rax, rbx
    pop     rbx
    ret
.LBB0_1:
    mov     edi, 400000
    mov     esi, 4
    call    qword ptr [rip + alloc::alloc::handle_alloc_error@GOTPCREL]
    ud2


foo2:
    push    rbx
    mov     edi, 400000
    mov     esi, 4
    call    qword ptr [rip + __rust_alloc@GOTPCREL]
    test    rax, rax
    je      .LBB1_1
    mov     rbx, rax
    mov     edx, 400000
    mov     rdi, rax
    xor     esi, esi
    call    qword ptr [rip + memset@GOTPCREL]
    mov     edx, 100000
    mov     rax, rbx
    pop     rbx
    ret
.LBB1_1:
    mov     edi, 400000
    mov     esi, 4
    call    qword ptr [rip + alloc::alloc::handle_alloc_error@GOTPCREL]
    ud2


foo3:
    push    rbx
    mov     edi, 400000
    mov     esi, 4
    call    qword ptr [rip + __rust_alloc@GOTPCREL]
    test    rax, rax
    je      .LBB2_1
    mov     rbx, rax
    mov     edx, 400000
    mov     rdi, rax
    xor     esi, esi
    call    qword ptr [rip + memset@GOTPCREL]
    mov     rax, rbx
    pop     rbx
    ret
.LBB2_1:
    mov     edi, 400000
    mov     esi, 4
    call    qword ptr [rip + alloc::alloc::handle_alloc_error@GOTPCREL]
    ud2


foo4:
    push    rax
    mov     edi, 400000
    mov     esi, 4
    call    qword ptr [rip + __rust_alloc_zeroed@GOTPCREL]
    test    rax, rax
    je      .LBB3_1
    pop     rcx
    ret
.LBB3_1:
    mov     edi, 400000
    mov     esi, 4
    call    qword ptr [rip + alloc::alloc::handle_alloc_error@GOTPCREL]
    ud2    

I think box should use rust_alloc_zeroed instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-boxArea: Our favorite opsem complicationC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-opsemRelevant to the opsem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions