Skip to content

[Driver][Index] Add driver support to specify an overriding output path to record in the index data #36272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/Basic/FileTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ TYPE("json-features", JSONFeatures, "features.json", "")


TYPE("index-data", IndexData, "", "")
TYPE("index-unit-output-path", IndexUnitOutputPath, "", "")
TYPE("yaml-opt-record", YAMLOptRecord, "opt.yaml", "")
TYPE("bitstream-opt-record",BitstreamOptRecord, "opt.bitstream", "")

Expand Down
10 changes: 9 additions & 1 deletion include/swift/Driver/Job.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ class CommandOutput {

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

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


/// Returns the output file path to record in the index data for each input.
/// The return value will contain one \c StringRef per primary input if any
/// input had an output filename for the index data that was different to its
/// primary output filename, and be empty otherwise.
SmallVector<StringRef, 16> getIndexUnitOutputFilenames() const;

/// Assuming (and asserting) that there are one or more input pairs, associate
/// an additional output named \p OutputFilename of type \p type with the
/// first primary input. If the provided \p type is the primary output type,
Expand Down
1 change: 1 addition & 0 deletions include/swift/Driver/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace driver {
SourceInputActions,
InputJobsAndSourceInputActions,
Output,
IndexUnitOutputPaths,
/// Batch mode frontend invocations may have so many supplementary
/// outputs that they don't comfortably fit as command-line arguments.
/// In that case, add a FilelistInfo to record the path to the file.
Expand Down
3 changes: 3 additions & 0 deletions lib/Basic/FileTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ bool file_types::isTextual(ID Id) {
case file_types::TY_Remapping:
case file_types::TY_IndexData:
case file_types::TY_BitstreamOptRecord:
case file_types::TY_IndexUnitOutputPath:
return false;
case file_types::TY_INVALID:
llvm_unreachable("Invalid type ID.");
Expand Down Expand Up @@ -155,6 +156,7 @@ bool file_types::isAfterLLVM(ID Id) {
case file_types::TY_PrivateSwiftModuleInterfaceFile:
case file_types::TY_JSONDependencies:
case file_types::TY_JSONFeatures:
case file_types::TY_IndexUnitOutputPath:
return false;
case file_types::TY_INVALID:
llvm_unreachable("Invalid type ID.");
Expand Down Expand Up @@ -205,6 +207,7 @@ bool file_types::isPartOfSwiftCompilation(ID Id) {
case file_types::TY_BitstreamOptRecord:
case file_types::TY_JSONDependencies:
case file_types::TY_JSONFeatures:
case file_types::TY_IndexUnitOutputPath:
return false;
case file_types::TY_INVALID:
llvm_unreachable("Invalid type ID.");
Expand Down
18 changes: 13 additions & 5 deletions lib/Driver/Compilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,12 @@ static void writeOutputToFilelist(llvm::raw_fd_ostream &out, const Job *job,
for (auto &output : outputInfo.getPrimaryOutputFilenames())
out << output << "\n";
}
static void writeIndexUnitOutputPathsToFilelist(llvm::raw_fd_ostream &out,
const Job *job) {
const CommandOutput &outputInfo = job->getOutput();
for (auto &output : outputInfo.getIndexUnitOutputFilenames())
out << output << "\n";
}
static void writeSupplementarOutputToFilelist(llvm::raw_fd_ostream &out,
const Job *job) {
job->getOutput().writeOutputFileMap(out);
Expand Down Expand Up @@ -1732,13 +1738,15 @@ static bool writeFilelistIfNecessary(const Job *job, const ArgList &args,
writeInputJobsToFilelist(out, job, filelistInfo.type);
writeSourceInputActionsToFilelist(out, job, args);
break;
case FilelistInfo::WhichFiles::Output: {
case FilelistInfo::WhichFiles::Output:
writeOutputToFilelist(out, job, filelistInfo.type);
break;
}
case FilelistInfo::WhichFiles::SupplementaryOutput:
writeSupplementarOutputToFilelist(out, job);
break;
case FilelistInfo::WhichFiles::IndexUnitOutputPaths:
writeIndexUnitOutputPathsToFilelist(out, job);
break;
case FilelistInfo::WhichFiles::SupplementaryOutput:
writeSupplementarOutputToFilelist(out, job);
break;
}
}
return ok;
Expand Down
33 changes: 30 additions & 3 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,7 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
case file_types::TY_RawSIB:
case file_types::TY_RawSIL:
case file_types::TY_Nothing:
case file_types::TY_IndexUnitOutputPath:
case file_types::TY_INVALID:
llvm_unreachable("these types should never be inferred");
}
Expand Down Expand Up @@ -2507,6 +2508,28 @@ static StringRef baseNameForImage(const JobAction *JA, const OutputInfo &OI,
return Buffer.str();
}

static StringRef getIndexUnitOutputFilename(Compilation &C,
const JobAction *JA,
const TypeToPathMap *OutputMap,
bool AtTopLevel) {
if (JA->getType() == file_types::TY_Nothing)
return {};

if (OutputMap) {
auto iter = OutputMap->find(file_types::TY_IndexUnitOutputPath);
if (iter != OutputMap->end())
return iter->second;
}

if (AtTopLevel) {
const llvm::opt::DerivedArgList &Args = C.getArgs();
if (Arg *FinalOutput = Args.getLastArg(options::OPT_index_unit_output_path))
return FinalOutput->getValue();
}

return {};
}

static StringRef getOutputFilename(Compilation &C,
const JobAction *JA,
const TypeToPathMap *OutputMap,
Expand Down Expand Up @@ -3021,7 +3044,7 @@ void Driver::computeMainOutput(
SmallVectorImpl<const Job *> &InputJobs, const TypeToPathMap *OutputMap,
StringRef workingDirectory, StringRef BaseInput, StringRef PrimaryInput,
llvm::SmallString<128> &Buf, CommandOutput *Output) const {
StringRef OutputFile;
StringRef OutputFile, IndexUnitOutputFile;
if (C.getOutputInfo().isMultiThreading() && isa<CompileJobAction>(JA) &&
file_types::isAfterLLVM(JA->getType())) {
// Multi-threaded compilation: A single frontend command produces multiple
Expand All @@ -3042,8 +3065,10 @@ void Driver::computeMainOutput(

OutputFile = getOutputFilename(C, JA, OMForInput, workingDirectory,
AtTopLevel, Base, Primary, Buf);
IndexUnitOutputFile = getIndexUnitOutputFilename(C, JA, OMForInput,
AtTopLevel);
Output->addPrimaryOutput(CommandInputPair(Base, Primary),
OutputFile);
OutputFile, IndexUnitOutputFile);
};
// Add an output file for each input action.
for (const Action *A : InputActions) {
Expand All @@ -3063,8 +3088,10 @@ void Driver::computeMainOutput(
// The common case: there is a single output file.
OutputFile = getOutputFilename(C, JA, OutputMap, workingDirectory,
AtTopLevel, BaseInput, PrimaryInput, Buf);
IndexUnitOutputFile = getIndexUnitOutputFilename(C, JA, OutputMap,
AtTopLevel);
Output->addPrimaryOutput(CommandInputPair(BaseInput, PrimaryInput),
OutputFile);
OutputFile, IndexUnitOutputFile);
}
}

Expand Down
39 changes: 38 additions & 1 deletion lib/Driver/Job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#include "swift/Basic/Range.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Driver/Job.h"
#include "swift/Driver/PrettyStackTrace.h"
Expand Down Expand Up @@ -145,7 +146,8 @@ file_types::ID CommandOutput::getPrimaryOutputType() const {
}

void CommandOutput::addPrimaryOutput(CommandInputPair Input,
StringRef PrimaryOutputFile) {
StringRef PrimaryOutputFile,
StringRef IndexUnitOutputPath) {
PrettyStackTraceDriverCommandOutputAddition CrashInfo(
"primary", this, Input.Primary, PrimaryOutputType, PrimaryOutputFile);
if (PrimaryOutputType == file_types::TY_Nothing) {
Expand All @@ -165,6 +167,13 @@ void CommandOutput::addPrimaryOutput(CommandInputPair Input,
assert(!PrimaryOutputFile.empty());
assert(AdditionalOutputTypes.count(PrimaryOutputType) == 0);
ensureEntry(Input.Primary, PrimaryOutputType, PrimaryOutputFile, false);

// If there is an overriding output path to record in the index store instead
// of the primary output path, add it.
if (!IndexUnitOutputPath.empty()) {
ensureEntry(Input.Primary, file_types::TY_IndexUnitOutputPath,
IndexUnitOutputPath, false);
}
}

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

SmallVector<StringRef, 16>
CommandOutput::getIndexUnitOutputFilenames() const {
SmallVector<StringRef, 16> V;
size_t NonEmpty = 0;
for (auto const &I: Inputs) {
auto Out = getOutputForInputAndType(I.Primary,
file_types::TY_IndexUnitOutputPath);
V.push_back(Out);
if (!Out.empty())
++NonEmpty;
}

if (!NonEmpty || NonEmpty == V.size()) {
if (!NonEmpty)
V.clear();
return V;
}

auto PrimaryOutputs = getPrimaryOutputFilenames();
assert(PrimaryOutputs.size() == V.size());

for (auto I: indices(V)) {
if (V[I].empty())
V[I] = PrimaryOutputs[I];
}
return V;
}

void CommandOutput::setAdditionalOutputForType(file_types::ID Type,
StringRef OutputFilename) {
PrettyStackTraceDriverCommandOutputAddition CrashInfo(
Expand Down
17 changes: 17 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,17 +498,32 @@ ToolChain::constructInvocation(const CompileJobAction &job,

// Add the output file argument if necessary.
if (context.Output.getPrimaryOutputType() != file_types::TY_Nothing) {
auto IndexUnitOutputs = context.Output.getIndexUnitOutputFilenames();

if (context.shouldUseMainOutputFileListInFrontendInvocation()) {
Arguments.push_back("-output-filelist");
Arguments.push_back(context.getTemporaryFilePath("outputs", ""));
II.FilelistInfos.push_back({Arguments.back(),
context.Output.getPrimaryOutputType(),
FilelistInfo::WhichFiles::Output});
if (!IndexUnitOutputs.empty()) {
Arguments.push_back("-index-unit-output-path-filelist");
Arguments.push_back(context.getTemporaryFilePath("index-unit-outputs",
""));
II.FilelistInfos.push_back({
Arguments.back(), file_types::TY_IndexUnitOutputPath,
FilelistInfo::WhichFiles::IndexUnitOutputPaths});
}
} else {
for (auto FileName : context.Output.getPrimaryOutputFilenames()) {
Arguments.push_back("-o");
Arguments.push_back(context.Args.MakeArgString(FileName));
}

for (auto FileName : IndexUnitOutputs) {
Arguments.push_back("-index-unit-output-path");
Arguments.push_back(context.Args.MakeArgString(FileName));
}
}
}

Expand Down Expand Up @@ -639,6 +654,7 @@ const char *ToolChain::JobContext::computeFrontendModeForCompile() const {
case file_types::TY_SwiftSourceInfoFile:
case file_types::TY_SwiftCrossImportDir:
case file_types::TY_SwiftOverlayFile:
case file_types::TY_IndexUnitOutputPath:
llvm_unreachable("Output type can never be primary output.");
case file_types::TY_INVALID:
llvm_unreachable("Invalid type ID");
Expand Down Expand Up @@ -896,6 +912,7 @@ ToolChain::constructInvocation(const BackendJobAction &job,
case file_types::TY_SwiftSourceInfoFile:
case file_types::TY_SwiftCrossImportDir:
case file_types::TY_SwiftOverlayFile:
case file_types::TY_IndexUnitOutputPath:
llvm_unreachable("Output type can never be primary output.");
case file_types::TY_INVALID:
llvm_unreachable("Invalid type ID");
Expand Down
3 changes: 3 additions & 0 deletions lib/Option/features.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
},
{
"name": "experimental-allow-module-with-compiler-errors"
},
{
"name": "index-unit-output-path"
}
]
}
Loading