JIT: some small profile related fixes (#43408)
authorAndy Ayers <andya@microsoft.com>
Fri, 16 Oct 2020 18:33:33 +0000 (11:33 -0700)
committerGitHub <noreply@github.com>
Fri, 16 Oct 2020 18:33:33 +0000 (11:33 -0700)
1. If we're inheriting a fraction of the profile weight of a profiled block,
mark the inheriting block as profiled. This prevents methods like
`optSetBlockWeights` or `optMarkLoopBlocks` from coming along later and setting
the weights to something else. Since the full inheritance method has similar
logic, make it delegate to the fractional one, with a scale of 100 (no scaling).

2. If we switch from Tier0 to FullOpt, make sure to clear the BBINSTR flag,
else we'll put probes into optimized code.

3. Dump edge weights in the dot graph, if we have them.

4. Only dump the flow graph twice per phase.

src/coreclr/src/jit/block.h
src/coreclr/src/jit/compiler.cpp
src/coreclr/src/jit/flowgraph.cpp

index c11383f..949b246 100644 (file)
@@ -568,47 +568,37 @@ struct BasicBlock : private LIR::Range
     }
 
     // this block will inherit the same weight and relevant bbFlags as bSrc
+    //
     void inheritWeight(BasicBlock* bSrc)
     {
-        this->bbWeight = bSrc->bbWeight;
-
-        if (bSrc->hasProfileWeight())
-        {
-            this->bbFlags |= BBF_PROF_WEIGHT;
-        }
-        else
-        {
-            this->bbFlags &= ~BBF_PROF_WEIGHT;
-        }
-
-        if (this->bbWeight == 0)
-        {
-            this->bbFlags |= BBF_RUN_RARELY;
-        }
-        else
-        {
-            this->bbFlags &= ~BBF_RUN_RARELY;
-        }
+        inheritWeightPercentage(bSrc, 100);
     }
 
     // Similar to inheritWeight(), but we're splitting a block (such as creating blocks for qmark removal).
-    // So, specify a percentage (0 to 99; if it's 100, just use inheritWeight()) of the weight that we're
-    // going to inherit. Since the number isn't exact, clear the BBF_PROF_WEIGHT flag.
+    // So, specify a percentage (0 to 100) of the weight the block should inherit.
+    //
     void inheritWeightPercentage(BasicBlock* bSrc, unsigned percentage)
     {
-        assert(0 <= percentage && percentage < 100);
+        assert(0 <= percentage && percentage <= 100);
 
         // Check for overflow
-        if (bSrc->bbWeight * 100 <= bSrc->bbWeight)
+        if ((bSrc->bbWeight * 100) <= bSrc->bbWeight)
         {
             this->bbWeight = bSrc->bbWeight;
         }
         else
         {
-            this->bbWeight = bSrc->bbWeight * percentage / 100;
+            this->bbWeight = (bSrc->bbWeight * percentage) / 100;
         }
 
-        this->bbFlags &= ~BBF_PROF_WEIGHT;
+        if (bSrc->hasProfileWeight())
+        {
+            this->bbFlags |= BBF_PROF_WEIGHT;
+        }
+        else
+        {
+            this->bbFlags &= ~BBF_PROF_WEIGHT;
+        }
 
         if (this->bbWeight == 0)
         {
index ebfe8e3..f14ffb4 100644 (file)
@@ -4215,9 +4215,7 @@ void Compiler::EndPhase(Phases phase)
         pCompJitTimer->EndPhase(this, phase);
     }
 #endif
-#if DUMP_FLOWGRAPHS
-    fgDumpFlowGraph(phase);
-#endif // DUMP_FLOWGRAPHS
+
     mostRecentlyActivePhase = phase;
 }
 
index 1fa2aa1..cb080b1 100644 (file)
@@ -4344,6 +4344,7 @@ void Compiler::fgSwitchToOptimized()
     JITDUMP("****\n**** JIT Tier0 jit request switching to Tier1 because of loop\n****\n");
     assert(opts.jitFlags->IsSet(JitFlags::JIT_FLAG_TIER0));
     opts.jitFlags->Clear(JitFlags::JIT_FLAG_TIER0);
+    opts.jitFlags->Clear(JitFlags::JIT_FLAG_BBINSTR);
 
     // Leave a note for jit diagnostics
     compSwitchedToOptimized = true;
@@ -19929,8 +19930,7 @@ bool Compiler::fgDumpFlowGraph(Phases phase)
         return false;
     }
     bool        validWeights  = fgHaveValidEdgeWeights;
-    unsigned    calledCount   = max(fgCalledCount, BB_UNITY_WEIGHT) / BB_UNITY_WEIGHT;
-    double      weightDivisor = (double)(calledCount * BB_UNITY_WEIGHT);
+    double      weightDivisor = (double)fgCalledCount;
     const char* escapedString;
     const char* regionString = "NONE";
 
@@ -19972,7 +19972,7 @@ bool Compiler::fgDumpFlowGraph(Phases phase)
 
         if (fgHaveProfileData())
         {
-            fprintf(fgxFile, "\n    calledCount=\"%d\"", calledCount);
+            fprintf(fgxFile, "\n    calledCount=\"%d\"", fgCalledCount);
             fprintf(fgxFile, "\n    profileData=\"true\"");
         }
         if (compHndBBtabCount > 0)
@@ -20122,20 +20122,32 @@ bool Compiler::fgDumpFlowGraph(Phases phase)
                 {
                     fprintf(fgxFile, "    " FMT_BB " -> " FMT_BB, bSource->bbNum, bTarget->bbNum);
 
+                    const char* sep = "";
+
                     if (bSource->bbNum > bTarget->bbNum)
                     {
                         // Lexical backedge
-                        fprintf(fgxFile, " [color=green]\n");
+                        fprintf(fgxFile, " [color=green");
+                        sep = ", ";
                     }
                     else if ((bSource->bbNum + 1) == bTarget->bbNum)
                     {
                         // Lexical successor
-                        fprintf(fgxFile, " [color=blue, weight=20]\n");
+                        fprintf(fgxFile, " [color=blue, weight=20");
+                        sep = ", ";
                     }
                     else
                     {
-                        fprintf(fgxFile, ";\n");
+                        fprintf(fgxFile, " [");
                     }
+
+                    if (validWeights)
+                    {
+                        unsigned edgeWeight = (edge->edgeWeightMin() + edge->edgeWeightMax()) / 2;
+                        fprintf(fgxFile, "%slabel=\"%7.2f\"", sep, (double)edgeWeight / weightDivisor);
+                    }
+
+                    fprintf(fgxFile, "];\n");
                 }
                 else
                 {