Skip to content

Commit 18499f9

Browse files
committed
synth: hash post-read_design_sources state to isolate slang non-determinism
yosys-slang was recently found to be non-idempotent in a few cases: re-running synth on the same RTL produces different post-frontend RTLIL. Master already has two file-based hashes that bracket the whole synthesis pipeline: synth__canonical_netlist__hash sha1 of `1_1_yosys_canonicalize.rtlil` (captured AFTER `read_design_sources` -> `hierarchy -check` -> `opt_clean -purge`). synth__netlist__hash sha1 of `1_2_yosys.v` (captured AFTER ABC). A slang-induced diff therefore collapses into `canonical_netlist__hash` alongside any hierarchy / opt_clean drift, with no way to tell them apart. Add one more hash, `synth__post_read_sources__hash`, captured immediately after `read_design_sources` returns -- i.e. the state straight out of the HDL frontend, before any other pass runs. An unstable hash here means the frontend itself is non-idempotent, distinct from drift introduced later. Mechanics: * `flow/scripts/synth_preamble.tcl` gains a `write_state_hash` proc that strips `src` attributes (file:line metadata, so the hash is path-independent across the bazel sandbox and the classic-make build), dumps the current RTLIL to a temp file under `$OBJECTS_DIR`, sha1's it, deletes the temp, and emits a `<metric>: <sha>` line to the yosys log. * `flow/scripts/synth_canonicalize.tcl` calls `write_state_hash synth__post_read_sources__hash` immediately after `read_design_sources`. * `flow/util/genMetrics.py` extracts the new hash from `1_1_yosys_canonicalize.log` into `metadata.json`. * `flow/util/genRuleFile.py` declares a `level=warning`, `compare="=="` literal rule for the new metric so a downstream `rules-base.json` can pin it without failing the build on a mismatch (matching how `canonical_netlist__hash` and `netlist__hash` are declared). No design's `rules-base.json` gets a baseline value pinned in this PR; declaring the rule type just makes the metric available to any future per-design pin. Pre-ABC / post-hierarchy / post-synth-main hashes are intentionally left out -- they belong in follow-up PRs if the slang-only signal isn't enough. Signed-off-by: Øyvind Harboe <oyvind@ascenium.com> Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
1 parent 2f6e9c9 commit 18499f9

4 files changed

Lines changed: 53 additions & 4 deletions

File tree

‎flow/scripts/synth_canonicalize.tcl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
source $::env(SCRIPTS_DIR)/synth_preamble.tcl
22
read_design_sources
3+
# Fingerprint the design state right after the HDL frontend
4+
# (slang / builtin Verilog) returns, before any other pass runs.
5+
# An unstable hash here means the frontend itself is non-idempotent
6+
# -- distinct from drift introduced later by hierarchy / opt_clean
7+
# / etc. Surfaced as a literal warning-level rule via genRuleFile.py.
8+
write_state_hash synth__post_read_sources__hash
39

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

‎flow/scripts/synth_preamble.tcl‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@ yosys -import
33
source $::env(SCRIPTS_DIR)/util.tcl
44
erase_non_stage_variables synth
55

6+
# Fingerprint the current yosys design state to a `<metric>: <sha>`
7+
# line in the surrounding yosys log so `genMetrics.py` can pick it up.
8+
# `setattr -unset src` strips file:line attribute lines from the
9+
# RTLIL before hashing so the hash is path-independent (bazel
10+
# sandbox paths differ from the classic-make build's relative
11+
# paths; without stripping, hashes always differ trivially). The
12+
# strip is permanent for the rest of the yosys run, matching the
13+
# SYNTH_REPEATABLE_BUILD=1 path's behavior -- src attributes are
14+
# debug metadata that no downstream pass relies on for decisions.
15+
proc write_state_hash { metric } {
16+
setattr -unset src *
17+
setattr -mod -unset src *
18+
set tmp $::env(OBJECTS_DIR)/.${metric}.tmp.rtlil
19+
write_rtlil $tmp
20+
set sha [lindex [split [exec sha1sum $tmp]] 0]
21+
file delete $tmp
22+
puts "${metric}: $sha"
23+
}
24+
625
# If using a cached, gate level netlist, then copy over to the results dir with
726
# preserve timestamps flag set. If you don't, subsequent runs will cause the
827
# floorplan step to be re-executed.

‎flow/util/genMetrics.py‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,29 @@ def extract_metrics(
262262
rptPath + "/synth_stat.txt",
263263
)
264264

265-
# Netlist hashes: fingerprints of the canonical RTLIL (pre-ABC) and
266-
# the final post-synthesis Verilog so the rules-base.json check
267-
# (level=warning) flags when bazel-built vs make-built yosys
268-
# disagree for the same RTL.
265+
# Netlist hashes: fingerprints at three points in the yosys
266+
# pipeline so the rules-base.json check (level=warning) can
267+
# isolate frontend drift (yosys-slang isn't idempotent) from
268+
# mid-synth drift from ABC drift.
269+
#
270+
# post_read_sources = state right after `read_design_sources`
271+
# (HDL frontend output only)
272+
# canonical_netlist = state after `opt_clean -purge`
273+
# (= `1_1_yosys_canonicalize.rtlil`)
274+
# netlist = `1_2_yosys.v`, post-ABC
275+
#
276+
# `post_read_sources` is emitted by synth_canonicalize.tcl via
277+
# `write_state_hash` (synth_preamble.tcl) as a `<metric>: <sha>`
278+
# line in 1_1_yosys_canonicalize.log; the other two come straight
279+
# from `file_sha1` of the already-emitted RTLIL / Verilog.
280+
extractTagFromFile(
281+
"synth__post_read_sources__hash",
282+
metrics_dict,
283+
r"^synth__post_read_sources__hash:\s+([0-9a-f]{40})\s*$",
284+
logPath + "/1_1_yosys_canonicalize.log",
285+
t=str,
286+
required=False,
287+
)
269288
metrics_dict["synth__canonical_netlist__hash"] = file_sha1(
270289
resultPath + "/1_1_yosys_canonicalize.rtlil"
271290
)

‎flow/util/genRuleFile.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ def gen_rule_file(
7979
# surfaces as a [WARN] diagnostic in checkMetadata.py without
8080
# failing the build, matching how rules-base.json already
8181
# treats warning counts.
82+
"synth__post_read_sources__hash": {
83+
"mode": "literal",
84+
"compare": "==",
85+
"level": "warning",
86+
},
8287
"synth__canonical_netlist__hash": {
8388
"mode": "literal",
8489
"compare": "==",

0 commit comments

Comments
 (0)