[llvm] cb63ad8 - [LTO] Fix incomplete optimization remarks for dead functions when PreOptModuleHook or PostInternalizeModuleHook is defined

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 20 18:16:13 PST 2021


Author: Xu Mingjie
Date: 2021-12-20T18:16:09-08:00
New Revision: cb63ad8d1d8c5d4d3b50a616067e1aac9ce1eda8

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

LOG: [LTO] Fix incomplete optimization remarks for dead functions when PreOptModuleHook or PostInternalizeModuleHook is defined

In 20a895c4be01769a37dfffb3c6b513a7bc9b8d17, we introduce `finalizeOptimizationRemarks()` to make sure we flush the diagnostic remarks file in case the linker doesn't call the global destructors before exiting.
In https://reviews.llvm.org/D73597, we add optimization remarks for removed functions for debugging or for detecting dead code.
But there is a case, if PreOptModuleHook or PostInternalizeModuleHook is defined (e.g. `--plugin-opt=emit-llvm` is passed to linker), we do not call `finalizeOptimizationRemarks()`, therefore we will get an incomplete optimization remarks file.
This patch make sure we flush the diagnostic remarks file when PreOptModuleHook or PostInternalizeModuleHook is defined.

Reviewed By: tejohnson, MaskRay

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

Added: 
    lld/test/ELF/lto/opt-remarks-incomplete.ll

Modified: 
    llvm/lib/LTO/LTO.cpp

Removed: 
    


################################################################################
diff  --git a/lld/test/ELF/lto/opt-remarks-incomplete.ll b/lld/test/ELF/lto/opt-remarks-incomplete.ll
new file mode 100644
index 000000000000..a8d6112cb4cf
--- /dev/null
+++ b/lld/test/ELF/lto/opt-remarks-incomplete.ll
@@ -0,0 +1,69 @@
+; REQUIRES: x86
+;; Ensure passing --plugin-opt=emit-llvm to lld, LTO should not emit
+;; incomplete optimization remarks for dead functions.
+
+; RUN: split-file %s %t.dir
+; RUN: opt -module-summary %t.dir/main.ll -o %t1.o
+; RUN: opt -module-summary %t.dir/other.ll -o %t2.o
+
+; RUN: rm -f %t.yaml
+; RUN: ld.lld --plugin-opt=emit-llvm --opt-remarks-filename %t.yaml %t1.o %t2.o -o %t
+; RUN: FileCheck %s --check-prefix=REMARK < %t.yaml
+
+; 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 '
+
+#--- main.ll
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @_start() {
+  call void @live1()
+  ret void
+}
+
+declare void @live1()
+
+define void @live2() {
+  ret void
+}
+
+define void @dead2() !dbg !7 {
+  ret void
+}
+
+!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)
+
+#--- other.ll
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @live1() {
+  call void @live2()
+  ret void
+}
+
+declare void @live2()
+
+define void @dead1() {
+  call void @dead2()
+  ret void
+}
+
+declare void @dead2()

diff  --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp
index 6ce2ed265739..f26ef4b21996 100644
--- a/llvm/lib/LTO/LTO.cpp
+++ b/llvm/lib/LTO/LTO.cpp
@@ -1106,7 +1106,7 @@ Error LTO::runRegularLTO(AddStreamFn AddStream) {
 
   if (Conf.PreOptModuleHook &&
       !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
-    return Error::success();
+    return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
 
   if (!Conf.CodeGenOnly) {
     for (const auto &R : GlobalResolutions) {
@@ -1132,7 +1132,7 @@ Error LTO::runRegularLTO(AddStreamFn AddStream) {
 
     if (Conf.PostInternalizeModuleHook &&
         !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
-      return Error::success();
+      return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
   }
 
   if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {


        


More information about the llvm-commits mailing list