Skip to content

Vector2.One is not always <1, 1> in some case (silent bad codegen) #54466

Closed

Description

@ryancheung

Reduced repro - Windows x64, repros at commit 9c0bf21 :

  1. Compile with optimizations on:
using System;
using System.Numerics;
using System.Runtime.CompilerServices;

t(1, 1, 1, 1, Vector2.One, Vector2.One, Vector2.One, Vector2.One);

[MethodImplAttribute(MethodImplOptions.NoInlining)]
static void t(int a1, int a2, int a3, int a4, Vector2 x1, Vector2 x2, Vector2 x3, Vector2 x4)
{
   if (x1 != Vector2.One) Console.WriteLine("FAIL");
}
  1. Run (with COMPlus_TieredCompilation=0)

Actual result:

  • Checked build: Assertion failed 'isFloatReg(dstReg) && isFloatReg(srcReg)' in '$:$(System.String[])' during 'Generate code'
  • Release build: FAIL

Expected result: passes without asserts or errors


I have some code:

        public static void DrawSpriteFrame(this CommandBuffer commandBuffer, SpriteFrame spriteFrame, Vector2 position, Color color, float layerDepth = 0f)
        {
            DrawSpriteFrame(commandBuffer, spriteFrame, position, null, color, Vector2.One, 0f, SpriteEffects.None, layerDepth);
        }

        public static void DrawSpriteFrame(this CommandBuffer commandBuffer, SpriteFrame spriteFrame, Vector2 position, Rectangle areaRewrite, Color color, float layerDepth = 0f)
        {
            DrawSpriteFrame(commandBuffer, spriteFrame, position, areaRewrite, color, Vector2.One, 0f, SpriteEffects.None, layerDepth);
        }

        public static void DrawSpriteFrame(this CommandBuffer commandBuffer, SpriteFrame spriteFrame, Vector2 position, Size sizeRewrite, Color color, float layerDepth = 0f)
        {
            var areaRewrite = new Rectangle(0, 0, sizeRewrite.Width, sizeRewrite.Height);
            DrawSpriteFrame(commandBuffer, spriteFrame, position, areaRewrite, color, Vector2.One, 0f, SpriteEffects.None, layerDepth);
        }

        public static void DrawSpriteFrame(this CommandBuffer commandBuffer, SpriteFrame spriteFrame, Vector2 position, Rectangle? areaRewrite, Color color, Vector2 scale, float rotation = 0f, SpriteEffects spriteEffects = SpriteEffects.None, float layerDepth = 0f)
        {
            if (scale != Vector2.One)
            {
                Console.WriteLine("Scale is: {0}", scale);
                return;
            }
            ...
        }

The Vector2.One passed to method calls is not actually equal to Vector2.One in NativeAOT sometimes. It returns <6.28E-43, 5.27E-43> instead. But everything is fine is CoreCLR. And I'm sure the value of the scale parameter is always <1, 1> in all code logic.

The workaround I found: Change the method call parameter Vector2.One to new Vector2(1, 1).

Which looks like:

        public static void DrawSpriteFrame(this CommandBuffer commandBuffer, SpriteFrame spriteFrame, Vector2 position, Color color, float layerDepth = 0f)
        {
            DrawSpriteFrame(commandBuffer, spriteFrame, position, null, color, new Vector2(1f, 1f), 0f, SpriteEffects.None, layerDepth);
        }

Activity

  1. jkotas commented on Jun 19, 2021

    @jkotas
    Member

    Any chance you can share a complete test that reproduces the problem?

    If you are unable to create a small repro and you are not confortable sharing the application that hits the problem publicily, would you be willing to share it privately? My email address is in my github profile.

  2. ryancheung commented on Jun 19, 2021

    @ryancheung
    Author

    Yeah, I can sent you a repro sample by email. Wait a moment.

  3. ryancheung commented on Jun 19, 2021

    @ryancheung
    Author

    @jkotas A repro sample project is sent to you. The vector2-one-issue-repro branch reproduces this issue.

  4. ryancheung commented on Jun 19, 2021

    @ryancheung
    Author

    BTW, I have tested with old Microsoft.DotNet.ILCompiler preview 3 version and it works fine.

  5. transferred this issue fromdotnet/runtimelabon Jun 20, 2021
  6. ghost added
    untriaged
    on Jun 20, 2021
  7. added
    area-CodeGen-coreclr
    and removed
    untriaged
    on Jun 20, 2021
  8. jkotas commented on Jun 20, 2021

    @jkotas
    Member

    @ryancheung Thank you for sharing the repro! This is a bug in the current .NET 6 preview builds. I have transfered to dotnet/runtime repo.

  9. jkotas commented on Jun 20, 2021

    @jkotas
    Member

    cc @dotnet/jit-contrib

  10. added this to the 6.0.0 milestone on Jun 20, 2021
  11. self-assigned this
    on Jun 25, 2021
  12. sandreenko commented on Jun 25, 2021

    @sandreenko
    Contributor

    Thanks for the repro @ryancheung , I will take a look.

  13. ghost added
    in-pr
    on Jun 25, 2021
  14. ghost removed
    in-pr
    on Jun 28, 2021
  15. ghost locked as resolved and limited conversation to collaborators on Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrbug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions