From a7263a83e2654aa162323083e680ef9ff784e5f9 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 b9ff493c87d49f..b9cd5043a37165 100644 --- a/src/coreclr/jit/fgprofilesynthesis.cpp +++ b/src/coreclr/jit/fgprofilesynthesis.cpp @@ -1408,12 +1408,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)) {