Skip to content

Commit

Permalink
For Sse41.Insert, do not contain vec-zero on op1/op2 if the immediate…
Browse files Browse the repository at this point in the history
… value is greater than 63
  • Loading branch information
TIHan committed Aug 7, 2024
1 parent 50d6cad commit fdeea5a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 40 deletions.
65 changes: 25 additions & 40 deletions src/coreclr/jit/lowerxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9415,6 +9415,29 @@ bool Lowering::IsContainableHWIntrinsicOp(GenTreeHWIntrinsic* parentNode, GenTre
supportsSIMDScalarLoads = (ival <= 0x3F);
supportsGeneralLoads = supportsSIMDScalarLoads;
}

if (supportsGeneralLoads && childNode->IsVectorZero() && ((op1 == childNode) || (op2 == childNode)))
{
// When op1 or op2 are zero, we can contain it and we expect that
// zmask is already in the correct state to account for it

#if DEBUG
ssize_t ival = op3->AsIntCon()->IconValue();

ssize_t zmask = (ival & 0x0F);
ssize_t count_d = (ival & 0x30) >> 4;
ssize_t count_s = (ival & 0xC0) >> 6;

zmask |= (ssize_t(1) << count_d);
zmask &= 0x0F;

ssize_t expected = (count_s << 6) | (count_d << 4) | (zmask);
assert(ival == expected);
#endif
*supportsRegOptional = false;
return true;
}

break;
}

Expand Down Expand Up @@ -11320,51 +11343,13 @@ void Lowering::ContainCheckHWIntrinsic(GenTreeHWIntrinsic* node)
// Where count_d specifies the destination index the value is being inserted to
// Where count_s specifies the source index of the value being inserted

if (op1->IsVectorZero())
if (IsContainableHWIntrinsicOp(node, op1, &supportsRegOptional))
{
// When op1 is zero, we can contain it and we expect that
// ival is already in the correct state to account for it

#if DEBUG
ssize_t ival = lastOp->AsIntConCommon()->IconValue();

ssize_t zmask = (ival & 0x0F);
ssize_t count_d = (ival & 0x30) >> 4;
ssize_t count_s = (ival & 0xC0) >> 6;

zmask |= ~(ssize_t(1) << count_d);
zmask &= 0x0F;

ssize_t expected = (count_s << 6) | (count_d << 4) | (zmask);
assert(ival == expected);
#endif

MakeSrcContained(node, op1);
}
else if (op2->IsVectorZero())
{
// When op2 is zero, we can contain it and we expect that
// zmask is already in the correct state to account for it

#if DEBUG
ssize_t ival = lastOp->AsIntConCommon()->IconValue();

ssize_t zmask = (ival & 0x0F);
ssize_t count_d = (ival & 0x30) >> 4;
ssize_t count_s = (ival & 0xC0) >> 6;

zmask |= (ssize_t(1) << count_d);
zmask &= 0x0F;

ssize_t expected = (count_s << 6) | (count_d << 4) | (zmask);
assert(ival == expected);
#endif

MakeSrcContained(node, op2);
}
}

if (IsContainableHWIntrinsicOp(node, op2, &supportsRegOptional))
if (!op1->isContained() && IsContainableHWIntrinsicOp(node, op2, &supportsRegOptional))
{
MakeSrcContained(node, op2);
}
Expand Down
35 changes: 35 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_105467/Runtime_105467.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

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

// Generated by Fuzzlyn v1.7 on 2024-07-25 11:29:20
// Run on X64 Windows
// Seed: 17281006834984098297-vectort,vector128,vector256,x86aes,x86avx,x86avx2,x86bmi1,x86bmi1x64,x86bmi2,x86bmi2x64,x86fma,x86lzcnt,x86lzcntx64,x86pclmulqdq,x86popcnt,x86popcntx64,x86sse,x86ssex64,x86sse2,x86sse2x64,x86sse3,x86sse41,x86sse41x64,x86sse42,x86sse42x64,x86ssse3,x86x86base
// Reduced from 159.4 KiB to 0.4 KiB in 00:00:57
// Hits JIT assert in Release:
// Assertion failed 'isContainable || supportsRegOptional' in 'Program:Main(Fuzzlyn.ExecutionServer.IRuntime)' during 'Generate code' (IL size 47; hash 0xade6b36b; FullOpts)
//
// File: C:\dev\dotnet\runtime4\src\coreclr\jit\hwintrinsiccodegenxarch.cpp Line: 61
//

public class Runtime_105467
{
public static Vector128<float>[] s_6 = new []{ Vector128<float>.Zero };

[Fact]
public static void TestEntryPoint()
{
if (Sse41.IsSupported)
{
var vr3 = s_6[0];
var vr4 = Vector128.Create<float>(0);
s_6[0] = Sse41.Insert(vr3, vr4, 254);
}
}
}
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>

0 comments on commit fdeea5a

Please sign in to comment.