<div dir="ltr">This looks like broken by the patch <a href="http://lab.llvm.org:8014/#/builders/68/builds/221">http://lab.llvm.org:8014/#/builders/68/builds/221</a></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 16 Oct 2020 at 13:28, Jameson Nash via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Author: Jameson Nash<br>
Date: 2020-10-16T16:27:31-04:00<br>
New Revision: ac2def2d8d8c9aa6f96f0c869a5b988ae602385a<br>
<br>
URL: <a href="https://github.com/llvm/llvm-project/commit/ac2def2d8d8c9aa6f96f0c869a5b988ae602385a" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/ac2def2d8d8c9aa6f96f0c869a5b988ae602385a</a><br>
DIFF: <a href="https://github.com/llvm/llvm-project/commit/ac2def2d8d8c9aa6f96f0c869a5b988ae602385a.diff" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/ac2def2d8d8c9aa6f96f0c869a5b988ae602385a.diff</a><br>
<br>
LOG: make the AsmPrinterHandler array public<br>
<br>
This lets external consumers customize the output, similar to how<br>
AssemblyAnnotationWriter lets the caller define callbacks when printing<br>
IR. The array of handlers already existed, this just cleans up the code<br>
so that it can be exposed publically.<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D74158" rel="noreferrer" target="_blank">https://reviews.llvm.org/D74158</a><br>
<br>
Added: <br>
<br>
<br>
Modified: <br>
    llvm/include/llvm/CodeGen/AsmPrinter.h<br>
    llvm/include/llvm/CodeGen/AsmPrinterHandler.h<br>
    llvm/include/llvm/CodeGen/DebugHandlerBase.h<br>
    llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp<br>
    llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp<br>
    llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp<br>
    llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp<br>
    llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h<br>
    llvm/lib/CodeGen/MachineModuleInfo.cpp<br>
    llvm/lib/Target/BPF/BTFDebug.cpp<br>
    llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp<br>
    llvm/unittests/CodeGen/TestAsmPrinter.h<br>
<br>
Removed: <br>
<br>
<br>
<br>
################################################################################<br>
diff  --git a/llvm/include/llvm/CodeGen/AsmPrinter.h b/llvm/include/llvm/CodeGen/AsmPrinter.h<br>
index 3056568ccf98..f6efdc664e18 100644<br>
--- a/llvm/include/llvm/CodeGen/AsmPrinter.h<br>
+++ b/llvm/include/llvm/CodeGen/AsmPrinter.h<br>
@@ -139,6 +139,23 @@ class AsmPrinter : public MachineFunctionPass {<br>
   using GOTEquivUsePair = std::pair<const GlobalVariable *, unsigned>;<br>
   MapVector<const MCSymbol *, GOTEquivUsePair> GlobalGOTEquivs;<br>
<br>
+  /// struct HandlerInfo and Handlers permit users or target extended<br>
+  /// AsmPrinter to add their own handlers.<br>
+  struct HandlerInfo {<br>
+    std::unique_ptr<AsmPrinterHandler> Handler;<br>
+    const char *TimerName;<br>
+    const char *TimerDescription;<br>
+    const char *TimerGroupName;<br>
+    const char *TimerGroupDescription;<br>
+<br>
+    HandlerInfo(std::unique_ptr<AsmPrinterHandler> Handler,<br>
+                const char *TimerName, const char *TimerDescription,<br>
+                const char *TimerGroupName, const char *TimerGroupDescription)<br>
+        : Handler(std::move(Handler)), TimerName(TimerName),<br>
+          TimerDescription(TimerDescription), TimerGroupName(TimerGroupName),<br>
+          TimerGroupDescription(TimerGroupDescription) {}<br>
+  };<br>
+<br>
 private:<br>
   MCSymbol *CurrentFnEnd = nullptr;<br>
<br>
@@ -162,26 +179,10 @@ class AsmPrinter : public MachineFunctionPass {<br>
 protected:<br>
   MCSymbol *CurrentFnBegin = nullptr;<br>
<br>
-  /// Protected struct HandlerInfo and Handlers permit target extended<br>
-  /// AsmPrinter adds their own handlers.<br>
-  struct HandlerInfo {<br>
-    std::unique_ptr<AsmPrinterHandler> Handler;<br>
-    const char *TimerName;<br>
-    const char *TimerDescription;<br>
-    const char *TimerGroupName;<br>
-    const char *TimerGroupDescription;<br>
-<br>
-    HandlerInfo(std::unique_ptr<AsmPrinterHandler> Handler,<br>
-                const char *TimerName, const char *TimerDescription,<br>
-                const char *TimerGroupName, const char *TimerGroupDescription)<br>
-        : Handler(std::move(Handler)), TimerName(TimerName),<br>
-          TimerDescription(TimerDescription), TimerGroupName(TimerGroupName),<br>
-          TimerGroupDescription(TimerGroupDescription) {}<br>
-  };<br>
-<br>
   /// A vector of all debug/EH info emitters we should use. This vector<br>
   /// maintains ownership of the emitters.<br>
-  SmallVector<HandlerInfo, 1> Handlers;<br>
+  std::vector<HandlerInfo> Handlers;<br>
+  size_t NumUserHandlers = 0;<br>
<br>
 public:<br>
   struct SrcMgrDiagInfo {<br>
@@ -446,6 +447,11 @@ class AsmPrinter : public MachineFunctionPass {<br>
   // Overridable Hooks<br>
   //===------------------------------------------------------------------===//<br>
<br>
+  void addAsmPrinterHandler(HandlerInfo Handler) {<br>
+    Handlers.insert(Handlers.begin(), std::move(Handler));<br>
+    NumUserHandlers++;<br>
+  }<br>
+<br>
   // Targets can, or in the case of EmitInstruction, must implement these to<br>
   // customize output.<br>
<br>
<br>
diff  --git a/llvm/include/llvm/CodeGen/AsmPrinterHandler.h b/llvm/include/llvm/CodeGen/AsmPrinterHandler.h<br>
index b9837dc168e9..dc81a3040097 100644<br>
--- a/llvm/include/llvm/CodeGen/AsmPrinterHandler.h<br>
+++ b/llvm/include/llvm/CodeGen/AsmPrinterHandler.h<br>
@@ -23,6 +23,7 @@ class MachineBasicBlock;<br>
 class MachineFunction;<br>
 class MachineInstr;<br>
 class MCSymbol;<br>
+class Module;<br>
<br>
 typedef MCSymbol *ExceptionSymbolProvider(AsmPrinter *Asm,<br>
                                           const MachineBasicBlock *MBB);<br>
@@ -37,6 +38,8 @@ class AsmPrinterHandler {<br>
   /// this tracks that size.<br>
   virtual void setSymbolSize(const MCSymbol *Sym, uint64_t Size) = 0;<br>
<br>
+  virtual void beginModule(Module *M) {}<br>
+<br>
   /// Emit all sections that should come after the content.<br>
   virtual void endModule() = 0;<br>
<br>
@@ -75,6 +78,7 @@ class AsmPrinterHandler {<br>
   /// Process end of a basic block during basic block sections.<br>
   virtual void endBasicBlock(const MachineBasicBlock &MBB) {}<br>
 };<br>
+<br>
 } // End of namespace llvm<br>
<br>
 #endif<br>
<br>
diff  --git a/llvm/include/llvm/CodeGen/DebugHandlerBase.h b/llvm/include/llvm/CodeGen/DebugHandlerBase.h<br>
index b488979f458c..02dba1b9e6de 100644<br>
--- a/llvm/include/llvm/CodeGen/DebugHandlerBase.h<br>
+++ b/llvm/include/llvm/CodeGen/DebugHandlerBase.h<br>
@@ -115,6 +115,8 @@ class DebugHandlerBase : public AsmPrinterHandler {<br>
<br>
   // AsmPrinterHandler overrides.<br>
 public:<br>
+  void beginModule(Module *M) override;<br>
+<br>
   void beginInstruction(const MachineInstr *MI) override;<br>
   void endInstruction() override;<br>
<br>
<br>
diff  --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp<br>
index f5587c9ae7dc..3353c6243387 100644<br>
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp<br>
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp<br>
@@ -188,7 +188,8 @@ AsmPrinter::AsmPrinter(TargetMachine &tm, std::unique_ptr<MCStreamer> Streamer)<br>
 }<br>
<br>
 AsmPrinter::~AsmPrinter() {<br>
-  assert(!DD && Handlers.empty() && "Debug/EH info didn't get finalized");<br>
+  assert(!DD && Handlers.size() == NumUserHandlers &&<br>
+         "Debug/EH info didn't get finalized");<br>
<br>
   if (GCMetadataPrinters) {<br>
     gcp_map_type &GCMap = getGCMap(GCMetadataPrinters);<br>
@@ -252,6 +253,9 @@ bool AsmPrinter::doInitialization(Module &M) {<br>
   auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();<br>
   MMI = MMIWP ? &MMIWP->getMMI() : nullptr;<br>
<br>
+  if (!M.debug_compile_units().empty())<br>
+    MMI->setDebugInfoAvailability(true);<br>
+<br>
   // Initialize TargetLoweringObjectFile.<br>
   const_cast<TargetLoweringObjectFile&>(getObjFileLowering())<br>
     .Initialize(OutContext, TM);<br>
@@ -313,8 +317,7 @@ bool AsmPrinter::doInitialization(Module &M) {<br>
                             CodeViewLineTablesGroupDescription);<br>
     }<br>
     if (!EmitCodeView || M.getDwarfVersion()) {<br>
-      DD = new DwarfDebug(this, &M);<br>
-      DD->beginModule();<br>
+      DD = new DwarfDebug(this);<br>
       Handlers.emplace_back(std::unique_ptr<DwarfDebug>(DD), DbgTimerName,<br>
                             DbgTimerDescription, DWARFGroupName,<br>
                             DWARFGroupDescription);<br>
@@ -379,6 +382,13 @@ bool AsmPrinter::doInitialization(Module &M) {<br>
     Handlers.emplace_back(std::make_unique<WinCFGuard>(this), CFGuardName,<br>
                           CFGuardDescription, DWARFGroupName,<br>
                           DWARFGroupDescription);<br>
+<br>
+  for (const HandlerInfo &HI : Handlers) {<br>
+    NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,<br>
+                       HI.TimerGroupDescription, TimePassesIsEnabled);<br>
+    HI.Handler->beginModule(&M);<br>
+  }<br>
+<br>
   return false;<br>
 }<br>
<br>
@@ -1110,8 +1120,6 @@ void AsmPrinter::emitFunctionBody() {<br>
   // Emit target-specific gunk before the function body.<br>
   emitFunctionBodyStart();<br>
<br>
-  bool ShouldPrintDebugScopes = MMI->hasDebugInfo();<br>
-<br>
   if (isVerbose()) {<br>
     // Get MachineDominatorTree or compute it on the fly if it's unavailable<br>
     MDT = getAnalysisIfAvailable<MachineDominatorTree>();<br>
@@ -1149,13 +1157,10 @@ void AsmPrinter::emitFunctionBody() {<br>
       if (MCSymbol *S = MI.getPreInstrSymbol())<br>
         OutStreamer->emitLabel(S);<br>
<br>
-      if (ShouldPrintDebugScopes) {<br>
-        for (const HandlerInfo &HI : Handlers) {<br>
-          NamedRegionTimer T(HI.TimerName, HI.TimerDescription,<br>
-                             HI.TimerGroupName, HI.TimerGroupDescription,<br>
-                             TimePassesIsEnabled);<br>
-          HI.Handler->beginInstruction(&MI);<br>
-        }<br>
+      for (const HandlerInfo &HI : Handlers) {<br>
+        NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,<br>
+                           HI.TimerGroupDescription, TimePassesIsEnabled);<br>
+        HI.Handler->beginInstruction(&MI);<br>
       }<br>
<br>
       if (isVerbose())<br>
@@ -1209,13 +1214,10 @@ void AsmPrinter::emitFunctionBody() {<br>
       if (MCSymbol *S = MI.getPostInstrSymbol())<br>
         OutStreamer->emitLabel(S);<br>
<br>
-      if (ShouldPrintDebugScopes) {<br>
-        for (const HandlerInfo &HI : Handlers) {<br>
-          NamedRegionTimer T(HI.TimerName, HI.TimerDescription,<br>
-                             HI.TimerGroupName, HI.TimerGroupDescription,<br>
-                             TimePassesIsEnabled);<br>
-          HI.Handler->endInstruction();<br>
-        }<br>
+      for (const HandlerInfo &HI : Handlers) {<br>
+        NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,<br>
+                           HI.TimerGroupDescription, TimePassesIsEnabled);<br>
+        HI.Handler->endInstruction();<br>
       }<br>
     }<br>
<br>
@@ -1637,7 +1639,11 @@ bool AsmPrinter::doFinalization(Module &M) {<br>
                        HI.TimerGroupDescription, TimePassesIsEnabled);<br>
     HI.Handler->endModule();<br>
   }<br>
-  Handlers.clear();<br>
+<br>
+  // This deletes all the ephemeral handlers that AsmPrinter added, while<br>
+  // keeping all the user-added handlers alive until the AsmPrinter is<br>
+  // destroyed.<br>
+  Handlers.erase(Handlers.begin() + NumUserHandlers, Handlers.end());<br>
   DD = nullptr;<br>
<br>
   // If the target wants to know about weak references, print them all.<br>
<br>
diff  --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp<br>
index bcace6264cd0..a24deb53a6a6 100644<br>
--- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp<br>
+++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp<br>
@@ -141,7 +141,6 @@ CodeViewDebug::CodeViewDebug(AsmPrinter *AP)<br>
   if (!MMI->getModule()->getNamedMetadata("<a href="http://llvm.dbg.cu" rel="noreferrer" target="_blank">llvm.dbg.cu</a>") ||<br>
       !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) {<br>
     Asm = nullptr;<br>
-    MMI->setDebugInfoAvailability(false);<br>
     return;<br>
   }<br>
   // Tell MMI that we have debug info.<br>
@@ -564,8 +563,6 @@ void CodeViewDebug::endModule() {<br>
   if (!Asm || !MMI->hasDebugInfo())<br>
     return;<br>
<br>
-  assert(Asm != nullptr);<br>
-<br>
   // The COFF .debug$S section consists of several subsections, each starting<br>
   // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length<br>
   // of the payload followed by the payload itself.  The subsections are 4-byte<br>
<br>
diff  --git a/llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp b/llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp<br>
index 826c5078ed50..ab53540ed2c0 100644<br>
--- a/llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp<br>
+++ b/llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp<br>
@@ -91,6 +91,11 @@ DbgVariableLocation::extractFromMachineInstruction(<br>
<br>
 DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}<br>
<br>
+void DebugHandlerBase::beginModule(Module *M) {<br>
+  if (M->debug_compile_units().empty())<br>
+    Asm = nullptr;<br>
+}<br>
+<br>
 // Each LexicalScope has first instruction and last instruction to mark<br>
 // beginning and end of a scope respectively. Create an inverse map that list<br>
 // scopes starts (and ends) with an instruction. One instruction may start (or<br>
@@ -276,7 +281,7 @@ void DebugHandlerBase::beginFunction(const MachineFunction *MF) {<br>
 }<br>
<br>
 void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {<br>
-  if (!MMI->hasDebugInfo())<br>
+  if (!Asm || !MMI->hasDebugInfo())<br>
     return;<br>
<br>
   assert(CurMI == nullptr);<br>
@@ -302,7 +307,7 @@ void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {<br>
 }<br>
<br>
 void DebugHandlerBase::endInstruction() {<br>
-  if (!MMI->hasDebugInfo())<br>
+  if (!Asm || !MMI->hasDebugInfo())<br>
     return;<br>
<br>
   assert(CurMI != nullptr);<br>
@@ -334,7 +339,7 @@ void DebugHandlerBase::endInstruction() {<br>
 }<br>
<br>
 void DebugHandlerBase::endFunction(const MachineFunction *MF) {<br>
-  if (hasDebugInfo(MMI, MF))<br>
+  if (Asm && hasDebugInfo(MMI, MF))<br>
     endFunctionImpl(MF);<br>
   DbgValues.clear();<br>
   DbgLabels.clear();<br>
<br>
diff  --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp<br>
index a315a7e2ca02..2dc0a762736a 100644<br>
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp<br>
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp<br>
@@ -148,10 +148,6 @@ static cl::opt<LinkageNameOption><br>
                                             "Abstract subprograms")),<br>
                       cl::init(DefaultLinkageNames));<br>
<br>
-static const char *const DWARFGroupName = "dwarf";<br>
-static const char *const DWARFGroupDescription = "DWARF Emission";<br>
-static const char *const DbgTimerName = "writer";<br>
-static const char *const DbgTimerDescription = "DWARF Debug Writer";<br>
 static constexpr unsigned ULEB128PadSize = 4;<br>
<br>
 void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) {<br>
@@ -330,7 +326,7 @@ static AccelTableKind computeAccelTableKind(unsigned DwarfVersion,<br>
   return AccelTableKind::None;<br>
 }<br>
<br>
-DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)<br>
+DwarfDebug::DwarfDebug(AsmPrinter *A)<br>
     : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()),<br>
       InfoHolder(A, "info_string", DIEValueAllocator),<br>
       SkeletonHolder(A, "skel_string", DIEValueAllocator),<br>
@@ -1111,21 +1107,17 @@ sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) {<br>
 // Emit all Dwarf sections that should come prior to the content. Create<br>
 // global DIEs and emit initial debug info sections. This is invoked by<br>
 // the target AsmPrinter.<br>
-void DwarfDebug::beginModule() {<br>
-  NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName,<br>
-                     DWARFGroupDescription, TimePassesIsEnabled);<br>
-  if (DisableDebugInfoPrinting) {<br>
-    MMI->setDebugInfoAvailability(false);<br>
-    return;<br>
-  }<br>
+void DwarfDebug::beginModule(Module *M) {<br>
+  DebugHandlerBase::beginModule(M);<br>
<br>
-  const Module *M = MMI->getModule();<br>
+  if (!Asm || !MMI->hasDebugInfo() || DisableDebugInfoPrinting)<br>
+    return;<br>
<br>
   unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(),<br>
                                        M->debug_compile_units_end());<br>
-  // Tell MMI whether we have debug info.<br>
-  assert(MMI->hasDebugInfo() == (NumDebugCUs > 0) &&<br>
-         "DebugInfoAvailabilty initialized unexpectedly");<br>
+  assert(NumDebugCUs > 0 && "Asm unexpectedly initialized");<br>
+  assert(MMI->hasDebugInfo() &&<br>
+         "DebugInfoAvailabilty unexpectedly not initialized");<br>
   SingleCU = NumDebugCUs == 1;<br>
   DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>><br>
       GVMap;<br>
@@ -1388,7 +1380,7 @@ void DwarfDebug::endModule() {<br>
   // If we aren't actually generating debug info (check beginModule -<br>
   // conditionalized on !DisableDebugInfoPrinting and the presence of the<br>
   // <a href="http://llvm.dbg.cu" rel="noreferrer" target="_blank">llvm.dbg.cu</a> metadata node)<br>
-  if (!MMI->hasDebugInfo())<br>
+  if (!Asm || !MMI->hasDebugInfo() || DisableDebugInfoPrinting)<br>
     return;<br>
<br>
   // Finalize the debug info for the module.<br>
@@ -1899,7 +1891,8 @@ void DwarfDebug::beginInstruction(const MachineInstr *MI) {<br>
   }<br>
<br>
   DebugHandlerBase::beginInstruction(MI);<br>
-  assert(CurMI);<br>
+  if (!CurMI)<br>
+    return;<br>
<br>
   if (NoDebug)<br>
     return;<br>
<br>
diff  --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h<br>
index 34c88f1a9c60..a542fdd89f8a 100644<br>
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h<br>
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h<br>
@@ -621,13 +621,13 @@ class DwarfDebug : public DebugHandlerBase {<br>
   //===--------------------------------------------------------------------===//<br>
   // Main entry points.<br>
   //<br>
-  DwarfDebug(AsmPrinter *A, Module *M);<br>
+  DwarfDebug(AsmPrinter *A);<br>
<br>
   ~DwarfDebug() override;<br>
<br>
   /// Emit all Dwarf sections that should come prior to the<br>
   /// content.<br>
-  void beginModule();<br>
+  void beginModule(Module *M) override;<br>
<br>
   /// Emit all Dwarf sections that should come after the content.<br>
   void endModule() override;<br>
<br>
diff  --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp b/llvm/lib/CodeGen/MachineModuleInfo.cpp<br>
index be08f0ae3117..ecae4961d5a9 100644<br>
--- a/llvm/lib/CodeGen/MachineModuleInfo.cpp<br>
+++ b/llvm/lib/CodeGen/MachineModuleInfo.cpp<br>
@@ -304,7 +304,7 @@ char MachineModuleInfoWrapperPass::ID = 0;<br>
 bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {<br>
   MMI.initialize();<br>
   MMI.TheModule = &M;<br>
-  MMI.DbgInfoAvailable = !M.debug_compile_units().empty();<br>
+  MMI.DbgInfoAvailable = false;<br>
   return false;<br>
 }<br>
<br>
@@ -319,6 +319,6 @@ MachineModuleInfo MachineModuleAnalysis::run(Module &M,<br>
                                              ModuleAnalysisManager &) {<br>
   MachineModuleInfo MMI(TM);<br>
   MMI.TheModule = &M;<br>
-  MMI.DbgInfoAvailable = !M.debug_compile_units().empty();<br>
+  MMI.DbgInfoAvailable = false;<br>
   return MMI;<br>
 }<br>
<br>
diff  --git a/llvm/lib/Target/BPF/BTFDebug.cpp b/llvm/lib/Target/BPF/BTFDebug.cpp<br>
index 709e599cd6b8..30d86e02f367 100644<br>
--- a/llvm/lib/Target/BPF/BTFDebug.cpp<br>
+++ b/llvm/lib/Target/BPF/BTFDebug.cpp<br>
@@ -1076,6 +1076,9 @@ void BTFDebug::beginInstruction(const MachineInstr *MI) {<br>
     }<br>
   }<br>
<br>
+  if (!CurMI) // no debug info<br>
+    return;<br>
+<br>
   // Skip this instruction if no DebugLoc or the DebugLoc<br>
   // is the same as the previous instruction.<br>
   const DebugLoc &DL = MI->getDebugLoc();<br>
<br>
diff  --git a/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp b/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp<br>
index 5c53f39fd9a3..c9e489ed2d8a 100644<br>
--- a/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp<br>
+++ b/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp<br>
@@ -8,8 +8,13 @@<br>
<br>
 #include "TestAsmPrinter.h"<br>
 #include "llvm/CodeGen/AsmPrinter.h"<br>
+#include "llvm/CodeGen/MachineModuleInfo.h"<br>
+#include "llvm/IR/LegacyPassManager.h"<br>
+#include "llvm/IR/Module.h"<br>
+#include "llvm/IR/PassManager.h"<br>
 #include "llvm/MC/MCContext.h"<br>
 #include "llvm/MC/MCSectionELF.h"<br>
+#include "llvm/Target/TargetMachine.h"<br>
 #include "llvm/Testing/Support/Error.h"<br>
<br>
 using namespace llvm;<br>
@@ -367,4 +372,58 @@ TEST_F(AsmPrinterEmitDwarfUnitLengthAsHiLoDiffTest, DWARF64) {<br>
   TestPrinter->getAP()->emitDwarfUnitLength(Hi, Lo, "");<br>
 }<br>
<br>
+class AsmPrinterHandlerTest : public AsmPrinterFixtureBase {<br>
+  class TestHandler : public AsmPrinterHandler {<br>
+    AsmPrinterHandlerTest &Test;<br>
+<br>
+  public:<br>
+    TestHandler(AsmPrinterHandlerTest &Test) : Test(Test) {}<br>
+    virtual ~TestHandler() {}<br>
+    virtual void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {}<br>
+    virtual void beginModule(Module *M) override { Test.BeginCount++; }<br>
+    virtual void endModule() override { Test.EndCount++; }<br>
+    virtual void beginFunction(const MachineFunction *MF) override {}<br>
+    virtual void endFunction(const MachineFunction *MF) override {}<br>
+    virtual void beginInstruction(const MachineInstr *MI) override {}<br>
+    virtual void endInstruction() override {}<br>
+  };<br>
+<br>
+protected:<br>
+  bool init(const std::string &TripleStr, unsigned DwarfVersion,<br>
+            dwarf::DwarfFormat DwarfFormat) {<br>
+    if (!AsmPrinterFixtureBase::init(TripleStr, DwarfVersion, DwarfFormat))<br>
+      return false;<br>
+<br>
+    auto *AP = TestPrinter->getAP();<br>
+    AP->addAsmPrinterHandler(AsmPrinter::HandlerInfo(<br>
+        std::unique_ptr<AsmPrinterHandler>(new TestHandler(*this)),<br>
+        "TestTimerName", "TestTimerDesc", "TestGroupName", "TestGroupDesc"));<br>
+    LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(&AP->TM);<br>
+    legacy::PassManager PM;<br>
+    PM.add(new MachineModuleInfoWrapperPass(LLVMTM));<br>
+    PM.add(TestPrinter->releaseAP()); // Takes ownership of destroying AP<br>
+    LLVMContext Context;<br>
+    std::unique_ptr<Module> M(new Module("TestModule", Context));<br>
+    M->setDataLayout(LLVMTM->createDataLayout());<br>
+    PM.run(*M);<br>
+    // Now check that we can run it twice.<br>
+    AP->addAsmPrinterHandler(AsmPrinter::HandlerInfo(<br>
+        std::unique_ptr<AsmPrinterHandler>(new TestHandler(*this)),<br>
+        "TestTimerName", "TestTimerDesc", "TestGroupName", "TestGroupDesc"));<br>
+    PM.run(*M);<br>
+    return true;<br>
+  }<br>
+<br>
+  int BeginCount = 0;<br>
+  int EndCount = 0;<br>
+};<br>
+<br>
+TEST_F(AsmPrinterHandlerTest, Basic) {<br>
+  if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))<br>
+    return;<br>
+<br>
+  ASSERT_EQ(BeginCount, 3);<br>
+  ASSERT_EQ(EndCount, 3);<br>
+}<br>
+<br>
 } // end namespace<br>
<br>
diff  --git a/llvm/unittests/CodeGen/TestAsmPrinter.h b/llvm/unittests/CodeGen/TestAsmPrinter.h<br>
index 65e557b9b4a6..b69cd34247a6 100644<br>
--- a/llvm/unittests/CodeGen/TestAsmPrinter.h<br>
+++ b/llvm/unittests/CodeGen/TestAsmPrinter.h<br>
@@ -73,6 +73,7 @@ class TestAsmPrinter {<br>
   void setDwarfUsesRelocationsAcrossSections(bool Enable);<br>
<br>
   AsmPrinter *getAP() const { return Asm.get(); }<br>
+  AsmPrinter *releaseAP() { return Asm.release(); }<br>
   MCContext &getCtx() const { return *MC; }<br>
   MockMCStreamer &getMS() const { return *MS; }<br>
 };<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>