From a76445568d42b9b7d37d9f6e94ec47393179585e Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Mon, 8 Jan 2018 08:44:14 -0800 Subject: JIT: improve return types in cases with spill temps (#15766) If the jit sees that an inlinee has multiple return sites or has gc ref locals it will choose to return the inline result via a temp. The jit was not assigning a type to that temp and so losing track of some type information. So, for inlinees returning ref types, initially type the return spill temp with the declared return type of the method. When importing we may discover that particular return sites will return more specific types. If all discovered return sites agree, we can update the return type for the spill temp to match the consensus improved type. This can lead to removal of some type checks and also to devirtualization. Addresses issues discussed in #9908 and #15743. --- .../src/JIT/opt/Devirtualization/spilledreturn.cs | 100 +++++++++++++++++++++ .../JIT/opt/Devirtualization/spilledreturn.csproj | 39 ++++++++ 2 files changed, 139 insertions(+) create mode 100644 tests/src/JIT/opt/Devirtualization/spilledreturn.cs create mode 100644 tests/src/JIT/opt/Devirtualization/spilledreturn.csproj (limited to 'tests') diff --git a/tests/src/JIT/opt/Devirtualization/spilledreturn.cs b/tests/src/JIT/opt/Devirtualization/spilledreturn.cs new file mode 100644 index 0000000000..ac63806019 --- /dev/null +++ b/tests/src/JIT/opt/Devirtualization/spilledreturn.cs @@ -0,0 +1,100 @@ +// 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; + +// Examples where methods potentially return multiple types +// but the jit can prune the set down to one type during +// importation, which then triggers late devirtualization. + +public class Base +{ + public virtual void Foo() { Console.WriteLine("Base:Foo"); } + public virtual void Bar() { Console.WriteLine("Base:Bar"); } +} + +public class Derived : Base +{ + public override sealed void Foo() { Console.WriteLine("Derived:Foo"); } + public override void Bar() { Console.WriteLine("Derived:Bar"); } +} + +public class Derived2 : Base +{ + public override sealed void Foo() { Console.WriteLine("Derived2:Foo"); } + public override void Bar() { Console.WriteLine("Derived2:Bar"); } +} + +public class Test +{ + static bool vague; + + // Constant prop + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Base M(int x) + { + if (x > 0) + { + return new Derived(); + } + else + { + return new Derived2(); + } + } + + // All returns agree on type + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Base N(bool b) + { + if (b) + { + Console.WriteLine("b true"); + return new Derived(); + } + else + { + Console.WriteLine("b false"); + return new Derived(); + } + } + + // Type specialization + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Base G() + { + if (typeof(T) == typeof(int)) + { + return new Derived(); + } + else + { + return new Derived2(); + } + } + + public static int Main(string[] args) + { + vague = args.Length > 0; + + M(0).Foo(); + M(0).Bar(); + M(1).Foo(); + M(1).Bar(); + + N(vague).Foo(); + N(!vague).Bar(); + + G().Foo(); + G().Foo(); + G().Foo(); + + return 100; + } +} + + + + diff --git a/tests/src/JIT/opt/Devirtualization/spilledreturn.csproj b/tests/src/JIT/opt/Devirtualization/spilledreturn.csproj new file mode 100644 index 0000000000..b3ee960d96 --- /dev/null +++ b/tests/src/JIT/opt/Devirtualization/spilledreturn.csproj @@ -0,0 +1,39 @@ + + + + + Debug + AnyCPU + $(MSBuildProjectName) + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT .0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + 1 + + + + + + + False + + + + PdbOnly + True + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3