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
//! Task 22 (`docs/superpowers/plans/2026-09-23-wave-A-codegen.md`, ledger
//! CG-060, CG-092, CG-117, CG-118): the top-level `CodegenSession` entry
//! object and `lower_instance_body`/`lower_all_instance_bodies`, the
//! rayon-parallel fan-out that lowers one unique `InstanceBody` `Symbol` per
//! worker thread, each worker building its own `inkwell::Context`.
//!
//! **BINDING CORRECTION 3 (supersedes the plan's original "`LoweredUnit` owns
//! its own Context+Module, self-referential" design):** `inkwell::module::
//! Module<'ctx>` is **not** `Send` (only `Context` is). A `LoweredUnit`
//! returned from a rayon worker and gathered back on the calling thread must
//! therefore never carry a live `Module`/`Context` pair across that boundary
//! -- a `Mutex` only serializes *access*, it does not make a `!Send` value
//! legal to move between threads at all. The worker-side pipeline is instead:
//! create a fresh `Context` **inside** the worker closure, lower the body's
//! functions into a `Module` on that `Context`, verify (`FunctionValue::
//! verify` per function, inside `func::lower_function`; the whole `Module`
//! once more here), serialize the verified `Module` to LLVM bitcode
//! (`Module::write_bitcode_to_memory() -> MemoryBuffer<'static>`, copied out
//! to an owned `Vec<u8>`), then let the `Context`/`Module` drop before the
//! closure returns. `LoweredUnit` owns only that `Vec<u8>` (plus plain-data
//! metadata) -- `Vec<u8>`/`String`/`Vec<String>` are all `Send + 'static`, so
//! `LoweredUnit` crosses the rayon boundary safely with no unsafe code and no
//! `Mutex` (`loweredunit_is_send`, tests/session_parallel.rs, is the
//! compile-time proof). The collector (a future JIT backend, Task 24 --
//! itself revised by BINDING CORRECTION 4 to parse each blob into its own
//! fresh context on the consuming side -- or an AOT backend, Task 25) parses
//! each blob back into a live `Module` in **its own** `Context` when and
//! where it actually needs one; this file's own
//! `lower_instance_body_bitcode_reparses_into_populated_module`/
//! `n_workers_produce_n_independently_parseable_bitcode_blobs`/
//! `merged_bitcode_blobs_jit_and_run_correctly` tests (tests/
//! session_parallel.rs) exercise exactly that reparse step, the last two
//! using the existing legacy `Jit`/`ExecutionEngine` path (BINDING CORRECTION
//! 4: Task 24's `LLJIT`/`JITDylib` backend does not exist yet in this
//! track's tree at this task) rather than the not-yet-built Task 24 backend
//! the plan's literal text names.
//!
//! This is a deviation from CG-118's literal "returns a `LoweredUnit`
//! containing a populated `Module`" wording; the module *was* fully
//! populated and LLVM-verified before serialization, which is what CG-118's
//! acceptance is actually protecting against (an unpopulated/unverified
//! module) -- the substance of the acceptance still holds even though the
//! literal type shape changed.
//!
//! **Lowering-unit scope (this task's own, narrower than the whole-design
//! Deferred/A2-slice pipeline):** `lower_instance_body` lowers every direct
//! member of `body` that is a `SymbolKind::Subroutine` (a leaf `function
//! automatic`, this crate's existing `func::lower_function` P0 capability) --
//! exactly mirroring `module.rs::discover_processes`'s own established
//! pattern of filtering an `InstanceBody`'s direct `members()` down to the
//! one kind it cares about and silently skipping everything else (ports,
//! nets, variables, procedural blocks, instances -- each some OTHER
//! subsystem's own concern, not a silently-dropped construct THIS function
//! was asked to lower). A `Subroutine` member that fails to lower (e.g. a
//! `real`-typed argument, per `func::lower_function`'s own scope note)
//! propagates as a loud `LoweringError`, never silently skipped.
//!
//! **Review fix (r2), duplicate-name detection:** an earlier revision of
//! this file claimed the whole-module `module.verify()` call in
//! `lower_instance_body_in` would catch "duplicate names across two
//! Subroutine members in the same body". That claim was false: LLVM's
//! `Module::add_function` (invoked by `func::lower_function`) silently
//! disambiguates a same-named second function itself (e.g. a real duplicate
//! `f0` becomes the LLVM-level name `f0.1`) before either that function's
//! own per-function verify or the later whole-module verify ever runs, so
//! neither verify ever sees a conflict to reject. `lower_instance_body_in`
//! now detects a duplicate SV name among the `Subroutine` members it is
//! about to lower explicitly (before creating any LLVM function for it) and
//! returns a `LoweringError` naming the collision; `tests/
//! session_parallel.rs`'s `duplicate_subroutine_name_is_a_loud_lowering_
//! error` proves this returns `Err` rather than an `Ok(LoweredUnit)` with a
//! silently-renamed function.
use Context;
use *;
use SymbolKind;
use ;
use crateLoweringError;
use cratelower_function;
use crateValueRepr;
/// Task 29 (CG-068): what [`symbol_role`]'s exhaustive `match` over
/// `SymbolKind` classifies a direct `InstanceBody` member as, for
/// `lower_instance_body_in`'s member-walk below.
/// Task 29 (CG-068): the crate's first genuine `match` over `SymbolKind`
/// (as opposed to `frontend.rs::find_function`'s plain `==` check) --
/// classifies one direct `InstanceBody` member for `lower_instance_body_in`'s
/// walk below. Every one of `sv_lang::kinds::SymbolKind`'s 101 named
/// variants (pinned, `kind_counts::SYMBOL_KIND_COUNT`) is listed in exactly
/// one arm below; the required-by-`#[non_exhaustive]` trailing wildcard
/// exists only for a future `sv-lang-kinds` variant beyond today's pinned
/// 101 (a version bump the `symbol_kind_count_matches_pinned_constant` test,
/// `tests/exhaustiveness.rs`, catches first) and, defensively, for
/// `Unknown` itself -- both end in a named [`LoweringError`], never a
/// silent default `Ok(SymbolRole::Structural)` (CG-066/067/068's shared
/// discipline, applied here to `SymbolKind`).
///
/// The deferred/unsupported bucket below (class/OOP, randomize/constraint,
/// functional coverage, SVA sequences/properties/clocking/checkers,
/// randsequence, gate primitives, `defparam` value overrides,
/// elaboration-time system tasks, anonymous `program` blocks, `alias`) is
/// every `SymbolKind` this crate has never validated a correct,
/// semantics-preserving way to silently skip -- per CG-070/CG-072, "we
/// haven't implemented this" must fail loudly, never silently produce wrong
/// codegen by pretending it was as harmless as a port or a type
/// declaration.
///
/// **`SpecifyBlock`/`Specparam`/`TimingPath`/`PulseStyle`/
/// `SystemTimingCheck` are deliberately NOT in that deferred bucket** (moved
/// out by `docs/superpowers/plans/2026-10-03-sdf.md` Task 1): this function
/// is dead on `run.rs`'s real pipeline (confirmed by that plan's own grep --
/// `lower_instance_body`/`lower_all_instance_bodies` have no call site in
/// `vj`), but it is still exercised by `tests/exhaustiveness.rs`'s
/// `symbol_kind_count_matches_pinned_constant`, so it must still classify
/// every kind correctly. These five kinds are declarative specify-block
/// content -- not executable statements, exactly like `ContinuousAssign`
/// below -- and are planned/registered as native Rust `CompiledProcess`es by
/// `vj`'s own `specify::plan`/`timingcheck::plan` (that plan's §1.3
/// decision 1), never by this crate's LLVM lowering. The sdf plan is the
/// owner of this classification going forward.
/// CG-117: the top-level entry object. Owns nothing thread-unsafe itself (a
/// `&'d Design` -- `Design` is `Send + Sync`, per `sv_lang::Design`'s own doc
/// comment -- plus two plain `Copy` scalars), so it can be constructed once
/// and shared *by reference* across worker threads with no `Mutex`/`Arc`
/// wrapper around the session itself (`session_is_shared_by_reference_
/// across_threads`, tests/session_parallel.rs, is the concrete proof: it
/// typechecks and runs only because `CodegenSession<'d>: Sync`).
/// CG-118's lowering-unit output, in BINDING CORRECTION 3's corrected shape:
/// `Send + 'static`, no `Module`/`Context`/LLVM-value field of any kind.
/// `def_name` is the `InstanceBody`'s own module-definition name (diagnostic/
/// bookkeeping only); `function_names` lists every LLVM function the module
/// actually defines, in lowering order, so a caller need not re-walk the
/// reparsed `Module` to discover what is in it.
/// CG-118: lowers one unique `InstanceBody` `Symbol` -- building its own
/// fresh `inkwell::Context` internally (CG-060's "one `Context` per
/// compile-worker thread" property, satisfied trivially here since this
/// function creates, uses, and drops exactly one `Context` entirely within
/// whichever single thread calls it; `lower_all_instance_bodies` below is
/// what actually calls this from more than one thread).
/// The core of [`lower_instance_body`], factored out so
/// [`lower_all_instance_bodies_with_probe`] can create the `Context` itself
/// (to hand its pointer identity to a test probe) and still share this exact
/// lowering path -- there is only one place that walks `body`'s members and
/// serializes the result, never two divergent copies.
/// CG-092: lowers every `Symbol` in `bodies` in parallel via `rayon`, each on
/// its own worker thread with its own fresh `Context` (never shared, never
/// pooled) -- riding plain `rayon::prelude::*` on the caller-supplied slice
/// rather than `sv_lang::Design::par_visit`/`par_visit_from` (see this file's
/// own module doc comment, and the `Cargo.toml` comment on the `rayon`
/// workspace dependency, for why: those two visit an entire design with a
/// `Fn(Symbol)` callback, not the "map a caller-owned `&[Symbol]` list to a
/// `Vec<Result<LoweredUnit, _>>`" shape this function needs).
/// As [`lower_all_instance_bodies`], but also invokes `on_worker_context`
/// with each worker's freshly-created `Context`'s pointer identity (as a
/// `usize`) immediately after creating it and before using it for anything.
///
/// **Deviation from the plan's literal `lower_all_instance_bodies` signature
/// (not a new public name the plan itself lists, but a minimal, documented
/// addition):** CG-060's own acceptance ("running codegen with N rayon worker
/// threads never touches a Context from a thread other than the one that
/// created it") needs a way to OBSERVE that N genuinely distinct `Context`s
/// were created, one per worker -- but `LoweredUnit` deliberately carries no
/// `Context`-derived handle at all (BINDING CORRECTION 3), so there is
/// nothing in `lower_all_instance_bodies`'s own return value a test could
/// inspect for this. This probe hook is the seam: it never exposes a live
/// `&Context` outside the crate (which would immediately violate `Module<
/// 'ctx>`'s own `!Send` boundary the moment a caller tried to hold onto one
/// across the call) -- only its address, exactly enough to prove distinctness
/// (`each_worker_gets_its_own_context_never_shared`, tests/
/// session_parallel.rs) without weakening the "no Context ever crosses a
/// thread boundary" property this task exists to establish. `on_worker_
/// context` itself runs ON the worker thread that owns the `Context`, so it
/// must be `Sync` (rayon calls it from whichever thread happens to run each
/// closure).