<div dir="auto"><div>That's fine with me.  Thanks again for looking into this.<br><br><div class="gmail_quote"><div dir="ltr">On Thu, Jun 21, 2018, 6:56 PM Chandler Carruth <<a href="mailto:chandlerc@gmail.com">chandlerc@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I think I'm going ot have to revert this. I fixed the test failure, but this patch is also causing us to leak memory.<div><br></div><div>The embedded function analysis support in the legacy PM *really* doesn't work well. In addition to breaking the '-debug-pass=Structure' output, it also appears to leak memory.</div><div><br></div><div>You can see the ASan leak reports here:</div><div><a href="http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-bootstrap/builds/6067/" target="_blank" rel="noreferrer">http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-bootstrap/builds/6067/</a><br></div><div><br></div><div>I'm not likely to have energy to debug this nasty part of the legacy PM. Personally, I'd suggest just adding this to the new PM and skipping the legacy PM. It doesn't seem worth the trouble. We're also *remarkably* close to being ready to switch the defaults.</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Jun 21, 2018 at 4:35 PM Michael J. Spencer via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank" rel="noreferrer">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: mspencer<br>
Date: Thu Jun 21 16:31:10 2018<br>
New Revision: 335306<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=335306&view=rev" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=335306&view=rev</a><br>
Log:<br>
[Instrumentation] Add Call Graph Profile pass<br>
<br>
This patch adds support for generating a call graph profile from Branch Frequency Info.<br>
<br>
The CGProfile module pass simply gets the block profile count for each BB and scans for call instructions. For each call instruction it adds an edge from the current function to the called function with the current BB block profile count as the weight.<br>
<br>
After scanning all the functions, it generates an appending module flag containing the data. The format looks like:<br>
<br>
!llvm.module.flags = !{!0}<br>
<br>
!0 = !{i32 5, !"CG Profile", !1}<br>
!1 = !{!2, !3, !4} ; List of edges<br>
!2 = !{void ()* @a, void ()* @b, i64 32} ; Edge from a to b with a weight of 32<br>
!3 = !{void (i1)* @freq, void ()* @a, i64 11}<br>
!4 = !{void (i1)* @freq, void ()* @b, i64 20}<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D48105" rel="noreferrer noreferrer" target="_blank">https://reviews.llvm.org/D48105</a><br>
<br>
Added:<br>
    llvm/trunk/lib/Transforms/Instrumentation/CGProfile.cpp<br>
    llvm/trunk/test/Instrumentation/cgprofile.ll<br>
    llvm/trunk/test/MC/ELF/cgprofile.ll<br>
    llvm/trunk/test/Verifier/module-flags-cgprofile.ll<br>
Modified:<br>
    llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h<br>
    llvm/trunk/include/llvm/InitializePasses.h<br>
    llvm/trunk/include/llvm/LinkAllPasses.h<br>
    llvm/trunk/include/llvm/Transforms/Instrumentation.h<br>
    llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp<br>
    llvm/trunk/lib/IR/Verifier.cpp<br>
    llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp<br>
    llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt<br>
    llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h?rev=335306&r1=335305&r2=335306&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h?rev=335306&r1=335305&r2=335306&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h (original)<br>
+++ llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h Thu Jun 21 16:31:10 2018<br>
@@ -36,11 +36,14 @@ class TargetLoweringObjectFileELF : publ<br>
 protected:<br>
   MCSymbolRefExpr::VariantKind PLTRelativeVariantKind =<br>
       MCSymbolRefExpr::VK_None;<br>
+  const TargetMachine *TM;<br>
<br>
 public:<br>
   TargetLoweringObjectFileELF() = default;<br>
   ~TargetLoweringObjectFileELF() override = default;<br>
<br>
+  void Initialize(MCContext &Ctx, const TargetMachine &TM) override;<br>
+<br>
   /// Emit Obj-C garbage collection and linker options.<br>
   void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override;<br>
<br>
<br>
Modified: llvm/trunk/include/llvm/InitializePasses.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=335306&r1=335305&r2=335306&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=335306&r1=335305&r2=335306&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/InitializePasses.h (original)<br>
+++ llvm/trunk/include/llvm/InitializePasses.h Thu Jun 21 16:31:10 2018<br>
@@ -94,6 +94,7 @@ void initializeCFGViewerLegacyPassPass(P<br>
 void initializeCFIInstrInserterPass(PassRegistry&);<br>
 void initializeCFLAndersAAWrapperPassPass(PassRegistry&);<br>
 void initializeCFLSteensAAWrapperPassPass(PassRegistry&);<br>
+void initializeCGProfilePassPass(PassRegistry&);<br>
 void initializeCallGraphDOTPrinterPass(PassRegistry&);<br>
 void initializeCallGraphPrinterLegacyPassPass(PassRegistry&);<br>
 void initializeCallGraphViewerPass(PassRegistry&);<br>
<br>
Modified: llvm/trunk/include/llvm/LinkAllPasses.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LinkAllPasses.h?rev=335306&r1=335305&r2=335306&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LinkAllPasses.h?rev=335306&r1=335305&r2=335306&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/LinkAllPasses.h (original)<br>
+++ llvm/trunk/include/llvm/LinkAllPasses.h Thu Jun 21 16:31:10 2018<br>
@@ -80,6 +80,7 @@ namespace {<br>
       (void) llvm::createCallGraphDOTPrinterPass();<br>
       (void) llvm::createCallGraphViewerPass();<br>
       (void) llvm::createCFGSimplificationPass();<br>
+      (void) llvm::createCGProfilePass();<br>
       (void) llvm::createCFLAndersAAWrapperPass();<br>
       (void) llvm::createCFLSteensAAWrapperPass();<br>
       (void) llvm::createStructurizeCFGPass();<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/Instrumentation.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Instrumentation.h?rev=335306&r1=335305&r2=335306&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Instrumentation.h?rev=335306&r1=335305&r2=335306&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/Instrumentation.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/Instrumentation.h Thu Jun 21 16:31:10 2018<br>
@@ -187,6 +187,8 @@ struct SanitizerCoverageOptions {<br>
 ModulePass *createSanitizerCoverageModulePass(<br>
     const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());<br>
<br>
+ModulePass *createCGProfilePass();<br>
+<br>
 /// Calculate what to divide by to scale counts.<br>
 ///<br>
 /// Given the maximum count, calculate a divisor that will scale all the<br>
<br>
Modified: llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp?rev=335306&r1=335305&r2=335306&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp?rev=335306&r1=335305&r2=335306&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp Thu Jun 21 16:31:10 2018<br>
@@ -91,6 +91,12 @@ static void GetObjCImageInfo(Module &M,<br>
 //                                  ELF<br>
 //===----------------------------------------------------------------------===//<br>
<br>
+void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,<br>
+                                             const TargetMachine &TgtM) {<br>
+  TargetLoweringObjectFile::Initialize(Ctx, TgtM);<br>
+  TM = &TgtM;<br>
+}<br>
+<br>
 void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer,<br>
                                                      Module &M) const {<br>
   auto &C = getContext();<br>
@@ -116,15 +122,49 @@ void TargetLoweringObjectFileELF::emitMo<br>
   StringRef Section;<br>
<br>
   GetObjCImageInfo(M, Version, Flags, Section);<br>
-  if (Section.empty())<br>
+  if (!Section.empty()) {<br>
+    auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);<br>
+    Streamer.SwitchSection(S);<br>
+    Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));<br>
+    Streamer.EmitIntValue(Version, 4);<br>
+    Streamer.EmitIntValue(Flags, 4);<br>
+    Streamer.AddBlankLine();<br>
+  }<br>
+<br>
+  SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;<br>
+  M.getModuleFlagsMetadata(ModuleFlags);<br>
+<br>
+  MDNode *CFGProfile = nullptr;<br>
+<br>
+  for (const auto &MFE : ModuleFlags) {<br>
+    StringRef Key = MFE.Key->getString();<br>
+    if (Key == "CG Profile") {<br>
+      CFGProfile = cast<MDNode>(MFE.Val);<br>
+      break;<br>
+    }<br>
+  }<br>
+<br>
+  if (!CFGProfile)<br>
     return;<br>
<br>
-  auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);<br>
-  Streamer.SwitchSection(S);<br>
-  Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));<br>
-  Streamer.EmitIntValue(Version, 4);<br>
-  Streamer.EmitIntValue(Flags, 4);<br>
-  Streamer.AddBlankLine();<br>
+  auto GetSym = [this](const MDOperand &MDO) {<br>
+    auto V = cast<ValueAsMetadata>(MDO);<br>
+    const Function *F = cast<Function>(V->getValue());<br>
+    return TM->getSymbol(F);<br>
+  };<br>
+<br>
+  for (const auto &Edge : CFGProfile->operands()) {<br>
+    MDNode *E = cast<MDNode>(Edge);<br>
+    const MCSymbol *From = GetSym(E->getOperand(0));<br>
+    const MCSymbol *To = GetSym(E->getOperand(1));<br>
+    uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))<br>
+                         ->getValue()<br>
+                         ->getUniqueInteger()<br>
+                         .getZExtValue();<br>
+    Streamer.emitCGProfileEntry(<br>
+        MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),<br>
+        MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);<br>
+  }<br>
 }<br>
<br>
 MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(<br>
<br>
Modified: llvm/trunk/lib/IR/Verifier.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Verifier.cpp?rev=335306&r1=335305&r2=335306&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Verifier.cpp?rev=335306&r1=335305&r2=335306&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/IR/Verifier.cpp (original)<br>
+++ llvm/trunk/lib/IR/Verifier.cpp Thu Jun 21 16:31:10 2018<br>
@@ -409,6 +409,7 @@ private:<br>
   void visitModuleFlag(const MDNode *Op,<br>
                        DenseMap<const MDString *, const MDNode *> &SeenIDs,<br>
                        SmallVectorImpl<const MDNode *> &Requirements);<br>
+  void visitModuleFlagCGProfileEntry(const MDOperand &MDO);<br>
   void visitFunction(const Function &F);<br>
   void visitBasicBlock(BasicBlock &BB);<br>
   void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty);<br>
@@ -1411,6 +1412,25 @@ Verifier::visitModuleFlag(const MDNode *<br>
     Assert(M.getNamedMetadata("llvm.linker.options"),<br>
            "'Linker Options' named metadata no longer supported");<br>
   }<br>
+<br>
+  if (ID->getString() == "CG Profile") {<br>
+    for (const MDOperand &MDO : cast<MDNode>(Op->getOperand(2))->operands())<br>
+      visitModuleFlagCGProfileEntry(MDO);<br>
+  }<br>
+}<br>
+<br>
+void Verifier::visitModuleFlagCGProfileEntry(const MDOperand &MDO) {<br>
+  auto Node = dyn_cast_or_null<MDNode>(MDO);<br>
+  Assert(Node && Node->getNumOperands() == 3, "expected a MDNode triple", MDO);<br>
+  auto From = dyn_cast_or_null<ValueAsMetadata>(Node->getOperand(0));<br>
+  Assert(From && isa<Function>(From->getValue()), "expected a Function",<br>
+         Node->getOperand(0));<br>
+  auto To = dyn_cast_or_null<ValueAsMetadata>(Node->getOperand(1));<br>
+  Assert(To && isa<Function>(To->getValue()), "expected a Function",<br>
+         Node->getOperand(1));<br>
+  auto Count = dyn_cast_or_null<ConstantAsMetadata>(Node->getOperand(2));<br>
+  Assert(Count && Count->getType()->isIntegerTy(),<br>
+         "expected an integer constant", Node->getOperand(2));<br>
 }<br>
<br>
 /// Return true if this attribute kind only applies to functions.<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp?rev=335306&r1=335305&r2=335306&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp?rev=335306&r1=335305&r2=335306&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp Thu Jun 21 16:31:10 2018<br>
@@ -694,6 +694,8 @@ void PassManagerBuilder::populateModuleP<br>
     MPM.add(createConstantMergePass());     // Merge dup global constants<br>
   }<br>
<br>
+  MPM.add(createCGProfilePass());<br>
+<br>
   if (MergeFunctions)<br>
     MPM.add(createMergeFunctionsPass());<br>
<br>
<br>
Added: llvm/trunk/lib/Transforms/Instrumentation/CGProfile.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/CGProfile.cpp?rev=335306&view=auto" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/CGProfile.cpp?rev=335306&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Instrumentation/CGProfile.cpp (added)<br>
+++ llvm/trunk/lib/Transforms/Instrumentation/CGProfile.cpp Thu Jun 21 16:31:10 2018<br>
@@ -0,0 +1,110 @@<br>
+//===-- CGProfile.cpp -----------------------------------------------------===//<br>
+//<br>
+//                      The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/ADT/MapVector.h"<br>
+#include "llvm/Analysis/BlockFrequencyInfo.h"<br>
+#include "llvm/Analysis/BranchProbabilityInfo.h"<br>
+#include "llvm/IR/Constants.h"<br>
+#include "llvm/IR/Instructions.h"<br>
+#include "llvm/IR/MDBuilder.h"<br>
+#include "llvm/IR/PassManager.h"<br>
+#include "llvm/Transforms/Instrumentation.h"<br>
+<br>
+#include <array><br>
+<br>
+using namespace llvm;<br>
+<br>
+class CGProfilePass : public ModulePass {<br>
+public:<br>
+  static char ID;<br>
+<br>
+  CGProfilePass() : ModulePass(ID) {<br>
+    initializeCGProfilePassPass(*PassRegistry::getPassRegistry());<br>
+  }<br>
+<br>
+  StringRef getPassName() const override { return "CGProfilePass"; }<br>
+<br>
+private:<br>
+  bool runOnModule(Module &M) override;<br>
+  bool addModuleFlags(<br>
+      Module &M,<br>
+      MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) const;<br>
+<br>
+  void getAnalysisUsage(AnalysisUsage &AU) const override {<br>
+    AU.addRequired<BlockFrequencyInfoWrapperPass>();<br>
+    AU.addRequired<BranchProbabilityInfoWrapperPass>();<br>
+  }<br>
+};<br>
+<br>
+bool CGProfilePass::runOnModule(Module &M) {<br>
+  if (skipModule(M))<br>
+    return false;<br>
+<br>
+  MapVector<std::pair<Function *, Function *>, uint64_t> Counts;<br>
+<br>
+  for (auto &F : M) {<br>
+    if (F.isDeclaration())<br>
+      continue;<br>
+    getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI();<br>
+    auto &BFI = getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI();<br>
+    for (const auto &BB : F) {<br>
+      Optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB);<br>
+      if (!BBCount)<br>
+        continue;<br>
+      for (const auto &I : BB) {<br>
+        auto *CI = dyn_cast<CallInst>(&I);<br>
+        if (!CI)<br>
+          continue;<br>
+        Function *CalledF = CI->getCalledFunction();<br>
+        if (!CalledF || CalledF->isIntrinsic())<br>
+          continue;<br>
+<br>
+        uint64_t &Count = Counts[std::make_pair(&F, CalledF)];<br>
+        Count = SaturatingAdd(Count, *BBCount);<br>
+      }<br>
+    }<br>
+  }<br>
+<br>
+  return addModuleFlags(M, Counts);<br>
+}<br>
+<br>
+bool CGProfilePass::addModuleFlags(<br>
+    Module &M,<br>
+    MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) const {<br>
+  if (Counts.empty())<br>
+    return false;<br>
+<br>
+  LLVMContext &Context = M.getContext();<br>
+  MDBuilder MDB(Context);<br>
+  std::vector<Metadata *> Nodes;<br>
+<br>
+  for (auto E : Counts) {<br>
+    SmallVector<Metadata *, 3> Vals;<br>
+    Vals.push_back(ValueAsMetadata::get(E.first.first));<br>
+    Vals.push_back(ValueAsMetadata::get(E.first.second));<br>
+    Vals.push_back(MDB.createConstant(<br>
+        ConstantInt::get(Type::getInt64Ty(Context), E.second)));<br>
+    Nodes.push_back(MDNode::get(Context, Vals));<br>
+  }<br>
+<br>
+  M.addModuleFlag(Module::Append, "CG Profile", MDNode::get(Context, Nodes));<br>
+  return true;<br>
+}<br>
+<br>
+char CGProfilePass::ID = 0;<br>
+INITIALIZE_PASS_BEGIN(CGProfilePass, "cg-profile",<br>
+                      "Generate profile information from the call graph.",<br>
+                      false, false)<br>
+INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)<br>
+INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)<br>
+INITIALIZE_PASS_END(CGProfilePass, "cg-profile",<br>
+                    "Generate profile information from the call graph.", false,<br>
+                    false)<br>
+<br>
+ModulePass *llvm::createCGProfilePass() { return new CGProfilePass(); }<br>
<br>
Modified: llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt?rev=335306&r1=335305&r2=335306&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt?rev=335306&r1=335305&r2=335306&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt (original)<br>
+++ llvm/trunk/lib/Transforms/Instrumentation/CMakeLists.txt Thu Jun 21 16:31:10 2018<br>
@@ -1,6 +1,7 @@<br>
 add_llvm_library(LLVMInstrumentation<br>
   AddressSanitizer.cpp<br>
   BoundsChecking.cpp<br>
+  CGProfile.cpp<br>
   DataFlowSanitizer.cpp<br>
   GCOVProfiling.cpp<br>
   MemorySanitizer.cpp<br>
<br>
Modified: llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp?rev=335306&r1=335305&r2=335306&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp?rev=335306&r1=335305&r2=335306&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Instrumentation/Instrumentation.cpp Thu Jun 21 16:31:10 2018<br>
@@ -60,6 +60,7 @@ void llvm::initializeInstrumentation(Pas<br>
   initializeAddressSanitizerModulePass(Registry);<br>
   initializeBoundsCheckingLegacyPassPass(Registry);<br>
   initializeGCOVProfilerLegacyPassPass(Registry);<br>
+  initializeCGProfilePassPass(Registry);<br>
   initializePGOInstrumentationGenLegacyPassPass(Registry);<br>
   initializePGOInstrumentationUseLegacyPassPass(Registry);<br>
   initializePGOIndirectCallPromotionLegacyPassPass(Registry);<br>
<br>
Added: llvm/trunk/test/Instrumentation/cgprofile.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/cgprofile.ll?rev=335306&view=auto" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/cgprofile.ll?rev=335306&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Instrumentation/cgprofile.ll (added)<br>
+++ llvm/trunk/test/Instrumentation/cgprofile.ll Thu Jun 21 16:31:10 2018<br>
@@ -0,0 +1,28 @@<br>
+; RUN: opt < %s -cg-profile -S | FileCheck %s<br>
+<br>
+declare void @b()<br>
+<br>
+define void @a() !prof !1 {<br>
+  call void @b()<br>
+  ret void<br>
+}<br>
+<br>
+define void @freq(i1 %cond) !prof !1 {<br>
+  br i1 %cond, label %A, label %B, !prof !2<br>
+A:<br>
+  call void @a();<br>
+  ret void<br>
+B:<br>
+  call void @b();<br>
+  ret void<br>
+}<br>
+<br>
+!1 = !{!"function_entry_count", i64 32}<br>
+!2 = !{!"branch_weights", i32 5, i32 10}<br>
+<br>
+; CHECK: !llvm.module.flags = !{![[cgprof:[0-9]+]]}<br>
+; CHECK: ![[cgprof]] = !{i32 5, !"CG Profile", ![[prof:[0-9]+]]}<br>
+; CHECK: ![[prof]] = !{![[e0:[0-9]+]], ![[e1:[0-9]+]], ![[e2:[0-9]+]]}<br>
+; CHECK: ![[e0]] = !{void ()* @a, void ()* @b, i64 32}<br>
+; CHECK: ![[e1]] = !{void (i1)* @freq, void ()* @a, i64 11}<br>
+; CHECK: ![[e2]] = !{void (i1)* @freq, void ()* @b, i64 20}<br>
<br>
Added: llvm/trunk/test/MC/ELF/cgprofile.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/cgprofile.ll?rev=335306&view=auto" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/cgprofile.ll?rev=335306&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/MC/ELF/cgprofile.ll (added)<br>
+++ llvm/trunk/test/MC/ELF/cgprofile.ll Thu Jun 21 16:31:10 2018<br>
@@ -0,0 +1,50 @@<br>
+; RUN: llc -filetype=asm %s -o - -mtriple x86_64-pc-linux-gnu | FileCheck %s<br>
+; RUN: llc -filetype=obj %s -o %t -mtriple x86_64-pc-linux-gnu<br>
+; RUN: llvm-readobj -elf-cg-profile %t | FileCheck %s --check-prefix=OBJ<br>
+<br>
+declare void @b()<br>
+<br>
+define void @a() {<br>
+  call void @b()<br>
+  ret void<br>
+}<br>
+<br>
+define void @freq(i1 %cond) {<br>
+  br i1 %cond, label %A, label %B<br>
+A:<br>
+  call void @a();<br>
+  ret void<br>
+B:<br>
+  call void @b();<br>
+  ret void<br>
+}<br>
+<br>
+!llvm.module.flags = !{!0}<br>
+<br>
+!0 = !{i32 5, !"CG Profile", !1}<br>
+!1 = !{!2, !3, !4}<br>
+!2 = !{void ()* @a, void ()* @b, i64 32}<br>
+!3 = !{void (i1)* @freq, void ()* @a, i64 11}<br>
+!4 = !{void (i1)* @freq, void ()* @b, i64 20}<br>
+<br>
+; CHECK: .cg_profile a, b, 32<br>
+; CHECK: .cg_profile freq, a, 11<br>
+; CHECK: .cg_profile freq, b, 20<br>
+<br>
+; OBJ: CGProfile [<br>
+; OBJ:  CGProfileEntry {<br>
+; OBJ:    From: a<br>
+; OBJ:    To: b<br>
+; OBJ:    Weight: 32<br>
+; OBJ:  }<br>
+; OBJ:  CGProfileEntry {<br>
+; OBJ:    From: freq<br>
+; OBJ:    To: a<br>
+; OBJ:    Weight: 11<br>
+; OBJ:  }<br>
+; OBJ:  CGProfileEntry {<br>
+; OBJ:    From: freq<br>
+; OBJ:    To: b<br>
+; OBJ:    Weight: 20<br>
+; OBJ:  }<br>
+; OBJ:]<br>
<br>
Added: llvm/trunk/test/Verifier/module-flags-cgprofile.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Verifier/module-flags-cgprofile.ll?rev=335306&view=auto" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Verifier/module-flags-cgprofile.ll?rev=335306&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Verifier/module-flags-cgprofile.ll (added)<br>
+++ llvm/trunk/test/Verifier/module-flags-cgprofile.ll Thu Jun 21 16:31:10 2018<br>
@@ -0,0 +1,30 @@<br>
+; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s<br>
+<br>
+declare void @b()<br>
+declare void @a()<br>
+<br>
+!llvm.module.flags = !{!0}<br>
+<br>
+!0 = !{i32 5, !"CG Profile", !1}<br>
+!1 = !{!2, !"", !3, !4, !5, !6, !7, !8}<br>
+!2 = !{void ()* @a, void ()* @b, i64 32}<br>
+!3 = !{void ()* @a, void ()* @b}<br>
+!4 = !{void ()* @a, void ()* @b, i64 32, i64 32}<br>
+!5 = !{!"a", void ()* @b, i64 32}<br>
+!6 = !{void ()* @a, !"b", i64 32}<br>
+!7 = !{void ()* @a, void ()* @b, !""}<br>
+!8 = !{void ()* @a, void ()* @b, null}<br>
+<br>
+; CHECK: expected a MDNode triple<br>
+; CHECK: !""<br>
+; CHECK: expected a MDNode triple<br>
+; CHECK: !3 = !{void ()* @a, void ()* @b}<br>
+; CHECK: expected a MDNode triple<br>
+; CHECK: !4 = !{void ()* @a, void ()* @b, i64 32, i64 32}<br>
+; CHECK: expected a Function<br>
+; CHECK: !"a"<br>
+; CHECK: expected a Function<br>
+; CHECK: !"b"<br>
+; CHECK: expected an integer constant<br>
+; CHECK: !""<br>
+; CHECK: expected an integer constant<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank" rel="noreferrer">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>
</blockquote></div></div></div>