Repository navigation
Expand file tree
/
Copy pathutil.tcl
More file actions
513 lines (471 loc) · 16.5 KB
/
Copy pathutil.tcl
File metadata and controls
513 lines (471 loc) · 16.5 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# Extract cell names
proc get_liberty_cell_names { } {
return [tee -q -s result.string select -list-mod =A:liberty_cell]
}
proc log_cmd { cmd args } {
# log the command, escape arguments with spaces
set log_cmd "$cmd[join [lmap arg $args { format " %s" [expr { [string match {* *} $arg] ? "\"$arg\"" : "$arg" }] }] ""]" ;# tclint-disable-line line-length
puts $log_cmd
# Tcl's `clock` lives in clock.tcl, auto-loaded from TCL_LIBRARY. Hermetic
# build environments (e.g. Bazel linking yosys against @tcl_lang) don't
# always ship the Tcl library in runfiles, so `clock` may be undefined.
# The timing log is cosmetic — skip it when `clock` isn't available.
set has_clock [expr { [info commands clock] ne "" }]
if { $has_clock } {
set start [clock seconds]
}
set result [uplevel 1 [list $cmd {*}$args]]
if { $has_clock } {
set time [expr { [clock seconds] - $start }]
if { $time >= 5 } {
# Ideally we'd use a single line, but the command can output text
# and we don't want to mix it with the log, so output the time it took afterwards.
puts "Took $time seconds: $log_cmd"
}
}
return $result
}
proc repair_timing_helper { args } {
set additional_args {}
append_env_var additional_args SETUP_SLACK_MARGIN -setup_margin 1
append_env_var additional_args HOLD_SLACK_MARGIN -hold_margin 1
append_env_var additional_args SETUP_MOVE_SEQUENCE -sequence 1
append_env_var additional_args TNS_END_PERCENT -repair_tns 1
append_env_var additional_args SKIP_PIN_SWAP -skip_pin_swap 0
append_env_var additional_args SKIP_GATE_CLONING -skip_gate_cloning 0
append_env_var additional_args SKIP_BUFFER_REMOVAL -skip_buffer_removal 0
append_env_var additional_args SKIP_LAST_GASP -skip_last_gasp 0
append_env_var additional_args SKIP_VT_SWAP -skip_vt_swap 0
append_env_var additional_args SKIP_CRIT_VT_SWAP -skip_crit_vt_swap 0
append_env_var additional_args MATCH_CELL_FOOTPRINT -match_cell_footprint 0
lappend additional_args {*}$args -verbose
log_cmd repair_timing {*}$additional_args
}
# Every detailed_placement in the flow goes through here, so the design's
# DETAIL_PLACEMENT_ARGS (the legalizer and its window, say) apply to the
# placement stage and to every re-legalization after it alike.
proc detailed_placement_helper { args } {
log_cmd detailed_placement {*}[env_var_or_empty DETAIL_PLACEMENT_ARGS] {*}$args
}
proc repair_design_helper { } {
puts "Perform buffer insertion and gate resizing..."
set additional_args "-verbose"
append_env_var additional_args CAP_MARGIN -cap_margin 1
append_env_var additional_args SLEW_MARGIN -slew_margin 1
append_env_var additional_args MATCH_CELL_FOOTPRINT -match_cell_footprint 0
log_cmd repair_design {*}$additional_args
}
proc recover_power_helper { } {
if { $::env(RECOVER_POWER) == 0 } {
return
}
puts "Downsizing/switching to higher Vt for non critical gates for power recovery"
puts "Percent of paths optimized $::env(RECOVER_POWER)"
report_tns
report_wns
report_power
set additional_args "-verbose"
append_env_var additional_args RECOVER_POWER -recover_power 1
append_env_var additional_args MATCH_CELL_FOOTPRINT -match_cell_footprint 0
log_cmd repair_timing {*}$additional_args
report_tns
report_wns
report_power
}
proc extract_stage { input_file } {
# Match the stage prefix on the basename, not the full path: an ancestor
# dir like ".../4_something/3_place.odb" would otherwise match "4_" -> stage 4.
set stage_file [file tail $input_file]
if { ![regexp {^([0-9])_(?:([0-9])_)?} $stage_file match num1 num2] } {
puts "Error: Could not determine design stage from $input_file"
exit 1
}
lappend number_groups $num1
if { $num2 != "" } {
lappend number_groups $num2
} else {
lappend number_groups "0"
}
}
proc find_sdc_file { input_file } {
# canonicalize input file, sometimes it is called with an input
# file relative to $::env(RESULTS_DIR), other times with
# an absolute path
if { ![file exists $input_file] } {
set input_file [file join $::env(RESULTS_DIR) $input_file]
}
set input_file [file normalize $input_file]
set stage [extract_stage $input_file]
set design_stage [lindex $stage 0]
set sdc_file ""
set exact_sdc [string map {.odb .sdc} $input_file]
set sdc_files \
[glob -nocomplain -directory $::env(RESULTS_DIR) -types f "\[1-9+\]_\[1-9_A-Za-z\]*\.sdc"]
set sdc_files [lsort -decreasing -dictionary $sdc_files]
set sdc_files [lmap file $sdc_files { file normalize $file }]
foreach name $sdc_files {
if { [lindex [lsort -decreasing -dictionary [list $name $exact_sdc]] 0] == $exact_sdc } {
set sdc_file $name
break
}
}
return [list $design_stage $sdc_file]
}
proc env_var_equals { env_var value } {
return [expr { [info exists ::env($env_var)] && $::env($env_var) == $value }]
}
proc env_var_exists_and_non_empty { env_var } {
return [expr { [info exists ::env($env_var)] && ![string equal $::env($env_var) ""] }]
}
proc append_env_var { list_name var_name prefix has_arg } {
upvar $list_name list
if {
(!$has_arg && [env_var_equals $var_name 1]) ||
($has_arg && [env_var_exists_and_non_empty $var_name])
} {
lappend list $prefix
if { $has_arg } {
lappend list $::env($var_name)
}
}
}
# Non-empty defaults should go into variables.yaml, generally
proc env_var_or_empty { env_var } {
if { [env_var_exists_and_non_empty $env_var] } {
return $::env($env_var)
}
return ""
}
proc pins_fixed_in_floorplan { } {
return [expr {
[env_var_exists_and_non_empty FLOORPLAN_DEF] ||
[env_var_exists_and_non_empty FOOTPRINT] ||
[env_var_exists_and_non_empty FOOTPRINT_TCL]
}]
}
# set_place_config arguments for global_placement -place_ios, from the
# place_pins settings.
proc place_pins_args_to_place_config { } {
set config_args [list \
-io_pin_hor_layers $::env(IO_PLACER_H) \
-io_pin_ver_layers $::env(IO_PLACER_V)]
set pins_args [env_var_or_empty PLACE_PINS_ARGS]
for { set i 0 } { $i < [llength $pins_args] } { incr i } {
set arg [lindex $pins_args $i]
switch -- $arg {
-corner_avoidance -
-min_distance {
lappend config_args -io_pin_[string range $arg 1 end] \
[lindex $pins_args [incr i]]
}
-min_distance_in_tracks {
lappend config_args -io_pin_min_distance_in_tracks
}
default {
utl::warn FLW 25 "PLACE_PINS_ARGS $arg is not used with GPL_PLACE_IOS."
if { ![string match -* [lindex $pins_args [expr { $i + 1 }]]] } {
incr i
}
}
}
}
return $config_args
}
proc find_macros { } {
set macros ""
set db [ord::get_db]
set block [[$db getChip] getBlock]
foreach inst [$block getInsts] {
set inst_master [$inst getMaster]
# BLOCK means MACRO cells
if { [string match [$inst_master getType] "BLOCK"] } {
append macros " " $inst
}
}
return $macros
}
proc erase_non_stage_variables { stage_name } {
if { $::env(KEEP_VARS) } {
return
}
# "$::env(SCRIPTS_DIR)/stage_variables.py stage_name" returns list of
# variables to erase.
#
# Tcl yaml package can't be imported in the sta/openroad environment:
#
# https://github.com/The-OpenROAD-Project/OpenROAD/issues/5875
set variables [exec $::env(SCRIPTS_DIR)/non_stage_variables.py $stage_name]
foreach var $variables {
if { [info exists ::env($var)] } {
unset ::env($var)
}
}
}
set global_route_congestion_report $::env(REPORTS_DIR)/congestion.rpt
proc place_density_with_lb_addon { } {
if { [env_var_exists_and_non_empty PLACE_DENSITY_LB_ADDON] } {
# check the lower boundary of the PLACE_DENSITY and add PLACE_DENSITY_LB_ADDON
set place_density_lb [gpl::get_global_placement_uniform_density \
-pad_left $::env(CELL_PAD_IN_SITES_GLOBAL_PLACEMENT) \
-pad_right $::env(CELL_PAD_IN_SITES_GLOBAL_PLACEMENT)]
set place_density \
[expr $place_density_lb + ((1.0 - $place_density_lb) * $::env(PLACE_DENSITY_LB_ADDON)) + 0.01]
if { $place_density > 1.0 } {
utl::error FLW 24 \
"Place density exceeds 1.0 (current PLACE_DENSITY_LB_ADDON = \
$::env(PLACE_DENSITY_LB_ADDON)). Please check if the value of \
PLACE_DENSITY_LB_ADDON is between 0 and 0.99."
}
puts "Placement density is $place_density, computed from PLACE_DENSITY_LB_ADDON \
$::env(PLACE_DENSITY_LB_ADDON) and lower bound $place_density_lb"
} else {
set place_density $::env(PLACE_DENSITY)
}
return $place_density
}
proc source_env_var_if_exists { env_var } {
if { [env_var_exists_and_non_empty $env_var] } {
log_cmd source $::env($env_var)
}
}
# Feature toggle for now, eventually the -hier option
# will be default and this code will be deleted.
proc hier_options { } {
if {
([env_var_equals SYNTH_WRAPPED_OPERATORS 1] ||
[env_var_equals SWAP_ARITH_OPERATORS 1]) &&
!$::env(OPENROAD_HIERARCHICAL)
} {
error "SYNTH_WRAPPED_OPERATORS or SWAP_ARITH_OPERATORS require OPENROAD_HIERARCHICAL to be set."
}
if { $::env(OPENROAD_HIERARCHICAL) } {
return "-hier"
} else {
return ""
}
}
proc is_physical_only_master { master } {
set physical_only_type_patterns [list \
"COVER" \
"COVER_BUMP" \
"RING" \
"PAD_SPACER" \
"CORE_FEEDTHROUGH" \
"CORE_SPACER" \
"CORE_ANTENNACELL" \
"CORE_WELLTAP" \
"ENDCAP*"]
set master_type [$master getType]
foreach pattern $physical_only_type_patterns {
if { [string match $pattern $master_type] } {
return 1
}
}
return 0
}
# Returns 1 if the master has no signal pins (only power/ground or none).
proc has_signal_pins { master } {
foreach mterm [$master getMTerms] {
set sig_type [$mterm getSigType]
if { $sig_type != "POWER" && $sig_type != "GROUND" } {
return 1
}
}
return 0
}
# Returns 1 if the master has a corresponding liberty cell.
proc has_liberty_cell { master } {
set master_name [$master getName]
set lib_cells [get_lib_cells -quiet */$master_name]
if { $lib_cells == {} } {
return 0
}
return 1
}
# Finds all physical-only masters in the current database and
# returns their names.
proc find_physical_only_masters { } {
set db [::ord::get_db]
set libs [$db getLibs]
set physical_only_masters [list]
foreach lib $libs {
foreach master [$lib getMasters] {
set master_name [$master getName]
if { [is_physical_only_master $master] } {
lappend physical_only_masters $master_name
continue
}
# Consider cells with no signal pins and no liberty cell as physical-only
if { [has_liberty_cell $master] == 0 } {
if { [has_signal_pins $master] == 0 } {
lappend physical_only_masters $master_name
} else {
puts "Warning: master $master_name has signal pins but no liberty cell"
}
}
}
}
return $physical_only_masters
}
proc endpoint_slack_file { step } {
return $::env(REPORTS_DIR)/${step}_endpoint_slack.json
}
# Write the setup and hold slack of every endpoint at the end of a flow
# step so they can be compared across steps with
# flow/util/endpointSlack.py. Parasitics are estimated the same way as
# open.tcl so that every step is timed consistently, unless the caller
# has already loaded parasitics (e.g. extracted ones) and passes their
# name as parasitics.
proc write_endpoint_slack { step { parasitics "" } } {
if { !$::env(REPORT_ENDPOINT_SLACK) || [string match "*-failed" $step] } {
return
}
set filename [endpoint_slack_file $step]
puts "Writing endpoint slack to $filename"
if { $parasitics == "" } {
set design_stage [lindex [split $step _] 0]
set parasitics none
if { $design_stage >= 5 } {
if { [grt::have_routes] } {
set parasitics global_routing
}
} elseif { $design_stage >= 3 } {
set parasitics placement
}
if { $parasitics != "none" } {
estimate_parasitics -$parasitics
}
}
# Update timing before sta::endpoints: the endpoint set is cached the
# first time it is requested and misses gated clock enable checks if the
# clock network has not been found yet.
sta::find_timing
set fileId [open $filename w]
puts $fileId "{"
puts $fileId "\"design\": \"$::env(DESIGN_NAME)\","
puts $fileId "\"step\": \"$step\","
puts $fileId "\"parasitics\": \"$parasitics\","
# Seconds per reported time unit
puts $fileId "\"time_unit\": [sta::time_ui_sta 1],"
# endpoint: [setup slack, hold slack], null if unconstrained
puts $fileId "\"endpoints\": {"
set sep ""
foreach pin [sta::endpoints] {
set name [string map {\\ \\\\ \" \\\"} [get_full_name $pin]]
set slacks {}
foreach min_max {max min} {
set slack [get_property $pin slack_$min_max]
if { $slack eq "INF" || $slack eq "-INF" } {
set slack null
}
lappend slacks $slack
}
puts -nonewline $fileId "$sep\"$name\": \[[join $slacks ,]\]"
set sep ",\n"
}
puts $fileId "\n}"
puts $fileId "}"
close $fileId
}
# A step that copies its input .odb leaves the design unchanged, so
# re-time it if it is loaded, otherwise copy the input step's snapshot.
proc copy_endpoint_slack { input_file output_file } {
if { !$::env(REPORT_ENDPOINT_SLACK) } {
return
}
set step [file rootname [file tail $output_file]]
set chip [[ord::get_db] getChip]
if { $chip != "NULL" && [$chip getBlock] != "NULL" } {
write_endpoint_slack $step
return
}
set input [endpoint_slack_file [file rootname [file tail $input_file]]]
if { ![file exists $input] } {
return
}
set fileId [open $input r]
set json [read $fileId]
close $fileId
regsub {"step": "[^"]*"} $json "\"step\": \"$step\"" json
set fileId [open [endpoint_slack_file $step] w]
puts -nonewline $fileId $json
close $fileId
}
# endpoint_slack 0 is for callers that write the endpoint slack snapshot
# themselves, e.g. 6_final after parasitic extraction.
proc orfs_write_db { output_file { endpoint_slack 1 } } {
if { $endpoint_slack } {
write_endpoint_slack [file rootname [file tail $output_file]]
}
if { !$::env(WRITE_ODB_AND_SDC_EACH_STAGE) } {
return
}
log_cmd write_db $output_file
}
proc orfs_write_sdc { output_file } {
if { !$::env(WRITE_ODB_AND_SDC_EACH_STAGE) } {
return
}
log_cmd write_sdc -no_timestamp $output_file
}
# For a stage that leaves the design unchanged, copying the input .odb
# forward is much faster than serializing the in-memory database again
# with write_db. Gated like orfs_write_db: in a single-process flow
# (WRITE_ODB_AND_SDC_EACH_STAGE=0) the input file may not exist and no
# stage file should be produced.
#
# exec cp rather than Tcl's file copy: file copy sets the destination
# mtime with second resolution, which breaks make's timestamp-based
# up-to-date checks for fast builds.
proc orfs_copy_db { input_file output_file } {
copy_endpoint_slack $input_file $output_file
if { !$::env(WRITE_ODB_AND_SDC_EACH_STAGE) } {
return
}
log_cmd exec cp $input_file $output_file
}
proc source_step_tcl { hook_type step_name } {
set env_var "${hook_type}_${step_name}_TCL"
source_env_var_if_exists $env_var
}
# Entry of lib_file in the binary liberty (.blib) cache:
# <platform>/<library>@<size>-<mtime>-<dirhash>.blib, so a changed library gets
# a new entry and same-named libraries from different directories don't
# collide. Libraries in the design's directory and those the flow generates
# are cached per run.
proc binary_lib_path { lib_file } {
set source [file normalize $lib_file]
set root [file join $::env(BINARY_LIBS_DIR) $::env(PLATFORM)]
set run_dirs [list \
[file join $::env(WORK_HOME) objects] [file join $::env(WORK_HOME) results]]
if { [env_var_exists_and_non_empty DESIGN_DIR] } {
lappend run_dirs $::env(DESIGN_DIR)
}
foreach dir $run_dirs {
if { [string first "[file normalize $dir]/" $source] == 0 } {
set root [file join $::env(OBJECTS_DIR) binary_libs]
break
}
}
set name [file tail $source]
regsub {\.gz$} $name "" name
set name [file rootname $name]
set dir_hash [format %08x [zlib crc32 [file dirname $source] 0]]
return [file join $root "$name@[file size $source]-[file mtime $source]-$dir_hash.blib"]
}
# The cached .blib for lib_file, or lib_file when the .blib is missing or unusable
proc lib_file_to_read { lib_file } {
if { ![env_var_equals USE_BINARY_LIBS 1] || ![file exists $lib_file] } {
return $lib_file
}
set blib [binary_lib_path $lib_file]
if { ![file exists $blib] } {
utl::warn FLW 26 "$blib not found, reading $lib_file."
return $lib_file
}
if { [catch { liberty_binary_info $blib } msg] } {
utl::warn FLW 27 "$blib is unusable, reading $lib_file: $msg"
return $lib_file
}
return $blib
}