Skip to content

Refactor JIT codegen into submodules (gradual)#2

Merged
SSL-ACTX merged 2 commits intomainfrom
refactor/jit-codegen-submodules
Mar 18, 2026
Merged

Refactor JIT codegen into submodules (gradual)#2
SSL-ACTX merged 2 commits intomainfrom
refactor/jit-codegen-submodules

Conversation

@SSL-ACTX
Copy link
Copy Markdown
Owner

This pull request introduces a modular refactor and significant enhancements to the JIT (Just-In-Time) code generation pipeline for the Iris project. The changes separate key components into their own files, add new type definitions, and implement logic for lowering parsed expressions into optimized kernel representations. The main goals are to improve maintainability, enable better kernel detection for vectorization, and provide a foundation for profiling and variant strategies.

The most important changes are:

JIT Kernel Lowering and Detection:

  • Introduced src/py/jit/codegen/lowering.rs with logic to analyze parsed expressions (Expr) and detect when they can be lowered into unary or binary kernels, or further into a generic lowered expression tree. This enables the JIT to recognize and optimize simple operations for vectorization and efficient execution.
  • Added helpers such as arg_index_of_var, normalize_intrinsic_name, and detect_lowered_expr to support the lowering process.

Type Definitions and Kernel Metadata:

  • Added src/py/jit/codegen/jit_types.rs (notably as types.rs in the file header) containing shared type definitions for the JIT pipeline, including enums for reduction modes, return types, kernel types, variant strategies, and profiling structures. This centralizes and clarifies the metadata associated with JIT-compiled functions.

JIT Compilation and Variant Strategy:

  • Refactored src/py/jit/codegen/compiler.rs to use the new lowering logic, and to support compiling multiple speculative variants of the same expression (e.g., optimized, scalar fallback, and experimental fast trig). This supports dynamic selection of the best-performing variant at runtime.

JIT Module Initialization and Profiling:

  • Added src/py/jit/codegen/jit_module.rs to encapsulate thread-local JIT module setup, backend selection (including SIMD capabilities), and function pointer profiling. This improves initialization robustness and enables per-thread JIT state management.
  • Introduced the JitExecProfile enum and thread-local storage for profiling execution signatures of JIT-compiled functions.

These changes lay the groundwork for more advanced JIT optimizations, better profiling, and easier extension of the codegen pipeline.

This is a partial refactor: moved JIT quantum/profiling state and type helpers into dedicated submodules (codegen/jit_types.rs, codegen/quantum.rs, codegen/registry.rs).
@SSL-ACTX SSL-ACTX self-assigned this Mar 18, 2026
@SSL-ACTX SSL-ACTX added the enhancement New feature or request label Mar 18, 2026
Copilot AI review requested due to automatic review settings March 18, 2026 18:56
@SSL-ACTX SSL-ACTX merged commit c36bb45 into main Mar 18, 2026
4 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the Python JIT codegen implementation into smaller, focused submodules and adjusts JIT logging to use tracing instead of direct stderr printing.

Changes:

  • Split the monolithic src/py/jit/codegen/mod.rs into dedicated modules (jit_types, compiler, gen_expr, lowering, jit_module, registry, quantum).
  • Switch jit_log output from eprintln! to tracing::info!.
  • Remove a couple of config re-exports from src/py/jit/mod.rs that are no longer needed.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/py/jit/mod.rs Drops re-exports for two quantum config helpers (now referenced directly where needed).
src/py/jit/config.rs Routes JIT logging through tracing and keeps the test hook path.
src/py/jit/codegen/mod.rs Becomes a façade that wires together the new codegen submodules and re-exports their APIs.
src/py/jit/codegen/jit_types.rs New: centralizes shared JIT type definitions (entry types, lowered kernels, strategies, profile structs).
src/py/jit/codegen/compiler.rs New: holds top-level compilation functions extracted from the old monolith.
src/py/jit/codegen/gen_expr.rs New: extracted expression → Cranelift IR lowering.
src/py/jit/codegen/lowering.rs New: extracted lowered-kernel detection helpers.
src/py/jit/codegen/jit_module.rs New: extracted TLS JIT module setup and execution profiling state.
src/py/jit/codegen/registry.rs New: extracted registry and iris_jit_invoke_* helper exports.
src/py/jit/codegen/quantum.rs New: extracted quantum profiling / variant selection state machine and helpers.

You can also share your feedback on Copilot code review. Take the survey.


use crate::py::jit::heuristics;
use crate::py::jit::parser::Expr;

}

eprintln!("{}", msg());
// Use `tracing` when available; falls back to stderr if no subscriber is set.
@@ -0,0 +1,149 @@
// src/py/jit/codegen/types.rs
Comment on lines +47 to +50
fn invoke_named_entry(func_ptr: i64, args: &[f64]) -> f64 {
let f: extern "C" fn(*const f64) -> f64 = unsafe { std::mem::transmute(func_ptr as usize) };
f(args.as_ptr())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants