From 6c1687247482acca944b56c47fc78b6a590962b8 Mon Sep 17 00:00:00 2001 From: Jakob Botsch Nielsen Date: Sat, 11 Sep 2021 11:07:13 +0200 Subject: [PATCH 1/2] Remove benign assert It is perfectly possible for us to replace a promoted struct by its only field where that field is marked do-not-enregister. Since this path does handle the proper retyping when normalization is required this assertion is benign. Fix #58972 --- src/coreclr/jit/lower.cpp | 1 - .../JitBlue/Runtime_58972/Runtime_58972_1.cs | 37 +++++++++++++++++++ .../Runtime_58972/Runtime_58972_1.csproj | 12 ++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.cs create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.csproj diff --git a/src/coreclr/jit/lower.cpp b/src/coreclr/jit/lower.cpp index b8752042b1c72f..46137c227399d2 100644 --- a/src/coreclr/jit/lower.cpp +++ b/src/coreclr/jit/lower.cpp @@ -3433,7 +3433,6 @@ void Lowering::LowerRetSingleRegStructLclVar(GenTreeUnOp* ret) if (varDsc->lvDoNotEnregister) { - assert(!replacedInLowering); lclVar->ChangeOper(GT_LCL_FLD); lclVar->AsLclFld()->SetLclOffs(0); diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.cs b/src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.cs new file mode 100644 index 00000000000000..c4abdc70b32b1c --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.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; + +public class Runtime_58972 +{ + public static unsafe int Main() + { + GetItem(new MyStruct[1], 0); + return 100; + } + + // This code results in a struct returned in register where we replace the local + // of type MyStruct by its only field, and where that field cannot be enregistered. + // We would potentially miss normalization if the struct was returned as an integer + // type and hit a defensive assertion because of it. + static MyStruct GetItem(MyStruct[] a, int i) + { + try + { + return a[i]; + } + catch (IndexOutOfRangeException) + { + ThrowHelper(); + return default; + } + } + + static void ThrowHelper() => throw new Exception(); + + struct MyStruct + { + byte b; + } +} diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.csproj new file mode 100644 index 00000000000000..f3e1cbd44b4041 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.csproj @@ -0,0 +1,12 @@ + + + Exe + + + None + True + + + + + From a073b2c4f7efd2b1cd7c132e713765830c1e332a Mon Sep 17 00:00:00 2001 From: Jakob Botsch Nielsen Date: Sat, 11 Sep 2021 21:59:11 +0200 Subject: [PATCH 2/2] Fix test --- .../Runtime_58972/{Runtime_58972_1.cs => Runtime_58972.cs} | 2 +- .../{Runtime_58972_1.csproj => Runtime_58972.csproj} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/tests/JIT/Regression/JitBlue/Runtime_58972/{Runtime_58972_1.cs => Runtime_58972.cs} (96%) rename src/tests/JIT/Regression/JitBlue/Runtime_58972/{Runtime_58972_1.csproj => Runtime_58972.csproj} (100%) diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.cs b/src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972.cs similarity index 96% rename from src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.cs rename to src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972.cs index c4abdc70b32b1c..b63c1d907f14c1 100644 --- a/src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.cs +++ b/src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972.cs @@ -5,7 +5,7 @@ public class Runtime_58972 { - public static unsafe int Main() + public static int Main() { GetItem(new MyStruct[1], 0); return 100; diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972.csproj similarity index 100% rename from src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972_1.csproj rename to src/tests/JIT/Regression/JitBlue/Runtime_58972/Runtime_58972.csproj