dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.95k stars 4.65k forks source link

JIT SVE: Bad codegen with `TestFirstTrue` #106871

Closed jakobbotsch closed 1 week ago

jakobbotsch commented 3 weeks ago
// Generated by Fuzzlyn v2.3 on 2024-08-23 10:47:43
// Run on Arm64 Windows
// Seed: 6363239363759785984-vectort,vector64,vector128,armsve
// Reduced from 29.6 KiB to 0.8 KiB in 00:00:27
// Debug: Prints 0 line(s)
// Release: Prints 1 line(s)
using System;
using System.Numerics;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;

public class Program
{
    public static void Main()
    {
        var vr11 = (byte)1;
        var vr12 = Vector128.CreateScalar(vr11).AsVector();
        var vr13 = Vector.Create<double>(1);
        var vr14 = Vector.Create<double>(1);
        var vr15 = (byte)Sve.ConditionalExtractAfterLastActiveElement(vr13, 0, vr14);
        var vr16 = Vector128.CreateScalar(vr15).AsVector();
        var vr17 = Vector.Create<byte>(0);
        var vr18 = (byte)1;
        var vr19 = Vector128.CreateScalar(vr18).AsVector();
        var vr20 = Sve.MinAcross(vr19);
        var vr21 = Sve.ConditionalSelect(vr16, vr20, vr17);
        if (Sve.TestFirstTrue(vr12, vr21))
        {
            System.Console.WriteLine(0);
        }
    }
}

cc @dotnet/arm64-contrib @dotnet/jit-contrib

dotnet-policy-service[bot] commented 3 weeks ago

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

a74nh commented 2 weeks ago

This is due to:

        var vr17 = Vector.Create<byte>(0);
        var vr20 = Sve.MinAcross(vr19);
        var vr21 = Sve.ConditionalSelect(vr16, vr20, vr17);

being generated into a single instruction:

  UMINV vr20, vr16, vr19

Instead this need to be:

  UMINV vr20, TRUE, vr19
  SEL vr16, vr20, ZERO

Any operations that work across the vector cannot be optimized like that.

a74nh commented 2 weeks ago

@SwapnilGaikwad this is the same issue as #106872 and #106868. Assigning to you.