-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
HVA structs can be unpacked without spilling to stack #96372
Comments
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch Issue DetailsDescriptionCurrently, when passing structs or their member that are HVA of This, however, can be improved by avoiding spilling by extracting contents of the structs passed in such way via bitwise operations. Configuration.NET 8.0.100 Regression?No DataGiven static int Sum(HVA32 value) => value.A + value.B;
static long Sum(HVA64 value) => value.A + value.B;
record struct HVA32(int A, int B);
record struct HVA64(long A, long B); The produced assembly is Sum(HVA32)G_M56534_IG01: ;; offset=0x0000
stp fp, lr, [sp, #-0x20]!
mov fp, sp
str x0, [fp, #0x18] // [V00 arg0]
G_M56534_IG02: ;; offset=0x000C
ldp w0, w1, [fp, #0x18] // [V00 arg0], [V00 arg0+0x04]
add w0, w0, w1
G_M56534_IG03: ;; offset=0x0014
ldp fp, lr, [sp], #0x20
ret lr
; Total bytes of code: 28 Sum(HVA64)G_M14860_IG01: ;; offset=0x0000
stp fp, lr, [sp, #-0x10]!
mov fp, sp
G_M14860_IG02: ;; offset=0x0008
add x0, x0, x1
G_M14860_IG03: ;; offset=0x000C
ldp fp, lr, [sp], #0x10
ret lr
; Total bytes of code: 20 AnalysisIn the data above, G_M49852_IG01: ;; offset=0x0000
stp fp, lr, [sp, #-0x10]!
mov fp, sp
G_M49852_IG02: ;; offset=0x0008
asr x1, x0, #32
add w0, w1, w0
G_M49852_IG03: ;; offset=0x0010
ldp fp, lr, [sp], #0x10
ret lr This can be manually replicated with static int Sum(long packed)
{
var a = (int)(packed >> 32);
var b = (int)packed;
return a + b;
} but leads to other codegen issues and is not usable as a micro-optimization as a result. Thanks!
|
I think we discussed independent promotion many times, but I don't see any existing issue to track it, perhaps, @jakobbotsch knows. |
Related to #89374 I think. |
Yeah, this, #11992, #89374, #91517 and likely a bunch other CQ issues would require some new fundamental handling of structs in the backend. It's one of the stretch goals of #93105. I do not expect to work towards that with the old promotion scheme, but long term I do want us to do better here. #92026 was an experiment trying out one representation to work towards it. |
Description
Currently, when passing structs or their member that are HVA of
(int, int)
,(short, short, short, short)
, etc., .NET spills them to stack first and then loads them back before performing further operations.This, however, can be improved by avoiding spilling by extracting contents of the structs passed in such way via bitwise operations.
Configuration
.NET 8.0.100
Regression?
No
Data
Given
The produced assembly is
Sum(HVA32)
Sum(HVA64)
Analysis
In the data above,
HVA64
variant has ideal codegen because its members are passed in separate regisers.HVA32
causes spill and load. Ideally, it would be nice to see the following emitted for it instead:This can be manually replicated with
but leads to other codegen issues and is not usable as a micro-optimization as a result.
Thanks!
The text was updated successfully, but these errors were encountered: