File size: 11,305 Bytes
784ee2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#
# Extracts and returns module names from Verilog file
#
proc get_module_names { file_path } {
  set module_list [list]
  if { [catch { set fid [open $file_path r] } err] } {
    error "Failed to open file $file_path: $err"
  }

  set regex {^[ \t]*module[ \t]+([A-Za-z_$][A-Za-z0-9_$]*)}

  while { [gets $fid line] >= 0 } {
    if { [regexp -nocase $regex $line match_all module_name] } {
      lappend module_list $module_name
    }
  }

  close $fid
  return $module_list
}

#
# Builds dfflegalize arg list
#
proc get_dfflegalize_args { file_path } {
  set legalize_args [list]
  set module_names [get_module_names $file_path]
  foreach module_name $module_names {
    lappend legalize_args -cell $module_name x
  }
  return $legalize_args
}

source $::env(SCRIPTS_DIR)/synth_preamble.tcl
if { [env_var_exists_and_non_empty SYNTH_CHECKPOINT] } {
  read_checkpoint $::env(SYNTH_CHECKPOINT)
} else {
  read_checkpoint $::env(RESULTS_DIR)/1_1_yosys_canonicalize.rtlil
}

# When this synthesis run is one partition of a parallel split (driven by
# an external orchestrator), `SYNTH_BLACKBOXES` lists modules outside this
# partition.  Blackboxing them before the hierarchy check lets each
# partition load the same canonical RTLIL checkpoint while only synthesising
# its own subhierarchy.  Names not present in the loaded design are skipped
# silently so the same list can be passed to every partition.
#
# This deliberately differs from the SYNTH_BLACKBOXES handling in
# synth_preamble.tcl's `read_design_sources`, and the difference is correct
# in both places — do not "harmonise" the two:
#   * Order: here the design is already elaborated (read from RTLIL), so
#     `blackbox` operates on resolved modules and must run before the
#     hierarchy check.  In synth_preamble.tcl the verilog frontend uses
#     `read_verilog -defer`, so `hierarchy -check -top` must run first to
#     elaborate from the top before `blackbox` sees a populated module table.
#   * Catch: here a missing name is expected because the same list is
#     reused across partitions, and only this partition's portion exists in
#     the checkpoint.  In synth_preamble.tcl a single design is being
#     synthesised, so an unknown name is almost certainly a user typo and
#     should fail loudly — `blackbox $m` without `catch` is intentional.
if { [env_var_exists_and_non_empty SYNTH_BLACKBOXES] } {
  foreach m $::env(SYNTH_BLACKBOXES) {
    catch { blackbox $m }
  }
}

hierarchy -check -top $::env(DESIGN_NAME)

if { $::env(SYNTH_GUT) } {
  # /deletes all cells at the top level, which will quickly optimize away
  # everything else, including macros.
  delete $::env(DESIGN_NAME)/c:*
}

if { [env_var_exists_and_non_empty SYNTH_KEEP_MODULES] } {
  foreach module $::env(SYNTH_KEEP_MODULES) {
    # Two patterns so both frontends work:
    #  - `$module` matches the bare name produced by verilog.
    #  - `$module\$*` matches the `$`-suffixed canonical names the
    #    slang frontend generates for parameterized instances
    #    (e.g. `\foo$1`); yosys's match_ids retries the pattern with
    #    the id's leading `\` stripped, so no `\`-prefix is needed
    #    here -- see tools/yosys/passes/cmds/select.cc match_ids().
    # Multiple patterns on one `select` are unioned at the end of the
    # command (select.cc: `while (work_stack.size() > 1) union`), so
    # when a pattern matches nothing it degrades to a warning and the
    # other pattern still applies -- no regression for non-slang.
    # `-module <name>` would error if the module doesn't exist, which
    # is why we use bare patterns instead.
    select "$module" "$module\\\$*"
    setattr -mod -set keep_hierarchy 1
    select -clear
  }
}

if { [env_var_exists_and_non_empty SYNTH_HIER_SEPARATOR] } {
  scratchpad -set flatten.separator $::env(SYNTH_HIER_SEPARATOR)
}

set synth_full_args [env_var_or_empty SYNTH_ARGS]
if { [env_var_exists_and_non_empty SYNTH_OPERATIONS_ARGS] } {
  set synth_full_args [concat $synth_full_args $::env(SYNTH_OPERATIONS_ARGS)]
} else {
  set synth_full_args [concat $synth_full_args \
    "-extra-map $::env(FLOW_HOME)/platforms/common/lcu_kogge_stone.v"]
}
if { [env_var_exists_and_non_empty SYNTH_OPT_HIER] } {
  set synth_full_args [concat $synth_full_args -hieropt]
}

if {
  [env_var_exists_and_non_empty SYNTH_CHECKPOINT] &&
  $::env(SYNTH_SKIP_KEEP)
} {
  # Partition mode where the checkpoint is still canonical RTLIL (the keep
  # decision for this partition is driven externally). Run the full
  # coarse+fine synthesis, flattened.
  synth -flatten -run :fine {*}$synth_full_args
} elseif { [env_var_exists_and_non_empty SYNTH_CHECKPOINT] } {
  # Partition mode where the checkpoint already holds coarse synth +
  # keep_hierarchy output. Just flatten and continue from coarse.
  synth -flatten -run coarse:fine {*}$synth_full_args
} elseif { !$::env(SYNTH_HIERARCHICAL) } {
  # Perform standard coarse-level synthesis script, flatten right away
  synth -flatten -run :fine {*}$synth_full_args
} else {
  # Perform standard coarse-level synthesis script,
  # defer flattening until we have decided what hierarchy to keep
  synth -run :fine

  if { [env_var_exists_and_non_empty SYNTH_MINIMUM_KEEP_SIZE] } {
    set ungroup_threshold $::env(SYNTH_MINIMUM_KEEP_SIZE)
    puts "Keep modules above estimated size of
      $ungroup_threshold gate equivalents"

    convert_liberty_areas
    keep_hierarchy -min_cost $ungroup_threshold
  } else {
    keep_hierarchy
  }

  # Re-run coarse-level script, this time do pass -flatten
  synth -flatten -run coarse:fine {*}$synth_full_args
}


if { $::env(SYNTH_MOCK_LARGE_MEMORIES) } {
  memory_collect
  set select [tee -q -s result.string select -list t:\$mem_v2]
  set report_file [open $::env(REPORTS_DIR)/synth_mocked_memories.txt "w"]
  foreach path [split [string trim $select] "\n"] {
    set index [string first "/" $path]
    set module [string range $path 0 [expr { $index - 1 }]]
    set instance [string range $path [expr { $index + 1 }] end]

    set width [rtlil::get_param -uint $module $instance WIDTH]
    set size [rtlil::get_param -uint $module $instance SIZE]
    set nbits [expr $width * $size]
    puts "Memory $path has dimensions $size x $width = $nbits"
    if { $nbits > $::env(SYNTH_MEMORY_MAX_BITS) } {
      rtlil::set_param -uint $module $instance SIZE 1
      puts "Shrunk memory $path from $size rows to 1"
      puts -nonewline $report_file "$module:\n  width: $width\n  size: $size\n"
      if { $::env(SYNTH_KEEP_MOCKED_MEMORIES) } {
        select -module $module
        setattr -mod -set keep_hierarchy 1
        select -clear
      }
    }
  }
  close $report_file
}

json -o $::env(RESULTS_DIR)/mem.json
# Run report and check here so as to fail early if this synthesis run is doomed
exec -- $::env(PYTHON_EXE) $::env(SCRIPTS_DIR)/mem_dump.py \
  --max-bits $::env(SYNTH_MEMORY_MAX_BITS) $::env(RESULTS_DIR)/mem.json

if { [env_var_exists_and_non_empty SYNTH_RETIME_MODULES] } {
  select {*}$::env(SYNTH_RETIME_MODULES)
  opt -fast -full
  memory_map
  opt -full
  techmap
  abc -dff -script $::env(SCRIPTS_DIR)/abc_retime.script
  select -clear
}

if {
  [env_var_exists_and_non_empty SYNTH_WRAPPED_OPERATORS] ||
  [env_var_exists_and_non_empty SWAP_ARITH_OPERATORS]
} {
  source $::env(SCRIPTS_DIR)/synth_wrap_operators.tcl
} else {
  synth -top $::env(DESIGN_NAME) -run fine: -noabc {*}$synth_full_args
}

# Get rid of indigestibles
chformal -remove
delete t:\$print

# rename registers to have the verilog register name in its name
# of the form \regName$_DFF_P_. We should fix yosys to make it the reg name.
# At least this is predictable.
renames -wire

# Optimize the design
opt -purge

# Technology mapping of adders
if {
  [env_var_exists_and_non_empty ADDER_MAP_FILE] &&
  (
    (![env_var_exists_and_non_empty SYNTH_WRAPPED_OPERATORS] &&
      ![env_var_exists_and_non_empty SWAP_ARITH_OPERATORS]) ||
    (([env_var_exists_and_non_empty SYNTH_WRAPPED_OPERATORS] ||
        [env_var_exists_and_non_empty SWAP_ARITH_OPERATORS]) &&
      ![design_has_extracted_operators])
  )
} {
  # extract the full adders
  extract_fa
  # map full adders
  techmap -map $::env(ADDER_MAP_FILE)
  techmap
  # Quick optimization
  opt -fast -purge
}

# Technology mapping of latches
if { [env_var_exists_and_non_empty LATCH_MAP_FILE] } {
  # Legalize async set/reset latches into the latches this map file provides
  # (soft-logic emulation); the trailing selection keeps dfflegalize off FFs.
  dfflegalize {*}[get_dfflegalize_args $::env(LATCH_MAP_FILE)] {t:$_DLATCH_*}
  techmap -map $::env(LATCH_MAP_FILE)
}

# Clock gate inference.
if { [env_var_equals INFER_CLKGATES 1] } {
  set clkgate_args {}
  if {
    [env_var_exists_and_non_empty POS_CLKGATE_AND_PORTS] ||
    [env_var_exists_and_non_empty NEG_CLKGATE_AND_PORTS]
  } {
    if { [env_var_exists_and_non_empty POS_CLKGATE_AND_PORTS] } {
      lappend clkgate_args "-pos"
      lappend clkgate_args {*}$::env(POS_CLKGATE_AND_PORTS)
    }
    if { [env_var_exists_and_non_empty NEG_CLKGATE_AND_PORTS] } {
      lappend clkgate_args "-neg"
      lappend clkgate_args {*}$::env(NEG_CLKGATE_AND_PORTS)
    }
  } else {
    # Let yosys decide on which clock gating cell to use
    lappend clkgate_args {*}$lib_args
  }
  log_cmd clockgate {*}$clkgate_args
}

# Technology mapping of flip-flops
# dfflibmap only supports one liberty file
if { [env_var_exists_and_non_empty DFF_LIB_FILE] } {
  dfflibmap -liberty $::env(DFF_LIB_FILE) {*}$lib_dont_use_args
} elseif { [env_var_exists_and_non_empty DFF_MAP_FILE] } {
  set legalize_args [get_dfflegalize_args $::env(DFF_MAP_FILE)]
  dfflegalize {*}$legalize_args
  techmap -map $::env(DFF_MAP_FILE)
} else {
  dfflibmap {*}$lib_args {*}$lib_dont_use_args
}
opt

# Replace undef values with defined constants
setundef -zero

if {
  ![env_var_exists_and_non_empty SYNTH_WRAPPED_OPERATORS] &&
  ![env_var_exists_and_non_empty SWAP_ARITH_OPERATORS]
} {
  log_cmd abc {*}$abc_args
} else {
  scratchpad -set abc9.script $::env(SCRIPTS_DIR)/abc_speed_gia_only.script
  # crop out -script from arguments
  set abc_args [lrange $abc_args 2 end]
  log_cmd abc_new {*}$abc_args
  delete {t:$specify*}
}

# Splitting nets resolves unwanted compound assign statements in
# netlist (assign {..} = {..})
splitnets

# Remove unused cells and wires
opt_clean -purge

# Technology mapping of constant hi- and/or lo-drivers
hilomap -singleton \
  -hicell {*}$::env(TIEHI_CELL_AND_PORT) \
  -locell {*}$::env(TIELO_CELL_AND_PORT)

if { $::env(SYNTH_INSBUF) } {
  # Insert buffer cells for pass through wires
  insbuf -buf {*}$::env(MIN_BUF_CELL_AND_PORTS)
}

# Reports
tee -o $::env(REPORTS_DIR)/synth_check.txt check

tee -o $::env(REPORTS_DIR)/synth_stat.txt stat -hierarchy {*}$lib_args

# check the design is composed exclusively of target cells, and
# check for other problems
if {
  ![env_var_exists_and_non_empty SYNTH_WRAPPED_OPERATORS] &&
  ![env_var_exists_and_non_empty SWAP_ARITH_OPERATORS]
} {
  check -assert -mapped
} else {
  # Wrapped operator synthesis leaves around $buf cells which `check -mapped`
  # gets confused by, once Yosys#4931 is merged we can remove this branch and
  # always run `check -assert -mapped`
  check -assert
}

# Write synthesized design
write_verilog -nohex -nodec $::env(RESULTS_DIR)/1_2_yosys.v