-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Unused struct fields sometimes improve the codegen #79928
Comments
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch, @kunalspathak Issue DetailsGiven: public struct T
{
private double x;
private byte y; // unused
private int z; // unused
public static double M(double v)
{
T t = new T { x = v };
return t.x;
}
} codegen of T:M(double):double:
ret after removing unused fields ( T:M(double):double:
push rax
movsd qword ptr [rsp], xmm0
add rsp, 8
ret IOW, it needs at least one unused field (i.e. size of struct > 8) for this optimization to kick in.
|
Based on .NET 7 on my tests (out of curiosity): it looks like this optimization kicks in for primitive types, except floating point types (float, double). If a floating point type is present, then adding at least one non-FP-type it kicks in again. |
It also gets optimized with two FP fields: https://godbolt.org/z/MnTWfvzK3 (daily build out of |
This is a known limitation of struct promotion -- it currently lacks a good model to decide whether promoting struct wrapped doubles and floats would be profitable. In cases like the above it is, but in cases where the struct wrapped double (or float) is primarily passed as an argument, promotion can be inefficient. cc @jakobbotsch who is looking into generalizing struct promotion. runtime/src/coreclr/jit/lclvars.cpp Lines 2118 to 2134 in e574fbe
|
Given:
codegen of
M
looks like:after removing unused fields (
y
andz
), it turns into:IOW, it needs at least one unused field (i.e. size of struct > 8) for this optimization to kick in.
The text was updated successfully, but these errors were encountered: