Skip to content

Commit cb63ad8

Browse files
Enna1MaskRay
authored andcommitted
[LTO] Fix incomplete optimization remarks for dead functions when PreOptModuleHook or PostInternalizeModuleHook is defined
In 20a895c, 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
1 parent 80c95bb commit cb63ad8

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
; REQUIRES: x86
2+
;; Ensure passing --plugin-opt=emit-llvm to lld, LTO should not emit
3+
;; incomplete optimization remarks for dead functions.
4+
5+
; RUN: split-file %s %t.dir
6+
; RUN: opt -module-summary %t.dir/main.ll -o %t1.o
7+
; RUN: opt -module-summary %t.dir/other.ll -o %t2.o
8+
9+
; RUN: rm -f %t.yaml
10+
; RUN: ld.lld --plugin-opt=emit-llvm --opt-remarks-filename %t.yaml %t1.o %t2.o -o %t
11+
; RUN: FileCheck %s --check-prefix=REMARK < %t.yaml
12+
13+
; REMARK: Pass: lto
14+
; REMARK-NEXT: Name: deadfunction
15+
; REMARK-NEXT: DebugLoc: { File: test.c, Line: 4, Column: 0 }
16+
; REMARK-NEXT: Function: dead2
17+
; REMARK-NEXT: Args:
18+
; REMARK-NEXT: - Function: dead2
19+
; REMARK-NEXT: DebugLoc: { File: test.c, Line: 4, Column: 0 }
20+
; REMARK-NEXT: - String: ' not added to the combined module '
21+
22+
#--- main.ll
23+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
24+
target triple = "x86_64-unknown-linux-gnu"
25+
26+
define void @_start() {
27+
call void @live1()
28+
ret void
29+
}
30+
31+
declare void @live1()
32+
33+
define void @live2() {
34+
ret void
35+
}
36+
37+
define void @dead2() !dbg !7 {
38+
ret void
39+
}
40+
41+
!llvm.dbg.cu = !{!0}
42+
!llvm.module.flags = !{!3, !4, !5}
43+
44+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: false, emissionKind: LineTablesOnly, enums: !2)
45+
!1 = !DIFile(filename: "test.c", directory: "/")
46+
!2 = !{}
47+
!3 = !{i32 2, !"Dwarf Version", i32 2}
48+
!4 = !{i32 2, !"Debug Info Version", i32 3}
49+
!5 = !{i32 1, !"ThinLTO", i32 0}
50+
!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)
51+
!8 = !DISubroutineType(types: !2)
52+
53+
#--- other.ll
54+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
55+
target triple = "x86_64-unknown-linux-gnu"
56+
57+
define void @live1() {
58+
call void @live2()
59+
ret void
60+
}
61+
62+
declare void @live2()
63+
64+
define void @dead1() {
65+
call void @dead2()
66+
ret void
67+
}
68+
69+
declare void @dead2()

llvm/lib/LTO/LTO.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ Error LTO::runRegularLTO(AddStreamFn AddStream) {
11061106

11071107
if (Conf.PreOptModuleHook &&
11081108
!Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
1109-
return Error::success();
1109+
return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
11101110

11111111
if (!Conf.CodeGenOnly) {
11121112
for (const auto &R : GlobalResolutions) {
@@ -1132,7 +1132,7 @@ Error LTO::runRegularLTO(AddStreamFn AddStream) {
11321132

11331133
if (Conf.PostInternalizeModuleHook &&
11341134
!Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
1135-
return Error::success();
1135+
return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
11361136
}
11371137

11381138
if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {

0 commit comments

Comments
 (0)