Stream: general

Topic: ✔ about riscv atomic store.


view this post on Zulip yang yu (Jul 04 2022 at 23:20):

atomic store a value can fail.
https://msyksphinz-self.github.io/riscv-isadoc/html/rv64a.html#sc-d
should I use a loop to implement atomic store.

loop {
   sc.d rd,rs1,rs2
   if rd == 0   break;
}

view this post on Zulip Chris Fallin (Jul 05 2022 at 05:09):

hi @yang yu -- actually the semantics of Cranelift's atomic store are infallible, i.e., there is no "failure". I think you may be confusing it for a "store conditional" (sc in RISC-V or other classic RISCs like MIPS, ARM32) instruction, which fails if some other CPU touched the value since the paired "load-linked"

view this post on Zulip Chris Fallin (Jul 05 2022 at 05:10):

an atomic store should have SeqCst ordering, which means that it occurs in some order relative to other atomic loads/stores on the same address and all CPUs agree on that order, and it also imposes ordering edges on the surrounding program (ordinary loads/stores prior to an atomic store always come before, and are observed by, ordinary loads/stores after an atomic load that observes the atomic store)

view this post on Zulip Chris Fallin (Jul 05 2022 at 05:11):

You'll need to reason about the memory model -- I don't know in detail what RISC-V provides -- but probably what you'll need is an ordinary store instruction, preceded by and/or followed by a memory fence or barrier instruction of some kind

view this post on Zulip Chris Fallin (Jul 05 2022 at 05:13):

one easy way to see what should be generated is to use an atomic with SeqCst ordering in C++ or Rust, and compile it to assembly; then do whatever clang or rustc does

view this post on Zulip Chris Fallin (Jul 05 2022 at 05:15):

here are atomic loads and stores with SeqCst in Rust, compiled to RISC-V: https://godbolt.org/z/G5h4c3cxc

use std::sync::atomic::{AtomicU64, Ordering}; pub fn store(atomic: &AtomicU64, value: u64) { atomic.store(value, Ordering::SeqCst); } pub fn load(atomic: &AtomicU64) -> u64 { atomic.load(Ordering::SeqCst) }

view this post on Zulip yang yu (Jul 05 2022 at 08:45):

@Chris Fallin thanks , But I am not very understand this. May be I will find some course to learn about this.

view this post on Zulip Chris Fallin (Jul 05 2022 at 14:49):

@yang yu for now I think just emitting the same output as what you see from rustc (in the Godbolt compiler explorer link above) should be fine

view this post on Zulip yang yu (Jul 05 2022 at 21:43):

ok

view this post on Zulip Notification Bot (Aug 10 2022 at 00:47):

yang yu has marked this topic as resolved.


Last updated: Nov 22 2024 at 17:03 UTC