Skip to content

Commit d3ba531

Browse files
committed
[frontend] Support passing the optimization record file via the supplemental outputs map
The Swift driver is passing the optimization record file path via the supplementals output, instead of the flag, on certain circumstances. Enhance the frontend to check supplemental outputs otherwise the record file will not get emitted when using the new swift driver.
1 parent bc8657b commit d3ba531

File tree

6 files changed

+91
-7
lines changed

6 files changed

+91
-7
lines changed

include/swift/Basic/SupplementaryOutputPaths.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ struct SupplementaryOutputPaths {
146146
/// The output path to generate ABI baseline.
147147
std::string ABIDescriptorOutputPath;
148148

149+
/// The output path for YAML optimization record file.
150+
std::string YAMLOptRecordPath;
151+
152+
/// The output path for bitstream optimization record file.
153+
std::string BitstreamOptRecordPath;
154+
149155
SupplementaryOutputPaths() = default;
150156
SupplementaryOutputPaths(const SupplementaryOutputPaths &) = default;
151157

@@ -179,6 +185,10 @@ struct SupplementaryOutputPaths {
179185
fn(ModuleSummaryOutputPath);
180186
if (!ABIDescriptorOutputPath.empty())
181187
fn(ABIDescriptorOutputPath);
188+
if (!YAMLOptRecordPath.empty())
189+
fn(YAMLOptRecordPath);
190+
if (!BitstreamOptRecordPath.empty())
191+
fn(BitstreamOptRecordPath);
182192
}
183193

184194
bool empty() const {
@@ -187,7 +197,8 @@ struct SupplementaryOutputPaths {
187197
ReferenceDependenciesFilePath.empty() &&
188198
SerializedDiagnosticsPath.empty() && LoadedModuleTracePath.empty() &&
189199
TBDPath.empty() && ModuleInterfaceOutputPath.empty() &&
190-
ModuleSourceInfoOutputPath.empty() && ABIDescriptorOutputPath.empty();
200+
ModuleSourceInfoOutputPath.empty() && ABIDescriptorOutputPath.empty() &&
201+
YAMLOptRecordPath.empty() && BitstreamOptRecordPath.empty();
191202
}
192203
};
193204
} // namespace swift

include/swift/Frontend/FrontendInputsAndOutputs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ class FrontendInputsAndOutputs {
259259
bool hasABIDescriptorOutputPath() const;
260260
bool hasModuleSummaryOutputPath() const;
261261
bool hasTBDPath() const;
262+
bool hasYAMLOptRecordPath() const;
263+
bool hasBitstreamOptRecordPath() const;
262264

263265
bool hasDependencyTrackerPath() const;
264266
};

lib/Frontend/ArgsToFrontendOutputsConverter.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,14 @@ SupplementaryOutputPathsComputer::getSupplementaryOutputPathsFromArguments()
341341
options::OPT_emit_module_summary_path);
342342
auto abiDescriptorOutput = getSupplementaryFilenamesFromArguments(
343343
options::OPT_emit_abi_descriptor_path);
344+
auto optRecordOutput = getSupplementaryFilenamesFromArguments(
345+
options::OPT_save_optimization_record_path);
344346
if (!objCHeaderOutput || !moduleOutput || !moduleDocOutput ||
345347
!dependenciesFile || !referenceDependenciesFile ||
346348
!serializedDiagnostics || !fixItsOutput || !loadedModuleTrace || !TBD ||
347349
!moduleInterfaceOutput || !privateModuleInterfaceOutput ||
348-
!moduleSourceInfoOutput || !moduleSummaryOutput || !abiDescriptorOutput) {
350+
!moduleSourceInfoOutput || !moduleSummaryOutput || !abiDescriptorOutput ||
351+
!optRecordOutput) {
349352
return None;
350353
}
351354
std::vector<SupplementaryOutputPaths> result;
@@ -368,6 +371,8 @@ SupplementaryOutputPathsComputer::getSupplementaryOutputPathsFromArguments()
368371
sop.ModuleSourceInfoOutputPath = (*moduleSourceInfoOutput)[i];
369372
sop.ModuleSummaryOutputPath = (*moduleSummaryOutput)[i];
370373
sop.ABIDescriptorOutputPath = (*abiDescriptorOutput)[i];
374+
sop.YAMLOptRecordPath = (*optRecordOutput)[i];
375+
sop.BitstreamOptRecordPath = (*optRecordOutput)[i];
371376
result.push_back(sop);
372377
}
373378
return result;
@@ -479,6 +484,15 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(
479484
file_types::TY_SwiftModuleFile, mainOutputIfUsableForModule,
480485
defaultSupplementaryOutputPathExcludingExtension);
481486

487+
auto YAMLOptRecordPath = determineSupplementaryOutputFilename(
488+
OPT_save_optimization_record_path, pathsFromArguments.YAMLOptRecordPath,
489+
file_types::TY_YAMLOptRecord, "",
490+
defaultSupplementaryOutputPathExcludingExtension);
491+
auto bitstreamOptRecordPath = determineSupplementaryOutputFilename(
492+
OPT_save_optimization_record_path, pathsFromArguments.BitstreamOptRecordPath,
493+
file_types::TY_BitstreamOptRecord, "",
494+
defaultSupplementaryOutputPathExcludingExtension);
495+
482496
SupplementaryOutputPaths sop;
483497
sop.ObjCHeaderOutputPath = objcHeaderOutputPath;
484498
sop.ModuleOutputPath = moduleOutputPath;
@@ -494,6 +508,8 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(
494508
sop.ModuleSourceInfoOutputPath = moduleSourceInfoOutputPath;
495509
sop.ModuleSummaryOutputPath = moduleSummaryOutputPath;
496510
sop.ABIDescriptorOutputPath = ABIDescriptorOutputPath;
511+
sop.YAMLOptRecordPath = YAMLOptRecordPath;
512+
sop.BitstreamOptRecordPath = bitstreamOptRecordPath;
497513
return sop;
498514
}
499515

@@ -576,6 +592,8 @@ createFromTypeToPathMap(const TypeToPathMap *map) {
576592
{file_types::TY_SwiftModuleSummaryFile, paths.ModuleSummaryOutputPath},
577593
{file_types::TY_PrivateSwiftModuleInterfaceFile,
578594
paths.PrivateModuleInterfaceOutputPath},
595+
{file_types::TY_YAMLOptRecord, paths.YAMLOptRecordPath},
596+
{file_types::TY_BitstreamOptRecord, paths.BitstreamOptRecordPath},
579597
};
580598
for (const std::pair<file_types::ID, std::string &> &typeAndString :
581599
typesAndStrings) {

lib/Frontend/FrontendInputsAndOutputs.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,18 @@ bool FrontendInputsAndOutputs::hasTBDPath() const {
521521
return outs.TBDPath;
522522
});
523523
}
524+
bool FrontendInputsAndOutputs::hasYAMLOptRecordPath() const {
525+
return hasSupplementaryOutputPath(
526+
[](const SupplementaryOutputPaths &outs) -> const std::string & {
527+
return outs.YAMLOptRecordPath;
528+
});
529+
}
530+
bool FrontendInputsAndOutputs::hasBitstreamOptRecordPath() const {
531+
return hasSupplementaryOutputPath(
532+
[](const SupplementaryOutputPaths &outs) -> const std::string & {
533+
return outs.BitstreamOptRecordPath;
534+
});
535+
}
524536

525537
bool FrontendInputsAndOutputs::hasDependencyTrackerPath() const {
526538
return hasDependenciesPath() || hasReferenceDependenciesPath() ||

lib/FrontendTool/FrontendTool.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,16 +695,28 @@ static bool performCompileStepsPostSema(CompilerInstance &Instance,
695695
int &ReturnValue,
696696
FrontendObserver *observer) {
697697
const auto &Invocation = Instance.getInvocation();
698-
const SILOptions &SILOpts = Invocation.getSILOptions();
699698
const FrontendOptions &opts = Invocation.getFrontendOptions();
700699

700+
auto getSILOptions = [&](const PrimarySpecificPaths &PSPs) -> SILOptions {
701+
SILOptions SILOpts = Invocation.getSILOptions();
702+
if (SILOpts.OptRecordFile.empty()) {
703+
// Check if the record file path was passed via supplemental outputs.
704+
SILOpts.OptRecordFile = SILOpts.OptRecordFormat ==
705+
llvm::remarks::Format::YAML ?
706+
PSPs.SupplementaryOutputs.YAMLOptRecordPath :
707+
PSPs.SupplementaryOutputs.BitstreamOptRecordPath;
708+
}
709+
return SILOpts;
710+
};
711+
701712
auto *mod = Instance.getMainModule();
702713
if (!opts.InputsAndOutputs.hasPrimaryInputs()) {
703714
// If there are no primary inputs the compiler is in WMO mode and builds one
704715
// SILModule for the entire module.
705-
auto SM = performASTLowering(mod, Instance.getSILTypes(), SILOpts);
706716
const PrimarySpecificPaths PSPs =
707717
Instance.getPrimarySpecificPathsForWholeModuleOptimizationMode();
718+
SILOptions SILOpts = getSILOptions(PSPs);
719+
auto SM = performASTLowering(mod, Instance.getSILTypes(), SILOpts);
708720
return performCompileStepsPostSILGen(Instance, std::move(SM), mod, PSPs,
709721
ReturnValue, observer);
710722
}
@@ -714,10 +726,11 @@ static bool performCompileStepsPostSema(CompilerInstance &Instance,
714726
if (!Instance.getPrimarySourceFiles().empty()) {
715727
bool result = false;
716728
for (auto *PrimaryFile : Instance.getPrimarySourceFiles()) {
717-
auto SM = performASTLowering(*PrimaryFile, Instance.getSILTypes(),
718-
SILOpts);
719729
const PrimarySpecificPaths PSPs =
720730
Instance.getPrimarySpecificPathsForSourceFile(*PrimaryFile);
731+
SILOptions SILOpts = getSILOptions(PSPs);
732+
auto SM = performASTLowering(*PrimaryFile, Instance.getSILTypes(),
733+
SILOpts);
721734
result |= performCompileStepsPostSILGen(Instance, std::move(SM),
722735
PrimaryFile, PSPs, ReturnValue,
723736
observer);
@@ -732,9 +745,10 @@ static bool performCompileStepsPostSema(CompilerInstance &Instance,
732745
for (FileUnit *fileUnit : mod->getFiles()) {
733746
if (auto SASTF = dyn_cast<SerializedASTFile>(fileUnit))
734747
if (opts.InputsAndOutputs.isInputPrimary(SASTF->getFilename())) {
735-
auto SM = performASTLowering(*SASTF, Instance.getSILTypes(), SILOpts);
736748
const PrimarySpecificPaths &PSPs =
737749
Instance.getPrimarySpecificPathsForPrimary(SASTF->getFilename());
750+
SILOptions SILOpts = getSILOptions(PSPs);
751+
auto SM = performASTLowering(*SASTF, Instance.getSILTypes(), SILOpts);
738752
result |= performCompileStepsPostSILGen(Instance, std::move(SM), mod,
739753
PSPs, ReturnValue, observer);
740754
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: echo '"%s": { yaml-opt-record: "%t/foo.opt.yaml" }' > %t/filemap.yaml.yaml
3+
// RUN: echo '"%s": { bitstream-opt-record: "%t/foo.opt.bitstream" }' > %t/filemap.bitstream.yaml
4+
5+
// RUN: %target-swift-frontend -c -O -wmo -save-optimization-record=bitstream %s -module-name foo -o %t/foo.o -supplementary-output-file-map %t/filemap.bitstream.yaml
6+
// RUN: llvm-bcanalyzer -dump "%t/foo.opt.bitstream" | %FileCheck -check-prefix=BITSTREAM %s
7+
8+
// RUN: %target-swift-frontend -c -O -wmo -save-optimization-record=yaml %s -module-name foo -o %t/foo.o -supplementary-output-file-map %t/filemap.yaml.yaml
9+
// RUN: %FileCheck %s -check-prefix=YAML --input-file=%t/foo.opt.yaml
10+
11+
// REQUIRES: VENDOR=apple
12+
13+
var a: Int = 1
14+
15+
#sourceLocation(file: "custom.swift", line: 2000)
16+
func foo() {
17+
a = 2
18+
}
19+
#sourceLocation() // reset
20+
21+
public func bar() {
22+
foo()
23+
// BITSTREAM: <Remark NumWords=13 BlockCodeSize=4>
24+
// BITSTREAM: </Remark>
25+
26+
// YAML: sil-assembly-vision-remark-gen
27+
}

0 commit comments

Comments
 (0)