From f8a9e06e7244774240a30b28f73f502e33417996 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Tue, 1 Dec 2026 22:06:08 -0800 Subject: [PATCH] Unify generic lookup helper exception classification Classify CORINFO_HELP_READYTORUN_GENERIC_HANDLE as non-throwing for NativeAOT, matching CoreCLR, and remove the NativeAOT-specific exception bookkeeping. The helper can throw out-of-memory or type-load exceptions. We do not guarantee the precise timing of these exceptions or that they will be thrown at all, so the JIT need not account for them when optimizing. Windows x64 Checked NativeAOT tests: 24,967 passed, 566 skipped. Eight skipped tests expect TypeLoadException. Addresses dotnet/runtime#137616 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- src/coreclr/jit/compiler.hpp | 4 ++-- src/coreclr/jit/gentree.cpp | 10 +++++----- src/coreclr/jit/gentree.h | 2 +- src/coreclr/jit/optimizer.cpp | 2 +- src/coreclr/jit/utils.cpp | 5 ----- src/coreclr/jit/utils.h | 9 ++++----- src/coreclr/jit/valuenum.cpp | 14 +------------- 7 files changed, 14 insertions(+), 32 deletions(-) diff --git a/src/coreclr/jit/compiler.hpp b/src/coreclr/jit/compiler.hpp index 7dff1c26ba5308..e21480eea01467 100644 --- a/src/coreclr/jit/compiler.hpp +++ b/src/coreclr/jit/compiler.hpp @@ -1536,7 +1536,7 @@ inline GenTreeCall* Compiler::gtNewHelperCallNode( { GenTreeCall* const result = gtNewCallNode(CT_HELPER, eeFindHelper(helper), type); - if (!s_helperCallProperties.NoThrow((CorInfoHelpFunc)helper, IsNativeAot())) + if (!s_helperCallProperties.NoThrow((CorInfoHelpFunc)helper)) { result->gtFlags |= GTF_EXCEPT; @@ -1599,7 +1599,7 @@ inline GenTreeCall* Compiler::gtNewVirtualFunctionLookupHelperCallNode( { GenTreeCall* const result = gtNewCallNode(CT_HELPER, eeFindHelper(helper), type); - if (!s_helperCallProperties.NoThrow((CorInfoHelpFunc)helper, IsNativeAot())) + if (!s_helperCallProperties.NoThrow((CorInfoHelpFunc)helper)) { result->gtFlags |= GTF_EXCEPT; diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index b54008e6ce2cdc..413e6d9b5befcd 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -2285,7 +2285,7 @@ bool GenTreeCall::HasSideEffects(Compiler* compiler, bool ignoreExceptions, bool } // If we also care about exceptions then check if the helper can throw - if (!ignoreExceptions && !helperProperties.NoThrow(helper, compiler->IsNativeAot())) + if (!ignoreExceptions && !helperProperties.NoThrow(helper)) { return true; } @@ -2495,7 +2495,7 @@ CorInfoHelpFunc GenTreeCall::GetHelperNum() const // Return Value: // A bit set of exceptions this call may throw. // -ExceptionSetFlags GenTreeCall::CallExceptions(Compiler* comp) const +ExceptionSetFlags GenTreeCall::CallExceptions() const { CorInfoHelpFunc helper = GetHelperNum(); if (helper == CORINFO_HELP_UNDEF) @@ -2503,7 +2503,7 @@ ExceptionSetFlags GenTreeCall::CallExceptions(Compiler* comp) const return ExceptionSetFlags::UnknownException; } - return Compiler::s_helperCallProperties.ThrownExceptions(helper, comp->IsNativeAot()); + return Compiler::s_helperCallProperties.ThrownExceptions(helper); } //-------------------------------------------------------------------------- @@ -8924,7 +8924,7 @@ ExceptionSetFlags GenTree::OperExceptions(Compiler* comp) return ExceptionSetFlags::None; case GT_CALL: - return AsCall()->CallExceptions(comp); + return AsCall()->CallExceptions(); case GT_LOCKADD: case GT_XAND: @@ -10417,7 +10417,7 @@ GenTreeCall* Compiler::gtNewCallNode(gtCallTypes callType, } node->gtReturnType = type; - if (node->CallExceptions(this) != ExceptionSetFlags::None) + if (node->CallExceptions() != ExceptionSetFlags::None) { node->gtFlags |= GTF_EXCEPT; } diff --git a/src/coreclr/jit/gentree.h b/src/coreclr/jit/gentree.h index a0ebed0b2f49db..7cc10a88d265c8 100644 --- a/src/coreclr/jit/gentree.h +++ b/src/coreclr/jit/gentree.h @@ -5896,7 +5896,7 @@ struct GenTreeCall final : public GenTree CorInfoHelpFunc GetHelperNum() const; - ExceptionSetFlags CallExceptions(Compiler* comp) const; + ExceptionSetFlags CallExceptions() const; bool AreArgsComplete() const; diff --git a/src/coreclr/jit/optimizer.cpp b/src/coreclr/jit/optimizer.cpp index 7d4330d72d48ed..7b2f11dce43f26 100644 --- a/src/coreclr/jit/optimizer.cpp +++ b/src/coreclr/jit/optimizer.cpp @@ -4816,7 +4816,7 @@ void Compiler::optHoistLoopBlocks(FlowGraphNaturalLoop* loop, if (!treeIsHoistable) { // Does this helper call throw? - if (!s_helperCallProperties.NoThrow(helpFunc, m_compiler->IsNativeAot())) + if (!s_helperCallProperties.NoThrow(helpFunc)) { m_canHoistSideEffects = false; } diff --git a/src/coreclr/jit/utils.cpp b/src/coreclr/jit/utils.cpp index 221cd88ec72376..cffd85d97c474a 100644 --- a/src/coreclr/jit/utils.cpp +++ b/src/coreclr/jit/utils.cpp @@ -1452,7 +1452,6 @@ void HelperCallProperties::init() bool mayRunCctor = false; // true if the helper call may cause a static constructor to be run. bool isNoEscape = false; // true if none of the GC ref arguments can escape bool isNoGC = false; // true if the helper cannot trigger GC - bool nativeAotMayThrow = false; // true if the helper may throw on NativeAOT even when 'exceptions' is None switch (helper) { @@ -1551,8 +1550,6 @@ void HelperCallProperties::init() isPure = true; exceptions = ExceptionSetFlags::None; nonNullReturn = true; - // NativeAOT compiles generic lookups it failed to build into helpers that throw when invoked. - nativeAotMayThrow = true; break; // type casting helpers @@ -1821,8 +1818,6 @@ void HelperCallProperties::init() m_mayRunCctor[helper] = mayRunCctor; m_isNoEscape[helper] = isNoEscape; m_isNoGC[helper] = isNoGC; - - m_nativeAotExceptions[helper] = nativeAotMayThrow ? ExceptionSetFlags::UnknownException : exceptions; } } diff --git a/src/coreclr/jit/utils.h b/src/coreclr/jit/utils.h index e67a2ccb6f967c..09899c926f7d8b 100644 --- a/src/coreclr/jit/utils.h +++ b/src/coreclr/jit/utils.h @@ -606,7 +606,6 @@ class HelperCallProperties private: bool m_isPure[CORINFO_HELP_COUNT]; ExceptionSetFlags m_exceptions[CORINFO_HELP_COUNT]; - ExceptionSetFlags m_nativeAotExceptions[CORINFO_HELP_COUNT]; bool m_alwaysThrow[CORINFO_HELP_COUNT]; bool m_nonNullReturn[CORINFO_HELP_COUNT]; bool m_isAllocator[CORINFO_HELP_COUNT]; @@ -630,16 +629,16 @@ class HelperCallProperties return m_isPure[helperId]; } - bool NoThrow(CorInfoHelpFunc helperId, bool isNativeAot) + bool NoThrow(CorInfoHelpFunc helperId) { - return ThrownExceptions(helperId, isNativeAot) == ExceptionSetFlags::None; + return ThrownExceptions(helperId) == ExceptionSetFlags::None; } - ExceptionSetFlags ThrownExceptions(CorInfoHelpFunc helperId, bool isNativeAot) + ExceptionSetFlags ThrownExceptions(CorInfoHelpFunc helperId) { assert(helperId > CORINFO_HELP_UNDEF); assert(helperId < CORINFO_HELP_COUNT); - return isNativeAot ? m_nativeAotExceptions[helperId] : m_exceptions[helperId]; + return m_exceptions[helperId]; } bool AlwaysThrow(CorInfoHelpFunc helperId) diff --git a/src/coreclr/jit/valuenum.cpp b/src/coreclr/jit/valuenum.cpp index 41ac9d2eb173de..9e0a598939b125 100644 --- a/src/coreclr/jit/valuenum.cpp +++ b/src/coreclr/jit/valuenum.cpp @@ -15040,14 +15040,6 @@ void Compiler::fgValueNumberHelperCallFunc(GenTreeCall* call, VNFunc vnf, ValueN } } } - if ((vnf == VNF_ReadyToRunGenericHandle) && - !s_helperCallProperties.NoThrow(call->GetHelperNum(), IsNativeAot())) - { - // A lookup that failed to build throws the same way wherever it is evaluated. - ValueNumPair lookupExc = vnStore->VNPairForFunc(TYP_REF, VNF_HelperOpaqueExc, call->gtVNPair); - vnpExc = vnStore->VNPExcSetUnion(vnpExc, vnStore->VNPExcSetSingleton(lookupExc)); - } - // Add the accumulated exceptions. call->gtVNPair = vnStore->VNPWithExc(call->gtVNPair, vnpExc); } @@ -15679,7 +15671,7 @@ bool Compiler::fgValueNumberHelperCall(GenTreeCall* call) bool isAlloc = s_helperCallProperties.IsAllocator(helpFunc); bool mayRunCctor = s_helperCallProperties.MayRunCctor(helpFunc); bool modHeap = s_helperCallProperties.MutatesHeap(helpFunc) || mayRunCctor; // a cctor may modify the heap - bool noThrow = s_helperCallProperties.NoThrow(helpFunc, IsNativeAot()); + bool noThrow = s_helperCallProperties.NoThrow(helpFunc); ValueNumPair vnpExc = ValueNumStore::VNPForEmptyExcSet(); @@ -15809,10 +15801,6 @@ bool Compiler::fgValueNumberHelperCall(GenTreeCall* call) vnpExc = fgValueNumberIndirNullCheckExceptions(call->gtArgs.GetUserArgByIndex(0)->GetNode()); break; - case CORINFO_HELP_READYTORUN_GENERIC_HANDLE: - // fgValueNumberHelperCallFunc adds an exception determined by the lookup itself. - break; - default: // Setup vnpExc with the information that multiple different exceptions // could be generated by this helper, in an opaque way