Formalizing Semantics of par
#932
Replies: 3 comments
-
Curious about thoughts from CIRCT folks: @mikeurbach @stephenneuendorffer @cgyurgyik |
Beta Was this translation helpful? Give feedback.
-
Just to summarize, some broad points here are:
So, You could bikeshed about the use of numbers as names for barriers (maybe they should be stringy symbols or declared in |
Beta Was this translation helpful? Give feedback.
-
@EclecticGriffin just curious–how hard would it be to implement the |
Beta Was this translation helpful? Give feedback.
-
TLDR:
par
blocks in Calyx currently do have well-defined guarantees on what kind of cross-thread communication is allowed. This proposal formalizes Calyx's memory models as: Data race free or nothing (DRF0) by default, and@sync
annotation for communication.See Semantics of
par
(#921) for background discussionDRF0 by Default
By default,
par
blocks in Calyx are not allowed to "communicate" with each. Communication is defined as any possible data race between the ports of a cell or its internal state. For example, the following innocuous program is incorrect:In normal hardware design languages (HDLs), we would expect the program to swap values between
x
andy
after one cycle. However, Calyx programs make no guarantees about the scheduling ofpar
blocks. This means that the compiler is free to assume thatg1
andg2
may start executing in the same cycle, or thatg1
executes 100 cycles afterg2
. The latter is just an egregious example but demonstrates that complete lack of guarantees thatpar
blocks provide.The lack of guarantees in the language model is a good thing---too many guarantees means that the compiler is limited in the kinds of optimizations it can perform.
@sync
Annotation for SynchronizationObviously, some programs do require some notion of synchronization when executing parallel threads. We propose a new annotation, called
@sync
that enables threads to declare that events other threads are visible to them.For example:
In the program above, the compiler needs to guarantee that the execution of
b
can observe the events froma
(in its own thread) andd
ande
in the second thread.However, it does not guarantee that both
b
andf
start executing in the same cycle.The number
n
in the@sync(n)
annotation acts as the name of the barrier and can be referred to the by many different threads. Programs are free to use as many@sync(n)
as needed.Well-Formedness Constraint
A required well-formedness constraint for
@sync
annotations is that the same thread may not mention the same barrier in sequentially ordered events. For example, the following is allowed:Since
b
andc
occur in two branches ofif
, they are not sequentially ordered with respect to each other. However, the following@sync
annotations are ill-formed:This is because in the first thread, execution of
a
andc
are sequentially ordered---a
must execute beforec
and therefore it is impossible fora
to view events beforec
(which includesa
itself).Optimizing with Barriers
Passes should be able to analyze the various barriers in the program and use that information to reason about ordering of execution of various groups. A simple litmus test of whether we have correctly implemented these semantics is if resource sharing can share components across various threads when it can establish specific ordering relationships.
Compiling Barriers
The compiler is responsible for reifying the barriers in the final program and maintaining their invariants. A natural choice for this is using synchronizing registers which implement M-structures useful for blocking reads and writes.
An implementation challenge is generating "multi-armed" synchronizing registers that can be referred to by many different threads.
Synchronizing with Static Time
The default
@sync
primitive only establishes an ordering between parallel threads and does not guarantee that, for example, two groups will start executing at the same time. This kind of clock-based synchronization can be useful for certain programs that do need to make use of clock-based tricks for optimizations.While we currently don't have a concrete proposal for this, here are our aspirations:
@sync
annotation in conjunction to@static
to enable this kind of reasoning.Beta Was this translation helpful? Give feedback.
All reactions