Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ext/ffi): Implement FFI fast-call trampoline with Dynasmrt #15305

Merged
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift click to select a range
8be9d6e
Replace tinycc-based JIT trampoline with js recv-as-first-arg "pattern"
arnauorriols Jul 21, 2022
4ebd951
add assertFastCall in test_ffi to ensure fast-API-call optimization
arnauorriols Jul 22, 2022
9a0b543
Implement JIT compilation of FFI fast-call trampoline with Dynasmrt
arnauorriols Jul 25, 2022
fed0fd2
Comment on rationale behind integer/sse argument classification in ff…
arnauorriols Jul 25, 2022
5ede98d
Merge branch 'main' into refactor/ffi-trampoline-plain-asm
arnauorriols Jul 26, 2022
a4182fa
Apply nitpick suggestions
arnauorriols Jul 26, 2022
f1abc06
Implement 8 and 16 bit integer casting to 32 bit in arguments and ret…
arnauorriols Jul 28, 2022
c34f764
Update test_util/std submodule
arnauorriols Jul 28, 2022
eb7b529
Implement Aarch64-apple-darwin and win64
arnauorriols Aug 10, 2022
c1b4341
Correct spelling errors
arnauorriols Aug 16, 2022
b018240
Cleanup and extend documentation
arnauorriols Aug 16, 2022
144442b
Implement stack-allocation unit tests for sysv and aarch64. Pending w…
arnauorriols Aug 16, 2022
93f43dc
Add integer-casting test for win64
arnauorriols Aug 16, 2022
a0d5201
Merge branch 'main' into refactor/ffi-trampoline-plain-asm
arnauorriols Aug 17, 2022
9ddf3b4
Remove prelude.h file
arnauorriols Aug 19, 2022
d40515e
Encapsulate optimized testing in helper function
arnauorriols Aug 19, 2022
5d372a6
Merge branch 'main' into refactor/ffi-trampoline-plain-asm
arnauorriols Aug 19, 2022
618d471
Extract alignment expression into helper function
arnauorriols Aug 20, 2022
1b9c192
Add various debug assertions to ensure memory-related preconditions
arnauorriols Aug 21, 2022
b6976b4
optimize loading of ffi pointer in aarch64-apple
arnauorriols Aug 22, 2022
90b4fe2
Rename `Integer::TypedArray` => `Integer::Pointer`
arnauorriols Aug 22, 2022
7ecad49
Remove leftover commented code
arnauorriols Aug 23, 2022
924b2fb
Use `padding_to_align` to calculate padding of stack parameters in aa…
arnauorriols Aug 24, 2022
f323ddf
add small helper macros to improve density of assembly lines
arnauorriols Aug 24, 2022
9728da9
move helper macros
arnauorriols Aug 24, 2022
65863c9
Merge branch 'main' into refactor/ffi-trampoline-plain-asm
arnauorriols Aug 30, 2022
ba0047f
Merge branch 'main' into refactor/ffi-trampoline-plain-asm
ry Sep 5, 2022
73f05c7
fix
ry Sep 5, 2022
a4e8b9f
Workaround Aarch64Apple calling convention to match V8's incorrect one
arnauorriols Sep 6, 2022
1680832
Cleanup temporary TODO comments
arnauorriols Sep 7, 2022
3b9f726
Improve clarifying comment and apply explicitness suggestion
arnauorriols Sep 7, 2022
d900b2b
format
littledivy Sep 7, 2022
7edffb1
Merge branch 'main' into refactor/ffi-trampoline-plain-asm
littledivy Sep 7, 2022
4000c7b
Merge branch 'main' into refactor/ffi-trampoline-plain-asm
littledivy Sep 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Correct spelling errors
  • Loading branch information
arnauorriols committed Aug 16, 2022
commit c1b43419f68b4dbd174469b0791441107bb6c35f
38 changes: 20 additions & 18 deletions ext/ffi/fast_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 158,8 @@ impl SysVAmd64 {
for param in &sym.parameter_types {
compiler.move_left(param)
}
if !compiler.is_recv_overriden() {
// the receiver object should never be expected. Avoid its unexpected or deliverated leak
if !compiler.is_recv_overridden() {
// the receiver object should never be expected. Avoid its unexpected or deliberate leak
compiler.zero_first_arg();
}

Expand Down Expand Up @@ -205,13 205,13 @@ impl SysVAmd64 {

let param_i = self.float_params 1;
self.float_params = param_i;
// floats are only moved to accomodate integer movement in the stack
// floats are only moved to accommodate integer movement in the stack
let is_in_stack = param_i > Self::FLOAT_REGISTERS;
let stack_has_moved = self.allocated_stack > 0
|| self.integer_params >= Self::INTEGER_REGISTERS;

if is_in_stack && stack_has_moved {
// SSE registers remain untouch. Only when the stack is modified, the floats in the stack need to be accomodated
// SSE registers remain untouched. Only when the stack is modified, the floats in the stack need to be accommodated
match param {
Single => dynasm!(self.assembler
; .arch x64
Expand Down Expand Up @@ -460,7 460,7 @@ impl SysVAmd64 {
);
}

fn is_recv_overriden(&self) -> bool {
fn is_recv_overridden(&self) -> bool {
self.integer_params > 0
}

Expand Down Expand Up @@ -514,9 514,9 @@ impl Aarch64Apple {
assembler: dynasmrt::aarch64::Assembler::new().unwrap(),
integer_params: 0,
float_params: 0,
arnauorriols marked this conversation as resolved.
Show resolved Hide resolved
stack_allocated: 0,
offset_trampoline: 0,
offset_original: 0,
stack_allocated: 0,
}
}

Expand All @@ -526,8 526,8 @@ impl Aarch64Apple {
for argument in &sym.parameter_types {
compiler.move_left(argument)
}
if !compiler.is_recv_overriden() {
// the receiver object should never be expected. Avoid its unexpected or deliverated leak
if !compiler.is_recv_overridden() {
// the receiver object should never be expected. Avoid its unexpected or deliberate leak
compiler.zero_first_arg();
}
// We can always tail-call. return values follow the same rules as register arguments.
Expand Down Expand Up @@ -565,7 565,7 @@ impl Aarch64Apple {
let padding_trampl = (size - self.offset_trampoline % size) % size;
let padding_orig = (size - self.offset_original % size) % size;

// floats are only moved to accomodate integer movement in the stack
// floats are only moved to accommodate integer movement in the stack
let stack_has_moved = self.stack_allocated > 0
|| self.integer_params >= Self::INTEGER_REGISTERS;
if stack_has_moved {
Expand All @@ -585,7 585,7 @@ impl Aarch64Apple {
),
}
}
// The trampoline and the orignal function always have the same amount of floats in the stack
// The trampoline and the original function always have the same amount of floats in the stack
self.offset_trampoline = padding_trampl size;
self.offset_original = padding_orig size;
}
Expand Down Expand Up @@ -791,6 791,8 @@ impl Aarch64Apple {
// stack pointer is never modified and remains aligned
// frame pointer remains the one provided by the trampoline's caller (V8)

// Like all ARM instructions, move instructions are 32bit long and can fit at most 16bit immediates.
// bigger immediates are loaded in multiple steps applying a left-shift modifier
let mut address = ptr as u64;
let mut imm16 = address & 0xFFFF;
dynasm!(self.assembler
Expand All @@ -814,7 816,7 @@ impl Aarch64Apple {
);
}

fn is_recv_overriden(&self) -> bool {
fn is_recv_overridden(&self) -> bool {
self.integer_params > 0
}

Expand Down Expand Up @@ -863,8 865,8 @@ impl Win64 {
for argument in &sym.parameter_types {
compiler.move_left(argument)
}
if !compiler.is_recv_overriden() {
// the receiver object should never be expected. Avoid its unexpected or deliverated leak
if !compiler.is_recv_overridden() {
// the receiver object should never be expected. Avoid its unexpected or deliberate leak
compiler.zero_first_arg();
}

Expand Down Expand Up @@ -910,7 912,7 @@ impl Win64 {
(1, I(B | W | DW)) => dynasm!(self.assembler; .arch x64; mov ecx, edx),
arnauorriols marked this conversation as resolved.
Show resolved Hide resolved
(1, I(QW)) => dynasm!(self.assembler; .arch x64; mov rcx, rdx),
(1, F) => {
// Use movaps for doubles as well, benefits of smaller encoding outweight those of using the correct instruction for the type (movapd)
// Use movaps for doubles as well, benefits of smaller encoding outweigh those of using the correct instruction for the type (movapd)
dynasm!(self.assembler; .arch x64; xor ecx, ecx; movaps xmm0, xmm1)
}

Expand Down Expand Up @@ -1049,7 1051,7 @@ impl Win64 {
);
}

fn is_recv_overriden(&self) -> bool {
fn is_recv_overridden(&self) -> bool {
self.params > 0
}

Expand Down Expand Up @@ -1123,8 1125,8 @@ use Param::*;
// for argument in &sym.parameter_types {
// compiler.move_left(argument)
// }
// if !compiler.is_recv_overriden() {
// // the receiver object should never be expected. Avoid its unexpected or deliverated leak
// if !compiler.is_recv_overridden() {
// // the receiver object should never be expected. Avoid its unexpected or deliberate leak
// compiler.zero_first_arg();
// }

Expand Down Expand Up @@ -1182,7 1184,7 @@ use Param::*;

// fn can_tailcall(&self) -> bool;

// fn is_recv_overriden(&self) -> bool;
// fn is_recv_overridden(&self) -> bool;

// fn must_cast_return_value(&self, rv: NativeType) -> bool;

Expand Down