This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this conveys accurate information: users of C/C++ would use the language's
_Atomic
,std::mutex
andstd::atomic
primitives, these would then compile to ShardArrayBuffer. Non-atomic access in these languages are racy if not synchronized properly, the same will be true with SharedArrayBuffer.The difference SAB tries to hit is in guaranteeing values observed when races do occur.
I've outlined details of the differences here: #88
LLVM isn't that different from the C++ memory model, except it's on a per-access basis and also supports a Java-oriented memory access (which we should ignore here). I'd also mention other compilers if we're to mention LLVM.
I think it's worth mentioning languages such as Rust which have a pretty different memory model (which under the covers can lower to something like LLV IR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I reworded this paragraph.
I'm not sure all C/C++ users will use the locking primitives, some programs may implement lock-free data structures using atomic accesses, which would then compile to SAB accesses.
I'm also not sure we'll want to ignore LLVM
unordered
, we could map SAB non-atomic to LLVMunordered
, depending on whether SAB non-atomics are required to be coherent.AFAIK the Rust memory model is built on top of the LLVM model (with extra guarantees for safe code).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lock-free data structures use
std::atomic
. Others usestd::mutex
. This all maps to SAB. I'm not sure I understand what you're disagreeing with.It's premature to consider
unordered
since SAB doesn't support anything butseq_cst
.Would be good to get @pcwalton's in put on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, good point re
std::atomic
.SAB only uses SC, but GetValueFromBuffer can call ReadSharedMemory with
accessAtomic
set tofalse
, and so result in non-atomic access. I don't think there's a coherence requirement for these non-atomics, is there?I'm talking to @nikomatsakis about Rust's model, and especially the model for unsafe code, tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. To be more precise: we likely want to enable most optimizations which JS engines already do or could do when accessing SAB non-atomically, except where that would cause races in a SAB where no race existed before (this can happen e.g. when the optimizer speculates).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I'm asking whether we can implement SAB non-atomic as LLVM unordered? (In particular, is there a requirement that SAB non-atomics are coherent?)
Relaxed operations can't be reordered as much as unordered operations, due to the requirement of coherence. For example,
x=1;x=2;x=3
can't be optimized tox=2;x=1;x=3
forrelaxed
writes, but can forunordered
.I'm meaning the implementation of SAB (e.g. inside a JS engine). The question is, should it be possible to implement SAB in C++ or LLVM, or does the implementation have to be in hand-crafted asm? (This impacts my life, as someone who may be implementing the Servo interactions with SAB, if they end up being visible in the browser.)
OK, what word should I use for non-atomic accesses to an SAB? Presumably non-atomic access is being mapped to something weaker than
seq_cst
.Yes,. that's the point I'm trying to make here, SAB non-atomic accesses can't be mapped to C11 non-atomics, since racy non-atomics are UB in C11.
??? -> SAB -> LLVM. This whole section is about how SAB can be implemented, not how to use it as a compilation target.
OK, I talked to @nikomatsakis, and the Rust memory model is still a work in progress, so while the current implementation is via LLVM (and so inherits its memory model) future implementations may provide weaker guarantees.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by "implement"?
Are you trying to compile asm.js / wasm code using SAB to LLVM IR, and trying to figure out which load / store type to use? (it sounds from your later reply that this is the case)
Or are you trying to figure out how to specify the semantics of SAB?
Sure, but that doesn't make my statement any less true:
Implementing SAB -> LLVM IR -> x86/ARM/etc should totally be possible. As you point out it's not clear what restrictions the SAB spec is currently putting on what LLVM IR you can generate.
I'll go further to say: that shouldn't be a significant design criteria. It should be possible to lower SAB to efficient machine code, but whether LLVM is well suited or not is a fixable issue.
Now that I understand what you're trying to do (SAB -> LLVM IR -> ...) I think mentioning C++ is misleading.
Yes, SAB's non-atomics are weaker than
seq_cst
. It's still not well defined what that is, and exactly how it interacts with atomics.That's not quite right: an implementation ca decide to specify the UB we care about here. I think the question you're trying to get to is: does LLVM implement SAB's memory model so your job is simpler. I don't know because:
seq_cst
is easy, non-atomics and their interactions aren't yet).That wasn't clear at all to me. I'd appreciate it if you could clarify the wording.
I wouldn't mention it if it's a moving target. Further, if Rust's recommended parallelism datastructures are shared-nothing then this discussion is kinda misleading. I'd at least like a link referencing the Rust memory model you have in mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay, we seem to be on the same page now!
OK, I can remove it and just talk about LLVM. Should I move the edits to being back under hardware if we're just talking about LLVM? Or would you still prefer a separate section?
Will do.
I'll remove Rust. If you're interested, the bit of Rust's stdlib that's of interest is the atomics, which are just a wrapper around LLVM's atomics: https://doc.rust-lang.org/std/sync/atomic/index.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what you want is a section which talks about lowering from SAB to other languages, as an intermediate to some form of machine code. Given that the interactions between non-atomics and atomics aren't fully specified in SAB at the moment I think that section will be "todo" more than anything :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll give it a shot. I'm trying to provide the spec for the interaction between atomics and non-atomics over in https://github.com/asajeffrey/ecmascript_sharedmem/blob/master/MEMMODEL_WIP.md you can spot a lot of LLVM influence!