Skip to content

Ensure VN produces correctly initialized simdmask_t #115227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/coreclr/jit/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,18 +330,26 @@ struct simdmask_t
return !(*this == other);
}

static simdmask_t AllBitsSet()
static simdmask_t AllBitsSet(unsigned elementCount)
{
assert((elementCount >= 1) && (elementCount <= 64));
simdmask_t result;

result.u64[0] = 0xFFFFFFFFFFFFFFFF;
if (elementCount == 64)
{
result.u64[0] = 0xFFFFFFFFFFFFFFFF;
}
else
{
result.u64[0] = (1ULL << elementCount) - 1;
}

return result;
}

bool IsAllBitsSet() const
{
return *this == AllBitsSet();
return *this == AllBitsSet(64);
}

bool IsZero() const
Expand Down
27 changes: 16 additions & 11 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2177,7 +2177,7 @@ ValueNum ValueNumStore::VNOneForType(var_types typ)
}
}

ValueNum ValueNumStore::VNAllBitsForType(var_types typ)
ValueNum ValueNumStore::VNAllBitsForType(var_types typ, unsigned elementCount)
{
switch (typ)
{
Expand Down Expand Up @@ -2224,7 +2224,7 @@ ValueNum ValueNumStore::VNAllBitsForType(var_types typ)
#if defined(FEATURE_MASKED_HW_INTRINSICS)
case TYP_MASK:
{
return VNForSimdMaskCon(simdmask_t::AllBitsSet());
return VNForSimdMaskCon(simdmask_t::AllBitsSet(elementCount));
}
#endif // FEATURE_MASKED_HW_INTRINSICS
#endif // FEATURE_SIMD
Expand Down Expand Up @@ -5350,7 +5350,7 @@ ValueNum ValueNumStore::EvalUsingMathIdentity(var_types typ, VNFunc func, ValueN
}

// Handle `x | AllBitsSet == AllBitsSet` and `AllBitsSet | x == AllBitsSet`
if (cnsVN == VNAllBitsForType(typ))
if (cnsVN == VNAllBitsForType(typ, 1))
{
resultVN = cnsVN;
break;
Expand Down Expand Up @@ -5395,7 +5395,7 @@ ValueNum ValueNumStore::EvalUsingMathIdentity(var_types typ, VNFunc func, ValueN
}

// Handle `x & AllBitsSet == x` and `AllBitsSet & x == x`
if (cnsVN == VNAllBitsForType(typ))
if (cnsVN == VNAllBitsForType(typ, 1))
{
resultVN = opVN;
break;
Expand Down Expand Up @@ -8393,7 +8393,7 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunBinary(
}

// Handle `x & AllBitsSet == x` and `AllBitsSet & x == x`
ValueNum allBitsVN = VNAllBitsForType(type);
ValueNum allBitsVN = VNAllBitsForType(type, simdSize);

if (cnsVN == allBitsVN)
{
Expand Down Expand Up @@ -8482,7 +8482,8 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunBinary(
// Handle `x >= 0 == true` for unsigned types.
if ((cnsVN == arg1VN) && (cnsVN == VNZeroForType(simdType)))
{
return VNAllBitsForType(type);
unsigned elementCount = simdSize / genTypeSize(baseType);
return VNAllBitsForType(type, elementCount);
}
}
else if (varTypeIsFloating(baseType))
Expand Down Expand Up @@ -8528,7 +8529,8 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunBinary(
// Handle `0 <= x == true` for unsigned types.
if ((cnsVN == arg0VN) && (cnsVN == VNZeroForType(simdType)))
{
return VNAllBitsForType(type);
unsigned elementCount = simdSize / genTypeSize(baseType);
return VNAllBitsForType(type, elementCount);
}
}
else if (varTypeIsFloating(baseType))
Expand Down Expand Up @@ -8598,7 +8600,8 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunBinary(
// Handle `(x != NaN) == true` and `(NaN != x) == true` for floating-point types
if (VNIsVectorNaN(simdType, baseType, cnsVN))
{
return VNAllBitsForType(type);
unsigned elementCount = simdSize / genTypeSize(baseType);
return VNAllBitsForType(type, elementCount);
}
}
break;
Expand All @@ -8615,7 +8618,7 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunBinary(
}

// Handle `x | ~0 == ~0` and `~0 | x== ~0`
ValueNum allBitsVN = VNAllBitsForType(type);
ValueNum allBitsVN = VNAllBitsForType(type, simdSize);

if (cnsVN == allBitsVN)
{
Expand Down Expand Up @@ -8849,7 +8852,8 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunBinary(

if (varTypeIsIntegral(baseType))
{
return VNAllBitsForType(type);
unsigned elementCount = simdSize / genTypeSize(baseType);
return VNAllBitsForType(type, elementCount);
}
break;
}
Expand Down Expand Up @@ -9054,6 +9058,7 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunTernary(
{
var_types type = tree->TypeGet();
var_types baseType = tree->GetSimdBaseType();
unsigned simdSize = tree->GetSimdSize();
NamedIntrinsic ni = tree->GetHWIntrinsicId();

switch (ni)
Expand All @@ -9078,7 +9083,7 @@ ValueNum ValueNumStore::EvalHWIntrinsicFunTernary(
}

// Handle `AllBitsSet ? x : y`
ValueNum allBitsVN = VNAllBitsForType(type);
ValueNum allBitsVN = VNAllBitsForType(type, simdSize);

if (arg0VN == allBitsVN)
{
Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/jit/valuenum.h
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,8 @@ class ValueNumStore

// Returns the value number for AllBitsSet of the given "typ".
// It has an unreached() for a "typ" that has no all bits set value, such as TYP_VOID.
ValueNum VNAllBitsForType(var_types typ);
// elementCount is used for TYP_MASK and indicates how many bits should be set
ValueNum VNAllBitsForType(var_types typ, unsigned elementCount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document elementCount?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

I added a description for it matching the existing format since none of the other methods in here are following the more "standard" method documentation format.

It might be desirable to normalize that later, but I went for consistency now.


#ifdef FEATURE_SIMD
// Returns the value number broadcast of the given "simdType" and "simdBaseType".
Expand Down
76 changes: 76 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_114978/Runtime_114978.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Generated by Fuzzlyn v2.7 on 2025-04-23 21:44:41
// Run on X64 Windows
// Seed: 3309115426102150651-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 179.3 KiB to 1.4 KiB in 00:01:49
// Debug: Outputs <255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255>
// Release: Outputs <255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>

using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using Xunit;

public class C0
{
}

public class C1
{
public Vector512<short> F4;
}

public class C2
{
public Vector128<sbyte> F6;
}

public class Runtime_114978
{
public static IRuntime s_rt;

[Fact]
public static void Problem()
{
if (Avx512BW.VL.IsSupported)
{
s_rt = new Runtime();
var vr14 = new C1();
var vr16 = new C1();
var vr17 = (byte)0;
var vr18 = Vector512.CreateScalar(vr17);
var vr19 = new C2();
var vr20 = new C0();
var vr21 = M10(vr14, vr18, vr19, vr20);
s_rt.AssertEqual(Vector512<byte>.AllBitsSet, vr21);
var vr22 = new C2();
var vr23 = new C0();
var vr24 = M10(vr16, vr21, vr22, vr23);
s_rt.AssertEqual(Vector512<byte>.AllBitsSet, vr24);
}
}

public static Vector512<byte> M10(C1 argThis, Vector512<byte> arg0, C2 arg1, C0 arg2)
{
var vr3 = arg1.F6;
var vr6 = arg1.F6;
var vr0 = Avx512BW.VL.CompareLessThanOrEqual(vr3, vr6);
var vr10 = (sbyte)1;
var vr9 = Vector128.CreateScalar(vr10);
if (Sse41.TestZ(vr0, vr9))
{
s_rt.AssertEqual(Vector512<short>.Zero, argThis.F4);
}

return Avx512BW.CompareEqual(arg0, arg0);
}
}

public interface IRuntime
{
void AssertEqual<T>(Vector512<T> value1, Vector512<T> value2);
}

public class Runtime : IRuntime
{
public void AssertEqual<T>(Vector512<T> value1, Vector512<T> value2)
=> Assert.Equal(value1, value2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>
Loading