Skip to content

Commit 599cde6

Browse files
committed
[ThinLTO] Skip opt pipeline and summary wrapper pass on empty modules
Follow up to PR118508, to avoid unnecessary compile time for an empty combind regular LTO module if all modules end up being ThinLTO only.
1 parent 56ab56c commit 599cde6

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

lld/test/ELF/lto/new-pass-manager.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99

1010
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
1111
target triple = "x86_64-unknown-linux-gnu"
12+
13+
define void @foo() {
14+
ret void
15+
}

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,13 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
351351
MPM.run(Mod, MAM);
352352
}
353353

354+
static bool isEmptyModule(const Module &Mod) {
355+
// Module is empty if it has no functions, no globals, no inline asm and no
356+
// named metadata (aliases and ifuncs require functions or globals so we
357+
// don't need to check those explicitly).
358+
return Mod.empty() && Mod.global_empty() && Mod.named_metadata_empty() && Mod.getModuleInlineAsm().empty();
359+
}
360+
354361
bool lto::opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
355362
bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
356363
const ModuleSummaryIndex *ImportSummary,
@@ -372,9 +379,16 @@ bool lto::opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
372379
/*EmbedBitcode*/ true, /*EmbedCmdline*/ true,
373380
/*Cmdline*/ CmdArgs);
374381
}
375-
// FIXME: Plumb the combined index into the new pass manager.
376-
runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO, ExportSummary,
377-
ImportSummary);
382+
// No need to run any opt passes if the module is empty.
383+
// In theory these passes should take almost no time for an empty
384+
// module, however, this guards against doing any unnecessary summary-based
385+
// analysis in the case of a ThinLTO build where this might be an empty
386+
// regular LTO combined module, with a large combined index from ThinLTO.
387+
if (!isEmptyModule(Mod)) {
388+
// FIXME: Plumb the combined index into the new pass manager.
389+
runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO, ExportSummary,
390+
ImportSummary);
391+
}
378392
return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
379393
}
380394

@@ -422,8 +436,14 @@ static void codegen(const Config &Conf, TargetMachine *TM,
422436
legacy::PassManager CodeGenPasses;
423437
TargetLibraryInfoImpl TLII(Triple(Mod.getTargetTriple()));
424438
CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
425-
CodeGenPasses.add(
426-
createImmutableModuleSummaryIndexWrapperPass(&CombinedIndex));
439+
// No need to make index available if the module is empty.
440+
// In theory these passes should not use the index for an empty
441+
// module, however, this guards against doing any unnecessary summary-based
442+
// analysis in the case of a ThinLTO build where this might be an empty
443+
// regular LTO combined module, with a large combined index from ThinLTO.
444+
if (!isEmptyModule(Mod))
445+
CodeGenPasses.add(
446+
createImmutableModuleSummaryIndexWrapperPass(&CombinedIndex));
427447
if (Conf.PreCodeGenPassesHook)
428448
Conf.PreCodeGenPassesHook(CodeGenPasses);
429449
if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS,

llvm/test/Feature/load_plugin_error.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
; RUN: opt %s -o %t.o
77
; RUN: not llvm-lto2 run -load-pass-plugin=%t/nonexistent.so %t.o -o %t \
8-
; RUN: -r %t.o,test 2>&1 | \
8+
; RUN: -r %t.o,test,plx 2>&1 | \
99
; RUN: FileCheck %s
1010

1111
; CHECK: Could not load library {{.*}}nonexistent.so

llvm/test/Other/X86/lto-hot-cold-split.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
1010
target triple = "x86_64-unknown-linux-gnu"
1111

1212
; OLDPM-ANYLTO-POSTLINK-Os: HotColdSplittingPass
13+
14+
define void @foo() {
15+
ret void
16+
}

0 commit comments

Comments
 (0)