Skip to content
Prev Previous commit
Next Next commit
re-add support for UnaryPositive in JIT
  • Loading branch information
ShaharNaveh committed Jan 5, 2026
commit 5641bc9a986c478d1d469de796783a00723f3015
19 changes: 17 additions & 2 deletions crates/jit/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
self, BinaryOperator, BorrowedConstant, CodeObject, ComparisonOperator, Instruction,
IntrinsicFunction1, Label, OpArg, OpArgState,
};
use std::collections::HashMap;

Expand Down Expand Up @@ -474,6 +474,21 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
_ => Err(JitCompileError::BadBytecode),
}
}
Instruction::CallIntrinsic1 { func } => {
match func {
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),
}
}
Comment on lines +477 to +491
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.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

# Search for UnaryPositive references and Float handling patterns
rg -n 'UnaryPositive' crates/jit/src/ --type rust -C 2

Repository: RustPython/RustPython

Length of output: 528


🏁 Script executed:

# Check if Float is handled for other unary operations
rg -n 'JitValue::Float' crates/jit/src/instructions.rs -C 3 | head -100

Repository: RustPython/RustPython

Length of output: 5237


🏁 Script executed:

# Search for IntrinsicFunction1 definition to understand what operations exist
rg -n 'enum IntrinsicFunction1|IntrinsicFunction1::' crates/jit/src/ --type rust -B2 -A5

Repository: RustPython/RustPython

Length of output: 790


🏁 Script executed:

# Check if there are any Float support patterns in the codebase
rg -n 'Float.*=>|JitValue::Float' crates/jit/src/instructions.rs --type rust -B2 -A2 | head -50

Repository: RustPython/RustPython

Length of output: 2409


🏁 Script executed:

# Search for other unary operations to see the pattern
rg -n 'UnaryNegative|UnaryInvert|UnaryNot' crates/jit/src/instructions.rs --type rust -B2 -A8

Repository: RustPython/RustPython

Length of output: 940


🏁 Script executed:

# Get more context for UnaryNegative to see if Float is handled
rg -n 'Instruction::UnaryNegative' crates/jit/src/instructions.rs --type rust -A20

Repository: RustPython/RustPython

Length of output: 1102


Add Float support for unary operations.

The implementation correctly handles UnaryPositive for Int, but Float support is missing. This is inconsistent with binary operations throughout the file (Add, Subtract, Multiply, TrueDivide, Power all support Float). Both UnaryPositive and UnaryNegative should handle JitValue::Float with the same semantics as Int (identity for +, negate for -).

🤖 Prompt for AI Agents
In @crates/jit/src/instructions.rs around lines 477-491, The unary intrinsic
handling in Instruction::CallIntrinsic1 { func } currently only supports
JitValue::Int for IntrinsicFunction1::UnaryPositive; update the match arms so
both IntrinsicFunction1::UnaryPositive and IntrinsicFunction1::UnaryNegative
handle JitValue::Float as well (for UnaryPositive push the same JitValue::Float
back, for UnaryNegative push a JitValue::Float with the negated value),
mirroring the existing Int semantics around the
self.stack.pop().ok_or(JitCompileError::BadBytecode)? pattern and returning the
same error branches for unsupported types.

Instruction::CompareOperation { op, .. } => {
let op = op.get(arg);
// the rhs is popped off first
Expand Down
Loading