Skip to content

Tuple Support and WIP Object Support in JIT#8188

Open
LuigiTheJokesterAlvarez wants to merge 6 commits into
RustPython:mainfrom
LuigiTheJokesterAlvarez:abi-fix
Open

Tuple Support and WIP Object Support in JIT#8188
LuigiTheJokesterAlvarez wants to merge 6 commits into
RustPython:mainfrom
LuigiTheJokesterAlvarez:abi-fix

Conversation

@LuigiTheJokesterAlvarez

@LuigiTheJokesterAlvarez LuigiTheJokesterAlvarez commented Jun 28, 2026

Copy link
Copy Markdown

Summary

This changes the JIT to support tuples and possibly, objects

Summary by CodeRabbit

  • New Features

    • Added JIT support for heap-backed object-style tuples and improved handling of composite values at runtime.
    • Extended the JIT ABI/type system to pass and return object/pointer-like values via the compiled calling convention.
  • Bug Fixes

    • Fixed tuple unpacking to work with the new heap-backed tuple representation.
    • Improved variable load/store behavior for scalar vs object values, including corresponding bytecode handling.
    • Tightened boolean conversion to reject unsupported value kinds.
    • Updated return handling to establish the return type from the first return path.

@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The JIT adds heap-backed object values for tuples, with new shape metadata, allocator wiring, ABI support, and updated tuple build/unpack and variable conversion paths. __jit__ also has a formatting-only edit.

Changes

Heap-backed JIT Tuple Objects

Layer / File(s) Summary
JitType::Object and AbiValue::Object contracts
crates/jit/src/lib.rs
Adds Object to JitType and AbiValue, extends lowering and marshalling helpers, and adds usize conversion support for object values.
ObjectKind and JitValue::Object
crates/jit/src/instructions.rs
Introduces ObjectKind, ElementShape, and TupleShape, and changes Local and JitValue to represent heap-backed objects instead of in-register tuple vectors.
jit_alloc_tuple allocator and FunctionCompiler wiring
crates/jit/src/lib.rs, crates/jit/src/instructions.rs
Adds jit_alloc_tuple, imports it into the JIT module, threads the imported allocator through function compilation, and stores it on FunctionCompiler.
local_from_value and local_to_value
crates/jit/src/instructions.rs
Adds scalar/object local conversion helpers, rewrites variable storage and load paths to use them, and rejects unsupported object-like values in boolean conversion.
Heap tuple build and unpack
crates/jit/src/instructions.rs
Compiles tuple constants into heap tuples, adds heap tuple build and unpack helpers, changes tuple bytecode to allocate and read the new representation, and relaxes return signature handling on first return.
Whitespace edits
crates/jit/src/instructions.rs, crates/vm/src/builtins/function.rs
Adds blank lines in compile_fpow and __jit__; no logic changes.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Suggested reviewers

  • youknowone

Poem

🐇 Heap tuples hop so neat,
Shapes and pointers in one beat.
jit_alloc_tuple lends a hand,
Objects now can take their stand.
A rabbit grins, ears in the breeze,
With packed-up tuples and happy keys.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 48.48% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: JIT tuple support with work-in-progress object support.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
crates/jit/src/instructions.rs (1)

55-60: 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift

Preserve tuple shape for object arguments before supporting tuple unpacking there.

JitType::Object arguments are materialized as ObjectKind::Opaque, but UnpackSequence only accepts ObjectKind::Tuple. A JIT function taking a tuple argument and unpacking it will reject its own object ABI value despite the new tuple support.

Also applies to: 878-884

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/jit/src/instructions.rs` around lines 55 - 60, In from_type_and_value,
JitType::Object is currently always wrapped as ObjectKind::Opaque, which breaks
tuple arguments that later flow into UnpackSequence. Update the object-argument
materialization path in from_type_and_value so tuple-shaped values are preserved
as ObjectKind::Tuple when the ABI value represents a tuple, and keep
UnpackSequence aligned to accept that preserved shape instead of rejecting the
function’s own argument value.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/jit/src/instructions.rs`:
- Around line 155-161: The changed Rust code needs rustfmt cleanup, with several
signatures and calls in functions like local_to_value and other touched Rust
sections needing standard formatting and trailing whitespace removed. Run cargo
fmt on the affected code so the updated blocks follow the default Rust style
consistently across the modified instructions and nearby changed lines.
- Around line 429-435: The return ABI handling in build_function/instructions.rs
is being mutated too late and can diverge across branches, so make the return
signature fixed before any return emission and enforce that all subsequent
returns match the first inferred ret_ty. Update the logic around self.sig.ret
and builder.func.signature.returns so the signature is established once, then
validate or reject mixed return types in the return-building path rather than
appending/changing ABI params later.

In `@crates/jit/src/lib.rs`:
- Around line 41-50: Add a matching deallocation path for the heap tuples
allocated by jit_alloc_tuple, since BuildTuple and tuple constants currently
return raw allocations with no owner or free. Introduce a clear ownership
strategy in crates/jit/src/lib.rs, and ensure the JIT runtime exposes a
corresponding free/release function or otherwise transfers ownership so tuple
allocations are reclaimed after use.

---

Outside diff comments:
In `@crates/jit/src/instructions.rs`:
- Around line 55-60: In from_type_and_value, JitType::Object is currently always
wrapped as ObjectKind::Opaque, which breaks tuple arguments that later flow into
UnpackSequence. Update the object-argument materialization path in
from_type_and_value so tuple-shaped values are preserved as ObjectKind::Tuple
when the ABI value represents a tuple, and keep UnpackSequence aligned to accept
that preserved shape instead of rejecting the function’s own argument value.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: e9eeb398-7e6d-4522-bc07-630d242533be

📥 Commits

Reviewing files that changed from the base of the PR and between 0009dd6 and bdf64b7.

📒 Files selected for processing (3)
  • crates/jit/src/instructions.rs
  • crates/jit/src/lib.rs
  • crates/vm/src/builtins/function.rs

Comment thread crates/jit/src/instructions.rs Outdated
Comment on lines +429 to +435
// single abi type
if self.sig.ret.is_none() {
self.sig.ret = Some(ret_ty.clone());
self.builder.func.signature
.returns
.push(AbiParam::new(ret_ty.to_cranelift()));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🔴 Critical | 🏗️ Heavy lift

Keep return signatures consistent before emitting returns.

build_function declares the Cranelift function before compilation, but this block mutates builder.func.signature.returns later and also allows later returns with a different ret_ty. For inferred returns, this can leave the declared function/call refs with the wrong ABI; for mixed branches, callers may treat an i64 int as an object pointer.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/jit/src/instructions.rs` around lines 429 - 435, The return ABI
handling in build_function/instructions.rs is being mutated too late and can
diverge across branches, so make the return signature fixed before any return
emission and enforce that all subsequent returns match the first inferred
ret_ty. Update the logic around self.sig.ret and builder.func.signature.returns
so the signature is established once, then validate or reject mixed return types
in the return-building path rather than appending/changing ABI params later.

Comment thread crates/jit/src/lib.rs
Comment on lines +41 to +50
extern "C" fn jit_alloc_tuple(len: i64) -> i64 {
let layout = Layout::array::<i64>(len as usize + 1).unwrap();
let ptr = unsafe { alloc(layout) };
if ptr.is_null() {
handle_alloc_error(layout);
}
unsafe {
*(ptr as *mut i64) = len;
}
ptr as i64

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

Add an ownership/freeing strategy for heap tuples.

jit_alloc_tuple allocates with the global allocator and returns a raw pointer, but this PR adds no matching deallocation path. Since BuildTuple and tuple constants call this allocator at runtime, repeated JIT invocations leak every tuple allocation.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/jit/src/lib.rs` around lines 41 - 50, Add a matching deallocation path
for the heap tuples allocated by jit_alloc_tuple, since BuildTuple and tuple
constants currently return raw allocations with no owner or free. Introduce a
clear ownership strategy in crates/jit/src/lib.rs, and ensure the JIT runtime
exposes a corresponding free/release function or otherwise transfers ownership
so tuple allocations are reclaimed after use.

@ShaharNaveh ShaharNaveh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi, thanks for contributing to RustPython:)

Can you please check the AI policy? (#8188 (comment))

Comment thread crates/jit/src/instructions.rs Outdated
@@ -1,6 +1,8 @@
// instructions.rs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the reasoning for this comment?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

when i forget what im doing i comment it out to make myself know again what im doing

yeah it's weird

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ill delete unnecessary comments and if i can right now, document the functions i did, and making the changes coderabbit requested

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i did documentation yesterday lol

Comment thread crates/jit/src/lib.rs Outdated
@@ -1,7 +1,9 @@
// lib.rs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants