Skip to content

Commit 799955e

Browse files
[ThinLTO] Skip opt pipeline and summary wrapper pass on empty modules (#120143)
Follow up to PR118508, to avoid unnecessary compile time for an empty combind regular LTO module if all modules end up being ThinLTO only. This required minor changes to a few tests to ensure they weren't empty.
1 parent 90eca3f commit 799955e

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-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: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@ 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() &&
359+
Mod.getModuleInlineAsm().empty();
360+
}
361+
354362
bool lto::opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
355363
bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
356364
const ModuleSummaryIndex *ImportSummary,
@@ -372,9 +380,16 @@ bool lto::opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
372380
/*EmbedBitcode*/ true, /*EmbedCmdline*/ true,
373381
/*Cmdline*/ CmdArgs);
374382
}
375-
// FIXME: Plumb the combined index into the new pass manager.
376-
runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO, ExportSummary,
377-
ImportSummary);
383+
// No need to run any opt passes if the module is empty.
384+
// In theory these passes should take almost no time for an empty
385+
// module, however, this guards against doing any unnecessary summary-based
386+
// analysis in the case of a ThinLTO build where this might be an empty
387+
// regular LTO combined module, with a large combined index from ThinLTO.
388+
if (!isEmptyModule(Mod)) {
389+
// FIXME: Plumb the combined index into the new pass manager.
390+
runNewPMPasses(Conf, Mod, TM, Conf.OptLevel, IsThinLTO, ExportSummary,
391+
ImportSummary);
392+
}
378393
return !Conf.PostOptModuleHook || Conf.PostOptModuleHook(Task, Mod);
379394
}
380395

@@ -422,8 +437,14 @@ static void codegen(const Config &Conf, TargetMachine *TM,
422437
legacy::PassManager CodeGenPasses;
423438
TargetLibraryInfoImpl TLII(Triple(Mod.getTargetTriple()));
424439
CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
425-
CodeGenPasses.add(
426-
createImmutableModuleSummaryIndexWrapperPass(&CombinedIndex));
440+
// No need to make index available if the module is empty.
441+
// In theory these passes should not use the index for an empty
442+
// module, however, this guards against doing any unnecessary summary-based
443+
// analysis in the case of a ThinLTO build where this might be an empty
444+
// regular LTO combined module, with a large combined index from ThinLTO.
445+
if (!isEmptyModule(Mod))
446+
CodeGenPasses.add(
447+
createImmutableModuleSummaryIndexWrapperPass(&CombinedIndex));
427448
if (Conf.PreCodeGenPassesHook)
428449
Conf.PreCodeGenPassesHook(CodeGenPasses);
429450
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)