Skip to content

Conversion of atomic builtins needlessly uses Rust intrinsics #372

@chrysn

Description

@chrysn

Example

Compiling this function:

static inline __attribute__((always_inline))
void cpu_reg_disable_bits(volatile uint32_t *reg, uint32_t mask)
{
    __atomic_fetch_and(reg, ~mask, __ATOMIC_RELAXED);
}

produces this Rust code:

#[inline(always)]
unsafe extern "C" fn cpu_reg_disable_bits(mut reg: *mut uint32_t,
                                          mut mask: uint32_t) {
    ::core::intrinsics::atomic_and_relaxed(reg, !mask);
}

This is correct, but requires the core_intrinsics on the produced code (which is otherwise usable on stable Rust).

Code orientation

The relevant code is produced in c2rust-transpile/src/translator/builtins.rs around line 530; note that one doesn't find precisely "atomic_and_relaxed" because the name is assembled from the operation and the ordering.

Replacement

The AtomicXX family of types has a fetch_and, which are suitable replacements -- at least if the type that is being operated on is part of the family.

I'm holding off on fixing this while #364 is hot; I plan to come back to this later and can probably come up with a PR then.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions