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
//! Task 23 (CG-014, CG-065): dedup cache -- hash-then-verify, never pointer
//! identity.
//!
//! ARCHITECTURE.md ยง7 item 7: `Design::compile()` forces
//! `DisableInstanceCaching` (the precondition for `Design: Send + Sync`), so
//! slang never pointer-shares two structurally identical `InstanceBody`s --
//! elaboration always walks every instance (`O(total instances)`, not
//! `O(distinct templates)`). Codegen recovers the sharing it needs itself:
//! bucket by `(Definition, resolved-params)` hash, then confirm every
//! bucket hit pairwise via `Symbol::instance_body_has_same_type` (mirrors
//! `slang::ast::InstanceBodySymbol::hasSameType`) before two `InstanceBody`s
//! are collapsed into one compiled kernel. Trusting the hash alone is
//! exactly the bug this cache exists to prevent (CG-065); see
//! `hash_collision_structurally_different_bodies_yield_two_kernels` in
//! `tests/dedup.rs`.
//!
//! The plan text that introduced this module (Task 23 of
//! `docs/superpowers/plans/2026-09-23-wave-A-codegen.md`) names the key
//! type `sv_lang::DefinitionId`; no such type exists in the real `sv_lang`
//! API. `Symbol::instance_body_definition()` returns the elaborated
//! `Definition` `Symbol`, and `SymbolUid::uid() -> SymbolId` (a stable,
//! hashable hierarchical-path key) is compilation-wide for a `Definition`
//! symbol -- exactly the per-module-type granularity this cache's key
//! needs -- so `SymbolId` is used in `DefinitionId`'s place (adaptation
//! noted in the task's deviations).
use HashMap;
use Symbol;
use SymbolId;
use crate;
/// The stable internal name assigned to one deduplicated compiled kernel
/// body (Task 21's `mangle.rs` `_SV$...` scheme). A plain `String` -- there
/// is no distinct `MangledName` newtype anywhere else in this crate
/// (`mangle::mangle_kernel` itself returns `String`), so this is a type
/// alias rather than a second wrapper around the same data.
pub type MangledName = String;
/// The outcome of [`DedupCache::get_or_insert`].
/// CG-014/CG-065's dedup cache: keys compiled kernels by `(definition,
/// param-binding hash)` and confirms every bucket hit pairwise via
/// `Symbol::instance_body_has_same_type` before treating two
/// `InstanceBody`s as the same compiled kernel. Never keys or compares by
/// `Symbol` pointer identity (`Design::compile()`'s forced
/// `DisableInstanceCaching` means two value-equal instances are guaranteed
/// to be distinct handles -- see `dedup_cache_is_not_pointer_identity`).
///
/// Branded with the `Design`'s lifetime `'d` because a bucket keeps one
/// representative `Symbol<'d>` per (rare) hash collision, to compare
/// future arrivals against.