Skip to content

Truly independent web browser

LibJS/JIT: Use intrinsics where natives do not return in registers

Aliaksandr Kalenik8433e156399c
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
index 2fc67be65e3..a7c2ab32af7 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -484,7 +484,7 @@ impl GraphBuilder<'_> {
unreachable!("calls are generic instructions");
};
let slots_offset = u64::from(self.runtime.offsets.execution_context_slots);
- if let Some(native) = feedback.native_call {
+ if let Some(native) = feedback.native_call.filter(|native| native.entry.is_some()) {
self.reserve_call_frame(slots_offset + 8 * u64::from(*argument_count));
self.embed(native.function);
self.embed(native.realm);
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
index 6307ff4567a..2d517bade4e 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
@@ -1054,7 +1054,7 @@ pub(crate) mod property_access {
let target = NativeCallTarget {
function: CellId(0x9000),
realm: CellId(0xa000),
- entry: 0xb000,
+ entry: core::num::NonZeroU64::new(0xb000),
direct_getter: validated.then_some(DirectGetterFields {
wrapper_implementation: 8,
implementation_value: 16,
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs b/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
index f13545c3c83..fcb357a4863 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
@@ -1353,6 +1353,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
slow: Option<Label>,
[check, done]: [Label; 2],
) -> Result<(), CompileFailure> {
+ // NB: Without an entry, the function is one JIT code cannot call itself.
+ let entry = target
+ .entry
+ .ok_or(CompileFailure::MissingRuntimeHelper { opcode: None })?;
let runtime = self.runtime;
let offsets = runtime.offsets;
let argument_count = u32::try_from(arguments.len()).map_err(|_| CompileFailure::InvalidBytecode {
@@ -1459,7 +1463,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
// Call the native function: it returns a ThrowCompletionOr<Value>,
// the value or exception in the first return register and whether it
// is an exception in the low byte of the second.
- self.masm.move_imm64(scratch, target.entry);
+ self.masm.move_imm64(scratch, entry.get());
self.masm.move64(registers[0], vm);
self.masm.call_register(scratch);
let [value_register, variant] = [M::RETURN_GPRS[0], M::RETURN_GPRS[1]];
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
index 8f9e9ca40ed..762499ac6a6 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
@@ -3150,7 +3150,7 @@ fn get_by_id_reads_direct_getter_wrappers_inline_from(compressed_field: bool) {
let target = NativeCallTarget {
function: CellId(function.as_ptr() as u64),
realm: CellId(0x7000),
- entry: fake_native_getter as *const () as u64,
+ entry: core::num::NonZeroU64::new(fake_native_getter as *const () as u64),
direct_getter: Some(DirectGetterFields {
wrapper_implementation: 80,
implementation_value: if compressed_field { 20 } else { 16 },
@@ -3249,7 +3249,7 @@ fn put_by_id_calls_native_setters_of_prototypes() {
let target = NativeCallTarget {
function: CellId(function.as_ptr() as u64),
realm: CellId(0x7000),
- entry: fake_native_setter as *const () as u64,
+ entry: core::num::NonZeroU64::new(fake_native_setter as *const () as u64),
direct_getter: None,
};
let mut entry = own(
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index a18bb20a50e..21628f0f6bb 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -421,7 +421,8 @@ pub struct CallFeedbackSnapshot {
pub inline_executable: Option<u32>,
/// `target`, if JIT code can call it directly.
pub direct_call: Option<DirectCallTarget>,
- /// `target`, if it is a raw native function JIT code can call directly.
+ /// `target`, if it is a raw native function. JIT code can call it directly if it has an `entry`, and can replace
+ /// a call of it with an intrinsic either way.
pub native_call: Option<NativeCallTarget>,
/// How `target` forwarded the calls of this site, if it always forwarded
/// them the same way to the same function.
@@ -600,8 +601,10 @@ pub struct NativeCallTarget {
pub function: CellId,
/// The function's realm, the frame's realm.
pub realm: CellId,
- /// `ThrowCompletionOr<Value> (*)(VM&)`: the native function.
- pub entry: u64,
+ /// `ThrowCompletionOr<Value> (*)(VM&)`: the native function, or None
+ /// where native functions do not return their completion in two
+ /// registers, and JIT code leaves calls of them to the runtime.
+ pub entry: Option<core::num::NonZeroU64>,
/// Where the function, a getter of a platform object attribute backed by
/// a field, finds the wrapper it would return, if it is one and the
/// interpreter validated the cache entry for it (see
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index a6b297b46d8..e5987d06c81 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -7,6 +7,7 @@
//! Snapshots of executables for compile jobs, captured on the main thread straight from the runtime's structures.
//! A snapshot refers to cells only by address; the cells it captured are kept alive by whoever owns the job.
+use core::num::NonZeroU64;
use libjs_abi::Builtin;
use std::collections::HashMap;
@@ -341,11 +342,12 @@ fn direct_call_target(feedback: &CallFeedback) -> Option<(Gc<EcmascriptFunctionO
function.can_inline_call().then_some((function, closures))
}
-/// The target and entry of a call site if it is a native function that JIT code can call directly, in the frame the
-/// interpreter's call fast path builds for it. Function.prototype.call and apply forward their calls, which the runtime
-/// does for JIT code.
-fn native_call_target(vm: &Vm, feedback: &CallFeedback) -> Option<(Gc<NativeFunction>, u64)> {
- if !RAW_NATIVE_FUNCTIONS_RETURN_IN_REGISTERS || feedback.flags.get() & call_feedback_flags::POLYMORPHIC != 0 {
+/// The target of a call site if it is a native function, which JIT code can call directly in the frame the
+/// interpreter's call fast path builds for it, or replace with an intrinsic, and the entry it calls it at, if it can:
+/// none where native functions do not return their completion in two registers, and JIT code leaves calls of them to
+/// the runtime. Function.prototype.call and apply forward their calls, which the runtime does for JIT code.
+fn native_call_target(vm: &Vm, feedback: &CallFeedback) -> Option<(Gc<NativeFunction>, Option<NonZeroU64>)> {
+ if feedback.flags.get() & call_feedback_flags::POLYMORPHIC != 0 {
return None;
}
let function = feedback.target()?.downcast::<NativeFunction>()?;
@@ -353,11 +355,14 @@ fn native_call_target(vm: &Vm, feedback: &CallFeedback) -> Option<(Gc<NativeFunc
if FunctionPrototype::is_call_function(vm, &raw) || FunctionPrototype::is_apply_function(vm, &raw) {
return None;
}
- raw.native_function(vm)?
+ raw.native_function(vm)
} else {
- function.capturing_call_entry()?
+ function.capturing_call_entry()
};
- Some((function, entry as usize as u64))
+ let entry = entry
+ .filter(|_| RAW_NATIVE_FUNCTIONS_RETURN_IN_REGISTERS)
+ .and_then(|pointer| NonZeroU64::new(pointer as usize as u64));
+ Some((function, entry))
}
/// The words of an ECMAScript function its frames start with, which stay the same for its lifetime.
@@ -941,7 +946,7 @@ impl SnapshotBuilder<'_> {
return None;
}
let native_function = function.downcast::<RawNativeFunction>()?;
- let entry = native_function.native_function(self.vm)? as usize as u64;
+ let entry = NonZeroU64::new(native_function.native_function(self.vm)? as usize as u64)?;
let direct_getter = function
.downcast::<DirectGetterFunction>()
.filter(|_| direct_getter_validated)
@@ -958,7 +963,7 @@ impl SnapshotBuilder<'_> {
Some(NativeCallTarget {
function: function_cell,
realm: self.cell(native_function.realm()),
- entry,
+ entry: Some(entry),
direct_getter,
})
}