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
//! Task S5 (`docs/superpowers/plans/2026-09-26-wave-A2-slice.md`): the
//! process TEMPLATE dedup key (PR-028, PR-029, PR-030) and the
//! `ProcessInstanceId` registration-order tie-break (PR-075, PR-076,
//! PR-077).
//!
//! **PR-028's core claim** ("process template dedup must key off slang's own
//! `InstanceBodySymbol` canonicalization ... compiling one kernel per
//! canonical process shape and sharing it across every instance") is
//! implemented here as a direct `(definition, resolved params)` equality key
//! ([`ProcessTemplateKey`]), not a from-scratch structural-equality pass over
//! the AST: `definition` is `sv_lang`'s own stable [`crate::symid::SymbolId`] for
//! the shared `Definition` symbol (the same symbol every structurally-
//! identical instance's body resolves back to via
//! `instance_body_definition()`), and `params` is the instance's own
//! elaborated, already-folded [`sv_lang::ConstantValue`] list. Two instances
//! with an identical `(definition, params)` pair are, by construction,
//! instances of the same module with the same resolved parameter values --
//! exactly `PR-028`'s dedup criterion -- with no separate call to
//! `Symbol::instance_body_has_same_type` needed at this layer (unlike
//! `netlist::dedup`'s own `TemplateKey`, which buckets on a lossy `u64` hash
//! and so *must* re-verify with the real oracle before merging; this key
//! carries the real, structured parameter values, so key equality already
//! *is* the ground truth).
//!
//! **Deviation from the plan's illustrative signature** (`fn
//! template_key_of(instance_body: &Symbol<'_>) -> ProcessTemplateKey`, taking
//! only the instance `body`): verified against pinned `sv-lang = "=0.1.9"`
//! that `Symbol::parameters()`'s own doc/SAFETY comment ("count is 0 for
//! non-instances") means it is populated only when called on an
//! `Instance`-kind symbol, never on that instance's `InstanceBody` -- the
//! exact same constraint `netlist::dedup::template_key_of` already
//! documents and works around. [`template_key_of`] therefore takes both the
//! owning `Instance` symbol (for `parameters()`) and its `InstanceBody`
//! (for `instance_body_definition()`), by value (`Symbol` is `Copy` and
//! pointer-sized), mirroring that precedent exactly.
//!
//! **Deviation: `ProcessTemplateKey`'s `Hash`/`Eq` are hand-written, not
//! derived.** `sv_lang::ConstantValue` derives only `Clone, Debug, PartialEq`
//! (it is `#[non_exhaustive]` and its `Real` variant is a bare `f64`, which
//! has no `Eq`; its `Integer` variant wraps `SVInt`, which derives `Eq` but
//! not `Hash`), so `#[derive(Eq, Hash)]` on a struct embedding
//! `Vec<ConstantValue>` does not compile. [`hash_constant_value`] below
//! provides a `Hash` implementation kept manually consistent with
//! `ConstantValue`'s derived `PartialEq` (structurally recursing the same
//! way, normalizing `-0.0`/`0.0` to the same hash since `PartialEq` already
//! treats them as equal), and `Eq` is asserted as a marker trait on top of
//! that same `PartialEq`. The one honest gap this leaves (documented, not
//! silently swept under a `#[derive]`): a `real`-valued module parameter
//! equal to `NaN` is `PartialEq`-unequal to itself, so `Eq`'s reflexivity
//! technically does not hold for that one value class. This can only ever
//! cost a missed dedup opportunity (two `NaN`-parameterized instances
//! compiling their own, functionally-identical kernels instead of sharing
//! one) -- never a wrong simulation result, since every instance still gets
//! its own correct `ActivationRecord`/state-arena storage regardless of
//! kernel sharing (PR-030) -- and the A2 slice's construct set has no `real`
//! parameters at all, so no slice fixture can even reach this path.
use crateSymbolUid;
use ;
use crateSymbolId;
use ;
use crateInstanceId;
use crateProcessInstanceId;
/// PR-029: `struct ProcessTemplateKey{definition: SymbolId, params:
/// Vec<ConstantValue>}` -- a hashable dedup key built from
/// `Symbol::definition()`+`parameters()` (see this module's doc comment for
/// the `instance_body_definition()` deviation), used for O(1) amortized
/// template lookup (an ordinary `HashMap<ProcessTemplateKey, _>` in the
/// caller, e.g. codegen's future per-template kernel cache).
/// See this module's doc comment ("`Hash`/`Eq` are hand-written, not
/// derived") for exactly why this is a plain marker `impl` over the
/// `PartialEq` above rather than `#[derive(Eq)]`.
/// A hand-written `Hash` for `sv_lang::ConstantValue`, kept structurally in
/// step with its own derived `PartialEq` (see this module's doc comment).
/// Each variant is tagged with a distinct discriminant byte before its
/// payload is hashed, so e.g. `Integer(0)` and `String("0")` never collide
/// on payload alone. `Integer`/`Real` are hashed via a stable textual/bit
/// form rather than field-by-field (`SVInt` has no public field accessors
/// besides its already-`Display`-backed `to_string()`, and does not itself
/// implement `Hash`).
/// PR-029: builds a [`ProcessTemplateKey`] from an instance's owning
/// `Instance` symbol (for `parameters()`) and its `InstanceBody` (for
/// `instance_body_definition()`) -- see this module's doc comment for why
/// both are needed. `params` uses each parameter's elaborated
/// [`ConstantValue`] (`Symbol::constant_value()`, forced during elaboration,
/// full-width, `defparam`-aware) rather than the printed-text
/// `parameter_value()` netlist's own `TemplateKey` uses, per this task's
/// literal `Vec<ConstantValue>` field type. A parameter whose constant value
/// failed to fold (`None`) is recorded as `ConstantValue::Bad` -- a real,
/// distinguishable member of the key rather than being silently dropped
/// (which would shift every later parameter's position and could spuriously
/// equate two instances with a different number of foldable parameters).
/// PR-075/076/077: assigns `ProcessInstanceId(0), ProcessInstanceId(1), ...`
/// in exactly `order`'s sequence. `order` is meant to be
/// `netlist::NetlistView::registration_order()`'s own DFS-order output --
/// this function performs no traversal of its own (see
/// `no_second_preorder_walk_of_design_exists_in_process_dedup_rs` in
/// `tests/process_dedup.rs`): PR-076's reconciliation note is explicit that
/// `registration_order()` (elaboration's own deterministic pre-order DFS
/// over the design's top-level instance list) directly **is** the tie-break
/// order, so a
/// second, independently-derived pre-order walk here would just be a second
/// place the "canonical instance order" could silently disagree with the
/// first -- exactly the bug class PR-076's `superseded_by` note names and
/// this function is deliberately shaped to make impossible.