From 52c513ea24de2a29ea29bd403c17a591d1c5fa01 Mon Sep 17 00:00:00 2001 From: Brian Sullivan Date: Wed, 22 Mar 2017 14:53:51 -0700 Subject: [PATCH] Change setBBProfileWeight so that we no longer multiply the profile count by 100 Fix fgAddSyncMethodEnterExit to properly set the IBC profile weight when we have profile data In fgComputeEdgeWeights properly set fgCalledCount when we inserted a new first block using fgFirstBBisScratch Fix fgOptimizeUncondBranchToSimpleCond to properly set the new BasicBlock's weight and flags Commit migrated from https://github.com/dotnet/coreclr/commit/753dbbb780fc215413e61dfd6adcf2786505cd54 --- src/coreclr/src/jit/block.h | 3 +-- src/coreclr/src/jit/flowgraph.cpp | 53 +++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/coreclr/src/jit/block.h b/src/coreclr/src/jit/block.h index 2d6cdf5..16f1b00 100644 --- a/src/coreclr/src/jit/block.h +++ b/src/coreclr/src/jit/block.h @@ -545,8 +545,7 @@ struct BasicBlock : private LIR::Range void setBBProfileWeight(unsigned weight) { this->bbFlags |= BBF_PROF_WEIGHT; - // Check if the multiplication by BB_UNITY_WEIGHT will overflow. - this->bbWeight = (weight <= BB_MAX_WEIGHT / BB_UNITY_WEIGHT) ? weight * BB_UNITY_WEIGHT : BB_MAX_WEIGHT; + this->bbWeight = weight; } // modifyBBWeight -- same as setBBWeight, but also make sure that if the block is rarely run, it stays that diff --git a/src/coreclr/src/jit/flowgraph.cpp b/src/coreclr/src/jit/flowgraph.cpp index dbaff9a..f502b4b 100644 --- a/src/coreclr/src/jit/flowgraph.cpp +++ b/src/coreclr/src/jit/flowgraph.cpp @@ -7634,8 +7634,15 @@ void Compiler::fgAddSyncMethodEnterExit() assert(fgFirstBB->bbFallsThrough()); BasicBlock* tryBegBB = fgNewBBafter(BBJ_NONE, fgFirstBB, false); + BasicBlock* tryNextBB = tryBegBB->bbNext; BasicBlock* tryLastBB = fgLastBB; + // If we have profile data the new block will inherit the next block's weight + if (tryNextBB->hasProfileWeight()) + { + tryBegBB->inheritWeight(tryNextBB); + } + // Create a block for the fault. assert(!tryLastBB->bbFallsThrough()); @@ -12687,20 +12694,47 @@ void Compiler::fgComputeEdgeWeights() // if (fgIsUsingProfileWeights()) { - // If the first block has one ref then it's weight is the fgCalledCount - // otherwise we have backedge's into the first block so instead - // we use the sum of the return block weights. - // If the profile data has a 0 for the returnWeoght - // then just use the first block weight rather than the 0 + BasicBlock* firstILBlock = fgFirstBB; // The first block for IL code (i.e. for the IL code at offset 0) + + // Did we allocate a scratch block as the new first BB? + if (fgFirstBBisScratch()) + { + firstILBlock = firstILBlock->bbNext; + // The second block is expected to have a profile-derived weight + assert(firstILBlock->hasProfileWeight()); + } + + // If the first block only has one ref then we use it's weight + // for fgCalledCount. Otherwise we have backedge's into the first block, + // so instead we use the sum of the return block weights for fgCalledCount. // - if ((fgFirstBB->countOfInEdges() == 1) || (returnWeight == 0)) + // If the profile data has a 0 for the returnWeight + // (i.e. the function never returns because it always throws) + // then just use the first block weight rather than 0. + // + if ((firstILBlock->countOfInEdges() == 1) || (returnWeight == 0)) { - fgCalledCount = fgFirstBB->bbWeight; + assert(firstILBlock->hasProfileWeight()); // This should always be a profile-derived weight + fgCalledCount = firstILBlock->bbWeight; } else { fgCalledCount = returnWeight; } + + // If we allocated a scratch block as the first BB then we need + // to set its profile-derived weight to be fgCalledCount + if (fgFirstBBisScratch()) + { + fgFirstBB->setBBProfileWeight(fgCalledCount); + } + +#if DEBUG + if (verbose) + { + printf("We are using the Profile Weights and fgCalledCount is %d.\n", fgCalledCount); + } +#endif } // Now we will compute the initial flEdgeWeightMin and flEdgeWeightMax values @@ -13832,10 +13866,9 @@ bool Compiler::fgOptimizeUncondBranchToSimpleCond(BasicBlock* block, BasicBlock* // add an unconditional block after this block to jump to the target block's fallthrough block BasicBlock* next = fgNewBBafter(BBJ_ALWAYS, block, true); - next->bbFlags = block->bbFlags | BBF_INTERNAL; - next->bbFlags &= ~(BBF_TRY_BEG | BBF_LOOP_HEAD | BBF_LOOP_CALL0 | BBF_LOOP_CALL1 | BBF_HAS_LABEL | BBF_JMP_TARGET | - BBF_FUNCLET_BEG | BBF_LOOP_PREHEADER | BBF_KEEP_BBJ_ALWAYS); + // The new block 'next' will inherit its weight from 'block' + next->inheritWeight(block); next->bbJumpDest = target->bbNext; target->bbNext->bbFlags |= BBF_JMP_TARGET; fgAddRefPred(next, block); -- 2.7.4