Skip to content

Atomic fetch_or fails to optimize right shift mask while left shift works #114541

Open
@SUPERCILEX

Description

@SUPERCILEX
use core::sync::atomic::*;

pub fn left(a: &AtomicU32, shift: u32) -> bool {
    let mask = 1 << shift;
    a.fetch_or(mask, Ordering::Relaxed) & mask == mask
}

pub fn right(a: &AtomicU32, shift: u32) -> bool {
    let mask = (1 << (u32::BITS - 1)) >> shift;
    a.fetch_or(mask, Ordering::Relaxed) & mask == mask
}

https://rust.godbolt.org/z/oYW1Y47EK

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Aug 6, 2023
SUPERCILEX

SUPERCILEX commented on Aug 6, 2023

@SUPERCILEX
ContributorAuthor

Hmmm, left also seems to fail with let mask = 2 << shift;. Very finicky.

added
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
I-slowIssue: Problems and improvements with respect to performance of generated code.
A-atomicArea: Atomics, barriers, and sync primitives
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-atomicArea: Atomics, barriers, and sync primitivesC-bugCategory: This is a bug.I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @SUPERCILEX@saethlin@rustbot

        Issue actions

          Atomic fetch_or fails to optimize right shift mask while left shift works · Issue #114541 · rust-lang/rust