Skip to content

Commit 9cc534a

Browse files
author
David Ungar
authored
Merge pull request #14702 from davidungar/PR-18-7-OutputsInInputs
[Batch mode]: Move SupplementaryOutputs into each InputFile and use the proper supplementary output. (7)
2 parents 22922c0 + 4866df6 commit 9cc534a

23 files changed

+489
-378
lines changed

include/swift/Basic/PrimarySpecificPaths.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,39 @@
1515

1616
#include "swift/Basic/LLVM.h"
1717
#include "swift/Basic/SupplementaryOutputPaths.h"
18+
#include "llvm/ADT/StringRef.h"
1819

1920
#include <string>
2021

2122
namespace swift {
23+
24+
/// Holds all of the output paths, and debugging-info path that are
25+
/// specific to which primary file is being compiled at the moment.
26+
2227
class PrimarySpecificPaths {
2328
public:
29+
/// The name of the main output file,
30+
/// that is, the .o file for this input. If there is no such file, contains an
31+
/// empty string. If the output is to be written to stdout, contains "-".
2432
std::string OutputFilename;
33+
2534
SupplementaryOutputPaths SupplementaryOutputs;
2635

2736
/// The name of the "main" input file, used by the debug info.
2837
std::string MainInputFilenameForDebugInfo;
2938

30-
PrimarySpecificPaths(
31-
std::string OutputFilename = std::string(),
32-
std::string MainInputFilenameForDebugInfo = std::string(),
33-
SupplementaryOutputPaths SupplementaryOutputs =
34-
SupplementaryOutputPaths())
39+
PrimarySpecificPaths(StringRef OutputFilename = StringRef(),
40+
StringRef MainInputFilenameForDebugInfo = StringRef(),
41+
SupplementaryOutputPaths SupplementaryOutputs =
42+
SupplementaryOutputPaths())
3543
: OutputFilename(OutputFilename),
3644
SupplementaryOutputs(SupplementaryOutputs),
3745
MainInputFilenameForDebugInfo(MainInputFilenameForDebugInfo) {}
46+
47+
bool haveModuleOrModuleDocOutputPaths() const {
48+
return !SupplementaryOutputs.ModuleOutputPath.empty() ||
49+
!SupplementaryOutputs.ModuleDocOutputPath.empty();
50+
}
3851
};
3952
} // namespace swift
4053

include/swift/Basic/SupplementaryOutputPaths.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,40 @@
2121
namespace swift {
2222
struct SupplementaryOutputPaths {
2323
/// The path to which we should emit an Objective-C header for the module.
24+
/// Currently only makes sense when the compiler has whole module knowledge.
25+
/// The modes for which it makes sense incuide both WMO and the "merge
26+
/// modules" job that happens after the normal compilation jobs. That's where
27+
/// the header is emitted in single-file mode, since it needs whole-module
28+
/// information.
29+
2430
std::string ObjCHeaderOutputPath;
2531

2632
/// The path to which we should emit a serialized module.
33+
/// It is valid whenever there are any inputs.
2734
std::string ModuleOutputPath;
2835

2936
/// The path to which we should emit a module documentation file.
37+
/// It is valid whenever there are any inputs.
3038
std::string ModuleDocOutputPath;
3139

3240
/// The path to which we should output a Make-style dependencies file.
41+
/// It is valid whenever there are any inputs.
3342
std::string DependenciesFilePath;
3443

3544
/// The path to which we should output a Swift reference dependencies file.
45+
/// It is valid whenever there are any inputs.
3646
std::string ReferenceDependenciesFilePath;
3747

3848
/// Path to a file which should contain serialized diagnostics for this
3949
/// frontend invocation.
4050
std::string SerializedDiagnosticsPath;
4151

4252
/// The path to which we should output a loaded module trace file.
53+
/// It is currently only used with WMO, but could be generalized.
4354
std::string LoadedModuleTracePath;
4455

4556
/// The path to which we should output a TBD file.
57+
/// It is currently only used with WMO, but could be generalized.
4658
std::string TBDPath;
4759

4860
SupplementaryOutputPaths() = default;

include/swift/Frontend/ArgsToFrontendOutputsConverter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ArgsToFrontendOutputsConverter {
4343
Diags(diags) {}
4444

4545
bool convert(std::vector<std::string> &mainOutputs,
46-
SupplementaryOutputPaths &supplementaryOutputs);
46+
std::vector<SupplementaryOutputPaths> &supplementaryOutputs);
4747

4848
/// Try to read an output file list file.
4949
/// \returns `None` if it could not open the filelist.
@@ -57,7 +57,7 @@ class OutputFilesComputer {
5757
const FrontendInputsAndOutputs &InputsAndOutputs;
5858
const std::vector<std::string> OutputFileArguments;
5959
const std::string OutputDirectoryArgument;
60-
const StringRef FirstInput;
60+
const std::string FirstInput;
6161
const FrontendOptions::ActionType RequestedAction;
6262
const llvm::opt::Arg *const ModuleNameArg;
6363
const StringRef Suffix;

include/swift/Frontend/Frontend.h

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,6 @@ class CompilerInvocation {
174174
return SearchPathOpts.SDKPath;
175175
}
176176

177-
void setSerializedDiagnosticsPath(StringRef Path) {
178-
FrontendOpts.InputsAndOutputs.supplementaryOutputs()
179-
.SerializedDiagnosticsPath = Path;
180-
}
181-
StringRef getSerializedDiagnosticsPath() const {
182-
return FrontendOpts.InputsAndOutputs.supplementaryOutputs()
183-
.SerializedDiagnosticsPath;
184-
}
185-
186177
LangOptions &getLangOptions() {
187178
return LangOpts;
188179
}
@@ -245,8 +236,7 @@ class CompilerInvocation {
245236
return FrontendOpts.ModuleName;
246237
}
247238

248-
249-
StringRef getOutputFilename() const {
239+
std::string getOutputFilename() const {
250240
return FrontendOpts.InputsAndOutputs.getSingleOutputFilename();
251241
}
252242

@@ -302,11 +292,25 @@ class CompilerInvocation {
302292
return FrontendOpts.InputKind == InputFileKind::IFK_Swift_Library;
303293
}
304294

305-
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary() const;
306-
PrimarySpecificPaths
295+
const PrimarySpecificPaths &
296+
getPrimarySpecificPathsForAtMostOnePrimary() const;
297+
const PrimarySpecificPaths &
307298
getPrimarySpecificPathsForPrimary(StringRef filename) const;
308-
PrimarySpecificPaths
299+
const PrimarySpecificPaths &
309300
getPrimarySpecificPathsForSourceFile(const SourceFile &SF) const;
301+
302+
std::string getOutputFilenameForAtMostOnePrimary() const;
303+
std::string getMainInputFilenameForDebugInfoForAtMostOnePrimary() const;
304+
std::string getObjCHeaderOutputPathForAtMostOnePrimary() const;
305+
std::string getModuleOutputPathForAtMostOnePrimary() const;
306+
std::string
307+
getReferenceDependenciesFilePathForPrimary(StringRef filename) const;
308+
std::string getSerializedDiagnosticsPathForAtMostOnePrimary() const;
309+
310+
/// TBDPath only makes sense in whole module compilation mode,
311+
/// so return the TBDPath when in that mode and fail an assert
312+
/// if not in that mode.
313+
std::string getTBDPathForWholeModule() const;
310314
};
311315

312316
/// A class which manages the state and execution of the compiler.
@@ -584,12 +588,13 @@ class CompilerInstance {
584588
void finishTypeChecking(OptionSet<TypeCheckingFlags> TypeCheckOptions);
585589

586590
public:
587-
PrimarySpecificPaths
591+
const PrimarySpecificPaths &
588592
getPrimarySpecificPathsForWholeModuleOptimizationMode() const;
589-
PrimarySpecificPaths
593+
const PrimarySpecificPaths &
590594
getPrimarySpecificPathsForPrimary(StringRef filename) const;
591-
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary() const;
592-
PrimarySpecificPaths
595+
const PrimarySpecificPaths &
596+
getPrimarySpecificPathsForAtMostOnePrimary() const;
597+
const PrimarySpecificPaths &
593598
getPrimarySpecificPathsForSourceFile(const SourceFile &SF) const;
594599
};
595600

include/swift/Frontend/FrontendInputsAndOutputs.h

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class FrontendInputsAndOutputs {
3535
friend class ArgsToFrontendInputsConverter;
3636

3737
std::vector<InputFile> AllInputs;
38-
39-
llvm::StringMap<unsigned> PrimaryInputs;
38+
llvm::StringMap<unsigned> PrimaryInputsByName;
39+
std::vector<unsigned> PrimaryInputsInOrder;
4040

4141
/// In Single-threaded WMO mode, all inputs are used
4242
/// both for importing and compiling.
@@ -45,25 +45,10 @@ class FrontendInputsAndOutputs {
4545
/// Punt where needed to enable batch mode experiments.
4646
bool AreBatchModeChecksBypassed = false;
4747

48-
SupplementaryOutputPaths SupplementaryOutputs;
49-
5048
public:
5149
bool areBatchModeChecksBypassed() const { return AreBatchModeChecksBypassed; }
5250
void setBypassBatchModeChecks(bool bbc) { AreBatchModeChecksBypassed = bbc; }
5351

54-
const SupplementaryOutputPaths &supplementaryOutputs() const {
55-
return SupplementaryOutputs;
56-
}
57-
SupplementaryOutputPaths &supplementaryOutputs() {
58-
return SupplementaryOutputs;
59-
}
60-
61-
/// When performing a compilation for zero or one primary input file,
62-
/// this will hold the PrimarySpecificPaths.
63-
/// In a future PR, each InputFile will hold its own PrimarySpecificPaths and
64-
/// this will go away.
65-
PrimarySpecificPaths PrimarySpecificPathsForAtMostOnePrimary;
66-
6752
FrontendInputsAndOutputs() = default;
6853
FrontendInputsAndOutputs(const FrontendInputsAndOutputs &other);
6954
FrontendInputsAndOutputs &operator=(const FrontendInputsAndOutputs &other);
@@ -88,6 +73,9 @@ class FrontendInputsAndOutputs {
8873

8974
std::vector<std::string> getInputFilenames() const;
9075

76+
/// \return nullptr if not a primary input file.
77+
const InputFile *primaryInputNamed(StringRef name) const;
78+
9179
unsigned inputCount() const { return AllInputs.size(); }
9280

9381
bool hasInputs() const { return !AllInputs.empty(); }
@@ -99,21 +87,23 @@ class FrontendInputsAndOutputs {
9987

10088
const InputFile &lastInput() const { return AllInputs.back(); }
10189

102-
StringRef getFilenameOfFirstInput() const;
90+
const std::string &getFilenameOfFirstInput() const;
10391

10492
bool isReadingFromStdin() const;
10593

106-
void forEachInput(llvm::function_ref<void(const InputFile &)> fn) const;
94+
/// If \p fn returns true, exits early and returns true.
95+
bool forEachInput(llvm::function_ref<bool(const InputFile &)> fn) const;
10796

10897
// Primaries:
10998

11099
const InputFile &firstPrimaryInput() const;
111100
const InputFile &lastPrimaryInput() const;
112101

113-
void
114-
forEachPrimaryInput(llvm::function_ref<void(const InputFile &)> fn) const;
102+
/// If \p fn returns true, exit early and return true.
103+
bool
104+
forEachPrimaryInput(llvm::function_ref<bool(const InputFile &)> fn) const;
115105

116-
unsigned primaryInputCount() const { return PrimaryInputs.size(); }
106+
unsigned primaryInputCount() const { return PrimaryInputsInOrder.size(); }
117107

118108
// Primary count readers:
119109

@@ -143,11 +133,8 @@ class FrontendInputsAndOutputs {
143133

144134
const InputFile &getRequiredUniquePrimaryInput() const;
145135

146-
/// \return the name of the unique primary input, or an empty StrinRef if
147-
/// there isn't one.
148-
StringRef getNameOfUniquePrimaryInputFile() const;
149-
150-
/// Combines all primaries for stats reporter
136+
/// FIXME: Should combine all primaries for the result
137+
/// instead of just answering "batch" if there is more than one primary.
151138
std::string getStatsFileMangledInputName() const;
152139

153140
bool isInputPrimary(StringRef file) const;
@@ -181,9 +168,9 @@ class FrontendInputsAndOutputs {
181168
private:
182169
friend class ArgsToFrontendOptionsConverter;
183170

184-
void
185-
setMainAndSupplementaryOutputs(ArrayRef<std::string> outputFiles,
186-
SupplementaryOutputPaths supplementaryOutputs);
171+
void setMainAndSupplementaryOutputs(
172+
ArrayRef<std::string> outputFiles,
173+
ArrayRef<SupplementaryOutputPaths> supplementaryOutputs);
187174

188175
public:
189176
unsigned countOfInputsProducingMainOutputs() const;
@@ -198,17 +185,18 @@ class FrontendInputsAndOutputs {
198185
/// Under single-threaded WMO, we pretend that the first input
199186
/// generates the main output, even though it will include code
200187
/// generated from all of them.
201-
void forEachInputProducingAMainOutputFile(
202-
llvm::function_ref<void(const InputFile &)> fn) const;
188+
///
189+
/// If \p fn returns true, return early and return true.
190+
bool forEachInputProducingAMainOutputFile(
191+
llvm::function_ref<bool(const InputFile &)> fn) const;
203192

204193
std::vector<std::string> copyOutputFilenames() const;
205194

206-
void
207-
forEachOutputFilename(llvm::function_ref<void(const std::string &)> fn) const;
195+
void forEachOutputFilename(llvm::function_ref<void(StringRef)> fn) const;
208196

209197
/// Gets the name of the specified output filename.
210198
/// If multiple files are specified, the last one is returned.
211-
StringRef getSingleOutputFilename() const;
199+
std::string getSingleOutputFilename() const;
212200

213201
bool isOutputFilenameStdout() const;
214202
bool isOutputFileDirectory() const;
@@ -218,17 +206,22 @@ class FrontendInputsAndOutputs {
218206

219207
unsigned countOfFilesProducingSupplementaryOutput() const;
220208

221-
void forEachInputProducingSupplementaryOutput(
222-
llvm::function_ref<void(const InputFile &)> fn) const;
209+
/// If \p fn returns true, exit early and return true.
210+
bool forEachInputProducingSupplementaryOutput(
211+
llvm::function_ref<bool(const InputFile &)> fn) const;
223212

224213
/// Assumes there is not more than one primary input file, if any.
225214
/// Otherwise, you would need to call getPrimarySpecificPathsForPrimary
226215
/// to tell it which primary input you wanted the outputs for.
216+
const PrimarySpecificPaths &
217+
getPrimarySpecificPathsForAtMostOnePrimary() const;
227218

228-
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary() const;
219+
const PrimarySpecificPaths &
220+
getPrimarySpecificPathsForPrimary(StringRef) const;
229221

230-
PrimarySpecificPaths
231-
getPrimarySpecificPathsForPrimary(StringRef filename) const;
222+
bool hasSupplementaryOutputPath(
223+
llvm::function_ref<const std::string &(const SupplementaryOutputPaths &)>
224+
extractorFn) const;
232225

233226
bool hasDependenciesPath() const;
234227
bool hasReferenceDependenciesPath() const;

include/swift/Frontend/FrontendOptions.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class FrontendOptions {
3939
InputFileKind InputKind = InputFileKind::IFK_Swift;
4040

4141
void forAllOutputPaths(const InputFile &input,
42-
std::function<void(const std::string &)> fn) const;
42+
std::function<void(StringRef)> fn) const;
4343

4444
bool isOutputFileDirectory() const;
4545

@@ -273,17 +273,17 @@ class FrontendOptions {
273273
return llvm::hash_value(0);
274274
}
275275

276-
StringRef originalPath() const;
277-
278276
StringRef determineFallbackModuleName() const;
279277

280278
bool isCompilingExactlyOneSwiftFile() const {
281279
return InputKind == InputFileKind::IFK_Swift &&
282280
InputsAndOutputs.hasSingleInput();
283281
}
284282

285-
PrimarySpecificPaths getPrimarySpecificPathsForAtMostOnePrimary() const;
286-
PrimarySpecificPaths getPrimarySpecificPathsForPrimary(StringRef) const;
283+
const PrimarySpecificPaths &
284+
getPrimarySpecificPathsForAtMostOnePrimary() const;
285+
const PrimarySpecificPaths &
286+
getPrimarySpecificPathsForPrimary(StringRef) const;
287287

288288
private:
289289
static bool canActionEmitDependencies(ActionType);

0 commit comments

Comments
 (0)