Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize typeof(T).IsValueType #1157

Merged
merged 27 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5ef9ec3
Intrinsify typeof(T).IsValueType, IsClass, IsPrimitiveType
EgorBo Dec 26, 2019
ff69f86
Add tests
EgorBo Dec 26, 2019
ffbacad
Improve tests, fix copy-paste
EgorBo Dec 26, 2019
5868336
Improve tests, add Enum
EgorBo Dec 26, 2019
7bf4a12
Ignore GT_RUNTIMELOOKUP argument
EgorBo Dec 26, 2019
76a0312
Add IL tests (for ldtoken type& case)
EgorBo Dec 26, 2019
86e7842
fix formatting issues
EgorBo Dec 26, 2019
b71f1be
Handle Enum (is not primitive), improve tests, add more test cases
EgorBo Dec 26, 2019
5c8ff26
Add more test cases
EgorBo Dec 26, 2019
631a9ca
implement isEnum
EgorBo Dec 26, 2019
ac03c32
re-implement isEnum check
EgorBo Dec 26, 2019
3b37a90
formatting issues
EgorBo Dec 26, 2019
0926bbf
temp commit: disable optimization to check my tests
EgorBo Dec 26, 2019
b24213f
Simplify IL tests
EgorBo Dec 26, 2019
e36adb3
Fix copy-paste errors in tests, add more test cases.
EgorBo Dec 26, 2019
627e7a0
Use gtGetHelperArgClassHandle
EgorBo Dec 26, 2019
1fc0b71
Add tests for __Canon
EgorBo Dec 26, 2019
6b37368
Use canInlineTypeCheckWithObjectVTable() to detect __Canon
EgorBo Dec 27, 2019
3a53e71
fix formatting issues
EgorBo Dec 27, 2019
363d683
Add __Canon to well-known types
EgorBo Dec 27, 2019
fc0d723
Cleanup
EgorBo Dec 27, 2019
22a064a
Cleanup
EgorBo Dec 27, 2019
75d5c84
Cleanup
EgorBo Dec 27, 2019
a6f6f13
Remove IsClass and IsPrimitive
EgorBo Dec 27, 2019
b7e054f
Formatting
EgorBo Dec 27, 2019
5837874
Use asCorInfoType instead of getTypeForPrimitiveValueClass
EgorBo Dec 27, 2019
5bfe840
formatting
EgorBo Dec 27, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/coreclr/src/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4015,6 +4015,45 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis,
break;
}

case NI_System_Type_get_IsClass:
case NI_System_Type_get_IsPrimitive:
case NI_System_Type_get_IsValueType:
{
if (impStackTop().val->IsCall())
{
GenTreeCall* call = impStackTop().val->AsCall();
if (call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE))
{
GenTreeIntCon* handle = call->Args().begin()->GetNode()->AsIntCon();
auto hClass = reinterpret_cast<CORINFO_CLASS_HANDLE>(handle->IconValue());
typeInfo tinfo = verMakeTypeInfo(hClass);
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
BOOL isValueType = tinfo.IsValueClass();
if (ni == NI_System_Type_get_IsPrimitive)
{
if (isValueType)
{
retNode = gtNewIconNode(tinfo.IsPrimitiveType() ? 1 : 0);
}
else
{
retNode = gtNewIconNode(0);
}
}
else if (ni == NI_System_Type_get_IsClass)
{
BOOL isInterface = info.compCompHnd->getClassAttribs(hClass) & CORINFO_FLG_INTERFACE;
jkotas marked this conversation as resolved.
Show resolved Hide resolved
retNode = gtNewIconNode(!isValueType && !isInterface ? 1 : 0);
}
else
{
retNode = gtNewIconNode(isValueType ? 1 : 0);
}
impPopStack();
}
}
break;
}

#ifdef FEATURE_HW_INTRINSICS
case NI_System_Math_FusedMultiplyAdd:
case NI_System_MathF_FusedMultiplyAdd:
Expand Down Expand Up @@ -4306,6 +4345,21 @@ NamedIntrinsic Compiler::lookupNamedIntrinsic(CORINFO_METHOD_HANDLE method)
result = NI_System_GC_KeepAlive;
}
}
else if (strcmp(className, "Type") == 0)
{
if (strcmp(methodName, "get_IsValueType") == 0)
{
result = NI_System_Type_get_IsValueType;
}
else if (strcmp(methodName, "get_IsClass") == 0)
{
result = NI_System_Type_get_IsClass;
}
else if (strcmp(methodName, "get_IsPrimitive") == 0)
{
result = NI_System_Type_get_IsPrimitive;
}
}
}
#if defined(_TARGET_XARCH_) // We currently only support BSWAP on x86
else if (strcmp(namespaceName, "System.Buffers.Binary") == 0)
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/src/jit/namedintrinsiclist.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ enum NamedIntrinsic : unsigned short
NI_System_Collections_Generic_EqualityComparer_get_Default,
NI_System_Buffers_Binary_BinaryPrimitives_ReverseEndianness,
NI_System_GC_KeepAlive,
NI_System_Type_get_IsValueType,
NI_System_Type_get_IsClass,
NI_System_Type_get_IsPrimitive,

#ifdef FEATURE_HW_INTRINSICS
NI_IsSupported_True,
Expand Down
163 changes: 163 additions & 0 deletions src/coreclr/tests/src/JIT/Intrinsics/TypeIsIntrinsics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

public class Program
{
public static unsafe int Main(string[] args)
{
int errors = 0;

if (!typeof(int).IsPrimitive) errors++;
if (!typeof(int).IsValueType) errors++;
if (typeof(int).IsClass) errors++;

if (typeof(int?).IsPrimitive) errors++;
if (!typeof(int?).IsValueType) errors++;
if (typeof(int?).IsClass) errors++;

if (typeof(int*).IsPrimitive) errors++;
if (typeof(int*).IsValueType) errors++;
if (!typeof(int*).IsClass) errors++;

if (typeof(void*).IsPrimitive) errors++;
if (typeof(void*).IsValueType) errors++;
if (!typeof(void*).IsClass) errors++;

if (typeof(decimal).IsPrimitive) errors++;
if (!typeof(decimal).IsValueType) errors++;
if (typeof(decimal).IsClass) errors++;

if (typeof(string).IsPrimitive) errors++;
if (typeof(string).IsValueType) errors++;
if (!typeof(string).IsClass) errors++;

if (typeof(object).IsPrimitive) errors++;
if (typeof(object).IsValueType) errors++;
if (!typeof(object).IsClass) errors++;

if (typeof(IEnumerable<int>).IsPrimitive) errors++;
if (typeof(IEnumerable<int>).IsValueType) errors++;
if (typeof(IEnumerable<int>).IsClass) errors++;

if (typeof(Action<int>).IsPrimitive) errors++;
if (typeof(Action<int>).IsValueType) errors++;
if (!typeof(Action<int>).IsClass) errors++;

if (typeof(GenericStruct<int>).IsPrimitive) errors++;
if (!typeof(GenericStruct<int>).IsValueType) errors++;
if (typeof(GenericStruct<int>).IsClass) errors++;

if (typeof(GenericStruct<string>).IsPrimitive) errors++;
if (!typeof(GenericStruct<string>).IsValueType) errors++;
if (typeof(GenericStruct<string>).IsClass) errors++;


if (!IsPrimitive<int>(42)) errors++;
if (!IsPrimitive<int?>(new Nullable<int>(42))) errors++;
if (IsPrimitive<decimal>(42M)) errors++;
if (IsPrimitive<string>("42")) errors++;
if (IsPrimitive<object>(new object())) errors++;
if (IsPrimitive<IEnumerable<int>>(new int[10])) errors++;
if (IsPrimitive<Action<int>>(_ => { })) errors++;
if (IsPrimitive<GenericStruct<int>>(default)) errors++;
if (IsPrimitive<GenericStruct<string>>(default)) errors++;
if (!IsPrimitive(CreateDynamic1())) errors++;
if (IsPrimitive(CreateDynamic2())) errors++;

if (!IsValueType<int>(42)) errors++;
if (!IsValueType<int?>(new Nullable<int>(42))) errors++;
if (!IsValueType<decimal>(42M)) errors++;
if (IsValueType<string>("42")) errors++;
if (IsValueType<object>(new object())) errors++;
if (IsValueType<IEnumerable<int>>(new int[10])) errors++;
if (IsValueType<Action<int>>(_ => { })) errors++;
if (!IsValueType<GenericStruct<int>>(default)) errors++;
if (!IsValueType<GenericStruct<string>>(default)) errors++;
if (!IsValueType(CreateDynamic1())) errors++;
if (IsValueType(CreateDynamic2())) errors++;

if (IsClass<int>(42)) errors++;
if (IsClass<int?>(new Nullable<int>(42))) errors++;
if (IsClass<decimal>(42M)) errors++;
if (!IsClass<string>("42")) errors++;
if (!IsClass<object>(new object())) errors++;
if (!IsClass<IEnumerable<int>>(new int[10])) errors++;
if (!IsClass<Action<int>>(_ => { })) errors++;
if (IsClass<GenericStruct<int>>(default)) errors++;
if (IsClass<GenericStruct<string>>(default)) errors++;
if (IsClass(CreateDynamic1())) errors++;
if (!IsClass(CreateDynamic2())) errors++;


if (!IsPrimitiveObj(42)) errors++;
if (!IsPrimitiveObj(new Nullable<int>(42))) errors++;
if (!IsPrimitiveObj(new decimal(42))) errors++;
if (IsPrimitiveObj("42")) errors++;
if (IsPrimitiveObj(new object())) errors++;
if (IsPrimitiveObj(new int[10])) errors++;
if (IsPrimitiveObj((Action<int>)(_ => { }))) errors++;
if (!IsPrimitiveObj(new GenericStruct<int>())) errors++;
if (!IsPrimitiveObj(new GenericStruct<string>())) errors++;
if (!IsPrimitiveObj(CreateDynamic1())) errors++;
if (IsPrimitiveObj(CreateDynamic2())) errors++;

if (!IsValueTypeObj(42)) errors++;
if (!IsValueTypeObj(new Nullable<int>(42))) errors++;
if (!IsValueTypeObj(42M)) errors++;
if (IsValueTypeObj("42")) errors++;
if (IsValueTypeObj(new object())) errors++;
if (IsValueTypeObj(new int[10])) errors++;
if (IsValueTypeObj((Action<int>)(_ => { }))) errors++;
if (!IsValueTypeObj(new GenericStruct<int>())) errors++;
if (!IsValueTypeObj(new GenericStruct<string>())) errors++;
if (!IsValueTypeObj(CreateDynamic1())) errors++;
if (IsValueTypeObj(CreateDynamic2())) errors++;

if (!IsClassObj(42)) errors++;
if (!IsClassObj(new Nullable<int>(42))) errors++;
if (!IsClassObj(42M)) errors++;
if (IsClassObj("42")) errors++;
if (IsClassObj(new object())) errors++;
if (IsClassObj(new int[10])) errors++;
if (IsClassObj((Action<int>)(_ => { }))) errors++;
if (!IsClassObj(new GenericStruct<int>())) errors++;
if (!IsClassObj(new GenericStruct<string>())) errors++;
if (!IsClassObj(CreateDynamic1())) errors++;
if (IsClassObj(CreateDynamic2())) errors++;

return 100 + errors;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPrimitive<T>(T val) => val.GetType().IsPrimitive;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsValueType<T>(T val) => val.GetType().IsValueType;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsClass<T>(T val) => val.GetType().IsClass;


[MethodImpl(MethodImplOptions.NoInlining)]
public static bool IsPrimitiveObj(object val) => val.GetType().IsValueType;

[MethodImpl(MethodImplOptions.NoInlining)]
public static bool IsValueTypeObj(object val) => val.GetType().IsValueType;

[MethodImpl(MethodImplOptions.NoInlining)]
public static bool IsClassObj(object val) => val.GetType().IsValueType;


[MethodImpl(MethodImplOptions.NoInlining)]
public static dynamic CreateDynamic1() => 42;

[MethodImpl(MethodImplOptions.NoInlining)]
public static dynamic CreateDynamic2() => new { Name = "Test" };
}

public struct GenericStruct<T>
{
public T field;
}

13 changes: 13 additions & 0 deletions src/coreclr/tests/src/JIT/Intrinsics/TypeIsIntrinsics_r.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>None</DebugType>
<Optimize />
</PropertyGroup>
<ItemGroup>
<Compile Include="MathFloorSingle.cs" />
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions src/coreclr/tests/src/JIT/Intrinsics/TypeIsIntrinsics_ro.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="TypeIsIntrinsics.cs" />
</ItemGroup>
</Project>
7 changes: 4 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Reflection;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace System
Expand Down Expand Up @@ -78,7 +79,7 @@ public virtual Type[] GetGenericParameterConstraints()
public bool IsSealed => (GetAttributeFlagsImpl() & TypeAttributes.Sealed) != 0;
public bool IsSpecialName => (GetAttributeFlagsImpl() & TypeAttributes.SpecialName) != 0;

public bool IsClass => (GetAttributeFlagsImpl() & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Class && !IsValueType;
public bool IsClass { [Intrinsic] get => (GetAttributeFlagsImpl() & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Class && !IsValueType; }
EgorBo marked this conversation as resolved.
Show resolved Hide resolved

public bool IsNestedAssembly => (GetAttributeFlagsImpl() & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
public bool IsNestedFamANDAssem => (GetAttributeFlagsImpl() & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
Expand All @@ -105,9 +106,9 @@ public virtual Type[] GetGenericParameterConstraints()
public virtual bool IsEnum => IsSubclassOf(typeof(Enum));
public bool IsMarshalByRef => IsMarshalByRefImpl();
protected virtual bool IsMarshalByRefImpl() => false;
public bool IsPrimitive => IsPrimitiveImpl();
public bool IsPrimitive { [Intrinsic] get => IsPrimitiveImpl(); }
protected abstract bool IsPrimitiveImpl();
public bool IsValueType => IsValueTypeImpl();
public bool IsValueType { [Intrinsic] get => IsValueTypeImpl(); }
protected virtual bool IsValueTypeImpl() => IsSubclassOf(typeof(ValueType));

public virtual bool IsSignatureType => false;
Expand Down