SharedArrayBuffer and Atomics Stage 2.95 to Stage 3 Shu-yu Guo - - PowerPoint PPT Presentation

sharedarraybuffer and atomics stage 2 95 to stage 3
SMART_READER_LITE
LIVE PREVIEW

SharedArrayBuffer and Atomics Stage 2.95 to Stage 3 Shu-yu Guo - - PowerPoint PPT Presentation

SharedArrayBuffer and Atomics Stage 2.95 to Stage 3 Shu-yu Guo Lars Hansen Mozilla November 30, 2016 What We Have Consensus On TC39 agreed on Stage 2.95, July 2016 Agents API (frozen) What We Have Consensus On TC39 agreed on Stage


slide-1
SLIDE 1

SharedArrayBuffer and Atomics Stage 2.95 to Stage 3

Shu-yu Guo Lars Hansen

Mozilla

November 30, 2016

slide-2
SLIDE 2

What We Have Consensus On

TC39 agreed on Stage 2.95, July 2016

◮ Agents ◮ API (frozen)

slide-3
SLIDE 3

What We Have Consensus On

TC39 agreed on Stage 2.95, July 2016

◮ Agents ◮ API (frozen)

Memory model had fatal bug

slide-4
SLIDE 4

Outline

Memory Model

  • 1. Motivation
  • 2. Intuition
  • 3. What the Model Does
slide-5
SLIDE 5

Should We Allow This Optimization?

let x = U8[0]; if (x) print(x);

if (U8[0]) print(U8[0]);

slide-6
SLIDE 6

What About This One?

while (U8[0] == 42) ;

let c = U8[0] == 42; while (c) ;

slide-7
SLIDE 7

Or This One?

let A = Atomics; while (A.load(U8,0) == 42) ;

let A = Atomics; let c = A.load(U8,0) == 42; while (c) ;

slide-8
SLIDE 8

What Can Be Printed Here?

U8[0] = 1; U8[1] = 1; print(U8[0]); print(U8[1]); print(U8[1]); print(U8[0]);

slide-9
SLIDE 9

What’s a Memory Model Good For?

◮ Arbitrates optimization affordance ◮ Captures hardware reality

slide-10
SLIDE 10

Memory Model Design Space

  • 1. No model
  • 2. Undefined behavior/values for data races
  • 3. Fully defined; races have meaning
slide-11
SLIDE 11

Why

Because we’re the web.

◮ Interoperability ◮ Security

slide-12
SLIDE 12

Why

Because we’re the web.

◮ Interoperability ◮ Security ◮ WebAssembly

slide-13
SLIDE 13

What

The model prescribes the set of values that can be read by SAB

  • perations.
slide-14
SLIDE 14

Intuition

Strong enough for programmers to reason about programs Weak enough for hardware and compiler reality

slide-15
SLIDE 15

Programmers’ Intuition

Sequential Consistency for Data Race Free Programs Sequential consistency just means interleaving. Data race freedom means no concurrent, non-atomic memory accesses where one’s a write.

slide-16
SLIDE 16

Implementors’ Intuition: Codegen

Obvious code generation

◮ Non-atomics compiled to bare stores and loads ◮ Atomics to atomic instructions or with fences

slide-17
SLIDE 17

Implementors’ Intuition: Optimizations

◮ Atomics are carved in stone ◮ Reads must be stable (e.g. no read rematerialization) ◮ Writes must be stable (i.e. can’t make observable changes to

writes)

◮ Don’t completely remove writes (i.e. can coalesce adjacent

writes but not remove them completely)

slide-18
SLIDE 18

What We Talk About When We Talk About Atomicity

Access atomicity

Indivisible action

slide-19
SLIDE 19

What We Talk About When We Talk About Atomicity

Access atomicity

Indivisible action

Copy atomicity

Ordering: what memory accesses become visible to what cores when

slide-20
SLIDE 20

What We Talk About When We Talk About Atomicity

The memory model orders shared memory events and prescribes what values can be read by them.

slide-21
SLIDE 21

Ordering Analogies: Atomics

◮ C++ memory_order_seq_cst ◮ LLVM SequentiallyConsistent

slide-22
SLIDE 22

Ordering Analogies: Non-Atomics

◮ Between C++ non-atomics and memory_order_relaxed ◮ Between LLVM non-atomics and Unordered

slide-23
SLIDE 23

Details with all the math in the spec.

slide-24
SLIDE 24

this slide intentionally left blank

slide-25
SLIDE 25

this slide intentionally left blank

2016-11-30

SAB MM The rest of the presentation is not planned to be presented as it is unlikely a good use of committee time to go into the actual math. Nevertheless, they may be valuable for folks who are reading the slides and are interested in some of the math without going down the rabbit hole.

slide-26
SLIDE 26

Model Overview

◮ Axiomatic memory model ◮ Interfacing with ES evaluation semantics

slide-27
SLIDE 27

Model Overview

◮ Axiomatic memory model ◮ Interfacing with ES evaluation semantics

2016-11-30

SAB MM Model Overview The model has two parts. The bulk of it is an axiomatic model that does the ordering of memory events as we talked about. But this model is axiomatic – it’s a set of constraints, not an algorithm like the rest of

  • ECMA262. So there’s also a second part built into the evaluation

semantics that interfaces with the axiomatic model.

slide-28
SLIDE 28

Axiomatic Model

Ordering is done by an axiomatic model. Input is a candidate execution—a set of memory events and a set of relations ordering them. Output is a decision whether the candidate execution is valid. The meaning of a program is the set of all valid executions.

slide-29
SLIDE 29

Axiomatic Model

Ordering is done by an axiomatic model. Input is a candidate execution—a set of memory events and a set of relations ordering them. Output is a decision whether the candidate execution is valid. The meaning of a program is the set of all valid executions.

2016-11-30

SAB MM Axiomatic Model Axiomatic semantics is a big departure from the kind of semantics we do at TC39, which are all operational and algorithmic. Weak memory models allow for some weird acausal behavior that aren’t capturable by a straightforward operational, algorithmic style. The state of the art in the literature of memory models is all axiomatic.

slide-30
SLIDE 30

Axiomatic Model

Ordering is done by an axiomatic model. Input is a candidate execution—a set of memory events and a set of relations ordering them. Output is a decision whether the candidate execution is valid. The meaning of a program is the set of all valid executions. Not operational!

slide-31
SLIDE 31

Axiomatic Model

Ordering is done by an axiomatic model. Input is a candidate execution—a set of memory events and a set of relations ordering them. Output is a decision whether the candidate execution is valid. The meaning of a program is the set of all valid executions. Not operational!

2016-11-30

SAB MM Axiomatic Model Axiomatic semantics is a big departure from the kind of semantics we do at TC39, which are all operational and algorithmic. Weak memory models allow for some weird acausal behavior that aren’t capturable by a straightforward operational, algorithmic style. The state of the art in the literature of memory models is all axiomatic.

slide-32
SLIDE 32

Events

◮ Read (atomic and non-atomic) ◮ Write (atomic and non-atomic) ◮ ReadModifyWrite (atomic) ◮ Host-specific events (e.g. postMessage)

slide-33
SLIDE 33

Events

◮ Read (atomic and non-atomic) ◮ Write (atomic and non-atomic) ◮ ReadModifyWrite (atomic) ◮ Host-specific events (e.g. postMessage)

2016-11-30

SAB MM Events There are 3 kinds of shared memory events. Read events, write events, and RMW events. The host-specific events depend on the embedding.

slide-34
SLIDE 34

Candidate Execution

A candidate execution is

◮ A set of events ◮ agent-order ◮ reads-from ◮ synchronizes-with ◮ happens-before

slide-35
SLIDE 35

agent-order

The union of evaluation orders of all agents. If E occurred before D in some agent, E is agent-order before D.

slide-36
SLIDE 36

reads-from

Maps Read and ReadModifyWrite events to Write and ReadModifyWrite events. If R reads-from W, then R reads one or more bytes written by W.

slide-37
SLIDE 37

synchronizes-with

A subset of reads-from that relates synchronizing atomic Read and ReadModifyWrite events to atomic Write and ReadModifyWrite events. An atomic Read R synchronizes-with an atomic Write W when R reads every byte from W.

slide-38
SLIDE 38

synchronizes-with

A subset of reads-from that relates synchronizing atomic Read and ReadModifyWrite events to atomic Write and ReadModifyWrite events. An atomic Read R synchronizes-with an atomic Write W when R reads every byte from W.

2016-11-30

SAB MM synchronizes-with Recall that SAB API allows aliasing, so it’s possible for an atomic read to read from multiple writes, atomic and non-atomic, such as in case of races.

slide-39
SLIDE 39

happens-before

◮ agent-order relates intra-agent events ◮ synchronizes-with relates inter-agent events ◮ happens-before connects the two

(agent-order ∪ synchronizes-with)+

slide-40
SLIDE 40

Valid Executions

A candidate execution is valid when it has. . .

◮ . . . coherent reads ◮ . . . tear free reads ◮ . . . sequentially consistent atomics

slide-41
SLIDE 41

Coherent Reads

A read of some byte is coherent if it reads the most happens-before recent write to that byte. R reads-from W ⇒ ∃W ′.W happens-before W ′

slide-42
SLIDE 42

Coherent Reads

A read of some byte is coherent if it reads the most happens-before recent write to that byte. R reads-from W ⇒ ∃W ′.W happens-before W ′

2016-11-30

SAB MM Coherent Reads Remember that not everything is related by happens-before. Mathematically, happens-before is a strict partial order. So if there is a data race, for example, a read can read a more wall-time recent write as long as that write isn’t more happens-before recent.

slide-43
SLIDE 43

Tear Free Reads

Aligned accesses are well-behaved.

slide-44
SLIDE 44

Tear Free Reads

Aligned accesses are well-behaved.

2016-11-30

SAB MM Tear Free Reads The details are in the spec. The point here is that aligned accesses via integer TypedArrays have more guarantees than accesses via float TypedArrays and unaligned accesses via DataViews.

slide-45
SLIDE 45

Sequentially Consistent Atomics

◮ All synchronizes-with atomic events exist in a strict total order

consistent with happens-before.

◮ An atomic write becomes visible to atomic reads in finite time.

slide-46
SLIDE 46

Sequentially Consistent Atomics

◮ All synchronizes-with atomic events exist in a strict total order

consistent with happens-before.

◮ An atomic write becomes visible to atomic reads in finite time.

2016-11-30

SAB MM Sequentially Consistent Atomics This total order is the interleaving. The finite time is a liveness

  • guarantee. Non-atomics don’t have either guarantee.
slide-47
SLIDE 47

Data Race Redux

E is in a data race with D iff

◮ E and D aren’t related by happens-before ◮ E or D is a Write or ReadModifyWrite event ◮ E and D aren’t synchronized atomics

slide-48
SLIDE 48

Data Race Redux

E is in a data race with D iff

◮ E and D aren’t related by happens-before ◮ E or D is a Write or ReadModifyWrite event ◮ E and D aren’t synchronized atomics

2016-11-30

SAB MM Data Race Redux A quick revisit to more precisely define data races now that we’re armed with math.

slide-49
SLIDE 49

Event Semantics

◮ A read event reads a value composed of bytes from write

events it reads-from in a valid execution.

◮ Even racy reads have well-defined values!

slide-50
SLIDE 50

Event Semantics

◮ A read event reads a value composed of bytes from write

events it reads-from in a valid execution.

◮ Even racy reads have well-defined values!

2016-11-30

SAB MM Event Semantics This is often a set of more than one possible values. But note that this is still an axiomatic thing: we only know the value of a read event after we have the entire event graph and have ordered it according to the memory model.

slide-51
SLIDE 51

Interface with Evaluation Semantics

Where do events come from?

slide-52
SLIDE 52

Interface with Evaluation Semantics

Where do events come from?

2016-11-30

SAB MM Interface with Evaluation Semantics To interface the axiomatic semantics with the evaluation semantics, we make the evaluation semantics nondeterministic. Read operations on SABs introduce read events, write operations write events, and Atomic RMW operations RMW events. The question is what is the value of read events during the evaluation semantics? It is nondeterministically any possible value.

slide-53
SLIDE 53

Interface with Evaluation Semantics

Where do events come from?

◮ Evaluation semantics introduces events

slide-54
SLIDE 54

Interface with Evaluation Semantics

Where do events come from?

◮ Evaluation semantics introduces events

2016-11-30

SAB MM Interface with Evaluation Semantics To interface the axiomatic semantics with the evaluation semantics, we make the evaluation semantics nondeterministic. Read operations on SABs introduce read events, write operations write events, and Atomic RMW operations RMW events. The question is what is the value of read events during the evaluation semantics? It is nondeterministically any possible value.

slide-55
SLIDE 55

Interface with Evaluation Semantics

Where do events come from?

◮ Evaluation semantics introduces events ◮ Value of read events is any possible byte value

slide-56
SLIDE 56

Interface with Evaluation Semantics

Where do events come from?

◮ Evaluation semantics introduces events ◮ Value of read events is any possible byte value

2016-11-30

SAB MM Interface with Evaluation Semantics To interface the axiomatic semantics with the evaluation semantics, we make the evaluation semantics nondeterministic. Read operations on SABs introduce read events, write operations write events, and Atomic RMW operations RMW events. The question is what is the value of read events during the evaluation semantics? It is nondeterministically any possible value.

slide-57
SLIDE 57

Interface with Evaluation Semantics

Without SAB the evaluation semantics constructs a correct execution directly. With SAB the evaluation semantics constructs many candidate executions nondeterministically and the memory-model decides which ones are valid.

slide-58
SLIDE 58

Interface with Evaluation Semantics

Without SAB the evaluation semantics constructs a correct execution directly. With SAB the evaluation semantics constructs many candidate executions nondeterministically and the memory-model decides which ones are valid.

2016-11-30

SAB MM Interface with Evaluation Semantics This makes sense intuitively – weak memory models permit many possible observed memory values, so the meaning of a program with SAB is the set of valid executions.