@@ -351,6 +351,14 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
351
351
MPM.run (Mod, MAM);
352
352
}
353
353
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
+
354
362
bool lto::opt (const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
355
363
bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
356
364
const ModuleSummaryIndex *ImportSummary,
@@ -372,9 +380,16 @@ bool lto::opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
372
380
/* EmbedBitcode*/ true , /* EmbedCmdline*/ true ,
373
381
/* Cmdline*/ CmdArgs);
374
382
}
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
+ }
378
393
return !Conf.PostOptModuleHook || Conf.PostOptModuleHook (Task, Mod);
379
394
}
380
395
@@ -422,8 +437,14 @@ static void codegen(const Config &Conf, TargetMachine *TM,
422
437
legacy::PassManager CodeGenPasses;
423
438
TargetLibraryInfoImpl TLII (Triple (Mod.getTargetTriple ()));
424
439
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));
427
448
if (Conf.PreCodeGenPassesHook )
428
449
Conf.PreCodeGenPassesHook (CodeGenPasses);
429
450
if (TM->addPassesToEmitFile (CodeGenPasses, *Stream->OS ,
0 commit comments