Module Low_level.Scope_order

gh-ocannl-2213: the order a statement's scope definitions are emitted in, in one place for the renderer and the analyses. C_syntax.pp_local_defs renders plan of the entries its pp_scalar collects; Access_fold's Emitted mode walks order of plan of the entries entries derives from the IR. The two derivations meet the same backend facts through an oracle, and the renderer asserts on every statement it renders that they agree (C_syntax.pp_local_defs' consistency check), so they cannot drift silently.

The emission order (gh-ocannl-2145, gh-ocannl-1895): the unconditional definitions, ascending by scope id except that a definition follows the sibling definitions its body reads; then the conditional ones' zero-initialized declarations, ascending; then the conditional bodies in one such dependency order, consecutive ones under the same conditions sharing one if. A definition runs under the disjunction of the conditions of its occurrences, of the reads of its local at operand position and, to a fixpoint, of the definitions reading it.

The renderer partitions its conditions by their rendered tests (plan's same); the derivation keys them by IR, and the renderer's check holds the two to one partition (same_plan). The IR key: the condition and the precision it renders at for a short-circuiting Where (Select); the operator, its first operand, the precision that operand renders at and the operator's evaluation precision for a short-circuiting gate (Gate, whose test is that the second operand matters: at an integer compute precision the operand renders at the integer precision and the test at a float one). Float constants compare by their bits. Two conditions keyed alike are one value: a condition reads no scope local (the cond_defs rule of arms), and the bodies run before it write only their own locals.

type key =
  1. | Select of {
    1. cond : scalar_t;
    2. prec : Ops.prec;
    3. unconverted : Base.bool;
    }
    (*

    unconverted: a float-valued operator condition at an integer compute precision, which C_syntax's integer_op renders as it stands where the float path converts it to prec; any other renders alike on both paths.

    *)
  2. | Gate of {
    1. op : Ops.binop;
    2. prec : Ops.prec;
    3. first : scalar_t;
    4. first_prec : Ops.prec;
    }
val equal_key : ?comp_prec:(Ops.prec -> Ops.prec) -> key -> key -> Base.bool

Normalize heterogeneous operand annotations through the backend's comp_prec (identity by default). Projection-discarded operands are ignored; Round_prec keeps its exact target.

type 'p lit = {
  1. key : key;
  2. holds : Base.bool;
  3. anywhere : Base.bool;
  4. payload : 'p;
}

One condition an occurrence is evaluated under: holds in the then-arm, not in the else-arm. anywhere: the condition can be evaluated in any iteration -- it reads no memory and no local, and divides nothing. payload is the caller's (the renderer's rendered test); equality ignores it.

type ('p, 'd) entry = {
  1. scope : Base.int;
  2. guard : 'p lit Base.list;
  3. def : 'd Base.option;
}

A scope's definition (def = Some _) or, with def = None, a read of its local at operand position, under guard, outermost first.

val same_cond : 'p lit -> 'q lit -> Base.bool

Whether two literals test one condition, polarity aside: equal keys.

val equal_lit : ?same:('p lit -> 'p lit -> Base.bool) -> 'p lit -> 'p lit -> Base.bool
val evaluable_anywhere : scalar_t -> Base.bool
val arms : key:key -> payload:'p -> v1:scalar_t -> cond_defs:('p, 'd) entry Base.list -> ('p, 'd) entry Base.list -> ('p, 'd) entry Base.list -> ('p, 'd) entry Base.list

The entries of a short-circuiting Where's arms, under its condition v1 (keyed key), whose own entries are cond_defs. A condition reading a scope local guards nothing: the arms keep only their conditions evaluable anywhere.

val gate : key:key -> payload:(Base.unit -> 'p) -> v1:scalar_t -> cond_defs:('p, 'd) entry Base.list -> ('p, 'd) entry Base.list -> ('p, 'd) entry Base.list

The entries of a short-circuiting gate's second operand: its definitions that run under a Where's condition keep that condition behind the gate's test.

type ('p, 'd) plan = {
  1. unconditional : (Base.int * 'd) Base.list;
    (*

    In emission order.

    *)
  2. conditional : (Base.int * 'd) Base.list;
    (*

    Ascending: the order of their zeroed declarations.

    *)
  3. groups : ('p lit Base.list Base.list * (Base.int * 'd) Base.list) Base.list;
    (*

    In emission order: each group's conditions (a disjunction of conjunctions) and its definitions.

    *)
}
val plan : ?same:('p lit -> 'p lit -> Base.bool) -> reads:('d -> Base.int Base.list) -> ('p, 'd) entry Base.list -> ('p, 'd) plan

same (default same_cond) tells which literals test one condition. The renderer passes equality of their rendered tests: what it emits is then partitioned by what it spells, and same_plan checks the IR-keyed derivation against it.

val order : ('p, 'd) plan -> (Base.int * Access_fold.placement) Base.list
val same_plan : ?same_a:('p lit -> 'p lit -> Base.bool) -> ?same_b:('q lit -> 'q lit -> Base.bool) -> ('p, 'd) plan -> ('q, 'e) plan -> Base.bool

Equal orders, placements and declaration orders, and groups of the same shape and polarities whose literals are one condition under same_a exactly where they are under same_b.

type oracle = {
  1. short_circuits : prec:Ops.prec -> scalar_t -> Base.bool;
    (*

    Whether a Where or a Relu_gate / Satur01_gate node rendered at prec short-circuits rather than blending its arms (the renderer's blend_for); true at every integer precision.

    *)
  2. comp_prec : Ops.prec -> Ops.prec;
  3. int_math_prec : Ops.prec;
    (*

    The float precision a float-valued subexpression at an integer compute precision is computed in.

    *)
  4. root_precs : t -> Ops.prec Base.list;
    (*

    The precision each scalar of a rendering unit renders at, in Access_fold's root order: a Set_dynamic's index, then its value.

    *)
}

The backend facts the emission order reads: C_syntax's scope_order_oracle.

val entries : oracle -> Ops.prec -> scalar_t -> (Base.unit, Base.int Base.list Base.Lazy.t) entry Base.list

The entries of a scalar rendered at a precision: the renderer's own descent.

val statement_entries : oracle -> (Ops.prec * scalar_t) Base.list -> (Base.unit, Base.int Base.list Base.Lazy.t) entry Base.list
val derived_plan : oracle -> (Ops.prec * scalar_t) Base.list -> (Base.unit, Base.int Base.list Base.Lazy.t) plan
val emission : oracle -> Access_fold.emission

Access_fold's Emitted mode under oracle.