@@ -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+
354361bool 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 ,
0 commit comments