From a7272a132db9ba1435ef997c717049db60b11c44 Mon Sep 17 00:00:00 2001 From: Lee Dogeon Date: Thu, 5 Mar 2026 15:19:56 +0900 Subject: [PATCH 1/2] Fold const bool with unary not --- crates/codegen/src/compile.rs | 21 +++++++++++++++++++ crates/codegen/src/ir.rs | 18 ++++++++++++++++ ...en__compile__tests__const_bool_not_op.snap | 14 +++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 crates/codegen/src/snapshots/rustpython_codegen__compile__tests__const_bool_not_op.snap diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index 6f7d8c15236..b4e24a30461 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -9069,6 +9069,18 @@ mod tests { fn compile_exec(source: &str) -> CodeObject { let opts = CompileOpts::default(); + compile_exec_with_options(source, opts) + } + + fn compile_exec_optimized(source: &str) -> CodeObject { + let opts = CompileOpts { + optimize: 1, + ..CompileOpts::default() + }; + compile_exec_with_options(source, opts) + } + + fn compile_exec_with_options(source: &str, opts: CompileOpts) -> CodeObject { let source_file = SourceFileBuilder::new("source_path", source).finish(); let parsed = ruff_python_parser::parse( source_file.source_text(), @@ -9137,6 +9149,15 @@ x = Test() and False or False )); } + #[test] + fn test_const_bool_not_op() { + assert_dis_snapshot!(compile_exec_optimized( + "\ +x = not True +" + )); + } + #[test] fn test_nested_double_async_with() { assert_dis_snapshot!(compile_exec( diff --git a/crates/codegen/src/ir.rs b/crates/codegen/src/ir.rs index 43a2dfa5107..5d69d202bc4 100644 --- a/crates/codegen/src/ir.rs +++ b/crates/codegen/src/ir.rs @@ -693,6 +693,24 @@ impl CodeInfo { None } } + (Instruction::LoadConst { consti }, Instruction::UnaryNot) => { + let constant = &self.metadata.consts[consti.get(curr.arg) as usize]; + match constant { + ConstantData::Boolean { value } => { + let (const_idx, _) = self + .metadata + .consts + .insert_full(ConstantData::Boolean { value: !value }); + Some(( + (Instruction::LoadConst { + consti: Arg::marker(), + }), + OpArg::new(const_idx as u32), + )) + } + _ => None, + } + } _ => None, } }; diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__const_bool_not_op.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__const_bool_not_op.snap new file mode 100644 index 00000000000..6e0b4bf8b26 --- /dev/null +++ b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__const_bool_not_op.snap @@ -0,0 +1,14 @@ +--- +source: crates/codegen/src/compile.rs +expression: "compile_exec_optimized(\"\\\nx = not True\n\")" +--- + 1 0 RESUME (0) + 1 LOAD_CONST (True) + 2 TO_BOOL + 3 CACHE + 4 CACHE + 5 CACHE + 6 UNARY_NOT + 7 STORE_NAME (0, x) + 8 LOAD_CONST (None) + 9 RETURN_VALUE From 573fe02ca58ae5c13f78c88b960e60ea6becf3d7 Mon Sep 17 00:00:00 2001 From: Lee Dogeon Date: Thu, 5 Mar 2026 15:45:07 +0900 Subject: [PATCH 2/2] Fold unnecessary TO_BOOL --- crates/codegen/src/ir.rs | 9 +++++++++ ..._codegen__compile__tests__const_bool_not_op.snap | 13 ++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/codegen/src/ir.rs b/crates/codegen/src/ir.rs index 5d69d202bc4..52499664fe0 100644 --- a/crates/codegen/src/ir.rs +++ b/crates/codegen/src/ir.rs @@ -693,6 +693,15 @@ impl CodeInfo { None } } + (Instruction::LoadConst { consti }, Instruction::ToBool) => { + let consti = consti.get(curr.arg); + let constant = &self.metadata.consts[consti as usize]; + if let ConstantData::Boolean { .. } = constant { + Some((curr_instr, OpArg::from(consti))) + } else { + None + } + } (Instruction::LoadConst { consti }, Instruction::UnaryNot) => { let constant = &self.metadata.consts[consti.get(curr.arg) as usize]; match constant { diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__const_bool_not_op.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__const_bool_not_op.snap index 6e0b4bf8b26..f9a74c2055c 100644 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__const_bool_not_op.snap +++ b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__const_bool_not_op.snap @@ -3,12 +3,7 @@ source: crates/codegen/src/compile.rs expression: "compile_exec_optimized(\"\\\nx = not True\n\")" --- 1 0 RESUME (0) - 1 LOAD_CONST (True) - 2 TO_BOOL - 3 CACHE - 4 CACHE - 5 CACHE - 6 UNARY_NOT - 7 STORE_NAME (0, x) - 8 LOAD_CONST (None) - 9 RETURN_VALUE + 1 LOAD_CONST (False) + 2 STORE_NAME (0, x) + 3 LOAD_CONST (None) + 4 RETURN_VALUE