From 1200a050ff59e16631972e25e93f24d34b982115 Mon Sep 17 00:00:00 2001 From: Adam Nemet Date: Fri, 2 Sep 2016 00:28:26 +0000 Subject: [PATCH] [CFGPrinter] Display branch weight on the edges Summary: This is pretty useful especially in connection with BFI's -view-block-freq-propagation-dags. It helped me to track down the bug that is being fixed in D24118. While -view-block-freq-propagation-dags displays the high-level information with static heuristics included (and block frequencies), the new thing only shows the raw weight as presented by PGO without any of the static estimates. This helps to distinguished what has been measured vs. estimated. For the sample loop in D24118, -view-block-freq-propagation-dags=integer looks like this: https://reviews.llvm.org/F2381352 While with -view-cfg-only you can see the underlying branch weights: https://reviews.llvm.org/F2392296 Reviewers: dexonsmith, bogner, davidxl Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D24144 llvm-svn: 280442 --- llvm/include/llvm/Analysis/CFGPrinter.h | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/llvm/include/llvm/Analysis/CFGPrinter.h b/llvm/include/llvm/Analysis/CFGPrinter.h index 035764837e6f..f73a13f533c1 100644 --- a/llvm/include/llvm/Analysis/CFGPrinter.h +++ b/llvm/include/llvm/Analysis/CFGPrinter.h @@ -118,6 +118,36 @@ struct DOTGraphTraits : public DefaultDOTGraphTraits { } return ""; } + + /// Display the raw branch weights from PGO. + std::string getEdgeAttributes(const BasicBlock *Node, succ_const_iterator I, + const Function *F) { + const TerminatorInst *TI = Node->getTerminator(); + if (TI->getNumSuccessors() == 1) + return ""; + + MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof); + if (!WeightsNode) + return ""; + + MDString *MDName = cast(WeightsNode->getOperand(0)); + if (MDName->getString() != "branch_weights") + return ""; + + unsigned OpNo = I.getSuccessorIndex() + 1; + if (OpNo >= WeightsNode->getNumOperands()) + return ""; + ConstantInt *Weight = + mdconst::dyn_extract(WeightsNode->getOperand(OpNo)); + if (!Weight) + return ""; + + // Append a 'W' to indicate that these are weights rather than actual + // profile + // count (due to scaling). + Twine Attrs = "label=\"W:" + Twine(Weight->getZExtValue()) + "\""; + return Attrs.str(); + } }; } // End llvm namespace