From fb2a3cd094cae25fd6a2534b8e4e3e320a3a6fa6 Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Tue, 11 Mar 2025 15:16:38 -0700 Subject: [PATCH] JIT: avoid fp divide by zero in profile synthesis This can trip up users that have enabled floating point exceptions. While we don't generally support changing the exception modes we also can easily avoid dividing by zero here. Addresses #113369 --- src/coreclr/jit/fgprofilesynthesis.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/coreclr/jit/fgprofilesynthesis.cpp b/src/coreclr/jit/fgprofilesynthesis.cpp index 4405a690f8d36f..ff4b0ac8dda869 100644 --- a/src/coreclr/jit/fgprofilesynthesis.cpp +++ b/src/coreclr/jit/fgprofilesynthesis.cpp @@ -1411,12 +1411,19 @@ void ProfileSynthesis::GaussSeidelSolver() countVector[block->bbNum] = newWeight; // Remember max absolute and relative change - // (note rel residual will be infinite at times, that's ok) + // (note rel residual will be as large as 1e9 at times, that's ok) // // Note we are using a "point" bound here ("infinity norm") rather than say // computing the L2-norm of the entire residual vector. // - weight_t const blockRelResidual = change / oldWeight; + weight_t const smallFractionOfChange = 1e-9 * change; + weight_t relDivisor = oldWeight; + if (relDivisor < smallFractionOfChange) + { + relDivisor = smallFractionOfChange; + } + + weight_t const blockRelResidual = change / relDivisor; if ((relResidualBlock == nullptr) || (blockRelResidual > relResidual)) {