Skip to content

Commit

Permalink
[Driver] Add output file to properties of Command
Browse files Browse the repository at this point in the history
Object of class `Command` contains various properties of a command to
execute, but output file was missed from them. This change adds this
property. It is required for reporting consumed time and memory implemented
in D78903 and may be used in other cases too.

Differential Revision: https://reviews.llvm.org/D78902
  • Loading branch information
spavloff committed Oct 8, 2020
1 parent 06758c6 commit 70bf350
Show file tree
Hide file tree
Showing 35 changed files with 214 additions and 132 deletions.
22 changes: 17 additions & 5 deletions clang/include/clang/Driver/Job.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class Command {
/// The list of program arguments which are inputs.
llvm::opt::ArgStringList InputFilenames;

/// The list of program arguments which are outputs. May be empty.
std::vector<std::string> OutputFilenames;

/// Response file name, if this command is set to use one, or nullptr
/// otherwise
const char *ResponseFile = nullptr;
Expand Down Expand Up @@ -158,8 +161,8 @@ class Command {

Command(const Action &Source, const Tool &Creator,
ResponseFileSupport ResponseSupport, const char *Executable,
const llvm::opt::ArgStringList &Arguments,
ArrayRef<InputInfo> Inputs);
const llvm::opt::ArgStringList &Arguments, ArrayRef<InputInfo> Inputs,
ArrayRef<InputInfo> Outputs = None);
// FIXME: This really shouldn't be copyable, but is currently copied in some
// error handling in Driver::generateCompilationDiagnostics.
Command(const Command &) = default;
Expand Down Expand Up @@ -201,6 +204,14 @@ class Command {

const llvm::opt::ArgStringList &getArguments() const { return Arguments; }

const llvm::opt::ArgStringList &getInputFilenames() const {
return InputFilenames;
}

const std::vector<std::string> &getOutputFilenames() const {
return OutputFilenames;
}

protected:
/// Optionally print the filenames to be compiled
void PrintFileNames() const;
Expand All @@ -212,7 +223,7 @@ class CC1Command : public Command {
CC1Command(const Action &Source, const Tool &Creator,
ResponseFileSupport ResponseSupport, const char *Executable,
const llvm::opt::ArgStringList &Arguments,
ArrayRef<InputInfo> Inputs);
ArrayRef<InputInfo> Inputs, ArrayRef<InputInfo> Outputs = None);

void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
CrashReportInfo *CrashInfo = nullptr) const override;
Expand All @@ -230,7 +241,7 @@ class FallbackCommand : public Command {
FallbackCommand(const Action &Source_, const Tool &Creator_,
ResponseFileSupport ResponseSupport, const char *Executable_,
const llvm::opt::ArgStringList &Arguments_,
ArrayRef<InputInfo> Inputs,
ArrayRef<InputInfo> Inputs, ArrayRef<InputInfo> Outputs,
std::unique_ptr<Command> Fallback_);

void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
Expand All @@ -250,7 +261,8 @@ class ForceSuccessCommand : public Command {
ResponseFileSupport ResponseSupport,
const char *Executable_,
const llvm::opt::ArgStringList &Arguments_,
ArrayRef<InputInfo> Inputs);
ArrayRef<InputInfo> Inputs,
ArrayRef<InputInfo> Outputs = None);

void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
CrashReportInfo *CrashInfo = nullptr) const override;
Expand Down
18 changes: 12 additions & 6 deletions clang/lib/Driver/Job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ using namespace driver;
Command::Command(const Action &Source, const Tool &Creator,
ResponseFileSupport ResponseSupport, const char *Executable,
const llvm::opt::ArgStringList &Arguments,
ArrayRef<InputInfo> Inputs)
ArrayRef<InputInfo> Inputs, ArrayRef<InputInfo> Outputs)
: Source(Source), Creator(Creator), ResponseSupport(ResponseSupport),
Executable(Executable), Arguments(Arguments) {
for (const auto &II : Inputs)
if (II.isFilename())
InputFilenames.push_back(II.getFilename());
for (const auto &II : Outputs)
if (II.isFilename())
OutputFilenames.push_back(II.getFilename());
}

/// Check if the compiler flag in question should be skipped when
Expand Down Expand Up @@ -357,8 +360,9 @@ CC1Command::CC1Command(const Action &Source, const Tool &Creator,
ResponseFileSupport ResponseSupport,
const char *Executable,
const llvm::opt::ArgStringList &Arguments,
ArrayRef<InputInfo> Inputs)
: Command(Source, Creator, ResponseSupport, Executable, Arguments, Inputs) {
ArrayRef<InputInfo> Inputs, ArrayRef<InputInfo> Outputs)
: Command(Source, Creator, ResponseSupport, Executable, Arguments, Inputs,
Outputs) {
InProcess = true;
}

Expand Down Expand Up @@ -415,9 +419,10 @@ FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
const char *Executable_,
const llvm::opt::ArgStringList &Arguments_,
ArrayRef<InputInfo> Inputs,
ArrayRef<InputInfo> Outputs,
std::unique_ptr<Command> Fallback_)
: Command(Source_, Creator_, ResponseSupport, Executable_, Arguments_,
Inputs),
Inputs, Outputs),
Fallback(std::move(Fallback_)) {}

void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
Expand Down Expand Up @@ -456,9 +461,10 @@ int FallbackCommand::Execute(ArrayRef<llvm::Optional<StringRef>> Redirects,
ForceSuccessCommand::ForceSuccessCommand(
const Action &Source_, const Tool &Creator_,
ResponseFileSupport ResponseSupport, const char *Executable_,
const llvm::opt::ArgStringList &Arguments_, ArrayRef<InputInfo> Inputs)
const llvm::opt::ArgStringList &Arguments_, ArrayRef<InputInfo> Inputs,
ArrayRef<InputInfo> Outputs)
: Command(Source_, Creator_, ResponseSupport, Executable_, Arguments_,
Inputs) {}
Inputs, Outputs) {}

void ForceSuccessCommand::Print(raw_ostream &OS, const char *Terminator,
bool Quote, CrashReportInfo *CrashInfo) const {
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Driver/ToolChains/AIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,

const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
Exec, CmdArgs, Inputs));
Exec, CmdArgs, Inputs, Output));
}

void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
Expand Down Expand Up @@ -170,7 +170,7 @@ void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,

const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
Exec, CmdArgs, Inputs));
Exec, CmdArgs, Inputs, Output));
}

/// AIX - AIX tool chain which can call as(1) and ld(1) directly.
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Driver/ToolChains/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("-shared");
CmdArgs.push_back("-o");
CmdArgs.push_back(Output.getFilename());
C.addCommand(
std::make_unique<Command>(JA, *this, ResponseFileSupport::AtFileCurCP(),
Args.MakeArgString(Linker), CmdArgs, Inputs));
C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
CmdArgs, Inputs, Output));
}

void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Driver/ToolChains/AVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back(Args.MakeArgString(std::string("-m") + *FamilyName));
}

C.addCommand(
std::make_unique<Command>(JA, *this, ResponseFileSupport::AtFileCurCP(),
Args.MakeArgString(Linker), CmdArgs, Inputs));
C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
CmdArgs, Inputs, Output));
}

llvm::Optional<std::string> AVRToolChain::findAVRLibcInstallation() const {
Expand Down
10 changes: 6 additions & 4 deletions clang/lib/Driver/ToolChains/Ananas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ void ananas::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back(II.getFilename());

const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::AtFileCurCP(), Exec, CmdArgs, Inputs));
C.addCommand(std::make_unique<Command>(JA, *this,
ResponseFileSupport::AtFileCurCP(),
Exec, CmdArgs, Inputs, Output));
}

void ananas::Linker::ConstructJob(Compilation &C, const JobAction &JA,
Expand Down Expand Up @@ -124,8 +125,9 @@ void ananas::Linker::ConstructJob(Compilation &C, const JobAction &JA,
}

const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::AtFileCurCP(), Exec, CmdArgs, Inputs));
C.addCommand(std::make_unique<Command>(JA, *this,
ResponseFileSupport::AtFileCurCP(),
Exec, CmdArgs, Inputs, Output));
}

// Ananas - Ananas tool chain which can call as(1) and ld(1) directly.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Driver/ToolChains/BareMetal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,5 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,

C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
Args.MakeArgString(TC.GetLinkerPath()),
CmdArgs, Inputs));
CmdArgs, Inputs, Output));
}
32 changes: 18 additions & 14 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4356,9 +4356,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
II.getInputArg().renderAsInput(Args, CmdArgs);
}

C.addCommand(
std::make_unique<Command>(JA, *this, ResponseFileSupport::AtFileUTF8(),
D.getClangProgramPath(), CmdArgs, Inputs));
C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::AtFileUTF8(), D.getClangProgramPath(),
CmdArgs, Inputs, Output));
return;
}

Expand Down Expand Up @@ -6314,20 +6314,23 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
getCLFallback()->GetCommand(C, JA, Output, Inputs, Args, LinkingOutput);
C.addCommand(std::make_unique<FallbackCommand>(
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs,
std::move(CLCommand)));
Output, std::move(CLCommand)));
} else if (Args.hasArg(options::OPT__SLASH_fallback) &&
isa<PrecompileJobAction>(JA)) {
// In /fallback builds, run the main compilation even if the pch generation
// fails, so that the main compilation's fallback to cl.exe runs.
C.addCommand(std::make_unique<ForceSuccessCommand>(
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs));
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs,
Output));
} else if (D.CC1Main && !D.CCGenDiagnostics) {
// Invoke the CC1 directly in this process
C.addCommand(std::make_unique<CC1Command>(
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs));
C.addCommand(std::make_unique<CC1Command>(JA, *this,
ResponseFileSupport::AtFileUTF8(),
Exec, CmdArgs, Inputs, Output));
} else {
C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs));
C.addCommand(std::make_unique<Command>(JA, *this,
ResponseFileSupport::AtFileUTF8(),
Exec, CmdArgs, Inputs, Output));
}

// Make the compile command echo its inputs for /showFilenames.
Expand Down Expand Up @@ -7074,8 +7077,9 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back(Input.getFilename());

const char *Exec = getToolChain().getDriver().getClangProgramPath();
C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs));
C.addCommand(std::make_unique<Command>(JA, *this,
ResponseFileSupport::AtFileUTF8(),
Exec, CmdArgs, Inputs, Output));
}

// Begin OffloadBundler
Expand Down Expand Up @@ -7161,7 +7165,7 @@ void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA,
C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::None(),
TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
CmdArgs, None));
CmdArgs, None, Output));
}

void OffloadBundler::ConstructJobMultipleOutputs(
Expand Down Expand Up @@ -7227,7 +7231,7 @@ void OffloadBundler::ConstructJobMultipleOutputs(
C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::None(),
TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
CmdArgs, None));
CmdArgs, None, Outputs));
}

void OffloadWrapper::ConstructJob(Compilation &C, const JobAction &JA,
Expand Down Expand Up @@ -7257,5 +7261,5 @@ void OffloadWrapper::ConstructJob(Compilation &C, const JobAction &JA,
C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::None(),
Args.MakeArgString(getToolChain().GetProgramPath(getShortName())),
CmdArgs, Inputs));
CmdArgs, Inputs, Output));
}
5 changes: 3 additions & 2 deletions clang/lib/Driver/ToolChains/CloudABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ void cloudabi::Linker::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));

const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::AtFileCurCP(), Exec, CmdArgs, Inputs));
C.addCommand(std::make_unique<Command>(JA, *this,
ResponseFileSupport::AtFileCurCP(),
Exec, CmdArgs, Inputs, Output));
}

// CloudABI - CloudABI tool chain which can call ld(1) directly.
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,12 +951,13 @@ void tools::SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T,
InputInfo II(types::TY_Object, Output.getFilename(), Output.getFilename());

// First extract the dwo sections.
C.addCommand(std::make_unique<Command>(
JA, T, ResponseFileSupport::AtFileCurCP(), Exec, ExtractArgs, II));
C.addCommand(std::make_unique<Command>(JA, T,
ResponseFileSupport::AtFileCurCP(),
Exec, ExtractArgs, II, Output));

// Then remove them from the original .o file.
C.addCommand(std::make_unique<Command>(
JA, T, ResponseFileSupport::AtFileCurCP(), Exec, StripArgs, II));
JA, T, ResponseFileSupport::AtFileCurCP(), Exec, StripArgs, II, Output));
}

// Claim options we don't want to warn if they are unused. We do this for
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/Driver/ToolChains/CrossWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void tools::CrossWindows::Assembler::ConstructJob(
Exec = Args.MakeArgString(Assembler);

C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
Exec, CmdArgs, Inputs));
Exec, CmdArgs, Inputs, Output));
}

void tools::CrossWindows::Linker::ConstructJob(
Expand Down Expand Up @@ -203,8 +203,9 @@ void tools::CrossWindows::Linker::ConstructJob(

Exec = Args.MakeArgString(TC.GetLinkerPath());

C.addCommand(std::make_unique<Command>(
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs));
C.addCommand(std::make_unique<Command>(JA, *this,
ResponseFileSupport::AtFileUTF8(),
Exec, CmdArgs, Inputs, Output));
}

CrossWindowsToolChain::CrossWindowsToolChain(const Driver &D,
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Driver/ToolChains/Cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
JA, *this,
ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,
"--options-file"},
Exec, CmdArgs, Inputs));
Exec, CmdArgs, Inputs, Output));
}

static bool shouldIncludePTX(const ArgList &Args, const char *gpu_arch) {
Expand Down Expand Up @@ -496,7 +496,7 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
JA, *this,
ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,
"--options-file"},
Exec, CmdArgs, Inputs));
Exec, CmdArgs, Inputs, Output));
}

void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA,
Expand Down Expand Up @@ -577,7 +577,7 @@ void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA,
JA, *this,
ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,
"--options-file"},
Exec, CmdArgs, Inputs));
Exec, CmdArgs, Inputs, Output));
}

/// CUDA toolchain. Our assembler is ptxas, and our "linker" is fatbinary,
Expand Down
Loading

0 comments on commit 70bf350

Please sign in to comment.