Skip to main content

shape_jit/compiler/
setup.rs

1//! JIT Compiler initialization and setup
2
3use cranelift::prelude::*;
4use cranelift_jit::{JITBuilder, JITModule};
5use std::collections::HashMap;
6
7use crate::context::JITConfig;
8use crate::error::JitError;
9use crate::ffi_symbols::{declare_ffi_functions, register_ffi_symbols};
10
11pub struct JITCompiler {
12    pub(super) module: JITModule,
13    pub(super) builder_context: FunctionBuilderContext,
14    #[allow(dead_code)]
15    pub(super) config: JITConfig,
16    pub(super) compiled_functions: HashMap<String, *const u8>,
17    pub(super) ffi_funcs: HashMap<String, cranelift_module::FuncId>,
18    pub(super) function_table: Vec<*const u8>,
19    /// Maps function_id โ†’ FuncId of their `opt_dc_*` direct-call entry point.
20    /// Used for cross-function speculative direct calls: when function A has
21    /// monomorphic feedback for callee B, and B has already been Tier-2
22    /// compiled, A can emit a direct `call` to B's direct-call entry.
23    pub(super) compiled_dc_funcs: HashMap<u16, (cranelift_module::FuncId, u16)>,
24}
25
26impl JITCompiler {
27    /// Borrow the underlying JITModule (for declaring/defining functions).
28    pub fn module_mut(&mut self) -> &mut JITModule {
29        &mut self.module
30    }
31
32    /// Borrow the FunctionBuilderContext (reused across compilations).
33    pub fn builder_context_mut(&mut self) -> &mut FunctionBuilderContext {
34        &mut self.builder_context
35    }
36}
37
38impl JITCompiler {
39    #[inline(always)]
40    pub fn new(config: JITConfig) -> Result<Self, JitError> {
41        let mut flag_builder = settings::builder();
42        let opt_level_str = if config.opt_level >= 2 {
43            "speed"
44        } else {
45            "speed_and_size"
46        };
47        flag_builder
48            .set("opt_level", opt_level_str)
49            .map_err(|e| JitError::Setup(format!("Failed to set opt_level: {}", e)))?;
50        flag_builder
51            .set("is_pic", "false")
52            .map_err(|e| JitError::Setup(format!("Failed to set is_pic: {}", e)))?;
53
54        let isa_builder = cranelift_native::builder()
55            .map_err(|e| JitError::Setup(format!("Failed to create ISA builder: {}", e)))?;
56        let isa = isa_builder
57            .finish(settings::Flags::new(flag_builder))
58            .map_err(|e| JitError::Setup(format!("Failed to create ISA: {}", e)))?;
59
60        let mut builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
61
62        register_ffi_symbols(&mut builder);
63
64        let mut module = JITModule::new(builder);
65
66        let ffi_funcs = declare_ffi_functions(&mut module);
67
68        Ok(Self {
69            module,
70            builder_context: FunctionBuilderContext::new(),
71            config,
72            compiled_functions: HashMap::new(),
73            ffi_funcs,
74            function_table: Vec::new(),
75            compiled_dc_funcs: HashMap::new(),
76        })
77    }
78}
79
80// ============================================================================
81// KernelCompiler Trait Implementation โ€” REMOVED (W11 / Phase-2c)
82// ============================================================================
83//
84// The `JITKernelCompiler` wrapper and its `impl KernelCompiler for
85// JITKernelCompiler` block are removed because the runtime-side
86// `shape_runtime::simulation` module (with `KernelCompiler`,
87// `KernelCompileConfig`, `SimulationKernelFn`) was bulldozed in
88// commit `2601ba7` ("phase-2c: bulldoze simulation engine subtree").
89// Per the 2026-05-06 defection entry, the simulation engine subtree
90// is deferred to extensions workstream
91// `simulation-kernel-extension-rebuild`.
92//
93// This is a deleted-runtime-side dependency, classified as W11 /
94// deeper Phase-2c per the wave-10 jit-playbook ยง6 ("What's NOT in
95// W10"). The JIT-side wrapper has no surviving consumer trait to
96// implement, so the surface-and-stop response is wholesale removal,
97// not a `todo!()` shell โ€” there is no shape to stub.
98//
99// The compile_simulation_kernel entry point on `JITCompiler` itself
100// remains (it is called only via the now-removed wrapper); its body
101// references shape_runtime types that are also gone, so any future
102// re-introduction must come through the simulation-kernel-extension-
103// rebuild workstream.