Skip to content

Commit e82fd4a

Browse files
authored
Merge pull request swiftlang#36272 from nathawes/update-driver-for-index-unit-output-path
2 parents a3cd7fb + 117f03b commit e82fd4a

File tree

11 files changed

+334
-11
lines changed

11 files changed

+334
-11
lines changed

include/swift/Basic/FileTypes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ TYPE("json-features", JSONFeatures, "features.json", "")
7979

8080

8181
TYPE("index-data", IndexData, "", "")
82+
TYPE("index-unit-output-path", IndexUnitOutputPath, "", "")
8283
TYPE("yaml-opt-record", YAMLOptRecord, "opt.yaml", "")
8384
TYPE("bitstream-opt-record",BitstreamOptRecord, "opt.bitstream", "")
8485

include/swift/Driver/Job.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ class CommandOutput {
157157

158158
/// Associate a new \p PrimaryOutputFile (of type \c getPrimaryOutputType())
159159
/// with the provided \p Input pair of Base and Primary inputs.
160-
void addPrimaryOutput(CommandInputPair Input, StringRef PrimaryOutputFile);
160+
void addPrimaryOutput(CommandInputPair Input, StringRef PrimaryOutputFile,
161+
StringRef IndexUnitOutputPath);
161162

162163
/// Return true iff the set of additional output types in \c this is
163164
/// identical to the set of additional output types in \p other.
@@ -184,6 +185,13 @@ class CommandOutput {
184185
/// assumed at several call sites.
185186
SmallVector<StringRef, 16> getPrimaryOutputFilenames() const;
186187

188+
189+
/// Returns the output file path to record in the index data for each input.
190+
/// The return value will contain one \c StringRef per primary input if any
191+
/// input had an output filename for the index data that was different to its
192+
/// primary output filename, and be empty otherwise.
193+
SmallVector<StringRef, 16> getIndexUnitOutputFilenames() const;
194+
187195
/// Assuming (and asserting) that there are one or more input pairs, associate
188196
/// an additional output named \p OutputFilename of type \p type with the
189197
/// first primary input. If the provided \p type is the primary output type,

include/swift/Driver/Util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace driver {
4949
SourceInputActions,
5050
InputJobsAndSourceInputActions,
5151
Output,
52+
IndexUnitOutputPaths,
5253
/// Batch mode frontend invocations may have so many supplementary
5354
/// outputs that they don't comfortably fit as command-line arguments.
5455
/// In that case, add a FilelistInfo to record the path to the file.

lib/Basic/FileTypes.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ bool file_types::isTextual(ID Id) {
105105
case file_types::TY_Remapping:
106106
case file_types::TY_IndexData:
107107
case file_types::TY_BitstreamOptRecord:
108+
case file_types::TY_IndexUnitOutputPath:
108109
return false;
109110
case file_types::TY_INVALID:
110111
llvm_unreachable("Invalid type ID.");
@@ -155,6 +156,7 @@ bool file_types::isAfterLLVM(ID Id) {
155156
case file_types::TY_PrivateSwiftModuleInterfaceFile:
156157
case file_types::TY_JSONDependencies:
157158
case file_types::TY_JSONFeatures:
159+
case file_types::TY_IndexUnitOutputPath:
158160
return false;
159161
case file_types::TY_INVALID:
160162
llvm_unreachable("Invalid type ID.");
@@ -205,6 +207,7 @@ bool file_types::isPartOfSwiftCompilation(ID Id) {
205207
case file_types::TY_BitstreamOptRecord:
206208
case file_types::TY_JSONDependencies:
207209
case file_types::TY_JSONFeatures:
210+
case file_types::TY_IndexUnitOutputPath:
208211
return false;
209212
case file_types::TY_INVALID:
210213
llvm_unreachable("Invalid type ID.");

lib/Driver/Compilation.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,12 @@ static void writeOutputToFilelist(llvm::raw_fd_ostream &out, const Job *job,
16991699
for (auto &output : outputInfo.getPrimaryOutputFilenames())
17001700
out << output << "\n";
17011701
}
1702+
static void writeIndexUnitOutputPathsToFilelist(llvm::raw_fd_ostream &out,
1703+
const Job *job) {
1704+
const CommandOutput &outputInfo = job->getOutput();
1705+
for (auto &output : outputInfo.getIndexUnitOutputFilenames())
1706+
out << output << "\n";
1707+
}
17021708
static void writeSupplementarOutputToFilelist(llvm::raw_fd_ostream &out,
17031709
const Job *job) {
17041710
job->getOutput().writeOutputFileMap(out);
@@ -1732,13 +1738,15 @@ static bool writeFilelistIfNecessary(const Job *job, const ArgList &args,
17321738
writeInputJobsToFilelist(out, job, filelistInfo.type);
17331739
writeSourceInputActionsToFilelist(out, job, args);
17341740
break;
1735-
case FilelistInfo::WhichFiles::Output: {
1741+
case FilelistInfo::WhichFiles::Output:
17361742
writeOutputToFilelist(out, job, filelistInfo.type);
17371743
break;
1738-
}
1739-
case FilelistInfo::WhichFiles::SupplementaryOutput:
1740-
writeSupplementarOutputToFilelist(out, job);
1741-
break;
1744+
case FilelistInfo::WhichFiles::IndexUnitOutputPaths:
1745+
writeIndexUnitOutputPathsToFilelist(out, job);
1746+
break;
1747+
case FilelistInfo::WhichFiles::SupplementaryOutput:
1748+
writeSupplementarOutputToFilelist(out, job);
1749+
break;
17421750
}
17431751
}
17441752
return ok;

lib/Driver/Driver.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,7 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
20422042
case file_types::TY_RawSIB:
20432043
case file_types::TY_RawSIL:
20442044
case file_types::TY_Nothing:
2045+
case file_types::TY_IndexUnitOutputPath:
20452046
case file_types::TY_INVALID:
20462047
llvm_unreachable("these types should never be inferred");
20472048
}
@@ -2507,6 +2508,28 @@ static StringRef baseNameForImage(const JobAction *JA, const OutputInfo &OI,
25072508
return Buffer.str();
25082509
}
25092510

2511+
static StringRef getIndexUnitOutputFilename(Compilation &C,
2512+
const JobAction *JA,
2513+
const TypeToPathMap *OutputMap,
2514+
bool AtTopLevel) {
2515+
if (JA->getType() == file_types::TY_Nothing)
2516+
return {};
2517+
2518+
if (OutputMap) {
2519+
auto iter = OutputMap->find(file_types::TY_IndexUnitOutputPath);
2520+
if (iter != OutputMap->end())
2521+
return iter->second;
2522+
}
2523+
2524+
if (AtTopLevel) {
2525+
const llvm::opt::DerivedArgList &Args = C.getArgs();
2526+
if (Arg *FinalOutput = Args.getLastArg(options::OPT_index_unit_output_path))
2527+
return FinalOutput->getValue();
2528+
}
2529+
2530+
return {};
2531+
}
2532+
25102533
static StringRef getOutputFilename(Compilation &C,
25112534
const JobAction *JA,
25122535
const TypeToPathMap *OutputMap,
@@ -3021,7 +3044,7 @@ void Driver::computeMainOutput(
30213044
SmallVectorImpl<const Job *> &InputJobs, const TypeToPathMap *OutputMap,
30223045
StringRef workingDirectory, StringRef BaseInput, StringRef PrimaryInput,
30233046
llvm::SmallString<128> &Buf, CommandOutput *Output) const {
3024-
StringRef OutputFile;
3047+
StringRef OutputFile, IndexUnitOutputFile;
30253048
if (C.getOutputInfo().isMultiThreading() && isa<CompileJobAction>(JA) &&
30263049
file_types::isAfterLLVM(JA->getType())) {
30273050
// Multi-threaded compilation: A single frontend command produces multiple
@@ -3042,8 +3065,10 @@ void Driver::computeMainOutput(
30423065

30433066
OutputFile = getOutputFilename(C, JA, OMForInput, workingDirectory,
30443067
AtTopLevel, Base, Primary, Buf);
3068+
IndexUnitOutputFile = getIndexUnitOutputFilename(C, JA, OMForInput,
3069+
AtTopLevel);
30453070
Output->addPrimaryOutput(CommandInputPair(Base, Primary),
3046-
OutputFile);
3071+
OutputFile, IndexUnitOutputFile);
30473072
};
30483073
// Add an output file for each input action.
30493074
for (const Action *A : InputActions) {
@@ -3063,8 +3088,10 @@ void Driver::computeMainOutput(
30633088
// The common case: there is a single output file.
30643089
OutputFile = getOutputFilename(C, JA, OutputMap, workingDirectory,
30653090
AtTopLevel, BaseInput, PrimaryInput, Buf);
3091+
IndexUnitOutputFile = getIndexUnitOutputFilename(C, JA, OutputMap,
3092+
AtTopLevel);
30663093
Output->addPrimaryOutput(CommandInputPair(BaseInput, PrimaryInput),
3067-
OutputFile);
3094+
OutputFile, IndexUnitOutputFile);
30683095
}
30693096
}
30703097

lib/Driver/Job.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "swift/Basic/Range.h"
1314
#include "swift/Basic/STLExtras.h"
1415
#include "swift/Driver/Job.h"
1516
#include "swift/Driver/PrettyStackTrace.h"
@@ -145,7 +146,8 @@ file_types::ID CommandOutput::getPrimaryOutputType() const {
145146
}
146147

147148
void CommandOutput::addPrimaryOutput(CommandInputPair Input,
148-
StringRef PrimaryOutputFile) {
149+
StringRef PrimaryOutputFile,
150+
StringRef IndexUnitOutputPath) {
149151
PrettyStackTraceDriverCommandOutputAddition CrashInfo(
150152
"primary", this, Input.Primary, PrimaryOutputType, PrimaryOutputFile);
151153
if (PrimaryOutputType == file_types::TY_Nothing) {
@@ -165,6 +167,13 @@ void CommandOutput::addPrimaryOutput(CommandInputPair Input,
165167
assert(!PrimaryOutputFile.empty());
166168
assert(AdditionalOutputTypes.count(PrimaryOutputType) == 0);
167169
ensureEntry(Input.Primary, PrimaryOutputType, PrimaryOutputFile, false);
170+
171+
// If there is an overriding output path to record in the index store instead
172+
// of the primary output path, add it.
173+
if (!IndexUnitOutputPath.empty()) {
174+
ensureEntry(Input.Primary, file_types::TY_IndexUnitOutputPath,
175+
IndexUnitOutputPath, false);
176+
}
168177
}
169178

170179
StringRef CommandOutput::getPrimaryOutputFilename() const {
@@ -188,6 +197,34 @@ SmallVector<StringRef, 16> CommandOutput::getPrimaryOutputFilenames() const {
188197
return V;
189198
}
190199

200+
SmallVector<StringRef, 16>
201+
CommandOutput::getIndexUnitOutputFilenames() const {
202+
SmallVector<StringRef, 16> V;
203+
size_t NonEmpty = 0;
204+
for (auto const &I: Inputs) {
205+
auto Out = getOutputForInputAndType(I.Primary,
206+
file_types::TY_IndexUnitOutputPath);
207+
V.push_back(Out);
208+
if (!Out.empty())
209+
++NonEmpty;
210+
}
211+
212+
if (!NonEmpty || NonEmpty == V.size()) {
213+
if (!NonEmpty)
214+
V.clear();
215+
return V;
216+
}
217+
218+
auto PrimaryOutputs = getPrimaryOutputFilenames();
219+
assert(PrimaryOutputs.size() == V.size());
220+
221+
for (auto I: indices(V)) {
222+
if (V[I].empty())
223+
V[I] = PrimaryOutputs[I];
224+
}
225+
return V;
226+
}
227+
191228
void CommandOutput::setAdditionalOutputForType(file_types::ID Type,
192229
StringRef OutputFilename) {
193230
PrettyStackTraceDriverCommandOutputAddition CrashInfo(

lib/Driver/ToolChains.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,17 +498,32 @@ ToolChain::constructInvocation(const CompileJobAction &job,
498498

499499
// Add the output file argument if necessary.
500500
if (context.Output.getPrimaryOutputType() != file_types::TY_Nothing) {
501+
auto IndexUnitOutputs = context.Output.getIndexUnitOutputFilenames();
502+
501503
if (context.shouldUseMainOutputFileListInFrontendInvocation()) {
502504
Arguments.push_back("-output-filelist");
503505
Arguments.push_back(context.getTemporaryFilePath("outputs", ""));
504506
II.FilelistInfos.push_back({Arguments.back(),
505507
context.Output.getPrimaryOutputType(),
506508
FilelistInfo::WhichFiles::Output});
509+
if (!IndexUnitOutputs.empty()) {
510+
Arguments.push_back("-index-unit-output-path-filelist");
511+
Arguments.push_back(context.getTemporaryFilePath("index-unit-outputs",
512+
""));
513+
II.FilelistInfos.push_back({
514+
Arguments.back(), file_types::TY_IndexUnitOutputPath,
515+
FilelistInfo::WhichFiles::IndexUnitOutputPaths});
516+
}
507517
} else {
508518
for (auto FileName : context.Output.getPrimaryOutputFilenames()) {
509519
Arguments.push_back("-o");
510520
Arguments.push_back(context.Args.MakeArgString(FileName));
511521
}
522+
523+
for (auto FileName : IndexUnitOutputs) {
524+
Arguments.push_back("-index-unit-output-path");
525+
Arguments.push_back(context.Args.MakeArgString(FileName));
526+
}
512527
}
513528
}
514529

@@ -639,6 +654,7 @@ const char *ToolChain::JobContext::computeFrontendModeForCompile() const {
639654
case file_types::TY_SwiftSourceInfoFile:
640655
case file_types::TY_SwiftCrossImportDir:
641656
case file_types::TY_SwiftOverlayFile:
657+
case file_types::TY_IndexUnitOutputPath:
642658
llvm_unreachable("Output type can never be primary output.");
643659
case file_types::TY_INVALID:
644660
llvm_unreachable("Invalid type ID");
@@ -896,6 +912,7 @@ ToolChain::constructInvocation(const BackendJobAction &job,
896912
case file_types::TY_SwiftSourceInfoFile:
897913
case file_types::TY_SwiftCrossImportDir:
898914
case file_types::TY_SwiftOverlayFile:
915+
case file_types::TY_IndexUnitOutputPath:
899916
llvm_unreachable("Output type can never be primary output.");
900917
case file_types::TY_INVALID:
901918
llvm_unreachable("Invalid type ID");

lib/Option/features.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
},
66
{
77
"name": "experimental-allow-module-with-compiler-errors"
8+
},
9+
{
10+
"name": "index-unit-output-path"
811
}
912
]
1013
}

0 commit comments

Comments
 (0)