Source file always_coverage.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
open! Core0
include Always_coverage_intf
module Type = Signal.Type

module Make (Prim : Always_prim.S) = struct
  open Prim

  let signal_type s = Signal.to_rep s |> fst

  let set_variable_metadata signal metadata =
    Type.update_coverage_metadata (signal_type signal) ~f:(fun m ->
      Coverage_metadata.set_kind m (Variable metadata))
  ;;

  let target_metadata (target : Variable.t) =
    { Coverage_prim.Always_metadata.Target.variable = target.internal.debug_info
    ; names = Signal.names_and_locs target.value
    }
  ;;

  let set_if_metadata signal (internal : If_internal.t) ~target =
    let%tydi { creation_pos; kind } = internal in
    Type.update_coverage_metadata (signal_type signal) ~f:(fun m ->
      Coverage_metadata.set_kind
        m
        (match kind with
         | Condition kind -> If { creation_pos; target = target_metadata target; kind }
         | One_hot_switch { state; _ } ->
           Switch_mux
             { creation_pos; target = target_metadata target; case = State state }))
  ;;

  let set_switch_cases_metadata signal (switch_internal : Switch_internal.t) cases ~target
    =
    let%tydi { creation_pos; states } = switch_internal in
    let cases =
      let non_default_cases =
        List.map cases ~f:(fun { internal; _ } ->
          match internal with
          | Basic -> `Positional
          | Always_state state -> `State state)
      in
      let default_states =
        List.fold
          non_default_cases
          ~init:(Set.of_list (module Coverage_prim.State.Named) states)
          ~f:(fun states c ->
            match c with
            | `Positional -> states
            | `State state -> Set.remove states state)
        |> Set.to_list
      in
      Coverage_prim.Cases.create ~non_default_cases ~default_states
    in
    Type.update_coverage_metadata (signal_type signal) ~f:(fun m ->
      Coverage_metadata.set_kind
        m
        (Switch_cases { creation_pos; target = target_metadata target; cases }))
  ;;

  let set_switch_mux_metadata
    signal
    (switch_internal : Switch_internal.t)
    (match_internal : Match_internal.t)
    ~target
    ~position
    =
    let%tydi { creation_pos; _ } = switch_internal in
    let case : Coverage_prim.Case.t =
      match match_internal with
      | Basic -> Positional position
      | Always_state state -> State state
    in
    Type.update_coverage_metadata (signal_type signal) ~f:(fun m ->
      Coverage_metadata.set_kind
        m
        (Switch_mux { creation_pos; target = target_metadata target; case }))
  ;;

  (* Module for computing cycle level state transitions from an always DSL. Transitions
     are tracked by walking the statements and computing the following.

     Possible starting states.

     Switches and if/else blocks restrict this set, but variable assignments do not, as we
     only care about transitions from the start to the end of the cycle and not in
     between. There is some complexity in this tracking to handle both restricted subsets
     of all possible starting states and explicitly known states.

     Possible ending states.

     We need to know all states that can be produced at the end of a cycle so that we can
     accurately compute the full set of possible starting states.

     Possible transitions.

     Conditional logic restricts which transitions can actually take place out of the
     possible starting and ending states, so we track these directly. We count no change
     to the starting state as its own 'transition'. At an assignment site we may not know
     the concrete set of possible starting states, as it maybe a restricted subset of the
     all possible starting state. We allow this in the transition type, and resolve it
     after we have walked all the statements.
  *)
  module Transition_tracking = struct
    module Transition = struct
      type t =
        | No_change of [ `Initial_excluding of Set.M(Int).t | `Value of int ]
        | Change of ([ `Initial_excluding of Set.M(Int).t | `Value of int ] * int)
      [@@deriving sexp_of]
    end

    module Starting_states = struct
      type t =
        { values : Set.M(Int).t
        ; initial : [ `No | `Excluding of Set.M(Int).t ]
        }
      [@@deriving sexp_of]

      let create () =
        { values = Set.empty (module Int); initial = `Excluding (Set.empty (module Int)) }
      ;;

      let of_value value = { values = Set.singleton (module Int) value; initial = `No }

      let remove_value t value =
        { values = Set.remove t.values value
        ; initial =
            (match t.initial with
             | `No -> `No
             | `Excluding set -> `Excluding (Set.add set value))
        }
      ;;

      let join a b =
        let values = Set.union a.values b.values in
        let initial =
          match a.initial, b.initial with
          | `No, `No -> `No
          | `No, `Excluding set | `Excluding set, `No -> `Excluding set
          | `Excluding s1, `Excluding s2 -> `Excluding (Set.inter s1 s2)
        in
        { values; initial }
      ;;
    end

    module Linked_list = Core.Doubly_linked

    type t =
      { possible_starting_states : Starting_states.t
      ; possible_ending_states : Set.M(Int).t
      ; transitions :
          (* The list of transitions can be large and we need to combine lists of them
             together frequently, so store them as a linked list *)
          Transition.t Linked_list.t
      }
    [@@deriving sexp_of]

    let create () =
      { possible_starting_states = Starting_states.create ()
      ; possible_ending_states = Set.empty (module Int)
      ; transitions = Linked_list.create ()
      }
    ;;

    let set_input_value t value =
      { t with possible_starting_states = Starting_states.of_value value }
    ;;

    let remove_input_value t value =
      { t with
        possible_starting_states =
          Starting_states.remove_value t.possible_starting_states value
      }
    ;;

    let insert_last linked a =
      ignore (Linked_list.insert_last linked a : Transition.t Linked_list.Elt.t)
    ;;

    let maybe_mark_assign t (metadata : Coverage_prim.State.Named.t option) =
      match metadata with
      | None ->
        (* We can't mark an assignment if we know which state is being assigned *)
        t
      | Some { value = new_value; _ } ->
        (* When we see an assignment, mark a transition from each of the possible starting
           states to the assigned state. Override any ending states with the assigned
           state, as we only care about the *last* assignment to happen. *)
        let transitions = Linked_list.create () in
        Set.iter t.possible_starting_states.values ~f:(fun value ->
          insert_last transitions (Transition.Change (`Value value, new_value)));
        (match t.possible_starting_states.initial with
         | `No -> ()
         | `Excluding set ->
           insert_last transitions (Change (`Initial_excluding set, new_value)));
        { t with
          transitions
        ; possible_ending_states = Set.singleton (module Int) new_value
        }
    ;;

    let maybe_mark_no_change t =
      (* If we've reached the bottom of a statement tree an there were no transitions,
         mark a no-change 'transition' for each of the possible starting states. *)
      if Linked_list.length t.transitions = 0
      then (
        let transitions = Linked_list.create () in
        Set.iter t.possible_starting_states.values ~f:(fun value ->
          insert_last transitions (Transition.No_change (`Value value)));
        (match t.possible_starting_states.initial with
         | `No -> ()
         | `Excluding set -> insert_last transitions (No_change (`Initial_excluding set)));
        { t with transitions; possible_ending_states = t.possible_starting_states.values })
      else t
    ;;

    let any_of t ts =
      (* When joining statement trees together (both sides of a if or arms of a case),
         combine all possible starting states, transitions, and ending states. *)
      List.fold
        ts
        ~init:t
        ~f:(fun acc { possible_starting_states; possible_ending_states; transitions } ->
          let joined_transitions = Linked_list.create () in
          Linked_list.transfer ~dst:joined_transitions ~src:acc.transitions;
          Linked_list.transfer ~dst:joined_transitions ~src:transitions;
          { possible_starting_states =
              Starting_states.join acc.possible_starting_states possible_starting_states
          ; possible_ending_states =
              Set.union acc.possible_ending_states possible_ending_states
          ; transitions = joined_transitions
          })
    ;;

    let is_target s ~target =
      Type.(Type.Uid.equal (uid (signal_type s)) (uid (signal_type target)))
    ;;

    let rec compute_transitions statements t ~target =
      match statements with
      | [] -> maybe_mark_no_change t
      | statement :: statements ->
        let t =
          match statement with
          | If { value = _, true_, false_; internal = { kind; _ } } ->
            let true_t, false_t =
              match kind with
              | Condition _ -> t, t
              | One_hot_switch { var; state = { value; _ } } ->
                if is_target var.value ~target
                then set_input_value t value, remove_input_value t value
                else t, t
            in
            let t = compute_transitions true_ true_t ~target in
            let f = compute_transitions false_ false_t ~target in
            any_of t [ f ]
          | Assign { value = { value = signal; _ }, _; internal = { state_metadata } } ->
            if is_target signal ~target then maybe_mark_assign t state_metadata else t
          | Switch { value = sel, cases; _ } ->
            let case_ts, default_t =
              List.fold
                cases
                ~init:([], t)
                ~f:(fun (case_ts, default_t) { value = _, always; internal } ->
                  let case_t, default_t =
                    match internal with
                    | Basic -> t, t
                    | Always_state { value; _ } ->
                      if is_target sel ~target
                      then set_input_value t value, remove_input_value default_t value
                      else t, t
                  in
                  let case_t = compute_transitions always case_t ~target in
                  case_t :: case_ts, default_t)
            in
            let default_t = compute_transitions [] default_t ~target in
            any_of default_t case_ts
        in
        compute_transitions statements t ~target
    ;;

    let mark_transitions
      t
      (state_reg : Coverage_prim.Always_metadata.Variable.State_reg.t)
      =
      let%tydi { transitions_by_value; initial_value; _ } = state_reg in
      let starting_states = Set.add t.possible_ending_states initial_value in
      let value_to_set v =
        match v with
        | `Initial_excluding excluding -> Set.diff starting_states excluding
        | `Value value -> Set.singleton (module Int) value
      in
      let add_transition ~from ~to_ =
        let prev_set =
          Hashtbl.update_and_return transitions_by_value from ~f:(function
            | None -> Hash_set.create (module Int)
            | Some set -> set)
        in
        Hash_set.add prev_set to_
      in
      Linked_list.iter t.transitions ~f:(function
        | No_change from_value ->
          Set.iter (value_to_set from_value) ~f:(fun from ->
            add_transition ~from ~to_:from)
        | Change (from_value, to_) ->
          Set.iter (value_to_set from_value) ~f:(fun from -> add_transition ~from ~to_))
    ;;

    let maybe_set_transitions_metadata statements ~target =
      (* Transition metadata is only used for Cyclesim coverage and can be expensive to
         compute, so skip it if we can. *)
      if Coverage_global_config.coverage_enabled ()
      then (
        match Type.coverage_metadata (signal_type target) with
        | Some { kind = Some (Variable (State_machine_state state_reg)); _ } ->
          let t = compute_transitions statements (create ()) ~target in
          mark_transitions t state_reg
        | _ -> ())
    ;;
  end

  let maybe_set_transitions_metadata = Transition_tracking.maybe_set_transitions_metadata
end