Skip to content

Commit

Permalink
Ensure that the Create(Dot(...)) optimization doesn't kick in for Vec…
Browse files Browse the repository at this point in the history
…tor2 pre SSE4.1 (dotnet#96951)

* Ensure that the Create(Dot(...)) optimization doesn't kick in for Vector2 pre SSE4.1

* Make sure to use #if defined(...)

* Add missing using

* Fix a type in the test
  • Loading branch information
tannergooding authored and tmds committed Jan 23, 2024
1 parent 80f1ed6 commit 1535f3d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10605,6 +10605,16 @@ GenTree* Compiler::fgOptimizeHWIntrinsic(GenTreeHWIntrinsic* node)
break;
}

#if defined(TARGET_XARCH)
if ((node->GetSimdSize() == 8) && !compOpportunisticallyDependsOn(InstructionSet_SSE41))
{
// When SSE4.1 isn't supported then Vector2 only needs a single horizontal add
// which means the result isn't broadcast across the entire vector and we can't
// optimize
break;
}
#endif // TARGET_XARCH

GenTree* op1 = node->Op(1);
GenTree* sqrt = nullptr;
GenTree* toScalar = nullptr;
Expand Down
38 changes: 38 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using Xunit;

public static class Runtime_96939
{
[Fact]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static void Problem()
{
Assert.Equal(new Vector2(13), TestVector2(new Vector2(2, 3)));
Assert.Equal(new Vector3(29), TestVector3(new Vector3(2, 3, 4)));
Assert.Equal(new Vector4(54), TestVector4(new Vector4(2, 3, 4, 5)));
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
public static Vector2 TestVector2(Vector2 value)
{
return Vector2.Dot(value, value) * new Vector2(1, 1);
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
public static Vector3 TestVector3(Vector3 value)
{
return Vector3.Dot(value, value) * new Vector3(1, 1, 1);
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
public static Vector4 TestVector4(Vector4 value)
{
return Vector4.Dot(value, value) * new Vector4(1, 1, 1, 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Optimize>True</Optimize>
<DebugType>None</DebugType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>

0 comments on commit 1535f3d

Please sign in to comment.