From 03a5e130622f96ab429791f88edae21603042826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Fri, 8 Aug 2025 14:41:11 +0200 Subject: [PATCH 1/3] Optimize out metadata for `typeof(X).IsValueType` and `.IsEnum` We don't need reflection metadata to answer this. Change salvaged out of #118479, rt-sz will decide if we want it. --- .../IL/ILImporter.Scanner.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs index 92e8126b482dd1..932f150e2cdafb 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs @@ -959,6 +959,24 @@ private void ImportLdToken(int token) } } + // We might also be able to optimize this if this is something like typeof(Foo).IsValueType + // that doesn't need reflection metadata for the answer. + if (helperId != ReadyToRunHelperId.NecessaryTypeHandle) + { + reader = new ILReader(_ilBytes, _currentOffset); + if (reader.HasNext + && reader.ReadILOpcode() == ILOpcode.call + && IsTypeGetTypeFromHandle((MethodDesc)_methodIL.GetObject(reader.ReadILToken()))) + { + if (reader.HasNext + && reader.ReadILOpcode() is ILOpcode.callvirt or ILOpcode.call + && _methodIL.GetObject(reader.ReadILToken()) is MethodDesc{ Name: "get_IsValueType" or "get_IsEnum"}) + { + helperId = ReadyToRunHelperId.NecessaryTypeHandle; + } + } + } + _factory.MetadataManager.GetDependenciesDueToAccess(ref _dependencies, _factory, _methodIL, (TypeDesc)_canonMethodIL.GetObject(token)); _dependencies.Add(GetHelperEntrypoint(ReadyToRunHelper.GetRuntimeTypeHandle), "ldtoken"); From 093671002ea4d1854f9a27b45def77795cc07e0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Fri, 8 Aug 2025 23:50:02 +0900 Subject: [PATCH 2/3] Update src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs index 932f150e2cdafb..8824980c5ed156 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs @@ -970,7 +970,7 @@ private void ImportLdToken(int token) { if (reader.HasNext && reader.ReadILOpcode() is ILOpcode.callvirt or ILOpcode.call - && _methodIL.GetObject(reader.ReadILToken()) is MethodDesc{ Name: "get_IsValueType" or "get_IsEnum"}) + && _methodIL.GetObject(reader.ReadILToken()) is MethodDesc { Name: "get_IsValueType" or "get_IsEnum"}) { helperId = ReadyToRunHelperId.NecessaryTypeHandle; } From a18cfc37e7b577db94b3ff7de9c9b4f29c7ab9e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Sat, 9 Aug 2025 01:10:33 +0200 Subject: [PATCH 3/3] Update ILImporter.Scanner.cs --- .../tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs index 8824980c5ed156..80e41d6e638101 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs @@ -970,7 +970,8 @@ private void ImportLdToken(int token) { if (reader.HasNext && reader.ReadILOpcode() is ILOpcode.callvirt or ILOpcode.call - && _methodIL.GetObject(reader.ReadILToken()) is MethodDesc { Name: "get_IsValueType" or "get_IsEnum"}) + && _methodIL.GetObject(reader.ReadILToken()) is MethodDesc + { Name: "get_IsValueType" or "get_IsEnum" or "get_IsPrimitive" }) { helperId = ReadyToRunHelperId.NecessaryTypeHandle; }