[llvm] 32ab405 - [LTO] Emit DebugLoc for dead function in optimization remarks

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 21 14:50:37 PDT 2021


Author: Xu Mingjie
Date: 2021-09-21T14:50:21-07:00
New Revision: 32ab405717ecb92041d8f753a1ccd0e5dd874cb7

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

LOG: [LTO] Emit DebugLoc for dead function in optimization remarks

Currently, the dead functions information getting from optimizations remarks does not contain debug location, but knowing where these dead functions locate could be useful for debugging or for detecting dead code.

Cause in `LTO::addRegularLTO()` we use `BitcodeModule::getLazyModule()` to read the bitcode module, when we pass Function F to `ore::NV()`, F is not materialized, so `F->getSubprogram()` returns nullptr, and there is no debug location information of dead functions in optimizations remarks.

This patch call `F->materialize()` before we pass Function F to `ore::NV()`, then debug location information will be emitted for dead functions in optimization remarks.

Reviewed By: tejohnson

Differential Revision: https://reviews.llvm.org/D109737

Added: 
    

Modified: 
    llvm/include/llvm/LTO/LTO.h
    llvm/lib/LTO/LTO.cpp
    llvm/test/LTO/Resolution/X86/dead-strip-fulllto.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/LTO/LTO.h b/llvm/include/llvm/LTO/LTO.h
index ea1dea2d6f42e..5ec654ef98d78 100644
--- a/llvm/include/llvm/LTO/LTO.h
+++ b/llvm/include/llvm/LTO/LTO.h
@@ -444,6 +444,9 @@ class LTO {
   // Identify symbols exported dynamically, and that therefore could be
   // referenced by a shared library not visible to the linker.
   DenseSet<GlobalValue::GUID> DynamicExportSymbols;
+
+  // Diagnostic optimization remarks file
+  std::unique_ptr<ToolOutputFile> DiagnosticOutputFile;
 };
 
 /// The resolution for a symbol. The linker must provide a SymbolResolution for

diff  --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp
index deb367bd5aa68..dfaf3b6fb79ec 100644
--- a/llvm/lib/LTO/LTO.cpp
+++ b/llvm/lib/LTO/LTO.cpp
@@ -857,10 +857,14 @@ Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
   for (GlobalValue *GV : Mod.Keep) {
     if (LivenessFromIndex && !ThinLTO.CombinedIndex.isGUIDLive(GV->getGUID())) {
       if (Function *F = dyn_cast<Function>(GV)) {
-        OptimizationRemarkEmitter ORE(F, nullptr);
-        ORE.emit(OptimizationRemark(DEBUG_TYPE, "deadfunction", F)
-                 << ore::NV("Function", F)
-                 << " not added to the combined module ");
+        if (DiagnosticOutputFile) {
+          if (Error Err = F->materialize())
+            return Err;
+          OptimizationRemarkEmitter ORE(F, nullptr);
+          ORE.emit(OptimizationRemark(DEBUG_TYPE, "deadfunction", F)
+                   << ore::NV("Function", F)
+                   << " not added to the combined module ");
+        }
       }
       continue;
     }
@@ -1049,6 +1053,7 @@ Error LTO::runRegularLTO(AddStreamFn AddStream) {
       Conf.RemarksHotnessThreshold);
   if (!DiagFileOrErr)
     return DiagFileOrErr.takeError();
+  DiagnosticOutputFile = std::move(*DiagFileOrErr);
 
   // Finalize linking of regular LTO modules containing summaries now that
   // we have computed liveness information.
@@ -1137,7 +1142,7 @@ Error LTO::runRegularLTO(AddStreamFn AddStream) {
       return Err;
   }
 
-  return finalizeOptimizationRemarks(std::move(*DiagFileOrErr));
+  return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
 }
 
 static const char *libcallRoutineNames[] = {

diff  --git a/llvm/test/LTO/Resolution/X86/dead-strip-fulllto.ll b/llvm/test/LTO/Resolution/X86/dead-strip-fulllto.ll
index fb4fde7661b7d..e9a63a1b23aae 100644
--- a/llvm/test/LTO/Resolution/X86/dead-strip-fulllto.ll
+++ b/llvm/test/LTO/Resolution/X86/dead-strip-fulllto.ll
@@ -19,9 +19,11 @@
 
 ; REMARK:      Pass:            lto
 ; REMARK-NEXT: Name:            deadfunction
+; REMARK-NEXT: DebugLoc:        { File: test.c, Line: 4, Column: 0 }
 ; REMARK-NEXT: Function:        dead2
 ; REMARK-NEXT: Args:
 ; REMARK-NEXT:   - Function:        dead2
+; REMARK-NEXT:     DebugLoc:        { File: test.c, Line: 4, Column: 0 }
 ; REMARK-NEXT:   - String:          ' not added to the combined module '
 
 ; FULL-NOT: dead
@@ -48,9 +50,18 @@ define void @live2() {
   ret void
 }
 
-define void @dead2() {
+define void @dead2() !dbg !7 {
   ret void
 }
 
-!0 = !{i32 1, !"ThinLTO", i32 0}
-!llvm.module.flags = !{ !0 }
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4, !5}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: false, emissionKind: LineTablesOnly, enums: !2)
+!1 = !DIFile(filename: "test.c", directory: "/")
+!2 = !{}
+!3 = !{i32 2, !"Dwarf Version", i32 2}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = !{i32 1, !"ThinLTO", i32 0}
+!7 = distinct !DISubprogram(name: "dead2", scope: !1, file: !1, line: 4, type: !8, isLocal: false, isDefinition: true, scopeLine: 4, isOptimized: false, unit: !0, retainedNodes: !2)
+!8 = !DISubroutineType(types: !2)


        


More information about the llvm-commits mailing list