-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Make use of Unary opcodes
#6647
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
Changes from all commits
eece3bf
fd3ef1d
c54b881
15d9e0a
5641bc9
cf8cf21
f12d2f3
50763ce
0696124
f86f0d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,8 @@ use cranelift::codegen::ir::FuncRef; | |
| use cranelift::prelude::*; | ||
| use num_traits::cast::ToPrimitive; | ||
| use rustpython_compiler_core::bytecode::{ | ||
| self, BinaryOperator, BorrowedConstant, CodeObject, ComparisonOperator, Instruction, Label, | ||
| OpArg, OpArgState, UnaryOperator, | ||
| self, BinaryOperator, BorrowedConstant, CodeObject, ComparisonOperator, Instruction, | ||
| IntrinsicFunction1, Label, OpArg, OpArgState, | ||
| }; | ||
| use std::collections::HashMap; | ||
|
|
||
|
|
@@ -474,6 +474,21 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> { | |
| _ => Err(JitCompileError::BadBytecode), | ||
| } | ||
| } | ||
| Instruction::CallIntrinsic1 { func } => { | ||
| match func.get(arg) { | ||
| IntrinsicFunction1::UnaryPositive => { | ||
| match self.stack.pop().ok_or(JitCompileError::BadBytecode)? { | ||
| JitValue::Int(val) => { | ||
| // Nothing to do | ||
| self.stack.push(JitValue::Int(val)); | ||
| Ok(()) | ||
| } | ||
| _ => Err(JitCompileError::NotSupported), | ||
| } | ||
| } | ||
| _ => Err(JitCompileError::NotSupported), | ||
| } | ||
| } | ||
| Instruction::CompareOperation { op, .. } => { | ||
| let op = op.get(arg); | ||
| // the rhs is popped off first | ||
|
|
@@ -620,28 +635,30 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> { | |
| self.stack.swap(i, j); | ||
| Ok(()) | ||
| } | ||
| Instruction::UnaryOperation { op, .. } => { | ||
| let op = op.get(arg); | ||
| Instruction::ToBool => { | ||
| let a = self.stack.pop().ok_or(JitCompileError::BadBytecode)?; | ||
| match (op, a) { | ||
| (UnaryOperator::Minus, JitValue::Int(val)) => { | ||
| // Compile minus as 0 - a. | ||
| let value = self.boolean_val(a)?; | ||
| self.stack.push(JitValue::Bool(value)); | ||
| Ok(()) | ||
| } | ||
| Instruction::UnaryNot => { | ||
| let boolean = match self.stack.pop().ok_or(JitCompileError::BadBytecode)? { | ||
| JitValue::Bool(val) => val, | ||
| _ => return Err(JitCompileError::BadBytecode), | ||
| }; | ||
| let not_boolean = self.builder.ins().bxor_imm(boolean, 1); | ||
| self.stack.push(JitValue::Bool(not_boolean)); | ||
| Ok(()) | ||
| } | ||
| Instruction::UnaryNegative => { | ||
| match self.stack.pop().ok_or(JitCompileError::BadBytecode)? { | ||
| JitValue::Int(val) => { | ||
| // Compile minus as 0 - val. | ||
| let zero = self.builder.ins().iconst(types::I64, 0); | ||
| let out = self.compile_sub(zero, val); | ||
| self.stack.push(JitValue::Int(out)); | ||
| Ok(()) | ||
| } | ||
| (UnaryOperator::Plus, JitValue::Int(val)) => { | ||
| // Nothing to do | ||
| self.stack.push(JitValue::Int(val)); | ||
| Ok(()) | ||
| } | ||
| (UnaryOperator::Not, a) => { | ||
| let boolean = self.boolean_val(a)?; | ||
| let not_boolean = self.builder.ins().bxor_imm(boolean, 1); | ||
| self.stack.push(JitValue::Bool(not_boolean)); | ||
| Ok(()) | ||
| } | ||
| _ => Err(JitCompileError::NotSupported), | ||
| } | ||
| } | ||
|
Comment on lines
+653
to
664
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Search for fneg and Float handling in unary operations
rg -nP --type=rust -C3 'fneg|UnaryNegative.*Float|Float.*UnaryNegative' crates/jit/src/Repository: RustPython/RustPython Length of output: 609 🏁 Script executed: #!/bin/bash
# Check the full context around UnaryNegative and look for Float handling patterns
rg -nP --type=rust -B5 -A10 'Instruction::UnaryNegative' crates/jit/src/Repository: RustPython/RustPython Length of output: 1435 🏁 Script executed: #!/bin/bash
# Search for UnaryPositive to see if it has similar patterns
rg -nP --type=rust -B2 -A8 'Instruction::UnaryPositive' crates/jit/src/Repository: RustPython/RustPython Length of output: 47 🏁 Script executed: #!/bin/bash
# Look for Float handling in other unary or binary operations for reference
rg -nP --type=rust 'JitValue::Float.*=>' crates/jit/src/ | head -20Repository: RustPython/RustPython Length of output: 556 Add Float support to UnaryNegative handler. The implementation handles JitValue::Float(val) => {
let out = self.builder.ins().fneg(val);
self.stack.push(JitValue::Float(out));
Ok(())
}🤖 Prompt for AI Agents |
||
|
|
||
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.
🧩 Analysis chain
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 528
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 5237
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 790
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 2409
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 940
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 1102
Add Float support for unary operations.
The implementation correctly handles
UnaryPositiveforInt, but Float support is missing. This is inconsistent with binary operations throughout the file (Add, Subtract, Multiply, TrueDivide, Power all support Float). BothUnaryPositiveandUnaryNegativeshould handleJitValue::Floatwith the same semantics as Int (identity for+, negate for-).🤖 Prompt for AI Agents