diff --git a/src/coreclr/jit/fgbasic.cpp b/src/coreclr/jit/fgbasic.cpp index 541bdc27e7c962..f818fd6018e6da 100644 --- a/src/coreclr/jit/fgbasic.cpp +++ b/src/coreclr/jit/fgbasic.cpp @@ -3247,14 +3247,6 @@ void Compiler::fgFindBasicBlocks() BADCODE("Handler Clause is invalid"); } -#if HANDLER_ENTRY_MUST_BE_IN_HOT_SECTION - // This will change the block weight from 0 to 1 - // and clear the rarely run flag - hndBegBB->makeBlockHot(); -#else - hndBegBB->bbSetRunRarely(); // handler entry points are rarely executed -#endif - if (hndEndOff < info.compILCodeSize) { hndEndBB = fgLookupBB(hndEndOff); @@ -3266,14 +3258,6 @@ void Compiler::fgFindBasicBlocks() filtBB->bbCatchTyp = BBCT_FILTER; hndBegBB->bbCatchTyp = BBCT_FILTER_HANDLER; -#if HANDLER_ENTRY_MUST_BE_IN_HOT_SECTION - // This will change the block weight from 0 to 1 - // and clear the rarely run flag - filtBB->makeBlockHot(); -#else - filtBB->bbSetRunRarely(); // filter entry points are rarely executed -#endif - // Mark all BBs that belong to the filter with the XTnum of the corresponding handler for (block = filtBB; /**/; block = block->bbNext) { diff --git a/src/coreclr/jit/fgprofile.cpp b/src/coreclr/jit/fgprofile.cpp index 4eb555a65ca94e..2551744d54444c 100644 --- a/src/coreclr/jit/fgprofile.cpp +++ b/src/coreclr/jit/fgprofile.cpp @@ -3584,6 +3584,27 @@ weight_t Compiler::fgComputeMissingBlockWeights() } } + // Handler entries are assumed to run rarely, except for + // finally blocks: These are executed regardless of if + // an exception is thrown, and thus should inherit weight. + if (bbIsHandlerBeg(bDst)) + { + bSrc = bDst->bbPreds->getBlock(); + + // To minimize asmdiffs for now, modify weights only if splitting. + if (fgFirstColdBlock != nullptr) + { + if (bSrc->bbJumpKind == BBJ_CALLFINALLY) + { + newWeight = bSrc->bbWeight; + } + else + { + newWeight = BB_ZERO_WEIGHT; + } + } + } + if ((newWeight != BB_MAX_WEIGHT) && (bDst->bbWeight != newWeight)) { changed = true; @@ -3599,6 +3620,20 @@ weight_t Compiler::fgComputeMissingBlockWeights() } } } + else if (!bDst->hasProfileWeight() && bbIsHandlerBeg(bDst) && !bDst->isRunRarely()) + { + // Assume handler/filter entries are rarely executed. + // To avoid unnecessary loop iterations, set weight + // only if bDst->bbWeight is not already zero. + + // To minimize asmdiffs for now, modify weights only if splitting. + if (fgFirstColdBlock != nullptr) + { + changed = true; + modified = true; + bDst->bbSetRunRarely(); + } + } // Sum up the weights of all of the return blocks and throw blocks // This is used when we have a back-edge into block 1 diff --git a/src/coreclr/jit/jit.h b/src/coreclr/jit/jit.h index 110c079ee58c40..f229cae217031c 100644 --- a/src/coreclr/jit/jit.h +++ b/src/coreclr/jit/jit.h @@ -400,7 +400,7 @@ class GlobalJitOptions #define CSE_INTO_HANDLERS 0 #define DUMP_FLOWGRAPHS DEBUG // Support for creating Xml Flowgraph reports in *.fgx files -#define HANDLER_ENTRY_MUST_BE_IN_HOT_SECTION 1 // if 1 we must have all handler entry points in the Hot code section +#define HANDLER_ENTRY_MUST_BE_IN_HOT_SECTION 0 // if 1 we must have all handler entry points in the Hot code section /*****************************************************************************/