From 04bc20c4b9cf039b8ed24a53cf2889f2f2ac91f4 Mon Sep 17 00:00:00 2001 From: Aman Khalid Date: Fri, 24 Jun 2022 12:30:49 -0700 Subject: [PATCH 1/4] Disable HANDLER_ENTRY_MUST_BE_IN_HOT_SECTION --- src/coreclr/jit/jit.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 /*****************************************************************************/ From 7dafd2fdc144d0772d1f366012804e64acf0c7d3 Mon Sep 17 00:00:00 2001 From: Aman Khalid Date: Mon, 27 Jun 2022 11:50:58 -0700 Subject: [PATCH 2/4] Fix setting finally clause to cold --- src/coreclr/jit/fgbasic.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/coreclr/jit/fgbasic.cpp b/src/coreclr/jit/fgbasic.cpp index 541bdc27e7c962..3b6820b902cf4f 100644 --- a/src/coreclr/jit/fgbasic.cpp +++ b/src/coreclr/jit/fgbasic.cpp @@ -3252,7 +3252,13 @@ void Compiler::fgFindBasicBlocks() // and clear the rarely run flag hndBegBB->makeBlockHot(); #else - hndBegBB->bbSetRunRarely(); // handler entry points are rarely executed + // Handler entry points are rarely executed, + // but don't assume finally clause is cold + // (we want to enable fgCloneFinally() optimization). + if (hndBegBB->bbJumpKind != BBJ_EHFINALLYRET) + { + hndBegBB->bbSetRunRarely(); + } #endif if (hndEndOff < info.compILCodeSize) From 8e510507db1c4b40adedc88e838769debd023766 Mon Sep 17 00:00:00 2001 From: Aman Khalid Date: Tue, 28 Jun 2022 15:32:08 -0700 Subject: [PATCH 3/4] Move handler entry weight logic to fgComputeMissingBlockWeights --- src/coreclr/jit/fgbasic.cpp | 22 ---------------------- src/coreclr/jit/fgprofile.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/coreclr/jit/fgbasic.cpp b/src/coreclr/jit/fgbasic.cpp index 3b6820b902cf4f..f818fd6018e6da 100644 --- a/src/coreclr/jit/fgbasic.cpp +++ b/src/coreclr/jit/fgbasic.cpp @@ -3247,20 +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 - // Handler entry points are rarely executed, - // but don't assume finally clause is cold - // (we want to enable fgCloneFinally() optimization). - if (hndBegBB->bbJumpKind != BBJ_EHFINALLYRET) - { - hndBegBB->bbSetRunRarely(); - } -#endif - if (hndEndOff < info.compILCodeSize) { hndEndBB = fgLookupBB(hndEndOff); @@ -3272,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..4360800be73f87 100644 --- a/src/coreclr/jit/fgprofile.cpp +++ b/src/coreclr/jit/fgprofile.cpp @@ -3584,6 +3584,22 @@ 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(); + 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 +3615,15 @@ 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. + 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 From 16cfa327a31fd43cccc508a7ad2e38d9b3458d35 Mon Sep 17 00:00:00 2001 From: Aman Khalid Date: Tue, 28 Jun 2022 16:51:02 -0700 Subject: [PATCH 4/4] Modify handler entry weights only when splitting --- src/coreclr/jit/fgprofile.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/coreclr/jit/fgprofile.cpp b/src/coreclr/jit/fgprofile.cpp index 4360800be73f87..2551744d54444c 100644 --- a/src/coreclr/jit/fgprofile.cpp +++ b/src/coreclr/jit/fgprofile.cpp @@ -3590,13 +3590,18 @@ weight_t Compiler::fgComputeMissingBlockWeights() if (bbIsHandlerBeg(bDst)) { bSrc = bDst->bbPreds->getBlock(); - if (bSrc->bbJumpKind == BBJ_CALLFINALLY) - { - newWeight = bSrc->bbWeight; - } - else + + // To minimize asmdiffs for now, modify weights only if splitting. + if (fgFirstColdBlock != nullptr) { - newWeight = BB_ZERO_WEIGHT; + if (bSrc->bbJumpKind == BBJ_CALLFINALLY) + { + newWeight = bSrc->bbWeight; + } + else + { + newWeight = BB_ZERO_WEIGHT; + } } } @@ -3620,9 +3625,14 @@ weight_t Compiler::fgComputeMissingBlockWeights() // Assume handler/filter entries are rarely executed. // To avoid unnecessary loop iterations, set weight // only if bDst->bbWeight is not already zero. - changed = true; - modified = true; - bDst->bbSetRunRarely(); + + // 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