Skip to content

Truly independent web browser

LibJS: Reuse compiled instructions in binary and comparison tests

Callum Lawabf6aad3ce84
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
index 64fb08e9e77..b64e532b270 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
@@ -355,6 +355,10 @@ fn run_instruction(instruction: Instruction, arguments: &[u64]) -> u64 {
/// `EXITED` if the code exits. Also returns the number of calls of the
/// fake comparison and jump slow paths.
fn run_instruction_with(feedback: u8, instruction: Instruction, arguments: &[u64]) -> (u64, u64) {
+ compile_instruction(feedback, instruction)(arguments)
+}
+
+fn compile_instruction(feedback: u8, instruction: Instruction) -> impl Fn(&[u64]) -> (u64, u64) {
let program = assemble(|_| {
vec![
Instruction::Enter,
@@ -362,12 +366,13 @@ fn run_instruction_with(feedback: u8, instruction: Instruction, arguments: &[u64
Instruction::Return { value: r(5) },
]
});
- let (result, machine) = run_program_with_snapshot(
- &program,
- arguments,
- |snapshot| snapshot.executables[0].feedback.arith = vec![feedback],
- |_| {},
- );
+ let (snapshot, compiled) = compile_program(&program, |snapshot| {
+ snapshot.executables[0].feedback.arith = vec![feedback]
+ });
+ move |arguments| instruction_result(run_compiled_program(&snapshot, &compiled, arguments, |_| {}))
+}
+
+fn instruction_result((result, machine): (JitResult, Machine)) -> (u64, u64) {
let slow_calls = machine.vm[VM_SLOW_JUMPS_WORD];
if result.status == crate::code::JitStatus::Resume as u64 {
return (EXITED, slow_calls);
@@ -623,9 +628,10 @@ fn binary_operations_on_two_operands() {
OperationInput::Operand(argument(0)),
OperationInput::Operand(argument(1)),
);
+ let run = compile_instruction(feedback, instruction);
for lhs in &values {
for rhs in &values {
- let (result, _) = run_instruction_with(feedback, instruction.clone(), &[*lhs, *rhs]);
+ let (result, _) = run(&[*lhs, *rhs]);
assert_eq!(
result,
binary(op, *lhs, *rhs, feedback),
@@ -648,8 +654,9 @@ fn binary_operations_with_an_int32_operand() {
OperationInput::Operand(argument(0)),
OperationInput::Int32(immediate),
);
+ let run = compile_instruction(feedback, instruction);
for lhs in &values {
- let (result, _) = run_instruction_with(feedback, instruction.clone(), &[*lhs, value::UNDEFINED]);
+ let (result, _) = run(&[*lhs, value::UNDEFINED]);
let expected = binary(op, *lhs, value::int32(immediate), feedback);
assert_eq!(result, expected, "{op:?} {lhs:#x} {immediate}: got {result:#x}");
}
@@ -659,9 +666,9 @@ fn binary_operations_with_an_int32_operand() {
OperationInput::Int32(immediate),
OperationInput::Operand(argument(0)),
);
+ let run = compile_instruction(feedback, instruction);
for rhs in &values {
- let (result, _) =
- run_instruction_with(feedback, instruction.clone(), &[*rhs, value::UNDEFINED]);
+ let (result, _) = run(&[*rhs, value::UNDEFINED]);
let expected = binary(op, value::int32(immediate), *rhs, feedback);
assert_eq!(result, expected, "{immediate} + {rhs:#x}: got {result:#x}");
}
@@ -1099,17 +1106,18 @@ fn boolean(result: Option<bool>) -> u64 {
#[test]
fn comparisons() {
let values = values();
- let check = |instruction: &Instruction, lhs: u64, rhs: u64, expected: Option<bool>| {
- let (result, slow_calls) = run_instruction_with(GENERAL_FEEDBACK, instruction.clone(), &[lhs, rhs]);
+ let check = |(result, slow_calls), instruction: &Instruction, lhs: u64, rhs: u64, expected: Option<bool>| {
let context = format!("{instruction:?} {lhs:#x} {rhs:#x}: got {result:#x}");
assert_eq!(result, boolean(expected), "{context}");
assert_eq!(slow_calls, u64::from(expected.is_none()), "{context}");
};
for comparison in COMPARISONS {
let instruction = compare_instruction(comparison, r(5), argument(0), OperationInput::Operand(argument(1)));
+ let run = compile_instruction(GENERAL_FEEDBACK, instruction.clone());
for lhs in &values {
for rhs in &values {
check(
+ run(&[*lhs, *rhs]),
&instruction,
*lhs,
*rhs,
@@ -1119,9 +1127,11 @@ fn comparisons() {
}
for immediate in [0, 1, -1, 7, i32::MAX, i32::MIN] {
let instruction = compare_instruction(comparison, r(5), argument(0), OperationInput::Int32(immediate));
+ let run = compile_instruction(GENERAL_FEEDBACK, instruction.clone());
for lhs in &values {
let expected = self::comparison(comparison, *lhs, value::int32(immediate), true);
- check(&instruction, *lhs, value::UNDEFINED, expected);
+ let actual = run(&[*lhs, value::UNDEFINED]);
+ check(actual, &instruction, *lhs, value::UNDEFINED, expected);
}
}
}