-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Optimize typeof(T).IsValueType #1157
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
Merged
Merged
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 ff69f86
Add tests
EgorBo ffbacad
Improve tests, fix copy-paste
EgorBo 5868336
Improve tests, add Enum
EgorBo 7bf4a12
Ignore GT_RUNTIMELOOKUP argument
EgorBo 76a0312
Add IL tests (for ldtoken type& case)
EgorBo 86e7842
fix formatting issues
EgorBo b71f1be
Handle Enum (is not primitive), improve tests, add more test cases
EgorBo 5c8ff26
Add more test cases
EgorBo 631a9ca
implement isEnum
EgorBo ac03c32
re-implement isEnum check
EgorBo 3b37a90
formatting issues
EgorBo 0926bbf
temp commit: disable optimization to check my tests
EgorBo b24213f
Simplify IL tests
EgorBo e36adb3
Fix copy-paste errors in tests, add more test cases.
EgorBo 627e7a0
Use gtGetHelperArgClassHandle
EgorBo 1fc0b71
Add tests for __Canon
EgorBo 6b37368
Use canInlineTypeCheckWithObjectVTable() to detect __Canon
EgorBo 3a53e71
fix formatting issues
EgorBo 363d683
Add __Canon to well-known types
EgorBo fc0d723
Cleanup
EgorBo 22a064a
Cleanup
EgorBo 75d5c84
Cleanup
EgorBo a6f6f13
Remove IsClass and IsPrimitive
EgorBo b7e054f
Formatting
EgorBo 5837874
Use asCorInfoType instead of getTypeForPrimitiveValueClass
EgorBo 5bfe840
formatting
EgorBo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
src/coreclr/tests/src/JIT/Intrinsics/TypeIsIntrinsics.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
13
src/coreclr/tests/src/JIT/Intrinsics/TypeIsIntrinsics_r.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
Outdated
|
||
| </ItemGroup> | ||
| </Project> | ||
13 changes: 13 additions & 0 deletions
13
src/coreclr/tests/src/JIT/Intrinsics/TypeIsIntrinsics_ro.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.