Skip to content

Huge stack allocation is generated when assigning a huge piece of memory to a reference #72211

@bugadani

Description

@bugadani

Hi!

I am developing an application for an armv7 embedded device. I'm holding the application state in a stack-allocated enum, where one of the enum variations is 16k (total memory is 64k)

Switching to the huge app state, my microcontroller hard faults. I have played with a short repro on godbolt.org and I found that a particular pattern reserves stack space where I don't believe is necessary.

I tried this code:

pub fn init(a: usize) -> [usize; 1600] {
    [a; 1600]
}

pub fn fun(bar: &mut [usize; 1600], w: bool, a: usize) {
    *bar = init(a);
}

I expected to see this happen:
generated machine code fills bar directly

Instead, this happened:
generated machine code fills stack then copies to bar

Code generated with -C opt-level=3 --target=armv7-unknown-linux-gnueabihf on godbolt.org

example::fun:
        push    {r7, lr}
        sub.w   sp, sp, #2000 ; I don't like this stack alloc
        movs    r1, #0
        mov     r3, sp
.LBB1_1:
        str     r2, [r3, r1]
        adds    r1, #4
        cmp.w   r1, #2000
        bne     .LBB1_1
        mov     r1, sp
        mov.w   r2, #2000
        bl      __aeabi_memcpy4
        add.w   sp, sp, #2000
        pop     {r7, pc}

rustc --version --verbose:

1.43 and newer

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-mir-optArea: MIR optimizationsA-mir-opt-nrvoFixed by the Named Return Value Opt. (NRVO)C-enhancementCategory: An issue proposing an enhancement or a PR with one.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-heavyIssue: Problems and improvements with respect to binary size of generated code.I-slowIssue: 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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions