Strengthen liveness analysis around CSPs

This commit is contained in:
Nigel Tao committed 2020-02-11 14:26:12 +11:00
1 parent 2f2ccdb696
commit 4c7c296015
2 files changed
+40 -15

No files matched your search

+32 -15
View File
@@ -23,7 +23,7 @@ package cgen
// For example, in this code: // For example, in this code:
// //
// i = etc // i = etc
// yield foo // The first CSP. // yield? foo // The first CSP.
// j = 0 // j = 0
// while j < i { // while j < i {
// a[j] = 42 // a[j] = 42
@@ -52,16 +52,15 @@ package cgen
// since the last write. // since the last write.
// //
// Essentially, the state transitions are: strong is sticky, weak to strong // Essentially, the state transitions are: strong is sticky, weak to strong
// happens on a read, weak to none happens on a write, and none to weak happens // happens on a read and weak to none happens on a write. On a CSP, none/weak
// on a CSP. When reconciling multiple code paths, the strongest wins, where // to strong happens for variables explicitly mentioned in the CSP expression
// strong > weak and weak > none. Loops are repeated until the reconciliations // and none to weak happens for other variables.
// between the N'th and N+1'th iteration are all no-ops: a steady state has
// been reached. There can be multiple reconciliations per iteration, due to
// break and continue statements.
// //
// Care is taken with a coroutine call in a statement or expression. Arguments // When reconciling multiple code paths, the strongest wins, where strong >
// to the coroutine are 'inside' and are before the CSP. The rest of the // weak and weak > none. Loops are repeated until the reconciliations between
// expression is 'outside' and are after the CSP. // the N'th and N+1'th iteration are all no-ops: a steady state has been
// reached. There can be multiple reconciliations per iteration, due to break
// and continue statements.
import ( import (
"fmt" "fmt"
@@ -106,6 +105,10 @@ func (r livenesses) lowerWeakToNone(i int) {
} }
} }
func (r livenesses) raiseToStrong(i int) {
r[i] = livenessStrong
}
func (r livenesses) raiseWeakToStrong(i int) { func (r livenesses) raiseWeakToStrong(i int) {
if r[i] == livenessWeak { if r[i] == livenessWeak {
r[i] = livenessStrong r[i] = livenessStrong
@@ -227,7 +230,7 @@ loop:
func (h *livenessHelper) doAssign(r livenesses, n *a.Assign, depth uint32) error { func (h *livenessHelper) doAssign(r livenesses, n *a.Assign, depth uint32) error {
if n.Operator() == t.IDEqQuestion { if n.Operator() == t.IDEqQuestion {
if err := h.doExpr1(r, n.RHS(), 0); err != nil { if err := h.doExpr1(r, n.RHS(), false, 0); err != nil {
return err return err
} }
} else { } else {
@@ -261,7 +264,17 @@ func (h *livenessHelper) doAssign(r livenesses, n *a.Assign, depth uint32) error
} }
func (h *livenessHelper) doExpr(r livenesses, n *a.Expr) error { func (h *livenessHelper) doExpr(r livenesses, n *a.Expr) error {
if err := h.doExpr1(r, n, 0); err != nil { allToStrong := false
if n.Effect().Coroutine() {
method := n.LHS().AsExpr().Ident()
if (method == t.IDSkip) || (method == t.IDSkip32) {
// No-op. These methods already save their args across suspensions.
} else {
allToStrong = true
}
}
if err := h.doExpr1(r, n, allToStrong, 0); err != nil {
return err return err
} }
if n.Effect().Coroutine() { if n.Effect().Coroutine() {
@@ -270,7 +283,7 @@ func (h *livenessHelper) doExpr(r livenesses, n *a.Expr) error {
return nil return nil
} }
func (h *livenessHelper) doExpr1(r livenesses, n *a.Expr, depth uint32) error { func (h *livenessHelper) doExpr1(r livenesses, n *a.Expr, allToStrong bool, depth uint32) error {
if depth > a.MaxBodyDepth { if depth > a.MaxBodyDepth {
return fmt.Errorf("body recursion depth too large") return fmt.Errorf("body recursion depth too large")
} }
@@ -278,7 +291,7 @@ func (h *livenessHelper) doExpr1(r livenesses, n *a.Expr, depth uint32) error {
for _, o := range n.AsNode().AsRaw().SubNodes() { for _, o := range n.AsNode().AsRaw().SubNodes() {
if o != nil && o.Kind() == a.KExpr { if o != nil && o.Kind() == a.KExpr {
if err := h.doExpr1(r, o.AsExpr(), depth); err != nil { if err := h.doExpr1(r, o.AsExpr(), allToStrong, depth); err != nil {
return err return err
} }
} }
@@ -293,16 +306,20 @@ func (h *livenessHelper) doExpr1(r livenesses, n *a.Expr, depth uint32) error {
default: default:
return fmt.Errorf("unrecognized arg kind") return fmt.Errorf("unrecognized arg kind")
} }
if err := h.doExpr1(r, e, depth); err != nil { if err := h.doExpr1(r, e, allToStrong, depth); err != nil {
return err return err
} }
} }
if n.Operator() == 0 { if n.Operator() == 0 {
if i, ok := h.vars[n.Ident()]; ok { if i, ok := h.vars[n.Ident()]; ok {
if allToStrong {
r.raiseToStrong(i)
} else {
r.raiseWeakToStrong(i) r.raiseWeakToStrong(i)
} }
} }
}
return nil return nil
} }
+8
View File
@@ -5048,6 +5048,12 @@ struct wuffs_json__decoder__struct {
uint32_t p_decode_tokens[1]; uint32_t p_decode_tokens[1];
} private_impl; } private_impl;
struct {
struct {
uint64_t v_length;
} s_decode_tokens[1];
} private_data;
#ifdef __cplusplus #ifdef __cplusplus
#if (__cplusplus >= 201103L) && !defined(WUFFS_IMPLEMENTATION) #if (__cplusplus >= 201103L) && !defined(WUFFS_IMPLEMENTATION)
// Disallow constructing or copying an object via standard C++ mechanisms, // Disallow constructing or copying an object via standard C++ mechanisms,
@@ -18130,6 +18136,7 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self,
uint32_t coro_susp_point = self->private_impl.p_decode_tokens[0]; uint32_t coro_susp_point = self->private_impl.p_decode_tokens[0];
if (coro_susp_point) { if (coro_susp_point) {
v_length = self->private_data.s_decode_tokens[0].v_length;
} }
switch (coro_susp_point) { switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
@@ -18157,6 +18164,7 @@ suspend:
wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_impl.active_coroutine = self->private_impl.active_coroutine =
wuffs_base__status__is_suspension(&status) ? 1 : 0; wuffs_base__status__is_suspension(&status) ? 1 : 0;
self->private_data.s_decode_tokens[0].v_length = v_length;
goto exit; goto exit;
exit: exit: