From db3fa7e09df202491cb6fe9e3fb24869452d0774 Mon Sep 17 00:00:00 2001 From: TIHan Date: Mon, 14 Aug 2023 10:50:24 -0700 Subject: [PATCH 1/7] Added regression test 85765 --- .../JitBlue/Runtime_85765/Runtime_85765.cs | 44 +++++++++++++++++++ .../Runtime_85765/Runtime_85765.csproj | 8 ++++ 2 files changed, 52 insertions(+) create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.csproj diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs new file mode 100644 index 0000000000000..ab8f9dccdd669 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs @@ -0,0 +1,44 @@ +// 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 Xunit; + +public class Runtime_85765 +{ + public struct S0 + { + public S0(bool f1): this() + { + } + } + + public struct S1 + { + public byte F0; + public bool F1; + public bool F2; + } + + [Fact] + public static void Test() + { + S1 vr2 = M4(); + vr2.F2 |= vr2.F1; + Assert.False(Consume(vr2.F2)); + } + + public static S1 M4() + { + S1 var1 = default(S1); + var vr0 = new S0(false); + return var1; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static bool Consume(bool value) + { + return value; + } +} diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.csproj new file mode 100644 index 0000000000000..de6d5e08882e8 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.csproj @@ -0,0 +1,8 @@ + + + True + + + + + From eaa5ac961212f8d48fbb86c695c4b984596c117b Mon Sep 17 00:00:00 2001 From: TIHan Date: Mon, 14 Aug 2023 14:06:18 -0700 Subject: [PATCH 2/7] Mark M4 as NoInlining --- src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs index ab8f9dccdd669..9f6844cd9c87e 100644 --- a/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs +++ b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs @@ -29,6 +29,7 @@ public static void Test() Assert.False(Consume(vr2.F2)); } + [MethodImpl(MethodImplOptions.NoInlining)] public static S1 M4() { S1 var1 = default(S1); From 782d7a241a0bd1f01931587f71f1f4b57cf0fb78 Mon Sep 17 00:00:00 2001 From: TIHan Date: Tue, 15 Aug 2023 13:59:14 -0700 Subject: [PATCH 3/7] Check current and last ins format to determine whether to proceed with the optimized ldr/str pair --- src/coreclr/jit/emitarm64.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/coreclr/jit/emitarm64.cpp b/src/coreclr/jit/emitarm64.cpp index b4e81322e2a69..f01ad548d2ee0 100644 --- a/src/coreclr/jit/emitarm64.cpp +++ b/src/coreclr/jit/emitarm64.cpp @@ -16615,6 +16615,15 @@ emitter::RegisterOrder emitter::IsOptimizableLdrStrWithPair( emitAttr prevSize = emitLastIns->idOpSize(); ssize_t prevImm = emitGetInsSC(emitLastIns); + // If we have this format, the 'imm' and/or 'prevImm' are not scaled(encoded), + // therefore we cannot proceed. + // TODO: In this context, 'imm' and 'prevImm' are assumed to be scaled(encoded). + // They should never be scaled(encoded) until its about to be written to the buffer. + if (fmt == IF_LS_2C || lastInsFmt == IF_LS_2C) + { + return eRO_none; + } + // Signed, *raw* immediate value fits in 7 bits, so for LDP/ STP the raw value is from -64 to +63. // For LDR/ STR, there are 9 bits, so we need to limit the range explicitly in software. if ((imm < -64) || (imm > 63) || (prevImm < -64) || (prevImm > 63)) From d62c69b141bc3c83a051461a961d221f165eb965 Mon Sep 17 00:00:00 2001 From: TIHan Date: Tue, 15 Aug 2023 14:10:23 -0700 Subject: [PATCH 4/7] Additional test --- .../JitBlue/Runtime_85765/Runtime_85765.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs index 9f6844cd9c87e..3817727719d5f 100644 --- a/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs +++ b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs @@ -42,4 +42,22 @@ private static bool Consume(bool value) { return value; } + + // ------ + + [Fact] + public static void Test2() + { + byte* bytes = stackalloc byte[1024]; + bytes[0x1A] = 1; + bytes[0x1B] = 2; + int sum = Foo(bytes); + Assert.Equal(515, sum); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public static int Foo(byte* b) + { + return Unsafe.ReadUnaligned(ref b[0x1A]) + Unsafe.ReadUnaligned(ref b[0x1B]); + } } From 8cebc500e85baf4a17ebcec24bd5297805315311 Mon Sep 17 00:00:00 2001 From: TIHan Date: Tue, 15 Aug 2023 15:32:19 -0700 Subject: [PATCH 5/7] Feedback --- src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs index 3817727719d5f..f04eed1b7981c 100644 --- a/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs +++ b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs @@ -52,7 +52,7 @@ public static void Test2() bytes[0x1A] = 1; bytes[0x1B] = 2; int sum = Foo(bytes); - Assert.Equal(515, sum); + Assert.True(sum == 515); } [MethodImpl(MethodImplOptions.NoInlining)] From 389dcb3ced076a01db7ce9eb60293a5682276ce9 Mon Sep 17 00:00:00 2001 From: TIHan Date: Wed, 16 Aug 2023 15:23:08 -0700 Subject: [PATCH 6/7] Fix test --- .../JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.csproj index de6d5e08882e8..a4cc9d0594f93 100644 --- a/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.csproj +++ b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.csproj @@ -1,6 +1,7 @@ True + True From 51eb887a44eabf2f0307fccc6c4f220f40015d87 Mon Sep 17 00:00:00 2001 From: TIHan Date: Wed, 16 Aug 2023 17:17:10 -0700 Subject: [PATCH 7/7] Fixing test again... --- .../JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs index f04eed1b7981c..89c22ae90dc82 100644 --- a/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs +++ b/src/tests/JIT/Regression/JitBlue/Runtime_85765/Runtime_85765.cs @@ -46,7 +46,7 @@ private static bool Consume(bool value) // ------ [Fact] - public static void Test2() + public unsafe static void Test2() { byte* bytes = stackalloc byte[1024]; bytes[0x1A] = 1; @@ -56,7 +56,7 @@ public static void Test2() } [MethodImpl(MethodImplOptions.NoInlining)] - public static int Foo(byte* b) + public unsafe static int Foo(byte* b) { return Unsafe.ReadUnaligned(ref b[0x1A]) + Unsafe.ReadUnaligned(ref b[0x1B]); }