From 899b70efc86f7a4c2e8db7e755a31250f43de78e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= Date: Sat, 13 Jun 2026 13:45:39 +0200 Subject: [PATCH 1/4] Optimize GetType with known type --- src/coreclr/jit/importercalls.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/coreclr/jit/importercalls.cpp b/src/coreclr/jit/importercalls.cpp index 73ae8326e07cde..7b7ddbf9ab8630 100644 --- a/src/coreclr/jit/importercalls.cpp +++ b/src/coreclr/jit/importercalls.cpp @@ -4704,6 +4704,30 @@ GenTree* Compiler::impIntrinsic(CORINFO_CLASS_HANDLE clsHnd, } } + // Try to load handle directly for known types + if (retNode == nullptr) + { + GenTree* tree = impStackTop().val; + bool isExact = false; + bool notNull = false; + CORINFO_CLASS_HANDLE typeHnd = gtGetClassHandle(tree, &isExact, ¬Null); + if ((typeHnd != NO_CLASS_HANDLE) && isExact) + { + assert((info.compCompHnd->getClassAttribs(typeHnd) & CORINFO_FLG_GENERIC_TYPE_VARIABLE) == 0); + if ((info.compCompHnd->getClassAttribs(typeHnd) & CORINFO_FLG_SHAREDINST) == 0) + { + if (notNull && !fgAddrCouldBeNull(tree)) + { + JITDUMP("Optimizing object.GetType() with known type to typeof\n"); + impPopStack(); + GenTree* handle = gtNewIconEmbClsHndNode(typeHnd); + GenTree* helper = gtNewHelperCallNode(CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE, TYP_REF, handle); + retNode = gtWrapWithSideEffects(helper, tree, GTF_ALL_EFFECT, true); + } + } + } + } + #ifdef DEBUG if (retNode != nullptr) { From 8ec4be523994ccdc93d476fa4d27b3bbc7381efc Mon Sep 17 00:00:00 2001 From: MihuBot Date: Sat, 13 Jun 2026 12:35:07 +0000 Subject: [PATCH 2/4] Apply jitformat patch --- src/coreclr/jit/importercalls.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/coreclr/jit/importercalls.cpp b/src/coreclr/jit/importercalls.cpp index 7b7ddbf9ab8630..2501d527a78fe8 100644 --- a/src/coreclr/jit/importercalls.cpp +++ b/src/coreclr/jit/importercalls.cpp @@ -4721,7 +4721,8 @@ GenTree* Compiler::impIntrinsic(CORINFO_CLASS_HANDLE clsHnd, JITDUMP("Optimizing object.GetType() with known type to typeof\n"); impPopStack(); GenTree* handle = gtNewIconEmbClsHndNode(typeHnd); - GenTree* helper = gtNewHelperCallNode(CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE, TYP_REF, handle); + GenTree* helper = + gtNewHelperCallNode(CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE, TYP_REF, handle); retNode = gtWrapWithSideEffects(helper, tree, GTF_ALL_EFFECT, true); } } From 5670688cf04e8cf7f26e792f50cac9927637a8e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Sat, 13 Jun 2026 16:36:51 +0200 Subject: [PATCH 3/4] Simplify shared generic check --- src/coreclr/jit/importercalls.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/coreclr/jit/importercalls.cpp b/src/coreclr/jit/importercalls.cpp index 2501d527a78fe8..0fa097189362d4 100644 --- a/src/coreclr/jit/importercalls.cpp +++ b/src/coreclr/jit/importercalls.cpp @@ -4714,17 +4714,13 @@ GenTree* Compiler::impIntrinsic(CORINFO_CLASS_HANDLE clsHnd, if ((typeHnd != NO_CLASS_HANDLE) && isExact) { assert((info.compCompHnd->getClassAttribs(typeHnd) & CORINFO_FLG_GENERIC_TYPE_VARIABLE) == 0); - if ((info.compCompHnd->getClassAttribs(typeHnd) & CORINFO_FLG_SHAREDINST) == 0) + if (!eeIsSharedInst(typeHnd) && notNull && !fgAddrCouldBeNull(tree)) { - if (notNull && !fgAddrCouldBeNull(tree)) - { - JITDUMP("Optimizing object.GetType() with known type to typeof\n"); - impPopStack(); - GenTree* handle = gtNewIconEmbClsHndNode(typeHnd); - GenTree* helper = - gtNewHelperCallNode(CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE, TYP_REF, handle); - retNode = gtWrapWithSideEffects(helper, tree, GTF_ALL_EFFECT, true); - } + JITDUMP("Optimizing object.GetType() with known type to typeof\n"); + impPopStack(); + GenTree* handle = gtNewIconEmbClsHndNode(typeHnd); + GenTree* helper = gtNewHelperCallNode(CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE, TYP_REF, handle); + retNode = gtWrapWithSideEffects(helper, tree, GTF_ALL_EFFECT, true); } } } From b3bad56814986e1c0404d7cf990c7399457ba9f1 Mon Sep 17 00:00:00 2001 From: MihuBot Date: Sat, 13 Jun 2026 17:05:32 +0000 Subject: [PATCH 4/4] Apply jitformat patch --- src/coreclr/jit/importercalls.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/coreclr/jit/importercalls.cpp b/src/coreclr/jit/importercalls.cpp index 0fa097189362d4..9c75435955b7d0 100644 --- a/src/coreclr/jit/importercalls.cpp +++ b/src/coreclr/jit/importercalls.cpp @@ -4719,7 +4719,8 @@ GenTree* Compiler::impIntrinsic(CORINFO_CLASS_HANDLE clsHnd, JITDUMP("Optimizing object.GetType() with known type to typeof\n"); impPopStack(); GenTree* handle = gtNewIconEmbClsHndNode(typeHnd); - GenTree* helper = gtNewHelperCallNode(CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE, TYP_REF, handle); + GenTree* helper = + gtNewHelperCallNode(CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE, TYP_REF, handle); retNode = gtWrapWithSideEffects(helper, tree, GTF_ALL_EFFECT, true); } }