From 1827d418c19b1ee1688598542021d80e6f6bef88 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Sat, 13 Jan 2024 20:17:52 -0800 Subject: [PATCH 1/4] Ensure that the Create(Dot(...)) optimization doesn't kick in for Vector2 pre SSE4.1 --- src/coreclr/jit/morph.cpp | 10 +++++ .../JitBlue/Runtime_96939/Runtime_96939.cs | 37 +++++++++++++++++++ .../Runtime_96939/Runtime_96939.csproj | 10 +++++ 3 files changed, 57 insertions(+) create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.csproj diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp index 3deada8eec085b..6c71dc50137f21 100644 --- a/src/coreclr/jit/morph.cpp +++ b/src/coreclr/jit/morph.cpp @@ -10724,6 +10724,16 @@ GenTree* Compiler::fgOptimizeHWIntrinsic(GenTreeHWIntrinsic* node) break; } +#if 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; diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs b/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs new file mode 100644 index 00000000000000..d9361a6045761e --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs @@ -0,0 +1,37 @@ +// 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.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(Vector2 value) + { + return Vector4.Dot(value, value) * new Vector4(1, 1, 1, 1); + } +} diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.csproj new file mode 100644 index 00000000000000..596f2374a8fb3b --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.csproj @@ -0,0 +1,10 @@ + + + True + None + true + + + + + From 144146265608be905b185c5e0254b0e155a30d7b Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Sat, 13 Jan 2024 20:30:20 -0800 Subject: [PATCH 2/4] Make sure to use #if defined(...) --- src/coreclr/jit/morph.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp index 6c71dc50137f21..b20cd585882b87 100644 --- a/src/coreclr/jit/morph.cpp +++ b/src/coreclr/jit/morph.cpp @@ -10724,7 +10724,7 @@ GenTree* Compiler::fgOptimizeHWIntrinsic(GenTreeHWIntrinsic* node) break; } -#if TARGET_XARCH +#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 From 1f194fc3895bb4a3f0c1c347f51e33f10849cd91 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Sun, 14 Jan 2024 19:32:01 -0800 Subject: [PATCH 3/4] Add missing using --- src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs b/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs index d9361a6045761e..b3242e76e6e05a 100644 --- a/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs +++ b/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs @@ -2,6 +2,7 @@ // 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; From 3ab32dc6ec35aee2b4932276a18aeb850362165d Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Mon, 15 Jan 2024 11:17:42 -0800 Subject: [PATCH 4/4] Fix a type in the test --- src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs b/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs index b3242e76e6e05a..ac26af680eea80 100644 --- a/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs +++ b/src/tests/JIT/Regression/JitBlue/Runtime_96939/Runtime_96939.cs @@ -31,7 +31,7 @@ public static Vector3 TestVector3(Vector3 value) } [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] - public static Vector4 TestVector4(Vector2 value) + public static Vector4 TestVector4(Vector4 value) { return Vector4.Dot(value, value) * new Vector4(1, 1, 1, 1); }