Skip to content

Reference to ZST occupies slot in function calling convention #141049

Closed as duplicate
@chadaustin

Description

@chadaustin

Please forgive if redundant - I did a search but could not find relevant discussion.

I work on embedded Rust and we use ZSTs and traits for hardware abstraction layers (HAL).

If we change call sites to accept HAL objects by reference (to support unit tests with stateful HALs), I noticed Rust will occupy an argument passing slot even though &ZST could also be zero-sized.

Notice the differences in https://gcc.godbolt.org/z/s6ocP1bds

Reproducing example here:

#![no_std]

struct MyDevice;

#[unsafe(no_mangle)]
pub fn zst_by_copy(z: MyDevice, a: usize, b: usize, c: usize) -> usize {
    a + b + c
}

#[unsafe(no_mangle)]
pub fn zst_by_reference(z: &MyDevice, a: usize, b: usize, c: usize) -> usize {
    a + b + c
}
zst_by_copy:
        add     a0, a0, a1
        add     a0, a0, a2
        ret

zst_by_reference:
        add     a0, a2, a1
        add     a0, a0, a3
        ret

Is there a fundamental reason or is this simply a missed optimization opportunity?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-zstArea: Zero-sized types (ZSTs).C-discussionCategory: Discussion or questions that doesn't represent real issues.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