Skip to content

JIT spills struct field to stack when used with ternary in compound assignment #117416

Closed

Description

@rameel

sharplab | godbolt

Sample.X()
    L0000: push rax
    L0001: xor eax, eax
    L0003: mov [rsp], rax
    L0007: mov eax, [rsp]
    L000a: add eax, 0x20
    L000d: mov [rsp], eax
    L0010: cmp eax, 0x64
    L0013: jl short L0007
    L0015: add rsp, 8
    L0019: ret

Sample.Y()
    L0000: xor eax, eax
    L0002: add eax, 0x20
    L0005: mov ecx, eax
    L0007: cmp eax, 0x64
    L000a: jl short L0002
    L000c: mov eax, ecx
    L000e: ret
using System;
using System.Runtime.CompilerServices;

public static class Sample
{
    public static int X()
    {
        var iterator = new Iterator();
        var v = 0;
        while (v < 100)
            v = iterator.Next_1();

        return v;
    }
    
    public static int Y()
    {
        var iterator = new Iterator();
        var v = 0;
        while (v < 100)
            v = iterator.Next_2();

        return v;
    }
}

public struct Iterator
{
    public static bool IsSupported 
    {
        // [MethodImpl(MethodImplOptions.NoInlining)]
        get => true;
    }
    
    private int _value;

    public int Next_1()
    {
        return _value += IsSupported ? 32 : 16;
    }

    public int Next_2()
    {
        var n = IsSupported ? 32 : 16;
        return _value += n;
    }
}

Activity

  1. added
    untriaged
    on Jul 8, 2025
  2. added
    area-CodeGen-coreclr
    on Jul 8, 2025
  3. dotnet-policy-service commented on Jul 8, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. jakobbotsch commented on Jul 8, 2025

    @jakobbotsch
    Member

    Works fine with DOTNET_JitStressModeNames=STRESS_NO_OLD_PROMOTION, so looks like it's just old promotion making a mess of things.

  5. removed
    untriaged
    on Jul 8, 2025
  6. self-assigned this
    on Jul 8, 2025
  7. added this to the 11.0.0 milestone on Jul 8, 2025
  8. added
    Priority:3
    on Jul 9, 2026
  9. locked and limited conversation to collaborators on Aug 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Priority:3area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions