@@ -731,7 +731,8 @@ static bool writeAPIDescriptorIfNeeded(CompilerInstance &Instance) {
731731static bool performCompileStepsPostSILGen (
732732 CompilerInstance &Instance, std::unique_ptr<SILModule> SM,
733733 ModuleOrSourceFile MSF, const PrimarySpecificPaths &PSPs, int &ReturnValue,
734- FrontendObserver *observer, ArrayRef<const char *> CommandLineArgs);
734+ FrontendObserver *observer, ArrayRef<const char *> CommandLineArgs,
735+ ArrayRef<PrimarySpecificPaths> auxPSPs = {});
735736
736737bool swift::performCompileStepsPostSema (
737738 CompilerInstance &Instance, int &ReturnValue, FrontendObserver *observer,
@@ -749,14 +750,48 @@ bool swift::performCompileStepsPostSema(
749750 PSPs.SupplementaryOutputs .YAMLOptRecordPath :
750751 PSPs.SupplementaryOutputs .BitstreamOptRecordPath ;
751752 }
753+
754+ auto populateOptRecordPathsFromCmdLine = [&]() {
755+ auto optRecordPaths = collectSupplementaryOutputPaths (
756+ CommandLineArgs, options::OPT_save_optimization_record_path);
757+ if (!optRecordPaths.empty ()) {
758+ // Set the first path. With multiple paths, CompilerInvocation leaves
759+ // OptRecordFile empty, so we populate it here along with aux paths.
760+ SILOpts.OptRecordFile = optRecordPaths[0 ];
761+ if (optRecordPaths.size () > 1 ) {
762+ SILOpts.AuxOptRecordFiles .assign (optRecordPaths.begin () + 1 ,
763+ optRecordPaths.end ());
764+ }
765+ }
766+ };
767+
752768 if (!auxPSPs.empty ()) {
753769 assert (SILOpts.AuxOptRecordFiles .empty ());
770+ // Check if ALL auxPSPs have optimization record paths populated
771+ bool allHaveOptRecordPaths = true ;
754772 for (const auto &auxFile: auxPSPs) {
755- SILOpts.AuxOptRecordFiles .push_back (
756- SILOpts.OptRecordFormat == llvm::remarks::Format::YAML ?
757- auxFile.SupplementaryOutputs .YAMLOptRecordPath :
758- auxFile.SupplementaryOutputs .BitstreamOptRecordPath );
773+ bool hasPath = SILOpts.OptRecordFormat == llvm::remarks::Format::YAML ?
774+ !auxFile.SupplementaryOutputs .YAMLOptRecordPath .empty () :
775+ !auxFile.SupplementaryOutputs .BitstreamOptRecordPath .empty ();
776+ if (!hasPath) {
777+ allHaveOptRecordPaths = false ;
778+ break ;
779+ }
759780 }
781+
782+ if (allHaveOptRecordPaths) {
783+ for (const auto &auxFile: auxPSPs) {
784+ SILOpts.AuxOptRecordFiles .push_back (
785+ SILOpts.OptRecordFormat == llvm::remarks::Format::YAML ?
786+ auxFile.SupplementaryOutputs .YAMLOptRecordPath :
787+ auxFile.SupplementaryOutputs .BitstreamOptRecordPath );
788+ }
789+ } else {
790+ populateOptRecordPathsFromCmdLine ();
791+ }
792+ } else {
793+ assert (SILOpts.AuxOptRecordFiles .empty ());
794+ populateOptRecordPathsFromCmdLine ();
760795 }
761796 return SILOpts;
762797 };
@@ -781,7 +816,7 @@ bool swift::performCompileStepsPostSema(
781816 &irgenOpts);
782817 return performCompileStepsPostSILGen (Instance, std::move (SM), mod, PSPs,
783818 ReturnValue, observer,
784- CommandLineArgs);
819+ CommandLineArgs, auxPSPs );
785820 }
786821
787822
@@ -1983,7 +2018,8 @@ static bool generateCode(CompilerInstance &Instance, StringRef OutputFilename,
19832018static bool performCompileStepsPostSILGen (
19842019 CompilerInstance &Instance, std::unique_ptr<SILModule> SM,
19852020 ModuleOrSourceFile MSF, const PrimarySpecificPaths &PSPs, int &ReturnValue,
1986- FrontendObserver *observer, ArrayRef<const char *> CommandLineArgs) {
2021+ FrontendObserver *observer, ArrayRef<const char *> CommandLineArgs,
2022+ ArrayRef<PrimarySpecificPaths> auxPSPs) {
19872023 const auto &Invocation = Instance.getInvocation ();
19882024 const auto &opts = Invocation.getFrontendOptions ();
19892025 FrontendOptions::ActionType Action = opts.RequestedAction ;
@@ -2156,10 +2192,38 @@ static bool performCompileStepsPostSILGen(
21562192 std::vector<std::string> ParallelOutputFilenames =
21572193 opts.InputsAndOutputs .copyOutputFilenames ();
21582194
2159- // Collect IR output paths from command line arguments
2160- std::vector<std::string> ParallelIROutputFilenames =
2161- collectSupplementaryOutputPaths (CommandLineArgs,
2162- options::OPT_ir_output_path);
2195+ // Collect IR output paths - check if supplementary output file map has paths,
2196+ // otherwise fall back to command line arguments
2197+ std::vector<std::string> ParallelIROutputFilenames;
2198+ if (!auxPSPs.empty ()) {
2199+ // Check if the first file (PSPs) and ALL auxPSPs have IR output paths populated
2200+ bool allHaveIRPaths = !PSPs.SupplementaryOutputs .LLVMIROutputPath .empty ();
2201+ if (allHaveIRPaths) {
2202+ for (const auto &auxFile : auxPSPs) {
2203+ if (auxFile.SupplementaryOutputs .LLVMIROutputPath .empty ()) {
2204+ allHaveIRPaths = false ;
2205+ break ;
2206+ }
2207+ }
2208+ }
2209+
2210+ if (allHaveIRPaths) {
2211+ // Paths are in supplementary output file map - include first file + aux files
2212+ ParallelIROutputFilenames.push_back (PSPs.SupplementaryOutputs .LLVMIROutputPath );
2213+ for (const auto &auxFile : auxPSPs) {
2214+ ParallelIROutputFilenames.push_back (
2215+ auxFile.SupplementaryOutputs .LLVMIROutputPath );
2216+ }
2217+ } else {
2218+ // Fall back to command line arguments
2219+ ParallelIROutputFilenames = collectSupplementaryOutputPaths (
2220+ CommandLineArgs, options::OPT_ir_output_path);
2221+ }
2222+ } else {
2223+ // No auxPSPs, use command line arguments
2224+ ParallelIROutputFilenames = collectSupplementaryOutputPaths (
2225+ CommandLineArgs, options::OPT_ir_output_path);
2226+ }
21632227
21642228 llvm::GlobalVariable *HashGlobal;
21652229 cas::SwiftCASOutputBackend *casBackend =
@@ -2310,10 +2374,13 @@ collectSupplementaryOutputPaths(ArrayRef<const char *> Args,
23102374 StringRef arg = Args[i];
23112375 StringRef optionName;
23122376
2313- if (OptionID == options::OPT_sil_output_path) {
2314- optionName = " -sil-output-path" ;
2315- } else if (OptionID == options::OPT_ir_output_path) {
2377+ // Note: SIL is NOT included here because in WMO mode, SIL is generated once
2378+ // per module, not per source file. Only IR and optimization records can have
2379+ // per-file outputs in multi-threaded WMO.
2380+ if (OptionID == options::OPT_ir_output_path) {
23162381 optionName = " -ir-output-path" ;
2382+ } else if (OptionID == options::OPT_save_optimization_record_path) {
2383+ optionName = " -save-optimization-record-path" ;
23172384 } else {
23182385 continue ;
23192386 }
0 commit comments