Skip to content

Instruction::Resume #5944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2025
Merged

Instruction::Resume #5944

merged 2 commits into from
Jul 11, 2025

Conversation

youknowone
Copy link
Member

@youknowone youknowone commented Jul 11, 2025

Summary by CodeRabbit

  • New Features

    • Introduced explicit resume points in bytecode execution, marking function start and positions after yield, yield from, and await expressions.
    • Added a new instruction to represent these resume points, enhancing bytecode clarity.
    • Replaced star import handling with an intrinsic function call for improved import behavior.
  • Bug Fixes

    • No user-facing bugs addressed.
  • Chores

    • Updated internal handling for new resume instructions, with signal checking logic prepared but currently disabled.

Copy link
Contributor

coderabbitai bot commented Jul 11, 2025

Walkthrough

The changes introduce a new Resume instruction and associated ResumeType enum to the bytecode system. The compiler now emits Resume instructions at function starts and after yield/await points. The VM is updated to recognize and handle the Resume instruction, though its logic is currently a no-op with signal checking commented out. Additionally, the ImportStar instruction is replaced by an intrinsic call.

Changes

File(s) Change Summary
compiler/codegen/src/compile.rs Replaces ImportStar with intrinsic call; emits Resume instructions at function start, after YieldValue, YieldFrom, and await-related instructions.
compiler/core/src/bytecode.rs Adds ResumeType enum and Instruction::Resume variant; removes ImportStar variant; updates formatting and stack effect logic accordingly; adds IntrinsicFunction1::ImportStar.
vm/src/frame.rs Removes ImportStar instruction handling; adds intrinsic function handling for ImportStar; adds handling for Instruction::Resume (currently no-op, signal check commented out).
jit/src/instructions.rs Adds placeholder match arm for Instruction::Resume in JIT instruction addition, currently no-op.

Sequence Diagram(s)

sequenceDiagram
    participant Compiler
    participant Bytecode
    participant VM

    Compiler->>Bytecode: Emit function body
    Compiler->>Bytecode: Emit Resume(AtFuncStart)
    Compiler->>Bytecode: Emit YieldValue/YieldFrom/Await
    Compiler->>Bytecode: Emit Resume(AfterYield/AfterYieldFrom/AfterAwait)
    VM->>Bytecode: Fetch Instruction
    alt Instruction is Resume
        VM->>VM: (Currently no-op, signal check commented out)
    end
Loading

Poem

In bytecode fields where rabbits leap,
New Resume points are sown so deep.
At function start, or after yield,
Each pause and hop is now revealed.
The VM sniffs, but takes no fright—
Awaiting signals, not tonight!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
compiler/core/src/bytecode.rs (2)

27-35: Consider implementing OpArgType for ResumeType for better type safety.

The ResumeType enum is well-defined with appropriate variants and representation. However, to improve type safety and consistency with other instruction-specific enums, consider implementing OpArgType for ResumeType.

+impl OpArgType for ResumeType {
+    fn from_op_arg(x: u32) -> Option<Self> {
+        match x {
+            0 => Some(Self::AtFuncStart),
+            1 => Some(Self::AfterYield),
+            2 => Some(Self::AfterYieldFrom),
+            3 => Some(Self::AfterAwait),
+            _ => None,
+        }
+    }
+    fn to_op_arg(self) -> u32 {
+        self as u32
+    }
+}

577-581: Consider using Arg instead of Arg for better type safety.

The Resume instruction is well-implemented, but using Arg<u32> instead of Arg<ResumeType> reduces type safety and consistency with other instruction designs. This would require implementing OpArgType for ResumeType (see previous comment).

    Resume {
-        arg: Arg<u32>,
+        arg: Arg<ResumeType>,
    },
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8b6c78c and 1469300.

📒 Files selected for processing (3)
  • compiler/codegen/src/compile.rs (10 hunks)
  • compiler/core/src/bytecode.rs (4 hunks)
  • vm/src/frame.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.rs`: Follow the default rustfmt code style (`cargo fmt` to format) Always ...

**/*.rs: Follow the default rustfmt code style (cargo fmt to format)
Always run clippy to lint code (cargo clippy) before completing tasks. Fix any warnings or lints that are introduced by your changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

📄 Source: CodeRabbit Inference Engine (.github/copilot-instructions.md)

List of files the instruction was applied to:

  • vm/src/frame.rs
  • compiler/core/src/bytecode.rs
  • compiler/codegen/src/compile.rs
🧠 Learnings (1)
compiler/core/src/bytecode.rs (1)
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-30T10:08:48.858Z
Learning: Applies to **/*.rs : Follow Rust best practices for error handling and memory management
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run tests under miri
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Run rust tests (windows-latest)
🔇 Additional comments (10)
compiler/core/src/bytecode.rs (2)

1344-1344: LGTM - Correct stack effect for Resume instruction.

The stack effect of 0 is appropriate for the Resume instruction as it's a no-op operation that doesn't modify the stack.


1539-1539: LGTM - Consistent formatting implementation.

The fmt_dis implementation for Resume follows the standard pattern and is consistent with other instructions.

compiler/codegen/src/compile.rs (8)

1531-1537: Correct placement of Resume instruction at function start.

The Resume instruction is properly emitted at the function entry point after the None constant insertion.


3522-3527: Proper Resume instruction after yield expressions.

The Resume instruction with AfterYield type is correctly placed after the YieldValue instruction.


3537-3542: Correct Resume instruction after await expressions.

The Resume instruction with AfterAwait type is properly emitted after the YieldFrom instruction in await expressions.


3559-3564: Appropriate Resume instruction for yield from expressions.

The Resume instruction correctly uses AfterYieldFrom type to distinguish yield from operations from await operations, even though both use the YieldFrom instruction.


1954-1959: Consistent Resume instructions in async with statements.

Both the async with enter and cleanup sections correctly emit Resume instructions with AfterAwait type after their respective YieldFrom operations.

Also applies to: 2001-2006


2045-2050: Proper Resume instruction in async for loops.

The Resume instruction with AfterAwait type is correctly emitted after the YieldFrom operation in async iteration.


1691-1696: Correct Resume instruction in generator expressions.

The Resume instruction with AfterYield type is properly placed after the YieldValue instruction in generator expressions.


4092-4097: Consistent Resume instructions in async comprehensions.

Resume instructions with AfterAwait type are correctly emitted after YieldFrom operations in both async generator comprehensions and async list/set/dict comprehensions.

Also applies to: 4176-4181

vm/src/frame.rs Outdated
Comment on lines 896 to 903
bytecode::Instruction::Resume { arg: resume_arg } => {
// Resume execution after yield, await, or at function start
// In CPython, this checks instrumentation and eval breaker
// For now, we just check for signals/interrupts
let _resume_type = resume_arg.get(arg);

// Temporarily disable signal checking to debug the issue
// Check for interrupts if not resuming from yield_from
// if resume_type < bytecode::ResumeType::AfterYieldFrom as u32 {
// vm.check_signals()?;
// }
Ok(None)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Complete the Resume instruction implementation or document the temporary nature.

The current implementation is essentially a no-op with critical signal checking logic commented out. This could lead to missing important interrupt handling during resume operations.

Issues identified:

  1. Unused variable: _resume_type is extracted but never used
  2. Commented-out logic: The signal checking is disabled "to debug the issue"
  3. Incomplete functionality: The instruction doesn't perform its intended purpose

Consider these approaches:

Option 1: Implement proper signal checking

 bytecode::Instruction::Resume { arg: resume_arg } => {
     // Resume execution after yield, await, or at function start
-    // In CPython, this checks instrumentation and eval breaker
-    // For now, we just check for signals/interrupts
-    let _resume_type = resume_arg.get(arg);
+    let resume_type = resume_arg.get(arg);
 
-    // Temporarily disable signal checking to debug the issue
-    // Check for interrupts if not resuming from yield_from
-    // if resume_type < bytecode::ResumeType::AfterYieldFrom as u32 {
-    //     vm.check_signals()?;
-    // }
+    // Check for interrupts if not resuming from yield_from
+    if resume_type < bytecode::ResumeType::AfterYieldFrom as u32 {
+        vm.check_signals()?;
+    }
     Ok(None)
 }

Option 2: Document the temporary nature

 bytecode::Instruction::Resume { arg: resume_arg } => {
+    // TODO: Temporary no-op implementation while debugging signal issues
+    // This instruction should check for interrupts and handle instrumentation
     // Resume execution after yield, await, or at function start
-    // In CPython, this checks instrumentation and eval breaker
-    // For now, we just check for signals/interrupts
     let _resume_type = resume_arg.get(arg);
 
-    // Temporarily disable signal checking to debug the issue
     // Check for interrupts if not resuming from yield_from
     // if resume_type < bytecode::ResumeType::AfterYieldFrom as u32 {
     //     vm.check_signals()?;
     // }
     Ok(None)
 }

What specific issue is being debugged that requires disabling signal checking? This context would help determine the appropriate approach.


🏁 Script executed:

#!/bin/bash
# Search for ResumeType definition and usages
rg "ResumeType" -n .

# Search for check_signals method to see its typical usage
rg "fn check_signals" -n .

# Search for other occurrences of vm.check_signals to understand context
rg "check_signals" -n vm

Length of output: 2053


Implement signal checking in the Resume handler or document its temporary no-op

The new Instruction::Resume arm in vm/src/frame.rs currently ignores its resume_type and has all interrupt checks commented out. This placeholder will silently skip important vm.check_signals() calls and leave an unused variable.

Issues to address:

  • _resume_type is never used.
  • Critical vm.check_signals()? logic is disabled.
  • No indication of why signal checking is temporarily removed or when it’ll be restored.

Please update one of the following:

• Option A – Restore proper signal checks:

 vm/src/frame.rs
@@ -896,13 +896,15 @@ impl Frame {
             bytecode::Instruction::Resume { arg: resume_arg } => {
-                let _resume_type = resume_arg.get(arg);
+                let resume_type = resume_arg.get(arg);
 
-                // Temporarily disable signal checking to debug the issue
-                // Check for interrupts if not resuming from yield_from
-                // if resume_type < bytecode::ResumeType::AfterYieldFrom as u32 {
-                //     vm.check_signals()?;
-                // }
+                // Check for interrupts unless we’re resuming from a yield_from
+                if resume_type < bytecode::ResumeType::AfterYieldFrom as u32 {
+                    vm.check_signals()?;
+                }
                 Ok(None)
             }

• Option B – Clearly mark as temporary:

 vm/src/frame.rs
@@ -896,6 +896,10 @@ impl Frame {
             bytecode::Instruction::Resume { arg: resume_arg } => {
+                // TODO: temporary no-op while debugging signal issues.
+                // This handler must later:
+                //   1) perform instrumentation/eval breaker checks
+                //   2) re-enable vm.check_signals()
                 let _resume_type = resume_arg.get(arg);
                 Ok(None)
             }

Also, please clarify “what specific issue is being debugged” that required disabling check_signals(), and when you plan to restore full behavior.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
bytecode::Instruction::Resume { arg: resume_arg } => {
// Resume execution after yield, await, or at function start
// In CPython, this checks instrumentation and eval breaker
// For now, we just check for signals/interrupts
let _resume_type = resume_arg.get(arg);
// Temporarily disable signal checking to debug the issue
// Check for interrupts if not resuming from yield_from
// if resume_type < bytecode::ResumeType::AfterYieldFrom as u32 {
// vm.check_signals()?;
// }
Ok(None)
}
bytecode::Instruction::Resume { arg: resume_arg } => {
// Resume execution after yield, await, or at function start
// In CPython, this checks instrumentation and eval breaker
// For now, we just check for signals/interrupts
let resume_type = resume_arg.get(arg);
// Check for interrupts unless we’re resuming from a yield_from
if resume_type < bytecode::ResumeType::AfterYieldFrom as u32 {
vm.check_signals()?;
}
Ok(None)
}
🤖 Prompt for AI Agents
In vm/src/frame.rs around lines 896 to 908, the Resume instruction handler
currently disables signal checking and leaves the resume_type variable unused,
which can cause missed interrupt handling and unclear code intent. To fix this,
either restore the vm.check_signals()? call with the appropriate condition on
resume_type to ensure signals are checked as intended, or if signal checking is
temporarily disabled for debugging, add a clear comment explaining the specific
issue being debugged, why check_signals() is disabled, and when full signal
checking will be restored. Also, remove the unused _resume_type variable if not
used or rename it to _ to avoid warnings.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
jit/src/instructions.rs (1)

615-618: Placeholder implementation is appropriate for current development phase.

The TODO comment clearly indicates the pending work for implementing the Resume instruction. This placeholder approach allows the JIT compiler to recognize the instruction while core functionality is developed elsewhere first.

Would you like me to help implement the Resume instruction functionality for the JIT compiler? I can generate a solution that handles the different resume types (AtFuncStart, AfterYield, AfterYieldFrom, AfterAwait) mentioned in the broader codebase changes.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 92e8735 and 0b22971.

⛔ Files ignored due to path filters (1)
  • compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap is excluded by !**/*.snap
📒 Files selected for processing (4)
  • compiler/codegen/src/compile.rs (11 hunks)
  • compiler/core/src/bytecode.rs (5 hunks)
  • jit/src/instructions.rs (1 hunks)
  • vm/src/frame.rs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • vm/src/frame.rs
  • compiler/core/src/bytecode.rs
  • compiler/codegen/src/compile.rs
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.rs`: Follow the default rustfmt code style (`cargo fmt` to format) Always ...

**/*.rs: Follow the default rustfmt code style (cargo fmt to format)
Always run clippy to lint code (cargo clippy) before completing tasks. Fix any warnings or lints that are introduced by your changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

📄 Source: CodeRabbit Inference Engine (.github/copilot-instructions.md)

List of files the instruction was applied to:

  • jit/src/instructions.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run tests under miri
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run rust tests (macos-latest)

@youknowone youknowone merged commit 392f9c2 into RustPython:main Jul 11, 2025
12 checks passed
@youknowone youknowone deleted the resume branch July 11, 2025 08:25
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.

1 participant