[clang] be87321 - [clang][Codegen] EmitBranchOnBoolExpr(): emit prof branch counts even at -O0

Roman Lebedev via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 21 13:26:37 PDT 2021


Author: Roman Lebedev
Date: 2021-03-21T23:24:27+03:00
New Revision: be8732128029530a0b8671af3a47ce6085039fa2

URL: https://github.com/llvm/llvm-project/commit/be8732128029530a0b8671af3a47ce6085039fa2
DIFF: https://github.com/llvm/llvm-project/commit/be8732128029530a0b8671af3a47ce6085039fa2.diff

LOG: [clang][Codegen] EmitBranchOnBoolExpr(): emit prof branch counts even at -O0

This restores the original behaviour before i unadvertedly broke it in
e3a470162738871bba982416748ae5f5e3572947 and clang/test/Profile/ caught it.

Added: 
    

Modified: 
    clang/lib/CodeGen/CodeGenFunction.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 1c538262737d..fd708849e609 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -1774,27 +1774,27 @@ void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
   llvm::MDNode *Weights = nullptr;
   llvm::MDNode *Unpredictable = nullptr;
 
-  // If optimizing, lower unpredictability/probability knowledge about cond.
-  if (CGM.getCodeGenOpts().OptimizationLevel != 0) {
-    // If the branch has a condition wrapped by __builtin_unpredictable,
-    // create metadata that specifies that the branch is unpredictable.
-    if (auto *Call = dyn_cast<CallExpr>(Cond->IgnoreImpCasts())) {
-      auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl());
-      if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) {
-        llvm::MDBuilder MDHelper(getLLVMContext());
-        Unpredictable = MDHelper.createUnpredictable();
-      }
+  // If the branch has a condition wrapped by __builtin_unpredictable,
+  // create metadata that specifies that the branch is unpredictable.
+  // Don't bother if not optimizing because that metadata would not be used.
+  auto *Call = dyn_cast<CallExpr>(Cond->IgnoreImpCasts());
+  if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) {
+    auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl());
+    if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) {
+      llvm::MDBuilder MDHelper(getLLVMContext());
+      Unpredictable = MDHelper.createUnpredictable();
     }
+  }
 
-    // If there is a Likelihood knowledge for the cond, lower it.
-    llvm::Value *NewCondV = emitCondLikelihoodViaExpectIntrinsic(CondV, LH);
-    if (CondV != NewCondV)
-      CondV = NewCondV;
-    else {
-      // Otherwise, lower profile counts.
-      uint64_t CurrentCount = std::max(getCurrentProfileCount(), TrueCount);
-      Weights = createProfileWeights(TrueCount, CurrentCount - TrueCount);
-    }
+  // If there is a Likelihood knowledge for the cond, lower it.
+  // Note that if not optimizing this won't emit anything.
+  llvm::Value *NewCondV = emitCondLikelihoodViaExpectIntrinsic(CondV, LH);
+  if (CondV != NewCondV)
+    CondV = NewCondV;
+  else {
+    // Otherwise, lower profile counts. Note that we do this even at -O0.
+    uint64_t CurrentCount = std::max(getCurrentProfileCount(), TrueCount);
+    Weights = createProfileWeights(TrueCount, CurrentCount - TrueCount);
   }
 
   Builder.CreateCondBr(CondV, TrueBlock, FalseBlock, Weights, Unpredictable);


        


More information about the cfe-commits mailing list