From 72b955cc23125c0bf63e6623ecc8496114ed186c Mon Sep 17 00:00:00 2001 From: Eugene Rozenfeld Date: Tue, 6 Feb 2018 11:19:32 -0800 Subject: [PATCH] Don't remove the first non-internal block that has profile weight. (#16227) When using profile weights, fgComputeEdgeWeights expects the first non-internal block to have profile weight. Make sure we don't break that invariant when removing an empty block. This fixes VSO 546031. --- src/jit/flowgraph.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/jit/flowgraph.cpp b/src/jit/flowgraph.cpp index 1b588e6..45e1ce4 100644 --- a/src/jit/flowgraph.cpp +++ b/src/jit/flowgraph.cpp @@ -13936,6 +13936,31 @@ bool Compiler::fgOptimizeEmptyBlock(BasicBlock* block) fgLastBB = bPrev; } + // When using profile weights, fgComputeEdgeWeights expects the first non-internal block to have profile + // weight. + // Make sure we don't break that invariant. + if (fgIsUsingProfileWeights() && block->hasProfileWeight() && (block->bbFlags & BBF_INTERNAL) == 0) + { + BasicBlock* bNext = block->bbNext; + + // Check if the next block can't maintain the invariant. + if ((bNext == nullptr) || ((bNext->bbFlags & BBF_INTERNAL) != 0) || !bNext->hasProfileWeight()) + { + // Check if the current block is the first non-internal block. + BasicBlock* curBB = bPrev; + while ((curBB != nullptr) && (curBB->bbFlags & BBF_INTERNAL) != 0) + { + curBB = curBB->bbPrev; + } + if (curBB == nullptr) + { + // This block is the first non-internal block and it has profile weight. + // Don't delete it. + break; + } + } + } + /* Remove the block */ compCurBB = block; fgRemoveBlock(block, false); -- 2.7.4