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