diff options
author | Andy Ayers <andya@microsoft.com> | 2017-03-17 11:04:35 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-17 11:04:35 -0700 |
commit | 8208cb59f24c43444c0049e8f3f39d3f6a34b647 (patch) | |
tree | 46bdcc19b54859f7501be0261f03038a43b78ca4 /tests | |
parent | e93885ed5ecf92c74056d5489add26dbcd527f1b (diff) | |
parent | 4e021953d8421ed6d53d8d0ff5bef2d09a738c5e (diff) | |
download | coreclr-8208cb59f24c43444c0049e8f3f39d3f6a34b647.tar.gz coreclr-8208cb59f24c43444c0049e8f3f39d3f6a34b647.tar.bz2 coreclr-8208cb59f24c43444c0049e8f3f39d3f6a34b647.zip |
Merge pull request #10192 from AndyAyersMS/InterfaceDevirt
Interface devirt
Diffstat (limited to 'tests')
-rw-r--r-- | tests/src/JIT/opt/Devirtualization/comparable.cs | 67 | ||||
-rw-r--r-- | tests/src/JIT/opt/Devirtualization/comparable.csproj | 41 | ||||
-rw-r--r-- | tests/src/JIT/opt/Devirtualization/contravariance.cs | 38 | ||||
-rw-r--r-- | tests/src/JIT/opt/Devirtualization/contravariance.csproj | 41 | ||||
-rw-r--r-- | tests/src/JIT/opt/Devirtualization/covariance.cs | 35 | ||||
-rw-r--r-- | tests/src/JIT/opt/Devirtualization/covariance.csproj | 41 | ||||
-rw-r--r-- | tests/src/JIT/opt/Devirtualization/late.cs | 48 | ||||
-rw-r--r-- | tests/src/JIT/opt/Devirtualization/late.csproj | 41 | ||||
-rw-r--r-- | tests/src/JIT/opt/Devirtualization/simple.cs | 58 | ||||
-rw-r--r-- | tests/src/JIT/opt/Devirtualization/simple.csproj | 41 |
10 files changed, 451 insertions, 0 deletions
diff --git a/tests/src/JIT/opt/Devirtualization/comparable.cs b/tests/src/JIT/opt/Devirtualization/comparable.cs new file mode 100644 index 0000000000..2322395bf2 --- /dev/null +++ b/tests/src/JIT/opt/Devirtualization/comparable.cs @@ -0,0 +1,67 @@ +// 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; + +public sealed class X: IComparable<X> +{ + int ival; + + public X(int i) + { + ival = i; + } + + public int CompareTo(X x) + { + return ival - x.ival; + } + + public bool Equals(X x) + { + return ival == x.ival; + } +} + +public class Y<T> where T : IComparable<T> +{ + public static int C(T x, T y) + { + // IL here is + // ldarga 0 + // ldarg 1 + // constrained ... callvirt ... + // + // The ldarga blocks both caller-arg direct sub and type + // propagation since the jit thinks arg0 might be redefined. + // + // For ref types the ldarga is undone in codegen just before + // the call so we end up with *(&arg0) and we know this is + // arg0. Ideally we'd also understand that this pattern can't + // lead to reassignment, but our view of the callee and what + // it does with address-taken args is quite limited. + // + // Even if we can't propagate the caller's value or type, we + // might be able to retype the generic __Canon for arg0 as the + // more specific type that the caller is using (here, X). + // + // An interesting variant on this would be to derive from X + // (say with XD) and have the caller pass instances of XD + // instead of instances of X. We'd need to make sure we retype + // arg0 as X and not XD. + return x.CompareTo(y); + } +} + +public class Z +{ + public static int Main() + { + // Ideally inlining Y.C would enable the interface call in Y + // to be devirtualized, since we know the exact type of the + // first argument. We can't get this yet. + int result = Y<X>.C(new X(103), new X(3)); + return result; + } +} diff --git a/tests/src/JIT/opt/Devirtualization/comparable.csproj b/tests/src/JIT/opt/Devirtualization/comparable.csproj new file mode 100644 index 0000000000..780cd910ca --- /dev/null +++ b/tests/src/JIT/opt/Devirtualization/comparable.csproj @@ -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>PdbOnly</DebugType> + <Optimize>True</Optimize> + </PropertyGroup> + <ItemGroup> + <Compile Include="comparable.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> diff --git a/tests/src/JIT/opt/Devirtualization/contravariance.cs b/tests/src/JIT/opt/Devirtualization/contravariance.cs new file mode 100644 index 0000000000..0594772c9a --- /dev/null +++ b/tests/src/JIT/opt/Devirtualization/contravariance.cs @@ -0,0 +1,38 @@ +// 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; + +interface I<out T> +{ + T A(); +} + +class X<T> : I<T> where T: class +{ + T I<T>.A() + { + return (T)(object)"X"; + } +} + +class T +{ + static object F(I<object> i) + { + return i.A(); + } + + public static int Main() + { + // Jit should inline F and then devirtualize the call to A. + // (inlining A blocked by runtime lookup) + object j = F(new X<string>()); + if (j is string) + { + return ((string)j)[0] + 12; + } + return -1; + } +} diff --git a/tests/src/JIT/opt/Devirtualization/contravariance.csproj b/tests/src/JIT/opt/Devirtualization/contravariance.csproj new file mode 100644 index 0000000000..287c2009f1 --- /dev/null +++ b/tests/src/JIT/opt/Devirtualization/contravariance.csproj @@ -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>PdbOnly</DebugType> + <Optimize>True</Optimize> + </PropertyGroup> + <ItemGroup> + <Compile Include="contravariance.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> diff --git a/tests/src/JIT/opt/Devirtualization/covariance.cs b/tests/src/JIT/opt/Devirtualization/covariance.cs new file mode 100644 index 0000000000..2ed7d2bdd8 --- /dev/null +++ b/tests/src/JIT/opt/Devirtualization/covariance.cs @@ -0,0 +1,35 @@ +// 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; + +interface I<in T> +{ + int A(T t); +} + +class X<T> : I<T> +{ + int c = 0; + int I<T>.A(T t) + { + return ++c; + } +} + +class T +{ + static int F(I<string> i) + { + return i.A("A"); + } + + public static int Main() + { + // Jit should inline F and then devirtualize + // and inline the call to A. + int j = F(new X<object>()); + return j + 99; + } +} diff --git a/tests/src/JIT/opt/Devirtualization/covariance.csproj b/tests/src/JIT/opt/Devirtualization/covariance.csproj new file mode 100644 index 0000000000..b94309945a --- /dev/null +++ b/tests/src/JIT/opt/Devirtualization/covariance.csproj @@ -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>PdbOnly</DebugType> + <Optimize>True</Optimize> + </PropertyGroup> + <ItemGroup> + <Compile Include="covariance.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> diff --git a/tests/src/JIT/opt/Devirtualization/late.cs b/tests/src/JIT/opt/Devirtualization/late.cs new file mode 100644 index 0000000000..3991e7df43 --- /dev/null +++ b/tests/src/JIT/opt/Devirtualization/late.cs @@ -0,0 +1,48 @@ +// 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; + +interface Ix<T> where T : class +{ + T F(); +} + +class Base : Ix<string> +{ + public virtual string F() { return "B"; } +} + +class Derived : Base +{ + public override string F() { return "D"; } +} + +class Bx +{ + public Ix<string> Get() { return new Derived(); } +} + +public class Z +{ + static string X(Base b) + { + return b.F(); + } + + public static int Main() + { + // Would like to be able to late devirtualize the call to F + // here after inlining Get exposes the exact type of the + // object, but since the return type of Get is a (shared) + // interface type, we need the exact context for F to do so + // safely. + // + // Unfortunately we lose track of that context, because when + // we import the call to F, it is not an inline candidate. + string s = new Bx().Get().F(); + return (int) s[0] + 32; + } +} + diff --git a/tests/src/JIT/opt/Devirtualization/late.csproj b/tests/src/JIT/opt/Devirtualization/late.csproj new file mode 100644 index 0000000000..6af10b47f6 --- /dev/null +++ b/tests/src/JIT/opt/Devirtualization/late.csproj @@ -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>PdbOnly</DebugType> + <Optimize>True</Optimize> + </PropertyGroup> + <ItemGroup> + <Compile Include="late.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> diff --git a/tests/src/JIT/opt/Devirtualization/simple.cs b/tests/src/JIT/opt/Devirtualization/simple.cs new file mode 100644 index 0000000000..46b039ce15 --- /dev/null +++ b/tests/src/JIT/opt/Devirtualization/simple.cs @@ -0,0 +1,58 @@ +// 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; + +// Some simple interface call devirtualization cases + +interface Ix +{ + int F(); +} + +interface Iy +{ + int G(); +} + +interface Iz +{ + int H(); + int I(); +} + +public class B : Iy, Ix, Iz +{ + public int F() { return 3; } + virtual public int G() { return 5; } + int Iz.H() { return 7; } + int Iz.I() { return 11; } +} + +public class Z : B, Iz +{ + new public int F() { return 13; } + override public int G() { return 17; } + int Iz.H() { return 19; } + + static int Fx(Ix x) { return x.F(); } + static int Gy(Iy y) { return y.G(); } + static int Hz(Iz z) { return z.H(); } + static int Hi(Iz z) { return z.I(); } + + public static int Main() + { + int callsBF = Fx(new Z()) + Fx(new B()) + ((Ix) new Z()).F() + ((Ix) new B()).F(); + int callsBG = Gy(new B()) + ((Iy) new B()).G() + (new B()).G(); + int callsBH = Hz(new B()) + ((Iz) new B()).H(); + int callsBI = Hi(new Z()) + Hi(new B()) + ((Iz) new Z()).I() + ((Iz) new B()).I(); + int callsZG = Gy(new Z()) + ((Iy) new Z()).G() + (new Z()).G(); + int callsZH = Hz(new Z()) + ((Iz) new Z()).H(); + + int expected = 4 * 3 + 3 * 5 + 2 * 7 + 4 * 11 + 3 * 17 + 2 * 19; + + return callsBF + callsBG + callsBI + callsBH + callsZG + callsZH - expected + 100; + } +} + diff --git a/tests/src/JIT/opt/Devirtualization/simple.csproj b/tests/src/JIT/opt/Devirtualization/simple.csproj new file mode 100644 index 0000000000..cb5b5e33e0 --- /dev/null +++ b/tests/src/JIT/opt/Devirtualization/simple.csproj @@ -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>PdbOnly</DebugType> + <Optimize>True</Optimize> + </PropertyGroup> + <ItemGroup> + <Compile Include="simple.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> |