-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Unify BINARY_OP bytecodes #6317
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
Conversation
WalkthroughReplaces separate Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Codegen
participant Bytecode
participant VM
participant JIT
Note over Codegen,Bytecode: Compile-time
Codegen->>Bytecode: emit Instruction::BinaryOp { op } (use op.as_inplace() when inplace)
Note right of Bytecode: BinaryOp holds BinaryOperator (incl. inplace variants)
Note over VM,JIT: Runtime
VM->>Bytecode: fetch Instruction::BinaryOp { op }
VM->>VM: match op
alt inplace variant
VM->>VM: invoke corresponding _i* method (e.g., _iadd, _isub, _imul)
else non-inplace
VM->>VM: invoke non-inplace method (e.g., _add, _sub, _truediv)
end
VM->>VM: push result on stack
JIT->>Bytecode: receive/translate BinaryOp { op }
JIT->>JIT: lower op (TrueDivide/Remainder adjustments) and emit native sequence
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used📓 Path-based instructions (1)**/*.rs📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
⏰ 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)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
crates/codegen/src/compile.rs(2 hunks)crates/compiler-core/src/bytecode.rs(4 hunks)crates/vm/src/frame.rs(2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style by runningcargo fmtto format Rust code
Always run clippy to lint Rust code (cargo clippy) before completing tasks and fix any warnings or lints introduced by 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
Files:
crates/codegen/src/compile.rscrates/compiler-core/src/bytecode.rscrates/vm/src/frame.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). (9)
- GitHub Check: Check the WASM package and demo
- GitHub Check: Run snippets and cpython tests on wasm-wasi
- GitHub Check: Check Rust code with clippy
- GitHub Check: Run tests under miri
- GitHub Check: Run rust tests (macos-latest)
- GitHub Check: Run rust tests (windows-latest)
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Ensure compilation on various targets
🔇 Additional comments (9)
crates/codegen/src/compile.rs (2)
3304-3307: LGTM on the binary operation refactor in pattern matching.The subtraction operation for computing negative sequence indices is correctly updated to use the unified
Instruction::BinaryOpwithBinaryOperator::Subtract.
4341-4359: VerifyBinaryOperator::as_inplace()method availability.The operator mapping is exhaustive and correctly structured, but the implementation depends on the
as_inplace()method existing on theBinaryOperatortype. This method must be verified to exist and properly convert each binary operator to its in-place variant. Confirm the method is implemented in the bytecode module before merging.crates/vm/src/frame.rs (2)
871-871: LGTM! Instruction consolidation aligns with PR objective.The unification of binary operation instruction variants into a single
BinaryOp { op }is correctly implemented.
2117-2153: LGTM! Binary operation consolidation is well-implemented.The unified
execute_bin_opmethod correctly handles both regular and inplace operations. The operator naming (TrueDivide, Remainder) aligns with Python 3 semantics, and all inplace variants follow a consistent pattern.crates/compiler-core/src/bytecode.rs (5)
597-599: LGTM! Instruction enum updated correctly.The consolidation into a single
BinaryOp { op }variant aligns with the PR objective and matches the usage in frame.rs.
1106-1160: LGTM! Comprehensive BinaryOperator enum definition.The expanded enum with explicit values and symbol annotations provides a complete set of binary operations, ensuring stable bytecode representation.
1192-1224: LGTM! Complete and correct Display implementation.All binary operators are mapped to their correct symbol representations, covering both regular and inplace variants.
1620-1620: LGTM! Correct stack effect for unified BinaryOp.The stack effect of -1 is correct for all binary operations (pops 2 operands, pushes 1 result).
1825-1825: LGTM! Disassembly formatting updated correctly.The fmt_dis implementation correctly displays the operator symbol using the Display trait, producing clear disassembly output like "BINARY_OP +".
There was a problem hiding this 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
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
crates/compiler-core/src/bytecode.rs(4 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style by runningcargo fmtto format Rust code
Always run clippy to lint Rust code (cargo clippy) before completing tasks and fix any warnings or lints introduced by 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
Files:
crates/compiler-core/src/bytecode.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). (7)
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Run tests under miri
- GitHub Check: Check the WASM package and demo
- GitHub Check: Check Rust code with clippy
- GitHub Check: Run rust tests (windows-latest)
- GitHub Check: Ensure compilation on various targets
🔇 Additional comments (7)
crates/compiler-core/src/bytecode.rs (7)
597-599: LGTM! Instruction unification aligns with CPython.The unification of
BinaryOperationandBinaryOperationInplaceinto a singleBinaryOpinstruction correctly implements the CPython approach referenced in the PR objectives.
1094-1105: LGTM! Documentation example is now correct.The documentation example correctly imports the required types and demonstrates proper usage of the
BinaryOpinstruction. This addresses the previous review feedback.
1107-1161: LGTM! Complete operator set with clear documentation.All 13 basic binary operators and their 13 in-place variants are properly defined with clear symbol documentation. The enum values (0-25) are sequential and fit well within the u8 representation.
1174-1191: LGTM! Complete and correct mapping inas_inplace().All 13 non-inplace operators correctly map to their in-place equivalents, and the wildcard arm properly handles operators that are already in-place. This addresses the previous review feedback about the missing
And => InplaceAndmapping.
1194-1226: LGTM! Comprehensive Display implementation.All 26 operators have their correct string representations, making disassembly output clear and readable. The implementation correctly distinguishes between non-inplace (e.g., "+") and in-place (e.g., "+=") operators.
1622-1622: LGTM! Stack effect correctly unified.The unified
BinaryOpinstruction correctly returns a stack effect of -1, which is appropriate for all binary operations (pop 2 operands, push 1 result).
1827-1827: LGTM! Improved disassembly formatting.The formatting now uses the
Displaytrait implementation, producing clearer output with operator symbols (e.g., "BINARY_OP +" or "BINARY_OP +=") instead of enum variant names.
youknowone
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Great, thank you!
Implements python/cpython#29482
Summary by CodeRabbit
Refactor
User-visible
✏️ Tip: You can customize this high-level summary in your review settings.