Skip to content

RyuJIT properly optimizes structs with a single field if the field type is int but not if it is double #4323

@mikedn

Description

@mikedn

Sample C# code:

struct Number {
    private double value;
    public static implicit operator Number(double value) {
        return new Number { value = value };
    }
    public static implicit operator double (Number number) {
        return number.value;
    }
    public static Number operator +(Number x, Number y) {
        return x.value + y.value;
    }
}
class Program {
    static int Main() {
        Number x = 4, y = 2;
        return (int)(x + y);
    }
}

A portion of the generated code:

vmovsd      xmm0,qword ptr [rsp+20h]
vaddsd      xmm0,xmm0,mmword ptr [rsp+18h]
xor         eax,eax
mov         qword ptr [rsp+10h],rax
mov         qword ptr [rsp+10h],rax
vmovsd      qword ptr [rsp+10h],xmm0
mov         rax,qword ptr [rsp+10h]
mov         qword ptr [rsp+38h],rax
mov         rax,qword ptr [rsp+38h]
mov         qword ptr [rsp+8],rax
vmovsd      xmm0,qword ptr [rsp+8]
vcvttsd2si  eax,xmm0

If int is used intstead of double or if double is used directly instead of Number then the generated code is simply mov eax, 6.

category:cq
theme:structs
skill-level:expert
cost:medium
impact:medium

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIenhancementProduct code improvement that does NOT require public API changes/additionsoptimizationtenet-performancePerformance related issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions