Skip to content

Run on .NET 8 Arm64 seems to remove some parts of a conditional statement, probably only after Tier 1 JIT compilation #99954

Closed

Description

Description

We have a code running that runs correctly on Windows x64 and that was running correctly on Arm64 with .NET 7.

Starting from .NET 8, we observed that one if statement does not seem to behave as written in the code.

Extract of the code is like this:

public void Select(Entity entity)
{
   if (ShouldSelectEntity(entity)
   {
      SelectEntity(entity);
   }
}

private void ShouldSelectEntity(Entity entity)
{
   if (entity.TestObject != null ||
      entity.Score <= m_scoreThreshold) // THIS DOES NOT SEEM TO WORK!!!
      return false;

   ... // Other complex conditions, returning true or false according to entity content.
}

private void SelectEntity(Entity entity)
{
   if (entity.Score <= m_scoreThreshold)
   {
      // We should never get here because of the test in 
      // ShouldSelectEntity.
      throw new Exception($"Should not have selected this object because Score {entity.Score} is lower than threshold {m_scoreThreshold}");
   }

   ... // Other stuff.
}

We observed that the exception in SelectEntity is thrown, even if it should never happens.

The code above is not sufficient to reproduce the bug. For the bug to be present, the above code, it needs to be part of a larger program. The initial program has been stripped greatly and is published here:
https://github.com/Louis-Antoine-Blais-Morin/JitBugArm64
It is still relatively large, but it seems that removing parts of the program seems to make the bug disappear. Maybe more effort can lead to a smaller code.

Reproduction Steps

Expected behavior

The if statement should behave as expected in the C# code.
Therefore, no exception should be thrown in the code linked to this bug.

Actual behavior

The if statement in the ShouldSelectEntity does not behave correctly.
The function seems to returns true even if entity.Score > m_scoreThreshold.

This seems to happen after a few seconds of correct operation.
I suspect that the behavior starts to be incorrect after Tier 1 JIT compilation.

Regression?

This is a regression since the program is working correctly with .NET 7.
The program is also working correctly under Windows x64.

Known Workarounds

The following workarounds seem to fix the behavior:

  • Replace the if ( ... || ...) return false; by two individual if statements.
  • Add the following attribute on the ShouldSelectEntity method: [MethodImpl(MethodImplOptions.NoInlining)]
  • Remove the <DebugSymbols>true</DebugSymbols> in the .csproj

Configuration

$ dotnet --list-runtimes
Microsoft.AspNetCore.App 8.0.3 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.3 [/usr/share/dotnet/shared/Microsoft.NETCore.App]`

Processor Architecture is Arm64 Cortex A53 / Armv8-A.

Linux is "Yocto Kirkstone".

Other information

No response

Activity

  1. ghost added
    area-CodeGen-coreclr
    on Mar 19, 2024
  2. added
    untriaged
    on Mar 19, 2024
  3. jakobbotsch commented on Mar 21, 2024

    @jakobbotsch
    Member

    cc @dotnet/jit-contrib

  4. JulieLeeMSFT commented on Mar 21, 2024

    @JulieLeeMSFT
    Member
  5. AndyAyersMS commented on Mar 26, 2024

    @AndyAyersMS
    Member

    @kunalspathak can you give an update on this? Or if you're tied up can we find somebody else to dig in?

  6. kunalspathak commented on Mar 26, 2024

    @kunalspathak
    Contributor

    @kunalspathak can you give an update on this? Or if you're tied up can we find somebody else to dig in?

    Sorry, I forgot about this. I was able to repro this and I can confirm that this doesn't repro on .NET 9 main. I will investigate.

  7. kunalspathak commented on Mar 27, 2024

    @kunalspathak
    Contributor

    The problem is the reverse condition was wrongly calculated as seen in screenshot( left= net9/correct, right= net8/wrong)

    image

    We fixed it in #92316 and might need to backport.

  8. kunalspathak commented on Mar 27, 2024

    @kunalspathak
    Contributor

    btw, this is easily reproducible with DOTNET_TieredCompilation=0 and marking Select as NoInlining and ShouldSelectEntity as AggresivelyInline. I also changed ScoreThreshold=10.0f and Score = (float)0.0f,//random.NextDouble(),

            [MethodImpl(MethodImplOptions.NoInlining)]
            private void Select(Entity trackingObject)
            {
                if (ShouldSelectEntity(trackingObject))
                {
                    SelectEntity(trackingObject);
                }
            }
    
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            private bool ShouldSelectEntity(Entity entity)
            {
                ...
            }
  9. kunalspathak commented on Mar 28, 2024

    @kunalspathak
    Contributor

    We fixed it in #92316 and might need to backport.

    I verified this fix locally and it doesn't throw Exception anymore.

  10. added this to the 9.0.0 milestone on Mar 29, 2024
  11. removed
    untriaged
    on Mar 29, 2024
  12. kunalspathak commented on May 2, 2024

    @kunalspathak
    Contributor

    This should be part of the upcoming .NET 8.0.5

  13. AndyAyersMS commented on May 31, 2024

    @AndyAyersMS
    Member

    @Louis-Antoine-Blais-Morin .NET 8.0.5 is now available -- when you get a chance can you verify it fixes this issue for you?

  14. Louis-Antoine-Blais-Morin commented on May 31, 2024

    @Louis-Antoine-Blais-Morin
    Author

    @AndyAyersMS
    Thanks for all this work. We followed it closely.
    With the fix now available, we are now trying once again to switch to .NET 8 (because of the problem, we stayed in .NET 7).
    I will inform you about the results in the coming days.

  15. kunalspathak commented on Jul 25, 2024

    @kunalspathak
    Contributor

    @Louis-Antoine-Blais-Morin - did you get a chance to validate this?

  16. added
    needs-author-action
    on Jul 25, 2024
  17. Louis-Antoine-Blais-Morin commented on Jul 29, 2024

    @Louis-Antoine-Blais-Morin
    Author

    Good morning,

    Providing feedback on this got of my radar for a while.

    I had time to test the correction today:

    • We had updated our Linux image to .NET runtime 8.0.5
    • I removed the decorator [MethodImpl(MethodImplOptions.NoInlining)] that was in our code as a workaround
    • Code runs correctly

    So, as far as I can see, the bug is fixed.
    Thanks a lot!

  18. added
    needs-further-triage
    and removed
    needs-author-action
    on Jul 29, 2024
  19. locked and limited conversation to collaborators on Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrneeds-further-triage

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions