Skip to content

JIT: wrong result with Avx512F.BlendVariable #114921

Closed

Description

@BruceForstall
// Generated by Fuzzlyn v2.5 on 2025-04-22 17:32:36
// Run on X64 Windows
// Seed: 7915602115310323123-vectort,vector128,vector256,vector512,x86aes,x86avx,x86avx2,x86avx512bw,x86avx512bwvl,x86avx512cd,x86avx512cdvl,x86avx512dq,x86avx512dqvl,x86avx512f,x86avx512fvl,x86avx512fx64,x86avx512vbmi,x86avx512vbmivl,x86bmi1,x86bmi1x64,x86bmi2,x86bmi2x64,x86fma,x86lzcnt,x86lzcntx64,x86pclmulqdq,x86popcnt,x86popcntx64,x86sse,x86ssex64,x86sse2,x86sse2x64,x86sse3,x86sse41,x86sse41x64,x86sse42,x86sse42x64,x86ssse3,x86x86base
// Reduced from 123.1 KiB to 0.5 KiB in 00:00:46
// Debug: Outputs <0, 0, 0, 0, 0, 0, 0, 0>
// Release: Outputs <0, 0, 0, 0, -1, -1, -1, -1>
using System;
using System.Numerics;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

public class Program
{
    public static Vector512<long> s_4 = Vector512.Create<long>(-1);
    public static Vector128<long> s_8;
    public static void Main()
    {
        var vr1 = Vector512.Create<long>(0);
        s_4 = Avx512F.BlendVariable(s_4, Avx512F.InsertVector128(vr1, s_8, 0), s_4);
        System.Console.WriteLine(s_4);
    }
}

another case:

// Generated by Fuzzlyn v2.5 on 2025-04-22 17:37:13
// Run on X64 Windows
// Seed: 14731447107126414231-vectort,vector128,vector256,vector512,x86aes,x86avx,x86avx2,x86avx512bw,x86avx512bwvl,x86avx512cd,x86avx512cdvl,x86avx512dq,x86avx512dqvl,x86avx512f,x86avx512fvl,x86avx512fx64,x86avx512vbmi,x86avx512vbmivl,x86bmi1,x86bmi1x64,x86bmi2,x86bmi2x64,x86fma,x86lzcnt,x86lzcntx64,x86pclmulqdq,x86popcnt,x86popcntx64,x86sse,x86ssex64,x86sse2,x86sse2x64,x86sse3,x86sse41,x86sse41x64,x86sse42,x86sse42x64,x86ssse3,x86x86base
// Reduced from 217.7 KiB to 1.0 KiB in 00:02:50
// Debug: Outputs <9223372036854775807, 0, 0, 0, 0, 0, 0, 0>
// Release: Outputs <4294967295, 0, 0, 0, 0, 0, 0, 0>
using System;
using System.Numerics;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

public struct S2
{
    public Vector128<long> F0;
    public S2(Vector128<long> f0) : this()
    {
        F0 = f0;
    }
}

public class Program
{
    public static IRuntime s_rt;
    public static void Main()
    {
        s_rt = new Runtime();
        long vr6 = default(long);
        S2 vr7 = new S2(Vector128.CreateScalar(9223372036854775807L));
        Vector512<long> vr14 = default(Vector512<long>);
        var vr9 = Vector512.Create<long>(vr6);
        var vr10 = vr7.F0;
        var vr11 = Avx512F.InsertVector128(vr9, vr10, 0);
        var vr12 = Vector512.CreateScalar(-9223372036854775808L);
        var vr13 = Avx512F.BlendVariable(vr14, vr11, vr12);
        s_rt.WriteLine(vr13);
    }
}

public interface IRuntime
{
    void WriteLine<T>(T value);
}

public class Runtime : IRuntime
{
    public void WriteLine<T>(T value) => System.Console.WriteLine(value);
}

cc @dotnet/jit-contrib @dotnet/intel

Activity

  1. added
    area-CodeGen-coreclr
    avx512
    on Apr 22, 2025
  2. added this to the 10.0.0 milestone on Apr 22, 2025
  3. dotnet-policy-service commented on Apr 22, 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. added a commit that references this issue on Apr 26, 2025
    e72f9bd
  5. BruceForstall commented on Apr 26, 2025

    @BruceForstall
    ContributorAuthor

    The problem is with embedded masking and Avx512F.InsertVector128 -> vinserti32x4.

    In the first example above, the JIT generates:

    vpmovq2m k1, zmm0
    vmovaps  zmm6, zmm0
    vinserti32x4 zmm6 {k1}, zmm1, xmmword ptr [rcx], 0
    

    the problem is that zmm0/zmm1/zmm2 are Vector512<long>. The mask k1 is properly constructed, with 8 bits for the 8 long lanes. But vinserti32x4 operates 32-bits at a time, so uses 16 k bits.

    The JIT could generate vinserti64x2 in this case, if DQ is available.

    It seems like the JIT assumes that a type with size N will generate an instruction that treats the opmask similarly, e.g., type long generates an instruction that uses 8 opmask bits, type int generates an instruction that uses 16 opmask bits, type short generates an instruction that uses 32 opmask bits. Is that true, and correct?

    I wrote a PR that fixes this problem by special-casing INS_vinserti128, INS_vinserti32x8, and INS_vinserti64x2, which gives zero asm diffs: #115074

    (fwiw, the JIT code is quite confusing because it uses INS_vinserti128 as a pseudo (?) instruction instead of using vinserti32x4 directly.)

    @dotnet/intel comments?

  6. added a commit that references this issue on May 3, 2025
    8392660
  7. added a commit that references this issue on May 14, 2025
    d7403e4
  8. locked and limited conversation to collaborators on Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclravx512

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions