Coder Social home page Coder Social logo

Comments (5)

RalfJung avatar RalfJung commented on September 27, 2024 1

I don't think it is allowed to completely remove independent SC operations

LLVM certainly does not agree with your reasoning: this becomes a NOP.

pub fn test() {
    let x = AtomicUsize::new(0);
    x.load(Ordering::SeqCst);
}

Stores also get removed. I think they just didn't implement the pass that removes dead RMWs.

from event-listener.

notgull avatar notgull commented on September 27, 2024

The MSRV of the smol-rs project previously did not allow for inline assembly. See smol-rs/smol#244. But now it does, so we should probably bump the MSRV so we can directly use the code that we want.

from event-listener.

taiki-e avatar taiki-e commented on September 27, 2024

Filed #71 to resolve this.

why doesn't this use inline assembly?

When this crate was first released, inline assembly was not stable. And for a long time after that, we used MSRV that older than 1.59.

Since we have recently raised the MSRV of some smol-rs crates to 1.63, I think this is an appropriate time to address this issue.

and the myth that no sane compiler is going to optimize atomics is a myth. However, I admit I don't know enough about this specific case to say whether this is a risk for this crate.

The C++20 memory model states that the SC operation "establish a single total modification order of all atomic operations that are so tagged". https://en.cppreference.com/w/cpp/atomic/memory_order#Sequentially-consistent_ordering

While it may be possible to "merge two atomic operations when there are only pure operations in the middle" as described in the article you linked to, I don't think it is allowed to completely remove independent SC operations (unless the compiler can prove that there are never any SC operations that could be synchronized with it by inspecting all programs). (It may be possible to replace SC CAS with SC fence or other SC RMW, but it doesn't lose the property we need here.)

So, unless the C++ memory model changes the property mentioned above in the future, I expect that no wrong code will be generated here, but inline assembly is the "definitely correct" choice here, and I agree that using inline assembly is definitely preferable.

from event-listener.

RalfJung avatar RalfJung commented on September 27, 2024

The MSRV of the smol-rs project previously did not allow for inline assembly. See smol-rs/smol#244. But now it does, so we should probably bump the MSRV so we can directly use the code that we want.

Ah, if it's just an MSRV issue that is great! And thanks @taiki-e for getting the ball rolling with that PR. :)

If the lock cmpxchg can be faster than mfence it might also be worth trying to get LLVM to generate the former instead of the latter for SC fences -- then all code would benefit.

The C++20 memory model states that the SC operation "establish a single total modification order of all atomic operations that are so tagged". https://en.cppreference.com/w/cpp/atomic/memory_order#Sequentially-consistent_ordering

Yeah, but the way that order interacts with the other orders isn't always entirely how one would expect, I think... so it's certainly not obvious to me that compilers are not allowed to drop the access. In particular this order AFAIK does not participate when determining synchronizes-with edges -- but an SC fence has the effects of a release-acquire fence and can hence establish synchronizes-with edges.

You have to study the formalizations of this model, I don't think the English prose can be precise enough for subtle questions like this.

from event-listener.

taiki-e avatar taiki-e commented on September 27, 2024

Oh, that is interesting. I can reproduce it, even with a very old rustc, even if I used compiler_fence around SC load/store. https://godbolt.org/z/xsvG5Ke58

from event-listener.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.