diff --git a/llvm/include/llvm/Analysis/ModuleDebugInfoPrinter.h b/llvm/include/llvm/Analysis/ModuleDebugInfoPrinter.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/Analysis/ModuleDebugInfoPrinter.h @@ -0,0 +1,29 @@ +//===- ModuleDebugInfoPrinter.h - -----------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ANALYSIS_MODULEDEBUGINFOPRINTER_H +#define LLVM_ANALYSIS_MODULEDEBUGINFOPRINTER_H + +#include "llvm/IR/DebugInfo.h" +#include "llvm/IR/PassManager.h" +#include "llvm/Support/raw_ostream.h" + +namespace llvm { + +class ModuleDebugInfoPrinterPass + : public PassInfoMixin { + DebugInfoFinder Finder; + raw_ostream &OS; + +public: + explicit ModuleDebugInfoPrinterPass(raw_ostream &OS); + PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); +}; +} // end namespace llvm + +#endif // LLVM_ANALYSIS_MODULEDEBUGINFOPRINTER_H diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h --- a/llvm/include/llvm/InitializePasses.h +++ b/llvm/include/llvm/InitializePasses.h @@ -306,7 +306,7 @@ void initializeMergeICmpsLegacyPassPass(PassRegistry &); void initializeMergedLoadStoreMotionLegacyPassPass(PassRegistry&); void initializeMetaRenamerPass(PassRegistry&); -void initializeModuleDebugInfoPrinterPass(PassRegistry&); +void initializeModuleDebugInfoLegacyPrinterPass(PassRegistry &); void initializeModuleMemProfilerLegacyPassPass(PassRegistry &); void initializeModuleSummaryIndexWrapperPassPass(PassRegistry&); void initializeModuloScheduleTestPass(PassRegistry&); diff --git a/llvm/lib/Analysis/Analysis.cpp b/llvm/lib/Analysis/Analysis.cpp --- a/llvm/lib/Analysis/Analysis.cpp +++ b/llvm/lib/Analysis/Analysis.cpp @@ -63,7 +63,7 @@ initializeMemDepPrinterPass(Registry); initializeMemDerefPrinterPass(Registry); initializeMemoryDependenceWrapperPassPass(Registry); - initializeModuleDebugInfoPrinterPass(Registry); + initializeModuleDebugInfoLegacyPrinterPass(Registry); initializeModuleSummaryIndexWrapperPassPass(Registry); initializeMustExecutePrinterPass(Registry); initializeMustBeExecutedContextPrinterPass(Registry); diff --git a/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp b/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp --- a/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp +++ b/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp @@ -14,9 +14,11 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Analysis/ModuleDebugInfoPrinter.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/Passes.h" #include "llvm/IR/DebugInfo.h" +#include "llvm/IR/PassManager.h" #include "llvm/InitializePasses.h" #include "llvm/Pass.h" #include "llvm/Support/ErrorHandling.h" @@ -24,32 +26,34 @@ using namespace llvm; namespace { - class ModuleDebugInfoPrinter : public ModulePass { - DebugInfoFinder Finder; - public: - static char ID; // Pass identification, replacement for typeid - ModuleDebugInfoPrinter() : ModulePass(ID) { - initializeModuleDebugInfoPrinterPass(*PassRegistry::getPassRegistry()); - } +class ModuleDebugInfoLegacyPrinter : public ModulePass { + DebugInfoFinder Finder; - bool runOnModule(Module &M) override; +public: + static char ID; // Pass identification, replacement for typeid + ModuleDebugInfoLegacyPrinter() : ModulePass(ID) { + initializeModuleDebugInfoLegacyPrinterPass( + *PassRegistry::getPassRegistry()); + } - void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.setPreservesAll(); - } - void print(raw_ostream &O, const Module *M) const override; - }; + bool runOnModule(Module &M) override; + + void getAnalysisUsage(AnalysisUsage &AU) const override { + AU.setPreservesAll(); + } + void print(raw_ostream &O, const Module *M) const override; +}; } -char ModuleDebugInfoPrinter::ID = 0; -INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo", +char ModuleDebugInfoLegacyPrinter::ID = 0; +INITIALIZE_PASS(ModuleDebugInfoLegacyPrinter, "module-debuginfo", "Decodes module-level debug info", false, true) ModulePass *llvm::createModuleDebugInfoPrinterPass() { - return new ModuleDebugInfoPrinter(); + return new ModuleDebugInfoLegacyPrinter(); } -bool ModuleDebugInfoPrinter::runOnModule(Module &M) { +bool ModuleDebugInfoLegacyPrinter::runOnModule(Module &M) { Finder.processModule(M); return false; } @@ -67,7 +71,8 @@ O << ":" << Line; } -void ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const { +static void printModuleDebugInfo(raw_ostream &O, const Module *M, + const DebugInfoFinder &Finder) { // Printing the nodes directly isn't particularly helpful (since they // reference other nodes that won't be printed, particularly for the // filenames), so just print a few useful things. @@ -126,3 +131,18 @@ O << '\n'; } } + +void ModuleDebugInfoLegacyPrinter::print(raw_ostream &O, + const Module *M) const { + printModuleDebugInfo(O, M, Finder); +} + +ModuleDebugInfoPrinterPass::ModuleDebugInfoPrinterPass(raw_ostream &OS) + : OS(OS) {} + +PreservedAnalyses ModuleDebugInfoPrinterPass::run(Module &M, + ModuleAnalysisManager &AM) { + Finder.processModule(M); + printModuleDebugInfo(OS, &M, Finder); + return PreservedAnalyses::all(); +} diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -49,6 +49,7 @@ #include "llvm/Analysis/LoopNestAnalysis.h" #include "llvm/Analysis/MemoryDependenceAnalysis.h" #include "llvm/Analysis/MemorySSA.h" +#include "llvm/Analysis/ModuleDebugInfoPrinter.h" #include "llvm/Analysis/ModuleSummaryAnalysis.h" #include "llvm/Analysis/ObjCARCAliasAnalysis.h" #include "llvm/Analysis/OptimizationRemarkEmitter.h" diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -85,6 +85,7 @@ MODULE_PASS("print-lcg", LazyCallGraphPrinterPass(dbgs())) MODULE_PASS("print-lcg-dot", LazyCallGraphDOTPrinterPass(dbgs())) MODULE_PASS("print-stack-safety", StackSafetyGlobalPrinterPass(dbgs())) +MODULE_PASS("print", ModuleDebugInfoPrinterPass(dbgs())) MODULE_PASS("rewrite-statepoints-for-gc", RewriteStatepointsForGC()) MODULE_PASS("rewrite-symbols", RewriteSymbolPass()) MODULE_PASS("rpo-function-attrs", ReversePostOrderFunctionAttrsPass()) diff --git a/llvm/test/DebugInfo/Generic/debuginfofinder-forward-declaration.ll b/llvm/test/DebugInfo/Generic/debuginfofinder-forward-declaration.ll --- a/llvm/test/DebugInfo/Generic/debuginfofinder-forward-declaration.ll +++ b/llvm/test/DebugInfo/Generic/debuginfofinder-forward-declaration.ll @@ -1,4 +1,6 @@ -; RUN: opt -analyze -module-debuginfo < %s | FileCheck %s +; RUN: opt -analyze -module-debuginfo -enable-new-pm=0 < %s | FileCheck %s +; RUN: opt -passes='print' -disable-output 2>&1 < %s \ +; RUN: | FileCheck %s ; This module is generated from the following c-code: ; diff --git a/llvm/test/DebugInfo/Generic/debuginfofinder-imported-global-variable.ll b/llvm/test/DebugInfo/Generic/debuginfofinder-imported-global-variable.ll --- a/llvm/test/DebugInfo/Generic/debuginfofinder-imported-global-variable.ll +++ b/llvm/test/DebugInfo/Generic/debuginfofinder-imported-global-variable.ll @@ -1,7 +1,9 @@ -; RUN: opt -analyze -module-debuginfo < %s | FileCheck %s +; RUN: opt -analyze -module-debuginfo -enable-new-pm=0 < %s | FileCheck %s +; RUN: opt -passes='print' -disable-output 2>&1 < %s \ +; RUN: | FileCheck %s ; This is to track DebugInfoFinder's ability to find the debug info metadata, -; in particular, properly visit different kinds of DIImportedEntit'ies. +; in particular, properly visit different kinds of DIImportedEntities. ; Derived from the following C++ snippet ; @@ -13,7 +15,6 @@ ; ; compiled with `clang -O1 -g3 -emit-llvm -S` -; CHECK: Printing analysis 'Decodes module-level debug info': ; CHECK: Compile unit: DW_LANG_C_plus_plus from /somewhere/source.cpp ; CHECK: Global variable: i from /somewhere/source.cpp:2 ('_ZN1s1iE') ; CHECK: Type: int DW_ATE_signed diff --git a/llvm/test/DebugInfo/Generic/debuginfofinder-inlined-cu.ll b/llvm/test/DebugInfo/Generic/debuginfofinder-inlined-cu.ll --- a/llvm/test/DebugInfo/Generic/debuginfofinder-inlined-cu.ll +++ b/llvm/test/DebugInfo/Generic/debuginfofinder-inlined-cu.ll @@ -1,4 +1,6 @@ -; RUN: opt -analyze -module-debuginfo < %s | FileCheck %s +; RUN: opt -analyze -module-debuginfo -enable-new-pm=0 < %s | FileCheck %s +; RUN: opt -passes='print' -disable-output 2>&1 < %s \ +; RUN: | FileCheck %s ; Verify that both compile units, even though one compile units's functions ; were entirely inlined into the other. diff --git a/llvm/test/DebugInfo/Generic/debuginfofinder-multiple-cu.ll b/llvm/test/DebugInfo/Generic/debuginfofinder-multiple-cu.ll --- a/llvm/test/DebugInfo/Generic/debuginfofinder-multiple-cu.ll +++ b/llvm/test/DebugInfo/Generic/debuginfofinder-multiple-cu.ll @@ -1,4 +1,6 @@ -; RUN: opt -analyze -module-debuginfo < %s | FileCheck %s +; RUN: opt -analyze -module-debuginfo -enable-new-pm=0 < %s | FileCheck %s +; RUN: opt -passes='print' -disable-output 2>&1 < %s \ +; RUN: | FileCheck %s ; Produced from linking: ; /tmp/test1.c containing f()