Truly independent web browser
LibJS: Let Flap count zeros on x86-64 CPUs without LZCNT and BMI1
Andreas Kling7dc5c82899de
diff --git a/Libraries/LibJS/Flap/benches/compiler.rs b/Libraries/LibJS/Flap/benches/compiler.rsindex 61e507e6e62..820dde109df 100644--- a/Libraries/LibJS/Flap/benches/compiler.rs+++ b/Libraries/LibJS/Flap/benches/compiler.rs@@ -54,6 +54,8 @@ fn compiler() -> Compiler {object_format: ObjectFormat::Elf,},has_jscvt: false,+ has_lzcnt: false,+ has_bmi1: false,enable_assertions: false,profiling: false,profile: flapc::Profile::JavaScript,diff --git a/Libraries/LibJS/Flap/src/lib.rs b/Libraries/LibJS/Flap/src/lib.rsindex 4f5570aef71..a4a277b4d61 100644--- a/Libraries/LibJS/Flap/src/lib.rs+++ b/Libraries/LibJS/Flap/src/lib.rs@@ -91,6 +91,11 @@ pub struct Target {pub struct CompileOptions {pub target: Target,pub has_jscvt: bool,+ /// Whether the x86-64 target has LZCNT. Without it, `lzcnt` runs as `bsr`, which counts from the other end.+ pub has_lzcnt: bool,+ /// Whether the x86-64 target has BMI1, and with it TZCNT. Without it, `tzcnt` runs as `bsf`, which leaves the+ /// destination undefined for zero.+ pub has_bmi1: bool,pub enable_assertions: bool,/// Compile the profiling variant of the interpreter: inline functions/// annotated with `@profiling` keep their bodies, and the entry points get@@ -845,6 +850,8 @@ const HEAP_REGION_OFFSET_MASK = 0x3FFFFFFFFFFobject_format: ObjectFormat::Elf,},has_jscvt: false,+ has_lzcnt: false,+ has_bmi1: false,enable_assertions: false,profiling: false,profile: Profile::JavaScript,diff --git a/Libraries/LibJS/Flap/src/main.rs b/Libraries/LibJS/Flap/src/main.rsindex 306c787a372..c5f0cf52743 100644--- a/Libraries/LibJS/Flap/src/main.rs+++ b/Libraries/LibJS/Flap/src/main.rs@@ -77,6 +77,8 @@ fn parse_command_line() -> Result<CommandLine, String> {let mut output_path = None;let mut constants_path = None;let mut has_jscvt = false;+ let mut has_lzcnt = false;+ let mut has_bmi1 = false;let mut enable_assertions = false;let mut optimization_report_path = None;let mut dump_changed_ir = false;@@ -86,6 +88,8 @@ fn parse_command_line() -> Result<CommandLine, String> {while let Some(argument) = args.next() {match argument.as_str() {"--has-jscvt" => has_jscvt = true,+ "--has-lzcnt" => has_lzcnt = true,+ "--has-bmi1" => has_bmi1 = true,"--enable-assertions" => enable_assertions = true,"--dump-changed-ir" => dump_changed_ir = true,"--profiling" => profiling = true,@@ -132,6 +136,8 @@ fn parse_command_line() -> Result<CommandLine, String> {object_format,},has_jscvt,+ has_lzcnt,+ has_bmi1,enable_assertions,profiling,profile,diff --git a/Libraries/LibJS/Flap/src/target/aarch64.rs b/Libraries/LibJS/Flap/src/target/aarch64.rsindex 55a530cc069..cc6bd12a647 100644--- a/Libraries/LibJS/Flap/src/target/aarch64.rs+++ b/Libraries/LibJS/Flap/src/target/aarch64.rs@@ -1430,6 +1430,8 @@ mod tests {object_format: ObjectFormat::Coff,},has_jscvt: false,+ has_lzcnt: false,+ has_bmi1: false,enable_assertions: false,profiling: false,profile: crate::Profile::JavaScript,diff --git a/Libraries/LibJS/Flap/src/target/finalize.rs b/Libraries/LibJS/Flap/src/target/finalize.rsindex 7c5d39bddb8..c7fd6e99d24 100644--- a/Libraries/LibJS/Flap/src/target/finalize.rs+++ b/Libraries/LibJS/Flap/src/target/finalize.rs@@ -65,6 +65,8 @@ fn finalize_function(function: AllocatedFunction, runtime: &RuntimeConstants) ->object_format: ObjectFormat::Elf,},has_jscvt: false,+ has_lzcnt: false,+ has_bmi1: false,enable_assertions: false,profiling: false,profile: crate::Profile::JavaScript,@@ -508,6 +510,8 @@ mod tests {object_format: ObjectFormat::Elf,},has_jscvt: false,+ has_lzcnt: false,+ has_bmi1: false,enable_assertions: false,profiling: false,profile: crate::Profile::JavaScript,diff --git a/Libraries/LibJS/Flap/src/target/finalize_support.rs b/Libraries/LibJS/Flap/src/target/finalize_support.rsindex e8ef2b9821b..24559ca3ab7 100644--- a/Libraries/LibJS/Flap/src/target/finalize_support.rs+++ b/Libraries/LibJS/Flap/src/target/finalize_support.rs@@ -33,6 +33,8 @@ pub(crate) struct Emit<'a> {pub(crate) object_format: ObjectFormat,pub(crate) enable_assertions: bool,pub(crate) has_jscvt: bool,+ pub(crate) has_lzcnt: bool,+ pub(crate) has_bmi1: bool,/// Whether this is the profiling build of the interpreter. Only its/// handlers switch to the dispatch table of the frame they continue in/// (see `VmDispatchTables`).@@ -63,6 +65,8 @@ impl<'a> Emit<'a> {object_format: options.target.object_format,enable_assertions: options.enable_assertions,has_jscvt: options.has_jscvt,+ has_lzcnt: options.has_lzcnt,+ has_bmi1: options.has_bmi1,profiling: options.profiling,profile: options.profile,unique_counter: 0,diff --git a/Libraries/LibJS/Flap/src/target/x86_64.rs b/Libraries/LibJS/Flap/src/target/x86_64.rsindex 514c9cd61a1..5ac6ca4da2d 100644--- a/Libraries/LibJS/Flap/src/target/x86_64.rs+++ b/Libraries/LibJS/Flap/src/target/x86_64.rs@@ -387,6 +387,8 @@ define_machine_opcodes! {[UnsignedDivide(IntegerWidth)] => Self::UnsignedDivide(width) => [[R]] printing simple!("div"; integer(0, width));[CountLeadingZeros(IntegerWidth)] => Self::CountLeadingZeros(width) => [[R, R]] printing simple!("lzcnt"; integer(0, width), integer(1, width));[CountTrailingZeros(IntegerWidth)] => Self::CountTrailingZeros(width) => [[R, R]] printing simple!("tzcnt"; integer(0, width), integer(1, width));+ [BitScanReverse(IntegerWidth)] => Self::BitScanReverse(width) => [[R, R]] printing simple!("bsr"; integer(0, width), integer(1, width));+ [BitScanForward(IntegerWidth)] => Self::BitScanForward(width) => [[R, R]] printing simple!("bsf"; integer(0, width), integer(1, width));[PopulationCount(IntegerWidth)] => Self::PopulationCount(width) => [[R, R]] printing simple!("popcnt"; integer(0, width), integer(1, width));[RotateLeft(IntegerWidth)] => Self::RotateLeft(width) => [[R]] printing simple!("rol"; integer(0, width), SimpleOperand::Literal("cl"));[RotateRight(IntegerWidth)] => Self::RotateRight(width) => [[R]] printing simple!("ror"; integer(0, width), SimpleOperand::Literal("cl"));diff --git a/Libraries/LibJS/Flap/src/target/x86_64/finalize.rs b/Libraries/LibJS/Flap/src/target/x86_64/finalize.rsindex 6be37dd45a9..47cafee7992 100644--- a/Libraries/LibJS/Flap/src/target/x86_64/finalize.rs+++ b/Libraries/LibJS/Flap/src/target/x86_64/finalize.rs@@ -2410,6 +2410,7 @@ impl Backend for X86_64Backend {| CountTrailingZeros32 | PopulationCount32 | RotateLeft32 | RotateRight32 => IntegerWidth::U32,_ => IntegerWidth::U64,};+ let bits: i64 = if width == IntegerWidth::U32 { 32 } else { 64 };match operation {DivideSigned32 | DivideUnsigned32 | RemainderSigned32 | RemainderUnsigned32 | DivideSigned64| DivideUnsigned64 | RemainderSigned64 | RemainderUnsigned64 => {@@ -2434,11 +2435,37 @@ impl Backend for X86_64Backend {}CountLeadingZeros32 | CountLeadingZeros64 => {let [destination, source] = operands.physical_registers();- emit!(emit.output, X86_64; Opcode::CountLeadingZeros(width) => [register destination, register source];);+ if emit.has_lzcnt {+ emit!(emit.output, X86_64; Opcode::CountLeadingZeros(width) => [register destination, register source];);+ } else {+ // NB: bsr finds the index of the highest set bit, and the xor turns it into bits - 1 - index, the+ // count. For zero, bsr leaves the destination undefined, so it takes 2 * bits - 1 instead,+ // which the xor turns into bits.+ let nonzero = emit.unique_label("leading_zeros_nonzero");+ emit!(emit.output, X86_64;+ Opcode::BitScanReverse(width) => [register destination, register source];+ Opcode::JumpCondition(super::Condition::NonZero) => [label nonzero.clone()];+ Opcode::Move32Immediate => [register destination, immediate 2 * bits - 1];+ Opcode::Label => [label nonzero];+ );+ push_alu_immediate(emit, super::AluOperation::Xor, width, destination, bits - 1);+ }}CountTrailingZeros32 | CountTrailingZeros64 => {let [destination, source] = operands.physical_registers();- emit!(emit.output, X86_64; Opcode::CountTrailingZeros(width) => [register destination, register source];);+ if emit.has_bmi1 {+ emit!(emit.output, X86_64; Opcode::CountTrailingZeros(width) => [register destination, register source];);+ } else {+ // NB: bsf finds the index of the lowest set bit, which is the count, but leaves the destination+ // undefined for zero, whose count is the bit width.+ let nonzero = emit.unique_label("trailing_zeros_nonzero");+ emit!(emit.output, X86_64;+ Opcode::BitScanForward(width) => [register destination, register source];+ Opcode::JumpCondition(super::Condition::NonZero) => [label nonzero.clone()];+ Opcode::Move32Immediate => [register destination, immediate bits];+ Opcode::Label => [label nonzero];+ );+ }}PopulationCount32 | PopulationCount64 => {let [destination, source] = operands.physical_registers();diff --git a/Libraries/LibJS/Flap/tests/assertions.rs b/Libraries/LibJS/Flap/tests/assertions.rsindex 08f63272c25..4fb2de35dfd 100644--- a/Libraries/LibJS/Flap/tests/assertions.rs+++ b/Libraries/LibJS/Flap/tests/assertions.rs@@ -14,6 +14,8 @@ fn compile(body: &str, architecture: Architecture, enabled: bool) -> String {object_format: ObjectFormat::Elf,},has_jscvt: false,+ has_lzcnt: false,+ has_bmi1: false,enable_assertions: enabled,profiling: false,profile: flapc::Profile::JavaScript,diff --git a/Libraries/LibJS/Flap/tests/closures.rs b/Libraries/LibJS/Flap/tests/closures.rsindex 5f36178ad75..c69f4d3be10 100644--- a/Libraries/LibJS/Flap/tests/closures.rs+++ b/Libraries/LibJS/Flap/tests/closures.rs@@ -13,6 +13,8 @@ fn compile(source: &str, architecture: Architecture) -> Result<String, String> {object_format: ObjectFormat::Elf,},has_jscvt: false,+ has_lzcnt: false,+ has_bmi1: false,enable_assertions: true,profiling: false,profile: flapc::Profile::JavaScript,diff --git a/Libraries/LibJS/Flap/tests/interpreter_assembly.rs b/Libraries/LibJS/Flap/tests/interpreter_assembly.rsindex 78865121f4b..e92582956f1 100644--- a/Libraries/LibJS/Flap/tests/interpreter_assembly.rs+++ b/Libraries/LibJS/Flap/tests/interpreter_assembly.rs@@ -18,6 +18,8 @@ fn compile_interpreter(architecture: Architecture) -> String {object_format: ObjectFormat::Elf,},has_jscvt: false,+ has_lzcnt: false,+ has_bmi1: false,enable_assertions: true,profiling: false,profile: flapc::Profile::JavaScript,diff --git a/Libraries/LibJS/Flap/tests/profiling.rs b/Libraries/LibJS/Flap/tests/profiling.rsindex 79415577f70..87265972f82 100644--- a/Libraries/LibJS/Flap/tests/profiling.rs+++ b/Libraries/LibJS/Flap/tests/profiling.rs@@ -27,6 +27,8 @@ fn compile(architecture: Architecture, profiling: bool) -> String {object_format: ObjectFormat::Elf,},has_jscvt: false,+ has_lzcnt: false,+ has_bmi1: false,enable_assertions: true,profiling,profile: flapc::Profile::JavaScript,diff --git a/Libraries/LibJS/Flap/tests/wasm_profile.rs b/Libraries/LibJS/Flap/tests/wasm_profile.rsindex b7e3d4aee71..4a13eb6ed1d 100644--- a/Libraries/LibJS/Flap/tests/wasm_profile.rs+++ b/Libraries/LibJS/Flap/tests/wasm_profile.rs@@ -13,6 +13,8 @@ fn compiler(architecture: Architecture) -> Compiler {object_format: ObjectFormat::Elf,},has_jscvt: false,+ has_lzcnt: true,+ has_bmi1: true,enable_assertions: true,profiling: false,profile: Profile::WebAssembly,@@ -152,6 +154,8 @@ fn compile_for(target: Target, source: &str) -> Result<String, String> {Compiler::new(CompileOptions {target,has_jscvt: false,+ has_lzcnt: false,+ has_bmi1: false,enable_assertions: true,profiling: false,profile: Profile::WebAssembly,@@ -930,6 +934,39 @@ handler Fallback() @fallback {}}+#[test]+fn counts_zeros_with_bit_scans_without_lzcnt_and_bmi1() {+ let assembly = compile_for(+ Target {+ architecture: Architecture::X86_64,+ object_format: ObjectFormat::Elf,+ },+ r#"+handler Zeros() @opcode(0x01) {+ let slots: Sequence<u64> = alias(vsp);+ let leading = clz32(u32(slots[0]));+ let trailing = ctz64(slots[1]);+ let wide_leading: u64 = alias(leading);+ slots[2] = wide_leading;+ slots[3] = trailing;+ dispatch_next;+}++handler Fallback() @fallback {+ exit();+}+"#,+ )+ .unwrap();+ let zeros = handler(&assembly, "Zeros");+ for instruction in ["bsr e", ", 63", "xor e", ", 31", "bsf r", ", 64"] {+ assert!(zeros.contains(instruction), "missing {instruction}:\n{zeros}");+ }+ for instruction in ["lzcnt", "tzcnt"] {+ assert!(!zeros.contains(instruction), "unexpected {instruction}:\n{zeros}");+ }+}+#[test]fn moves_continuing_cold_ifs_out_of_line() {let assembly = compile(diff --git a/Libraries/LibJS/Rust/build.rs b/Libraries/LibJS/Rust/build.rsindex b0a81e26962..873b1f5c840 100644--- a/Libraries/LibJS/Rust/build.rs+++ b/Libraries/LibJS/Rust/build.rs@@ -316,6 +316,8 @@ fn interpreter_compiler(target: &Target, profiling: bool) -> flapc::Compiler {object_format: target.object_format,},has_jscvt: target.is_apple && matches!(target.architecture, flapc::Architecture::Aarch64),+ has_lzcnt: false,+ has_bmi1: false,enable_assertions: true,profiling,profile: flapc::Profile::JavaScript,diff --git a/Libraries/LibWasm/Engine/build.rs b/Libraries/LibWasm/Engine/build.rsindex 2fb88e6e167..5e434eb8af1 100644--- a/Libraries/LibWasm/Engine/build.rs+++ b/Libraries/LibWasm/Engine/build.rs@@ -70,6 +70,8 @@ fn main() {object_format,},has_jscvt: false,+ has_lzcnt: false,+ has_bmi1: false,enable_assertions: true,profiling: false,profile: flapc::Profile::WebAssembly,