Skip to content

MaybeUninit::uninit zero initializes large uninit array besides small other field #152541

@steffahn

Description

@steffahn

The code is slightly minimized from this forum thread.

use std::mem::MaybeUninit;

#[derive(Debug)]
pub struct Stack<T, const CAP: usize> {
    len: usize,
    v: MaybeUninit<[T; CAP]>,
}

impl<T, const CAP: usize> Stack<T, CAP> {
    /// Create a new empty `Stack`.
    pub fn new() -> Self {

        Stack {
            len: 0,
            v: MaybeUninit::uninit(),
        }
    }
}

type MyStack = Stack<i32,1000>;

pub struct C {
    a: MyStack,
    b: MyStack,
}

impl C {
    #[inline(never)]
    pub fn new() -> C
    {
        C { a:MyStack::new(), b:MyStack::new() }
    }
}

This produces a large memset since 1.93

example::C::new::h966d6519f96c8d05:
        push    rbx
        mov     rbx, rdi
        mov     edx, 8016
        xor     esi, esi
        call    qword ptr [rip + memset@GOTPCREL]
        mov     rax, rbx
        pop     rbx
        ret

previously (1.92) the asm was much more reasonable:

example::C::new::hb6aa5d89c35231d2:
        mov     rax, rdi
        mov     qword ptr [rdi + 4000], 0
        mov     qword ptr [rdi + 8008], 0
        ret

(same comparison on Compiler Explorer)


Bisection points to:

Metadata

Metadata

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-bugCategory: This is a bug.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchP-highHigh priorityS-has-bisectionStatus: A bisection has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.llvm-fixed-upstreamIssue expected to be fixed by the next major LLVM upgrade, or backported fixesregression-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