-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Strange Code Generation for Custom DST methods #106683
Comments
The instructions you've identified are used to compute that pointer. This takes a few instructions because Isolating just the function call: https://godbolt.org/z/374oroPjq example::example:
; example() arguments:
; rdi = data ptr to `A<dyn Any>`
; rsi = vtable ptr to an instantiation of `Any`
mov rax, qword ptr [rsi + 16] ; load alignment of DST field (`alignof(T)`) from vtable
add rax, 3 ; these two instructions effectively compute
and rax, -4 ; `max(sizeof(i32), alignof(T))`, relying on both being powers of 2
add rdi, rax ; offset data pointer from start of `A` to `A.b`
jmp qword ptr [rsi + 24] ; call `Any::type_id` Looking at different instantiations of
The instruction sequence gets even more complex when the size of the non-DST fields is no longer a power of 2, because it's even harder to find the offset of the |
The padding issue indeed makes sense. I really didn't realize those were alignment calculations for the custom DST. Thanks for the detailed explanation! @erikdesjardins It seems to me though this is going to complicate the case for custom DST a lot, since any method call into the DST field have an extra cost of alignment calculations compared to a normal trait object implementation. |
Godbolt link: https://godbolt.org/z/YM65qf41h
I tried this simple custom DST method:
It turns out the generated assembly (
rustc 1.68.0-nightly (659e169d3 2023-01-04) -Copt-level=3
) contains strange operations on the%rdi
register, while never actually using the%rdi
register. It doesn't seem to be performing any caller-reserving-space sort of stuff, and there doesn't seem to be the need for such behavior for this method.If we re-implement it as a trait object method call, there won't be operations on %rdi register but rather a direct call.
I haven't tried with more complex code yet, but with simple methods, there seems to be a consistent pattern where custom DST produces longer code than a typical trait object.
Meta
rustc 1.68.0-nightly (659e169d3 2023-01-04)
:Backtrace
The text was updated successfully, but these errors were encountered: