package hardcaml

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file circuit_utilization.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
open! Import

module Total_bits = struct
  type t =
    { count : int
    ; total_bits : int
    }
  [@@deriving sexp_of]

  let none = { count = 0; total_bits = 0 }

  let add ?(u = none) s =
    Some { count = u.count + 1; total_bits = u.total_bits + Signal.width s }
  ;;
end

module Total_and_max_bits = struct
  type t =
    { count : int
    ; total_bits : int
    ; max_instance_bits : int
    }
  [@@deriving sexp_of]

  let none = { count = 0; total_bits = 0; max_instance_bits = 0 }

  let add ?(u = none) s =
    let w = Signal.width s in
    Some
      { count = u.count + 1
      ; total_bits = u.total_bits + w
      ; max_instance_bits = max u.max_instance_bits w
      }
  ;;
end

module Multiplexer = struct
  type t =
    { max_instance_bits : int
    ; total_bits : int
    ; count : int
    }
  [@@deriving sexp_of]
end

module Multiplexers = struct
  (* Index mutliplexers by their depth (ie numberof_data_elements) as this most directly
     effects timing and means we tend to print fewer instances. *)
  module Mux_map = struct
    type t = Multiplexer.t Map.M(Int).t

    let sexp_of_elt (number_of_data_elements, (m : Multiplexer.t)) =
      let max_instance_bits = m.max_instance_bits in
      let total_bits = m.total_bits in
      let count = m.count in
      [%message
        (number_of_data_elements : int)
          (max_instance_bits : int)
          (total_bits : int)
          (count : int)]
    ;;

    let sexp_of_t t = [%sexp (Map.to_alist t : elt list)]
  end

  type t =
    { count : int
    ; total_bits : int
    ; multiplexers : Mux_map.t
    }
  [@@deriving sexp_of]

  let none = { count = 0; total_bits = 0; multiplexers = Map.empty (module Int) }

  let add ?(u = none) s =
    let number_of_data_elements = List.length (Signal.deps s) - 1 in
    let data_width = Signal.width s in
    let key = number_of_data_elements in
    let total_bits = number_of_data_elements * data_width in
    Some
      { count = u.count + 1
      ; total_bits = u.total_bits + total_bits
      ; multiplexers =
          (match Map.find u.multiplexers key with
           | None ->
             Map.add_exn
               u.multiplexers
               ~key
               ~data:
                 { max_instance_bits = number_of_data_elements; total_bits; count = 1 }
           | Some data ->
             Map.set
               u.multiplexers
               ~key
               ~data:
                 { max_instance_bits = max data.max_instance_bits total_bits
                 ; total_bits = data.total_bits + total_bits
                 ; count = data.count + 1
                 })
      }
  ;;
end

module Memory = struct
  module T = struct
    type t =
      {
        data_width : int
      ; depth : int
      ; total_bits : int
      }
    [@@deriving sexp_of, compare]
  end

  include T
  include Comparable.Make (T)
end

module Memories = struct
  module Mem_map = struct
    type t = int Map.M(Memory).t

    let sexp_of_elt ({ Memory.data_width; depth; total_bits }, count) =
      [%message (data_width : int) (depth : int) (total_bits : int) (count : int)]
    ;;

    let sexp_of_t t = [%sexp (Map.to_alist t : elt list)]
  end

  type t =
    { count : int
    ; total_bits : int
    ; memories : Mem_map.t
    }
  [@@deriving sexp_of]

  let none = { count = 0; total_bits = 0; memories = Map.empty (module Memory) }

  let add ?(u = none) s =
    let data_width = Signal.width s in
    let depth =
      match s with
      | Mem { memory = m; _ } -> m.mem_size
      | Multiport_mem { size; _ } -> size
      | _ -> 0
    in
    let total_bits = depth * data_width in
    let key = { Memory.data_width; depth; total_bits } in
    Some
      { count = u.count + 1
      ; total_bits = u.total_bits + key.total_bits
      ; memories =
          (match Map.find u.memories key with
           | None -> Map.add_exn u.memories ~key ~data:1
           | Some count -> Map.set u.memories ~key ~data:(count + 1))
      }
  ;;
end

module rec Instantiation : sig
  type t =
    | Instantiation of string
    | Submodule of T.t
  [@@deriving sexp_of]
end = struct
  type t =
    | Instantiation of string
    | Submodule of T.t
  [@@deriving sexp_of]
end

and Instantiations : sig
  type t = Instantiation.t list [@@deriving sexp_of]

  val add : ?u:t -> Signal.t -> t option
end = struct
  type t = Instantiation.t list [@@deriving sexp_of]

  let none = []

  let add ?(u = none) s =
    let name =
      match (s : Signal.t) with
      | Inst { instantiation; _ } -> instantiation.inst_name
      | _ -> "<not an instantiation>"
    in
    Some (Instantiation.Instantiation name :: u)
  ;;
end

and T : sig
  type t =
    { name : string
    ; adders : Total_and_max_bits.t option
    ; subtractors : Total_and_max_bits.t option
    ; unsigned_multipliers : Total_and_max_bits.t option
    ; signed_multipliers : Total_and_max_bits.t option
    ; and_gates : Total_bits.t option
    ; or_gates : Total_bits.t option
    ; xor_gates : Total_bits.t option
    ; not_gates : Total_bits.t option
    ; equals : Total_and_max_bits.t option
    ; comparators : Total_and_max_bits.t option
    ; multiplexers : Multiplexers.t option
    ; registers : Total_bits.t option
    ; memories : Memories.t option (* the following do not generate gates. *)
    ; constants : Total_bits.t option
    ; wires : Total_bits.t option
    ; concatenation : Total_bits.t option
    ; part_selects : Total_bits.t option (* (recursive) sub modules. *)
    ; instantiations : Instantiations.t option
    }
  [@@deriving sexp_of]
end = struct
  type t =
    { name : string
    ; adders : Total_and_max_bits.t option [@sexp.option]
    ; subtractors : Total_and_max_bits.t option [@sexp.option]
    ; unsigned_multipliers : Total_and_max_bits.t option [@sexp.option]
    ; signed_multipliers : Total_and_max_bits.t option [@sexp.option]
    ; and_gates : Total_bits.t option [@sexp.option]
    ; or_gates : Total_bits.t option [@sexp.option]
    ; xor_gates : Total_bits.t option [@sexp.option]
    ; not_gates : Total_bits.t option [@sexp.option]
    ; equals : Total_and_max_bits.t option [@sexp.option]
    ; comparators : Total_and_max_bits.t option [@sexp.option]
    ; multiplexers : Multiplexers.t option [@sexp.option]
    ; registers : Total_bits.t option [@sexp.option]
    ; memories : Memories.t option [@sexp.option]
    ; constants : Total_bits.t option [@sexp.option]
    ; wires : Total_bits.t option [@sexp.option]
    ; concatenation : Total_bits.t option [@sexp.option]
    ; part_selects : Total_bits.t option [@sexp.option]
    ; instantiations : Instantiations.t option [@sexp.option]
    }
  [@@deriving sexp_of]
end

type t = T.t [@@deriving sexp_of]

let rec create ?database circuit =
  let expand_submodules t =
    match database with
    | None -> t
    | Some database ->
      { t with
        T.instantiations =
          Option.map t.T.instantiations ~f:(fun instantiations ->
            List.map instantiations ~f:(function
              | Instantiation.Instantiation name as inst ->
                (match Circuit_database.find database ~mangled_name:name with
                 | None -> inst
                 | Some circuit -> Instantiation.Submodule (create ~database circuit))
              | Instantiation.Submodule _ as x -> x))
      }
  in
  Signal_graph.fold
    (Circuit.signal_graph circuit)
    ~init:
      { T.name = Circuit.name circuit
      ; constants = None
      ; adders = None
      ; subtractors = None
      ; unsigned_multipliers = None
      ; signed_multipliers = None
      ; and_gates = None
      ; or_gates = None
      ; xor_gates = None
      ; not_gates = None
      ; equals = None
      ; comparators = None
      ; concatenation = None
      ; multiplexers = None
      ; part_selects = None
      ; wires = None
      ; registers = None
      ; memories = None
      ; instantiations = None
      }
    ~f:(fun utilization signal ->
      match (signal : Signal.t) with
      | Empty -> utilization
      | Const _ ->
        { utilization with constants = Total_bits.add ?u:utilization.constants signal }
      | Wire _ -> { utilization with wires = Total_bits.add ?u:utilization.wires signal }
      | Select _ ->
        { utilization with
          part_selects = Total_bits.add ?u:utilization.part_selects signal
        }
      | Reg _ ->
        { utilization with registers = Total_bits.add ?u:utilization.registers signal }
      | Mem _ | Multiport_mem _ ->
        { utilization with memories = Memories.add ?u:utilization.memories signal }
      | Mem_read_port _ -> utilization
      | Inst _ ->
        { utilization with
          instantiations = Instantiations.add ?u:utilization.instantiations signal
        }
      | Not _ ->
        { utilization with not_gates = Total_bits.add ?u:utilization.not_gates signal }
      | Mux _ ->
        { utilization with
          multiplexers = Multiplexers.add ?u:utilization.multiplexers signal
        }
      | Cat _ ->
        { utilization with
          concatenation = Total_bits.add ?u:utilization.concatenation signal
        }
      | Op2 { op; _ } ->
        (match (op : Signal.signal_op) with
         | Signal_add ->
           { utilization with
             adders = Total_and_max_bits.add ?u:utilization.adders signal
           }
         | Signal_sub ->
           { utilization with
             subtractors = Total_and_max_bits.add ?u:utilization.subtractors signal
           }
         | Signal_mulu ->
           { utilization with
             unsigned_multipliers =
               Total_and_max_bits.add ?u:utilization.unsigned_multipliers signal
           }
         | Signal_muls ->
           { utilization with
             signed_multipliers =
               Total_and_max_bits.add ?u:utilization.signed_multipliers signal
           }
         | Signal_and ->
           { utilization with and_gates = Total_bits.add ?u:utilization.and_gates signal }
         | Signal_or ->
           { utilization with or_gates = Total_bits.add ?u:utilization.or_gates signal }
         | Signal_xor ->
           { utilization with xor_gates = Total_bits.add ?u:utilization.xor_gates signal }
         | Signal_eq ->
           { utilization with
             equals = Total_and_max_bits.add ?u:utilization.equals signal
           }
         | Signal_lt ->
           { utilization with
             comparators = Total_and_max_bits.add ?u:utilization.comparators signal
           }))
  |> expand_submodules
;;
OCaml

Innovation. Community. Security.