diff --git a/crates/stela-compiler/src/x86_64/integer_math.rs b/crates/stela-compiler/src/x86_64/integer_math.rs index c25d7dac63..553a8fc599 100644 --- a/crates/stela-compiler/src/x86_64/integer_math.rs +++ b/crates/stela-compiler/src/x86_64/integer_math.rs @@ -25,10 +25,12 @@ fn size(width: Width) -> Size { } pub(super) fn emit_with(asm: &mut Asm, operand: &Operand, target: TargetCpu) { - if let Kind::Extract { width, amount } = operand.kind - && operand.left != operand.right - { - extract_distinct(asm, operand, width, amount); + if let Kind::Extract { width, amount } = operand.kind { + if operand.left == operand.right { + rotate_immediate(asm, operand, width, amount, target); + } else { + extract_distinct(asm, operand, width, amount); + } return; } load_with(asm, operand.left); @@ -155,13 +157,6 @@ pub(super) fn emit_with(asm: &mut Asm, operand: &Operand, target: TargetCpu) { store_with(asm, operand.destination); return; } - Kind::Extract { width, amount } => { - asm.shrd_imm(size(width), Rcx, Rax, amount); - // The W move also clears the upper half at shift zero. - asm.mov(size(width), Rax, Rcx); - store_with(asm, operand.destination); - return; - } _ => {} } if let Kind::Divide { signed, width } = operand.kind { @@ -213,6 +208,36 @@ pub(super) fn emit_with(asm: &mut Asm, operand: &Operand, target: TargetCpu) { store_with(asm, operand.destination); } +fn rotate_immediate(asm: &mut Asm, operand: &Operand, width: Width, amount: u8, target: TargetCpu) { + let destination = asm.guest(operand.destination); + let result = match destination { + Some(Rm::Reg(host)) => host, + _ => Rax, + }; + let size = size(width); + if let Some(source) = asm.guest(operand.left) { + // A resident in-place rotate has a shorter encoding than RORX. + let in_place = source == Rm::Reg(result); + if amount != 0 && !in_place && target.supports(TargetCpu::Bmi2) { + asm.rorx(size, result, source, amount); + } else { + // At count zero the move keeps a source-map range and provides + // the required zero-extension for W, even in place. + if amount == 0 || !in_place { + asm.mov_load(size, result, source); + } + if amount != 0 { + asm.shift_imm(ShiftOp::Ror, size, result, amount); + } + } + } else { + asm.alu(Alu::Xor, Size::Dword, result, result); + } + if let Some(Rm::Mem(slot)) = destination { + asm.mov_store(Size::Qword, slot, result); + } +} + fn extract_distinct(asm: &mut Asm, operand: &Operand, width: Width, amount: u8) { if amount == 0 { // The low source is the result, with a W write clearing high bits. diff --git a/crates/stela-compiler/src/x86_64/integer_math/extract_tests.rs b/crates/stela-compiler/src/x86_64/integer_math/extract_tests.rs index 10c4542d63..c550933e7d 100644 --- a/crates/stela-compiler/src/x86_64/integer_math/extract_tests.rs +++ b/crates/stela-compiler/src/x86_64/integer_math/extract_tests.rs @@ -7,6 +7,8 @@ use crate::{ // EXTR X16,X17,X16,#63 from the archived OpenSSL RSA workload. const OPENSSL: (GuestAddress, u32) = (GuestAddress(0xef008), 0x93d0_fe30); +// ROR X3,X14,#33 from the archived xxHash workload is an EXTR alias. +const XXHASH: (GuestAddress, u32) = (GuestAddress(0x10000b450), 0x93ce_85c3); fn compile(pc: GuestAddress, word: u32, target: TargetCpu, profile: BlockProfile) -> CompiledBlock { CompilationPlan::new(&word.to_le_bytes(), pc, &Options::new(profile, target)) @@ -15,6 +17,15 @@ fn compile(pc: GuestAddress, word: u32, target: TargetCpu, profile: BlockProfile .unwrap() } +fn old_extract(code: &mut Code, operand: &Operand, amount: u8) { + let mut asm = code.asm(); + load_with(&mut asm, operand.left); + right_operand_with(&mut asm, operand.right, Width::X64, Shift::Lsl, 0); + asm.shrd_imm(Size::Qword, Rcx, Rax, amount); + asm.mov(Size::Qword, Rax, Rcx); + store_with(&mut asm, operand.destination); +} + #[test] fn extract_commits_shrd_without_a_result_move() { let (pc, word) = OPENSSL; @@ -27,12 +38,7 @@ fn extract_commits_shrd_without_a_result_move() { (system, Code::pinned as fn() -> Code, 17, 5, 1), ] { let mut old = new_code(); - let mut asm = old.asm(); - load_with(&mut asm, operand.left); - right_operand_with(&mut asm, operand.right, Width::X64, Shift::Lsl, 0); - asm.shrd_imm(Size::Qword, Rcx, Rax, 63); - asm.mov(Size::Qword, Rax, Rcx); - store_with(&mut asm, operand.destination); + old_extract(&mut old, &operand, 63); assert_eq!((old.len(), old.instructions()), (old_bytes, 5)); for target in [ @@ -56,6 +62,64 @@ fn extract_commits_shrd_without_a_result_move() { } } +#[test] +fn extract_alias_uses_a_native_rotate_for_each_tier() { + let (pc, word) = XXHASH; + let Operation::IntegerMath(operand) = Decoder.lift(pc, word).unwrap().operation else { + panic!("decoded ROR alias"); + }; + let system = BlockProfile::new(Features::SYSTEM_MODE); + for (profile, new_code, old_bytes, fallback_bytes, bmi2_bytes) in [ + ( + BlockProfile::ORDINARY, + Code::new as fn() -> Code, + 29, + 18, + 17, + ), + (system, Code::pinned as fn() -> Code, 20, 12, 11), + ] { + let mut old = new_code(); + old_extract(&mut old, &operand, 33); + assert_eq!((old.len(), old.instructions()), (old_bytes, 5)); + + for target in [ + TargetCpu::Baseline, + TargetCpu::Ssse3, + TargetCpu::Sse41, + TargetCpu::Baseline.with_data_independent_timing(), + TargetCpu::Sse42.with_bmi2(), + TargetCpu::X86_64_V3, + TargetCpu::X86_64_V3.with_data_independent_timing(), + ] { + let mut result = new_code(); + emit(&mut result, &operand, target); + let expected = if target.supports(TargetCpu::Bmi2) { + (bmi2_bytes, 2) + } else { + (fallback_bytes, 3) + }; + assert_eq!((result.len(), result.instructions()), expected); + let actual = compile(pc, word, target, profile); + assert_eq!(actual.native.source_map, [(pc, 0)]); + assert!(actual.native.x86_64_code.starts_with(&result)); + assert!(actual.fault_recovery.is_empty()); + } + } +} + +#[test] +fn extract_alias_zero_count_clears_a_pinned_w_destination() { + let pc = XXHASH.0; + let word = 0x1390_0210; // EXTR W16,W16,W16,#0. + let system = BlockProfile::new(Features::SYSTEM_MODE); + for target in [TargetCpu::Baseline, TargetCpu::Sse42.with_bmi2()] { + let block = compile(pc, word, target, system); + assert_eq!(block.native.source_map, [(pc, 0)]); + assert!(block.native.x86_64_code.starts_with(&[0x45, 0x8b, 0xe4])); // mov r12d,r12d + } +} + #[cfg(all(target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))] #[test] fn extract_matches_arm_concatenation_in_frame_and_pinned_code() { @@ -64,8 +128,6 @@ fn extract_matches_arm_concatenation_in_frame_and_pinned_code() { x86_64::{native::run, pinned}, }; - // ROR X3,X14,#33 from the archived xxHash workload is an EXTR alias. - let xxhash = (GuestAddress(0x10000b450), 0x93ce_85c3); let check = |pc: GuestAddress, word: u32| { let wide = word >> 31 != 0; let bits = if wide { 64 } else { 32 }; @@ -76,8 +138,15 @@ fn extract_matches_arm_concatenation_in_frame_and_pinned_code() { for profile in [BlockProfile::ORDINARY, system] { for target in [ TargetCpu::Baseline, - TargetCpu::X86_64_V3.with_data_independent_timing(), + TargetCpu::Baseline.with_data_independent_timing(), + TargetCpu::Sse42.with_bmi2(), + TargetCpu::Sse42.with_bmi2().with_data_independent_timing(), ] { + if target.supports(TargetCpu::Bmi2) + && !TargetCpu::detect().supports(TargetCpu::Bmi2) + { + continue; + } let block = compile(pc, word, target, profile); let code = if profile == system { pinned::entered(&block.native.x86_64_code) @@ -102,7 +171,7 @@ fn extract_matches_arm_concatenation_in_frame_and_pinned_code() { } }; check(OPENSSL.0, OPENSSL.1); - check(xxhash.0, xxhash.1); + check(XXHASH.0, XXHASH.1); for wide in [false, true] { let form = 0x1380_0000 | if wide { (1 << 31) | (1 << 22) } else { 0 }; for shift in [0, 1, if wide { 63 } else { 31 }] { @@ -115,6 +184,12 @@ fn extract_matches_arm_concatenation_in_frame_and_pinned_code() { (31, 16, 16), (17, 31, 17), (17, 16, 31), + (16, 16, 16), + (17, 17, 16), + (1, 1, 3), + (3, 3, 16), + (31, 31, 16), + (16, 16, 31), ] { let word = form | (low << 16) | (shift << 10) | (high << 5) | dst; check(OPENSSL.0, word);