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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! Strategy compilation
//!
//! This module provides two compilation modes:
//!
//! 1. **Standard ABI** (`compile_strategy`): Uses `fn(*mut JITContext) -> i32`
//! - Full access to VM features (closures, FFI, etc.)
//! - Suitable for general-purpose JIT compilation
//!
//! 2. **Kernel ABI** (`compile_simulation_kernel`): Uses `fn(usize, *const *const f64, *mut u8) -> i32`
//! - Zero-allocation hot path for simulation
//! - Direct memory access to series data and state
//! - Enables >10M ticks/sec performance
use cranelift::codegen::ir::FuncRef;
use cranelift::prelude::*;
use cranelift_module::{Linkage, Module};
use std::collections::HashMap;
use super::setup::JITCompiler;
use crate::context::{
CorrelatedKernelFn, JittedStrategyFn, SimulationKernelConfig, SimulationKernelFn,
};
use shape_vm::bytecode::BytecodeProgram;
impl JITCompiler {
#[inline(always)]
pub fn compile_strategy(
&mut self,
name: &str,
program: &BytecodeProgram,
) -> Result<JittedStrategyFn, String> {
// MirToIR is the ONLY compilation path.
let mir_data = program.top_level_mir.as_ref().ok_or_else(|| {
"MirToIR: top-level code has no MIR data".to_string()
})?;
let preflight = crate::mir_compiler::preflight(mir_data);
if !preflight.can_compile {
return Err(format!(
"MirToIR: top-level preflight failed: {}",
preflight.blockers.join("; ")
));
}
let mut sig = self.module.make_signature();
sig.params.push(AbiParam::new(types::I64));
sig.returns.push(AbiParam::new(types::I32));
let func_id = self
.module
.declare_function(name, Linkage::Export, &sig)
.map_err(|e| format!("Failed to declare function: {}", e))?;
let mut ctx = self.module.make_context();
ctx.func.signature = sig;
let mut func_builder_ctx = FunctionBuilderContext::new();
{
let mut builder = FunctionBuilder::new(&mut ctx.func, &mut func_builder_ctx);
let entry_block = builder.create_block();
builder.append_block_params_for_function_params(entry_block);
builder.switch_to_block(entry_block);
builder.seal_block(entry_block);
let ctx_ptr = builder.block_params(entry_block)[0];
let ffi = self.build_ffi_refs(&mut builder)?;
{
let slot_kinds: Vec<Option<shape_vm::type_tracking::NativeKind>> = program
.top_level_frame
.as_ref()
.map(|fd| fd.slots.iter().copied().map(Some).collect())
.unwrap_or_default();
// ADR-006 §2.7.5 conduit: thread the bytecode compiler's
// proven per-slot `ConcreteType` for top-level locals into
// MirToIR. The bytecode compiler stamps the side-table at
// `populate_program_storage_hints` time from
// `local_array_element_types`, `local_map_key_value_types`,
// and the type-tracker's schema registry (W12-top-level-
// concrete-types-conduit close, 2026-05-12). MirToIR's v2
// fast path uses `Array<scalar>` / `Struct(_)` /
// `HashMap(K, V)` slot kinds to bypass `Rvalue::Aggregate`
// surface-and-stop and the kind-blind ObjectStore path.
// Empty vec (no top-level code) → MirToIR falls through to
// the legacy NaN-boxed path naturally.
let concrete_types: Vec<shape_value::v2::ConcreteType> =
program.top_level_local_concrete_types.clone();
let function_indices: std::collections::HashMap<String, u16> = program
.functions
.iter()
.enumerate()
.map(|(i, f)| (f.name.clone(), i as u16))
.collect();
let closure_function_layouts: HashMap<
u16,
std::sync::Arc<shape_value::v2::closure_layout::ClosureLayout>,
> = program
.closure_function_layouts
.iter()
.enumerate()
.filter_map(|(i, opt)| opt.as_ref().map(|l| (i as u16, l.clone())))
.collect();
let mut mir_compiler = crate::mir_compiler::MirToIR::new_with_closure_layouts(
&mut builder,
ctx_ptr,
ffi,
mir_data,
slot_kinds,
concrete_types,
&program.strings,
entry_block,
&function_indices,
HashMap::new(),
HashMap::new(),
closure_function_layouts,
);
// V3-S6c-jit-method-monomorph-routing: thread the V3-S6b
// side-table for top-level (`__main__`) code. Caller id is
// `None` per the bytecode compiler's convention
// (`self.current_function == None` when compiling
// top-level statements at
// `expressions/function_calls.rs:3278`).
mir_compiler.set_monomorph_routing_context(
program.monomorphized_method_call_sites.clone(),
None,
);
// W10 jit-call-method-user-trait-fix (2026-05-17): top-
// level mirror of the per-user-function threading at
// `compiler/program.rs::compile_function_with_user_funcs`.
mir_compiler.set_operator_trait_dispatch_sites(
program.operator_trait_dispatch_sites.clone(),
);
// Bounds-check elision: scan the MIR for trusted index
// accesses and install the plan before compile_body. The
// analyzer is conservative; an empty plan preserves the
// bounds-checked path for every access.
let elision_plan =
crate::mir_compiler::bounds_elision::analyze(&mir_data.mir);
mir_compiler.set_bounds_elision_plan(elision_plan);
// W14.2-E-followup SURFACE-A2 fix (2026-05-19): same as
// the per-user-function path at `program.rs` — pre-
// populate `field_byte_offsets` from the schema registry
// so top-level field reads resolve at JIT-compile time.
mir_compiler
.populate_field_byte_offsets_from_schemas(&program.type_schema_registry);
mir_compiler.create_blocks();
mir_compiler.declare_locals();
mir_compiler.initialize_locals();
// Session 1 Commit 3: allocate Arc<SharedCell>s for
// every SharedCow local slot before the body runs.
mir_compiler.initialize_shared_local_slots();
mir_compiler.compile_body()?;
}
builder.finalize();
}
self.module
.define_function(func_id, &mut ctx)
.map_err(|e| format!("Failed to define function (strategy): {:?}", e))?;
self.module.clear_context(&mut ctx);
self.module
.finalize_definitions()
.map_err(|e| format!("Failed to finalize (strategy): {:?}", e))?;
let code_ptr = self.module.get_finalized_function(func_id);
self.compiled_functions.insert(name.to_string(), code_ptr);
Ok(unsafe { std::mem::transmute(code_ptr) })
}
#[inline(always)]
pub(super) fn compile_strategy_with_user_funcs(
&mut self,
name: &str,
program: &BytecodeProgram,
user_func_ids: &HashMap<u16, cranelift_module::FuncId>,
user_func_arities: &HashMap<u16, u16>,
) -> Result<cranelift_module::FuncId, String> {
let mut sig = self.module.make_signature();
sig.params.push(AbiParam::new(types::I64));
sig.returns.push(AbiParam::new(types::I32));
let func_id = self
.module
.declare_function(name, Linkage::Export, &sig)
.map_err(|e| format!("Failed to declare function: {}", e))?;
let mut ctx = self.module.make_context();
ctx.func.signature = sig;
// MirToIR is the ONLY JIT compilation path (Phase 4: BytecodeToIR removed).
let mir_data = program.top_level_mir.as_ref().ok_or_else(|| {
"MirToIR: top-level code has no MIR data".to_string()
})?;
let preflight = crate::mir_compiler::preflight(mir_data);
if !preflight.can_compile {
return Err(format!(
"MirToIR: top-level preflight failed: {}",
preflight.blockers.join("; ")
));
}
let mut func_builder_ctx = FunctionBuilderContext::new();
{
let mut builder = FunctionBuilder::new(&mut ctx.func, &mut func_builder_ctx);
let entry_block = builder.create_block();
builder.append_block_params_for_function_params(entry_block);
builder.switch_to_block(entry_block);
builder.seal_block(entry_block);
let ctx_ptr = builder.block_params(entry_block)[0];
let mut user_func_refs: HashMap<u16, FuncRef> = HashMap::new();
for (&fn_idx, &fn_id) in user_func_ids {
let func_ref = self.module.declare_func_in_func(fn_id, builder.func);
user_func_refs.insert(fn_idx, func_ref);
}
let ffi = self.build_ffi_refs(&mut builder)?;
{
let slot_kinds: Vec<Option<shape_vm::type_tracking::NativeKind>> = program
.top_level_frame
.as_ref()
.map(|fd| fd.slots.iter().copied().map(Some).collect())
.unwrap_or_default();
// ADR-006 §2.7.5 conduit: thread the bytecode compiler's
// proven per-slot `ConcreteType` for top-level locals into
// MirToIR (W12-top-level-concrete-types-conduit close,
// 2026-05-12). Same source as the no-user-funcs path
// above; see the populate_program_storage_hints
// commentary in `crates/shape-vm/src/compiler/helpers.rs`.
let concrete_types: Vec<shape_value::v2::ConcreteType> =
program.top_level_local_concrete_types.clone();
let function_indices: std::collections::HashMap<String, u16> = program
.functions
.iter()
.enumerate()
.map(|(i, f)| (f.name.clone(), i as u16))
.collect();
let closure_function_layouts: HashMap<
u16,
std::sync::Arc<shape_value::v2::closure_layout::ClosureLayout>,
> = program
.closure_function_layouts
.iter()
.enumerate()
.filter_map(|(i, opt)| opt.as_ref().map(|l| (i as u16, l.clone())))
.collect();
let mut mir_compiler = crate::mir_compiler::MirToIR::new_with_closure_layouts(
&mut builder,
ctx_ptr,
ffi,
mir_data,
slot_kinds,
concrete_types,
&program.strings,
entry_block,
&function_indices,
user_func_refs.clone(),
user_func_arities.clone(),
closure_function_layouts,
);
// V3-S6c-jit-method-monomorph-routing: top-level path with
// user-funcs visible. Caller id = None per the same
// convention as the no-user-funcs path above.
mir_compiler.set_monomorph_routing_context(
program.monomorphized_method_call_sites.clone(),
None,
);
// W10 jit-call-method-user-trait-fix (2026-05-17): same
// top-level mirror as the no-user-funcs branch above.
mir_compiler.set_operator_trait_dispatch_sites(
program.operator_trait_dispatch_sites.clone(),
);
let elision_plan =
crate::mir_compiler::bounds_elision::analyze(&mir_data.mir);
mir_compiler.set_bounds_elision_plan(elision_plan);
// W14.2-E-followup SURFACE-A2 fix (2026-05-19): top-level
// with user-funcs path — same schema pre-population as
// the sibling top-level no-user-funcs branch above.
mir_compiler
.populate_field_byte_offsets_from_schemas(&program.type_schema_registry);
mir_compiler.create_blocks();
mir_compiler.declare_locals();
mir_compiler.initialize_locals();
// Session 1 Commit 3: allocate Arc<SharedCell>s for
// every SharedCow local slot before the body runs.
mir_compiler.initialize_shared_local_slots();
mir_compiler.compile_body()?;
tracing::debug!(
target: "shape_jit",
"jit-mir compiled top-level code via MirToIR",
);
}
builder.finalize();
}
self.module
.define_function(func_id, &mut ctx)
.map_err(|e| format!("Failed to define function (strategy): {:?}", e))?;
self.module.clear_context(&mut ctx);
Ok(func_id)
}
/// Compute instruction index ranges to skip when compiling the main strategy.
///
/// Bytecode layout for programs with user functions:
/// ```text
/// [0] Jump → trampoline1 (skip func0 body)
/// [entry0 .. t1) func0 body
/// [t1] Jump → trampoline2 (skip func1 body)
/// [entry1 .. t2) func1 body
/// ...
/// [main_start ..) main code
/// ```
///
/// Returns the function body ranges (excluding trampoline jumps between them).
pub(super) fn compute_skip_ranges(program: &BytecodeProgram) -> Vec<(usize, usize)> {
let mut ranges = Vec::new();
// Skip function bodies (they are compiled separately).
for f in program.functions.iter() {
if f.body_length == 0 {
continue;
}
ranges.push((f.entry_point, f.entry_point + f.body_length));
}
ranges
}
// ========================================================================
// Simulation Kernel Compilation (Zero-Allocation Hot Path)
// ========================================================================
/// Compile a simulation kernel with the specialized kernel ABI.
///
/// The kernel ABI bypasses JITContext to achieve maximum throughput:
/// - Direct pointer arithmetic for data access
/// - No allocations in the hot path
/// - Inlined field access with known offsets
///
/// # Arguments
/// * `name` - Function name for the compiled kernel
/// * `program` - Bytecode program containing the strategy
/// * `config` - Kernel configuration with field offset mappings
///
/// # Returns
/// A function pointer with signature: `fn(usize, *const *const f64, *mut u8) -> i32`
///
/// # Generated Code Pattern
///
/// For a strategy like:
/// ```shape
/// let price = candle.close
/// if price > state.threshold {
/// state.signal = 1.0
/// }
/// ```
///
/// The kernel generates:
/// ```asm
/// ; price = candle.close (column 3)
/// mov rax, [series_ptrs + 3*8] ; column pointer
/// mov xmm0, [rax + cursor_index*8] ; price value
///
/// ; state.threshold (offset 16)
/// mov xmm1, [state_ptr + 16] ; threshold value
///
/// ; comparison and store
/// ucomisd xmm0, xmm1
/// jbe skip
/// mov qword [state_ptr + 24], 1.0 ; state.signal
/// skip:
/// ```
#[inline(always)]
pub fn compile_simulation_kernel(
&mut self,
name: &str,
program: &BytecodeProgram,
config: &SimulationKernelConfig,
) -> Result<SimulationKernelFn, String> {
// Kernel ABI signature: fn(cursor_index: usize, series_ptrs: *const *const f64, state_ptr: *mut u8) -> i32
let mut sig = self.module.make_signature();
sig.params.push(AbiParam::new(types::I64)); // cursor_index
sig.params.push(AbiParam::new(types::I64)); // series_ptrs
sig.params.push(AbiParam::new(types::I64)); // state_ptr
sig.returns.push(AbiParam::new(types::I32)); // result code
let func_id = self
.module
.declare_function(name, Linkage::Export, &sig)
.map_err(|e| format!("Failed to declare kernel function: {}", e))?;
let mut ctx = self.module.make_context();
ctx.func.signature = sig;
let mut func_builder_ctx = FunctionBuilderContext::new();
{
let mut builder = FunctionBuilder::new(&mut ctx.func, &mut func_builder_ctx);
let entry_block = builder.create_block();
builder.append_block_params_for_function_params(entry_block);
builder.switch_to_block(entry_block);
builder.seal_block(entry_block);
// Get kernel parameters
let cursor_index = builder.block_params(entry_block)[0];
let series_ptrs = builder.block_params(entry_block)[1];
let state_ptr = builder.block_params(entry_block)[2];
// Build kernel-specific IR
let result = self.build_kernel_ir(
&mut builder,
program,
config,
cursor_index,
series_ptrs,
state_ptr,
)?;
builder.ins().return_(&[result]);
builder.finalize();
}
self.module
.define_function(func_id, &mut ctx)
.map_err(|e| format!("Failed to define kernel function: {:?}", e))?;
self.module.clear_context(&mut ctx);
self.module
.finalize_definitions()
.map_err(|e| format!("Failed to finalize kernel: {:?}", e))?;
let code_ptr = self.module.get_finalized_function(func_id);
self.compiled_functions.insert(name.to_string(), code_ptr);
Ok(unsafe { std::mem::transmute(code_ptr) })
}
/// Build kernel IR using BytecodeToIR in kernel mode.
///
/// This compiles bytecode to kernel ABI IR with direct memory access:
/// - GetFieldTyped → state_ptr + offset
/// - GetDataField → series_ptrs[col][cursor]
/// - All locals as Cranelift variables
fn build_kernel_ir(
&mut self,
_builder: &mut FunctionBuilder,
_program: &BytecodeProgram,
_config: &SimulationKernelConfig,
_cursor_index: Value,
_series_ptrs: Value,
_state_ptr: Value,
) -> Result<Value, String> {
Err("Simulation kernel compilation requires v2 runtime migration".to_string())
}
// ========================================================================
// Correlated Kernel Compilation (Multi-Series Simulation)
// ========================================================================
/// Compile a correlated (multi-series) simulation kernel.
///
/// This extends the simulation kernel to support multiple aligned time series,
/// enabling cross-series strategies (e.g., SPY vs VIX, temperature vs pressure).
///
/// # Arguments
/// * `name` - Function name for the compiled kernel
/// * `program` - Bytecode program containing the strategy
/// * `config` - Kernel configuration with series mappings
///
/// # Returns
/// A function pointer with signature:
/// `fn(cursor_index: usize, series_ptrs: *const *const f64, table_count: usize, state_ptr: *mut u8) -> i32`
///
/// # Generated Code Pattern
///
/// For a strategy like:
/// ```shape
/// let spy_price = context.spy // series index 0
/// let vix_level = context.vix // series index 1
/// if vix_level > 25.0 && state.position == 0 {
/// state.signal = 1.0
/// }
/// ```
///
/// The kernel generates:
/// ```asm
/// ; spy_price = context.spy (series index 0)
/// mov rax, [series_ptrs + 0*8] ; series 0 pointer
/// mov xmm0, [rax + cursor_index*8] ; spy value
///
/// ; vix_level = context.vix (series index 1)
/// mov rax, [series_ptrs + 1*8] ; series 1 pointer
/// mov xmm1, [rax + cursor_index*8] ; vix value
///
/// ; comparison and conditional store
/// mov xmm2, [const_25.0]
/// ucomisd xmm1, xmm2
/// jbe skip
/// ; ... check state.position == 0 ...
/// mov qword [state_ptr + signal_offset], 1.0
/// skip:
/// ```
#[inline(always)]
pub fn compile_correlated_kernel(
&mut self,
name: &str,
program: &BytecodeProgram,
config: &SimulationKernelConfig,
) -> Result<CorrelatedKernelFn, String> {
// Validate config is for multi-series mode
if !config.is_multi_table() {
return Err(
"compile_correlated_kernel requires multi-series config (use new_multi_table)"
.to_string(),
);
}
// Correlated kernel ABI:
// fn(cursor_index: usize, series_ptrs: *const *const f64, table_count: usize, state_ptr: *mut u8) -> i32
let mut sig = self.module.make_signature();
sig.params.push(AbiParam::new(types::I64)); // cursor_index
sig.params.push(AbiParam::new(types::I64)); // series_ptrs
sig.params.push(AbiParam::new(types::I64)); // table_count
sig.params.push(AbiParam::new(types::I64)); // state_ptr
sig.returns.push(AbiParam::new(types::I32)); // result code
let func_id = self
.module
.declare_function(name, Linkage::Export, &sig)
.map_err(|e| format!("Failed to declare correlated kernel function: {}", e))?;
let mut ctx = self.module.make_context();
ctx.func.signature = sig;
let mut func_builder_ctx = FunctionBuilderContext::new();
{
let mut builder = FunctionBuilder::new(&mut ctx.func, &mut func_builder_ctx);
let entry_block = builder.create_block();
builder.append_block_params_for_function_params(entry_block);
builder.switch_to_block(entry_block);
builder.seal_block(entry_block);
// Get kernel parameters
let cursor_index = builder.block_params(entry_block)[0];
let series_ptrs = builder.block_params(entry_block)[1];
let _table_count = builder.block_params(entry_block)[2]; // For validation/debugging
let state_ptr = builder.block_params(entry_block)[3];
// Build correlated kernel IR
// Note: table_count is known at compile time from config, used for validation
let result = self.build_correlated_kernel_ir(
&mut builder,
program,
config,
cursor_index,
series_ptrs,
state_ptr,
)?;
builder.ins().return_(&[result]);
builder.finalize();
}
self.module
.define_function(func_id, &mut ctx)
.map_err(|e| format!("Failed to define correlated kernel function: {:?}", e))?;
self.module.clear_context(&mut ctx);
self.module
.finalize_definitions()
.map_err(|e| format!("Failed to finalize correlated kernel: {:?}", e))?;
let code_ptr = self.module.get_finalized_function(func_id);
self.compiled_functions.insert(name.to_string(), code_ptr);
Ok(unsafe { std::mem::transmute(code_ptr) })
}
/// Build correlated kernel IR for multi-series access.
///
/// Handles series access via compile-time resolved indices:
/// - `context.spy` → `series_ptrs[0][cursor_idx]` (if spy mapped to index 0)
/// - `context.vix` → `series_ptrs[1][cursor_idx]` (if vix mapped to index 1)
fn build_correlated_kernel_ir(
&mut self,
builder: &mut FunctionBuilder,
program: &BytecodeProgram,
config: &SimulationKernelConfig,
cursor_index: Value,
series_ptrs: Value,
state_ptr: Value,
) -> Result<Value, String> {
Err("Correlated kernel compilation requires v2 runtime migration".to_string())
}
}