JIT: relax assert in fgMergeFinallyChains
authorAndy Ayers <andya@microsoft.com>
Fri, 17 Feb 2017 16:04:55 +0000 (08:04 -0800)
committerAndy Ayers <andya@microsoft.com>
Fri, 17 Feb 2017 16:04:55 +0000 (08:04 -0800)
In some rare and likely IL-only cases the jit may create unreachable
callfinallys. This trips up finally chain merging since it spots a
merge opportunity but no merge actually happens.

Relax the assertion checks so that `canMerge && !didMerge` is now ok.

Closes dotnet/coreclr#9651.

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

src/coreclr/src/jit/flowgraph.cpp
src/coreclr/tests/src/jit/Regression/JitBlue/GitHub_9651/GitHub_9651.il [new file with mode: 0644]
src/coreclr/tests/src/jit/Regression/JitBlue/GitHub_9651/GitHub_9651.ilproj [new file with mode: 0644]

index 5831582..f33ecb5 100644 (file)
@@ -24114,20 +24114,31 @@ void Compiler::fgMergeFinallyChains()
     }
     else
     {
-        assert(didMerge);
-        JITDUMP("Method had try-finallys and some callfinally merges were performed.\n");
+        if (didMerge)
+        {
+            JITDUMP("Method had mergeable try-finallys and some callfinally merges were performed.\n");
 
 #if DEBUG
+            if (verbose)
+            {
+                printf("\n*************** After fgMergeFinallyChains()\n");
+                fgDispBasicBlocks();
+                fgDispHandlerTab();
+                printf("\n");
+            }
 
-        if (verbose)
+#endif // DEBUG
+        }
+        else
         {
-            printf("\n*************** After fgMergeFinallyChains()\n");
-            fgDispBasicBlocks();
-            fgDispHandlerTab();
-            printf("\n");
+            // We may not end up doing any merges, because we are only
+            // merging continuations for callfinallys that can
+            // actually be invoked, and the importer may leave
+            // unreachable callfinallys around (for instance, if it
+            // is forced to re-import a leave).
+            JITDUMP("Method had mergeable try-finallys but no callfinally merges were performed,\n"
+                    "likely the non-canonical callfinallys were unreachable\n");
         }
-
-#endif // DEBUG
     }
 }
 
@@ -24183,6 +24194,7 @@ bool Compiler::fgRetargetBranchesToCanonicalCallFinally(BasicBlock*      block,
     // If the block already jumps to the canoncial call finally, no work needed.
     if (block->bbJumpDest == canonicalCallFinally)
     {
+        JITDUMP("BB%02u already canonical\n", block->bbNum);
         return false;
     }
 
diff --git a/src/coreclr/tests/src/jit/Regression/JitBlue/GitHub_9651/GitHub_9651.il b/src/coreclr/tests/src/jit/Regression/JitBlue/GitHub_9651/GitHub_9651.il
new file mode 100644 (file)
index 0000000..28ca36a
--- /dev/null
@@ -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.
+// See the LICENSE file in the project root for more information.
+
+.assembly extern mscorlib {}
+.assembly a {}
+.module a.exe
+
+// Test case triggers reimportation of a leave,
+// leading to an unreachable callfinally pair.
+
+.class FinallyReimp
+{
+.method static int32 Main(class [mscorlib]System.String[]) cil managed
+{
+   .entrypoint
+   .locals init (int32 V0, native int V1)
+   .try 
+   {
+   ldarg.0
+   ldlen
+   ldc.i4 1
+   beq FOO
+   ldloca V0
+   br JOIN
+FOO:
+   ldc.i4 0
+JOIN:
+   stloc V1
+   leave AFTER
+   }
+   finally
+   {
+    ldc.i4 100
+    stloc.0
+    endfinally
+   }
+AFTER:
+   ldloc.1
+   ldind.i
+   ret
+}
+}
+
diff --git a/src/coreclr/tests/src/jit/Regression/JitBlue/GitHub_9651/GitHub_9651.ilproj b/src/coreclr/tests/src/jit/Regression/JitBlue/GitHub_9651/GitHub_9651.ilproj
new file mode 100644 (file)
index 0000000..0a2003a
--- /dev/null
@@ -0,0 +1,41 @@
+<?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>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</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  .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>None</DebugType>
+    <Optimize>True</Optimize>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="GitHub_9651.il" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' ">
+  </PropertyGroup> 
+</Project>