From e6ee8a2c075ac906f4d86d831cac124f2f22a504 Mon Sep 17 00:00:00 2001 From: Eugene Rozenfeld Date: Fri, 26 Apr 2019 15:22:53 -0700 Subject: [PATCH] Fix for a jit liveness bug. `fgRemoveDeadStore` has special logic for removing dead assignments whose rhs was of type `TYP_STRUCT`: https://github.com/dotnet/coreclr/blob/311b5e2fe413c6c74a2a3680ab54d8a978651472/src/jit/liveness.cpp#L2264-L2274 That logic was applied to "normal" assignments (i.e., direct children of `GT_STMT`) but not to "internal" assignments (e.g., children of `GT_COMMA`). The test case has an internal assignment and, because this logic wasn't applied, we ended up with a standalone `GT_IND` of type `TYP_STRUCT` that the register allocator can't handle. This change apples the missing logic to "internal" assignments. Fixes #24253. --- src/jit/liveness.cpp | 13 +++++ .../JitBlue/GitHub_24253/GitHub_24253.cs | 57 ++++++++++++++++++++++ .../JitBlue/GitHub_24253/GitHub_24253.csproj | 33 +++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_24253/GitHub_24253.cs create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_24253/GitHub_24253.csproj diff --git a/src/jit/liveness.cpp b/src/jit/liveness.cpp index e06df34..9e6eeda 100644 --- a/src/jit/liveness.cpp +++ b/src/jit/liveness.cpp @@ -2359,6 +2359,19 @@ bool Compiler::fgRemoveDeadStore(GenTree** pTree, printf("\n"); } #endif // DEBUG + + if (rhsNode->TypeGet() == TYP_STRUCT) + { + // This is a block assignment. An indirection of the rhs is not considered to + // happen until the assignment, so we will extract the side effects from only + // the address. + if (rhsNode->OperIsIndir()) + { + assert(rhsNode->OperGet() != GT_NULLCHECK); + rhsNode = rhsNode->AsIndir()->Addr(); + } + } + gtExtractSideEffList(rhsNode, &sideEffList); if (!sideEffList) diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_24253/GitHub_24253.cs b/tests/src/JIT/Regression/JitBlue/GitHub_24253/GitHub_24253.cs new file mode 100644 index 0000000..65110fe --- /dev/null +++ b/tests/src/JIT/Regression/JitBlue/GitHub_24253/GitHub_24253.cs @@ -0,0 +1,57 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.CompilerServices; + +// Test removal of a dead struct assignment when the assignment is "internal" +// i.e., is not a direct child of a statement node. +// In this example, the statement is STMT(COMMA(CALL HELPER.CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE, ASG)). + +namespace GitHub_24253 +{ + struct TestStruct + { + public static readonly TestStruct ZeroStruct = new TestStruct(0); + + [MethodImpl(MethodImplOptions.NoInlining)] + public TestStruct(int i) + { + this.i = i; + this.j = 5; + this.k = 5; + this.l = 5; + } + + int i; + int j; + short k; + short l; + } + + class Program + { + static int Main(string[] args) + { + GetStruct(1); + return 100; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static TestStruct GetStruct(int i) + { + // This is the dead assignment that is causing the bug assert. + TestStruct result = TestStruct.ZeroStruct; + try + { + result = new TestStruct(i); + } + catch + { + throw new ArgumentException(); + } + return result; + } + } +} diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_24253/GitHub_24253.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_24253/GitHub_24253.csproj new file mode 100644 index 0000000..95052d9 --- /dev/null +++ b/tests/src/JIT/Regression/JitBlue/GitHub_24253/GitHub_24253.csproj @@ -0,0 +1,33 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + + + + + + + False + + + + None + True + + + + + + + + + + -- 2.7.4