Skip to content

[RISCV] Unsigned atomics use signed load + zero-extension instead of unsigned loads #141627

Closed
@tom-rein

Description

@tom-rein

The following code (godbolt):

#include <atomic>
unsigned char load(std::atomic_uchar &x) {
    return x.load(std::memory_order_relaxed);
}

Compiles into:

load(std::atomic<unsigned char>&):
        lb      a0, 0(a0)
        andi    a0, a0, 255
        ret

Instead of:

load(std::atomic<unsigned char>&):
        lbu     a0, 0(a0)
        ret

Same is the case for unsigned short and signed atomics casted to unsigned.
For unsigned int signed load is correct because of the ABI, but when it is zero-extended, llvm fails to combine the zero-extension with the load.
Non-atomic loads correctly result in unsigned loads.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions