Jit: decrement finally nesting level when removing empty trys
authorAndy Ayers <andya@microsoft.com>
Sat, 1 Apr 2017 01:13:32 +0000 (18:13 -0700)
committerAndy Ayers <andya@microsoft.com>
Sat, 1 Apr 2017 01:22:23 +0000 (18:22 -0700)
For non-funclet EH models, the GT_END_LFIN statement tracks the
nesting level of the associated finally. When removing a try-finally
with an empty try we need to decrement this level for any try-finallys
nested within the associated finally.

Added a test case with several levels of empty try and nesting.

Closes dotnet/coreclr#10621.

Commit migrated from https://github.com/dotnet/coreclr/commit/b65b40426527f7ee1813ea640ecf1701df66007c

src/coreclr/src/jit/flowgraph.cpp
src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_10621/GitHub_10621.cs [new file with mode: 0644]
src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_10621/GitHub_10621.csproj [new file with mode: 0644]

index 00068c6..534cb40 100644 (file)
@@ -23375,6 +23375,7 @@ void Compiler::fgRemoveEmptyTry()
         // Handler index of any nested blocks will update when we
         // remove the EH table entry.  Change handler exits to jump to
         // the continuation.  Clear catch type on handler entry.
+        // Decrement nesting level of enclosed GT_END_LFINs.
         for (BasicBlock* block = firstHandlerBlock; block != endHandlerBlock; block = block->bbNext)
         {
             if (block == firstHandlerBlock)
@@ -23404,6 +23405,22 @@ void Compiler::fgRemoveEmptyTry()
                     fgAddRefPred(continuation, block);
                 }
             }
+
+#if !FEATURE_EH_FUNCLETS
+            // If we're in a non-funclet model, decrement the nesting
+            // level of any GT_END_LFIN we find in the handler region,
+            // since we're removing the enclosing handler.
+            for (GenTreeStmt* stmt = block->firstStmt(); stmt != nullptr; stmt = stmt->gtNextStmt)
+            {
+                GenTreePtr expr = stmt->gtStmtExpr;
+                if (expr->gtOper == GT_END_LFIN)
+                {
+                    const unsigned nestLevel = expr->gtVal.gtVal1;
+                    assert(nestLevel > 0);
+                    expr->gtVal.gtVal1 = nestLevel - 1;
+                }
+            }
+#endif // !FEATURE_EH_FUNCLETS
         }
 
         // (6) Remove the try-finally EH region. This will compact the
diff --git a/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_10621/GitHub_10621.cs b/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_10621/GitHub_10621.cs
new file mode 100644 (file)
index 0000000..9f2a4b3
--- /dev/null
@@ -0,0 +1,81 @@
+// 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;
+
+class GitHub_10621
+{
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    static int F(int x) 
+    { 
+        return x * x;
+    }
+
+    // An empty try with nested try finallys where
+    // the inner finally cannot be cloned.
+    public static int Main()
+    {
+        int x = 0;
+        try {
+            // empty
+        }
+        finally {
+            try {
+                for (int i = 0; i < 11; i++) {
+                    x += F(i);
+                }
+            }
+            finally {
+
+                x -= 81;
+
+                try {
+                    // empty
+                }
+                finally
+                {
+                    x -= 64; 
+                    try {
+                        x -= 49;
+                    }
+                    finally {
+                        try {
+                            // empty
+                        }
+                        finally {
+                            x -= 36;
+                            try {
+                                x -= 25;
+                            }
+                            finally {
+                                try {
+                                    // empty
+                                }
+                                finally
+                                {
+                                    x -= 16; 
+                                    try {
+                                        x -= 9;
+                                    }
+                                    finally {
+                                        try {
+                                            // empty
+                                        }
+                                        finally {
+                                            x -= 4;
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
+        return x - 1;
+    }
+}
+
diff --git a/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_10621/GitHub_10621.csproj b/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_10621/GitHub_10621.csproj
new file mode 100644 (file)
index 0000000..6b95745
--- /dev/null
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <AssemblyName>$(MSBuildProjectName)</AssemblyName>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{DADEC17C-DA8A-4F7D-9927-47A9033A2E80}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+
+    <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+  </PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <PropertyGroup>
+    <DebugType></DebugType>
+    <Optimize>True</Optimize>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="$(MSBuildProjectName).cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' ">
+  </PropertyGroup>
+</Project>