Skip to content

Commit 70bf350

Browse files
committed
[Driver] Add output file to properties of Command
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
1 parent 06758c6 commit 70bf350

35 files changed

+214
-132
lines changed

clang/include/clang/Driver/Job.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ class Command {
122122
/// The list of program arguments which are inputs.
123123
llvm::opt::ArgStringList InputFilenames;
124124

125+
/// The list of program arguments which are outputs. May be empty.
126+
std::vector<std::string> OutputFilenames;
127+
125128
/// Response file name, if this command is set to use one, or nullptr
126129
/// otherwise
127130
const char *ResponseFile = nullptr;
@@ -158,8 +161,8 @@ class Command {
158161

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

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

207+
const llvm::opt::ArgStringList &getInputFilenames() const {
208+
return InputFilenames;
209+
}
210+
211+
const std::vector<std::string> &getOutputFilenames() const {
212+
return OutputFilenames;
213+
}
214+
204215
protected:
205216
/// Optionally print the filenames to be compiled
206217
void PrintFileNames() const;
@@ -212,7 +223,7 @@ class CC1Command : public Command {
212223
CC1Command(const Action &Source, const Tool &Creator,
213224
ResponseFileSupport ResponseSupport, const char *Executable,
214225
const llvm::opt::ArgStringList &Arguments,
215-
ArrayRef<InputInfo> Inputs);
226+
ArrayRef<InputInfo> Inputs, ArrayRef<InputInfo> Outputs = None);
216227

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

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

255267
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
256268
CrashReportInfo *CrashInfo = nullptr) const override;

clang/lib/Driver/Job.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ using namespace driver;
3838
Command::Command(const Action &Source, const Tool &Creator,
3939
ResponseFileSupport ResponseSupport, const char *Executable,
4040
const llvm::opt::ArgStringList &Arguments,
41-
ArrayRef<InputInfo> Inputs)
41+
ArrayRef<InputInfo> Inputs, ArrayRef<InputInfo> Outputs)
4242
: Source(Source), Creator(Creator), ResponseSupport(ResponseSupport),
4343
Executable(Executable), Arguments(Arguments) {
4444
for (const auto &II : Inputs)
4545
if (II.isFilename())
4646
InputFilenames.push_back(II.getFilename());
47+
for (const auto &II : Outputs)
48+
if (II.isFilename())
49+
OutputFilenames.push_back(II.getFilename());
4750
}
4851

4952
/// Check if the compiler flag in question should be skipped when
@@ -357,8 +360,9 @@ CC1Command::CC1Command(const Action &Source, const Tool &Creator,
357360
ResponseFileSupport ResponseSupport,
358361
const char *Executable,
359362
const llvm::opt::ArgStringList &Arguments,
360-
ArrayRef<InputInfo> Inputs)
361-
: Command(Source, Creator, ResponseSupport, Executable, Arguments, Inputs) {
363+
ArrayRef<InputInfo> Inputs, ArrayRef<InputInfo> Outputs)
364+
: Command(Source, Creator, ResponseSupport, Executable, Arguments, Inputs,
365+
Outputs) {
362366
InProcess = true;
363367
}
364368

@@ -415,9 +419,10 @@ FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
415419
const char *Executable_,
416420
const llvm::opt::ArgStringList &Arguments_,
417421
ArrayRef<InputInfo> Inputs,
422+
ArrayRef<InputInfo> Outputs,
418423
std::unique_ptr<Command> Fallback_)
419424
: Command(Source_, Creator_, ResponseSupport, Executable_, Arguments_,
420-
Inputs),
425+
Inputs, Outputs),
421426
Fallback(std::move(Fallback_)) {}
422427

423428
void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
@@ -456,9 +461,10 @@ int FallbackCommand::Execute(ArrayRef<llvm::Optional<StringRef>> Redirects,
456461
ForceSuccessCommand::ForceSuccessCommand(
457462
const Action &Source_, const Tool &Creator_,
458463
ResponseFileSupport ResponseSupport, const char *Executable_,
459-
const llvm::opt::ArgStringList &Arguments_, ArrayRef<InputInfo> Inputs)
464+
const llvm::opt::ArgStringList &Arguments_, ArrayRef<InputInfo> Inputs,
465+
ArrayRef<InputInfo> Outputs)
460466
: Command(Source_, Creator_, ResponseSupport, Executable_, Arguments_,
461-
Inputs) {}
467+
Inputs, Outputs) {}
462468

463469
void ForceSuccessCommand::Print(raw_ostream &OS, const char *Terminator,
464470
bool Quote, CrashReportInfo *CrashInfo) const {

clang/lib/Driver/ToolChains/AIX.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
7171

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

7777
void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
@@ -170,7 +170,7 @@ void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
170170

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

176176
/// AIX - AIX tool chain which can call as(1) and ld(1) directly.

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,9 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
356356
CmdArgs.push_back("-shared");
357357
CmdArgs.push_back("-o");
358358
CmdArgs.push_back(Output.getFilename());
359-
C.addCommand(
360-
std::make_unique<Command>(JA, *this, ResponseFileSupport::AtFileCurCP(),
361-
Args.MakeArgString(Linker), CmdArgs, Inputs));
359+
C.addCommand(std::make_unique<Command>(
360+
JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
361+
CmdArgs, Inputs, Output));
362362
}
363363

364364
void amdgpu::getAMDGPUTargetFeatures(const Driver &D,

clang/lib/Driver/ToolChains/AVR.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
142142
CmdArgs.push_back(Args.MakeArgString(std::string("-m") + *FamilyName));
143143
}
144144

145-
C.addCommand(
146-
std::make_unique<Command>(JA, *this, ResponseFileSupport::AtFileCurCP(),
147-
Args.MakeArgString(Linker), CmdArgs, Inputs));
145+
C.addCommand(std::make_unique<Command>(
146+
JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
147+
CmdArgs, Inputs, Output));
148148
}
149149

150150
llvm::Optional<std::string> AVRToolChain::findAVRLibcInstallation() const {

clang/lib/Driver/ToolChains/Ananas.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ void ananas::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
3939
CmdArgs.push_back(II.getFilename());
4040

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

4647
void ananas::Linker::ConstructJob(Compilation &C, const JobAction &JA,
@@ -124,8 +125,9 @@ void ananas::Linker::ConstructJob(Compilation &C, const JobAction &JA,
124125
}
125126

126127
const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
127-
C.addCommand(std::make_unique<Command>(
128-
JA, *this, ResponseFileSupport::AtFileCurCP(), Exec, CmdArgs, Inputs));
128+
C.addCommand(std::make_unique<Command>(JA, *this,
129+
ResponseFileSupport::AtFileCurCP(),
130+
Exec, CmdArgs, Inputs, Output));
129131
}
130132

131133
// Ananas - Ananas tool chain which can call as(1) and ld(1) directly.

clang/lib/Driver/ToolChains/BareMetal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,5 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
202202

203203
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
204204
Args.MakeArgString(TC.GetLinkerPath()),
205-
CmdArgs, Inputs));
205+
CmdArgs, Inputs, Output));
206206
}

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4356,9 +4356,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
43564356
II.getInputArg().renderAsInput(Args, CmdArgs);
43574357
}
43584358

4359-
C.addCommand(
4360-
std::make_unique<Command>(JA, *this, ResponseFileSupport::AtFileUTF8(),
4361-
D.getClangProgramPath(), CmdArgs, Inputs));
4359+
C.addCommand(std::make_unique<Command>(
4360+
JA, *this, ResponseFileSupport::AtFileUTF8(), D.getClangProgramPath(),
4361+
CmdArgs, Inputs, Output));
43624362
return;
43634363
}
43644364

@@ -6314,20 +6314,23 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
63146314
getCLFallback()->GetCommand(C, JA, Output, Inputs, Args, LinkingOutput);
63156315
C.addCommand(std::make_unique<FallbackCommand>(
63166316
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs,
6317-
std::move(CLCommand)));
6317+
Output, std::move(CLCommand)));
63186318
} else if (Args.hasArg(options::OPT__SLASH_fallback) &&
63196319
isa<PrecompileJobAction>(JA)) {
63206320
// In /fallback builds, run the main compilation even if the pch generation
63216321
// fails, so that the main compilation's fallback to cl.exe runs.
63226322
C.addCommand(std::make_unique<ForceSuccessCommand>(
6323-
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs));
6323+
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs,
6324+
Output));
63246325
} else if (D.CC1Main && !D.CCGenDiagnostics) {
63256326
// Invoke the CC1 directly in this process
6326-
C.addCommand(std::make_unique<CC1Command>(
6327-
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs));
6327+
C.addCommand(std::make_unique<CC1Command>(JA, *this,
6328+
ResponseFileSupport::AtFileUTF8(),
6329+
Exec, CmdArgs, Inputs, Output));
63286330
} else {
6329-
C.addCommand(std::make_unique<Command>(
6330-
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs));
6331+
C.addCommand(std::make_unique<Command>(JA, *this,
6332+
ResponseFileSupport::AtFileUTF8(),
6333+
Exec, CmdArgs, Inputs, Output));
63316334
}
63326335

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

70767079
const char *Exec = getToolChain().getDriver().getClangProgramPath();
7077-
C.addCommand(std::make_unique<Command>(
7078-
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs));
7080+
C.addCommand(std::make_unique<Command>(JA, *this,
7081+
ResponseFileSupport::AtFileUTF8(),
7082+
Exec, CmdArgs, Inputs, Output));
70797083
}
70807084

70817085
// Begin OffloadBundler
@@ -7161,7 +7165,7 @@ void OffloadBundler::ConstructJob(Compilation &C, const JobAction &JA,
71617165
C.addCommand(std::make_unique<Command>(
71627166
JA, *this, ResponseFileSupport::None(),
71637167
TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
7164-
CmdArgs, None));
7168+
CmdArgs, None, Output));
71657169
}
71667170

71677171
void OffloadBundler::ConstructJobMultipleOutputs(
@@ -7227,7 +7231,7 @@ void OffloadBundler::ConstructJobMultipleOutputs(
72277231
C.addCommand(std::make_unique<Command>(
72287232
JA, *this, ResponseFileSupport::None(),
72297233
TCArgs.MakeArgString(getToolChain().GetProgramPath(getShortName())),
7230-
CmdArgs, None));
7234+
CmdArgs, None, Outputs));
72317235
}
72327236

72337237
void OffloadWrapper::ConstructJob(Compilation &C, const JobAction &JA,
@@ -7257,5 +7261,5 @@ void OffloadWrapper::ConstructJob(Compilation &C, const JobAction &JA,
72577261
C.addCommand(std::make_unique<Command>(
72587262
JA, *this, ResponseFileSupport::None(),
72597263
Args.MakeArgString(getToolChain().GetProgramPath(getShortName())),
7260-
CmdArgs, Inputs));
7264+
CmdArgs, Inputs, Output));
72617265
}

clang/lib/Driver/ToolChains/CloudABI.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ void cloudabi::Linker::ConstructJob(Compilation &C, const JobAction &JA,
9292
CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
9393

9494
const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
95-
C.addCommand(std::make_unique<Command>(
96-
JA, *this, ResponseFileSupport::AtFileCurCP(), Exec, CmdArgs, Inputs));
95+
C.addCommand(std::make_unique<Command>(JA, *this,
96+
ResponseFileSupport::AtFileCurCP(),
97+
Exec, CmdArgs, Inputs, Output));
9798
}
9899

99100
// CloudABI - CloudABI tool chain which can call ld(1) directly.

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -951,12 +951,13 @@ void tools::SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T,
951951
InputInfo II(types::TY_Object, Output.getFilename(), Output.getFilename());
952952

953953
// First extract the dwo sections.
954-
C.addCommand(std::make_unique<Command>(
955-
JA, T, ResponseFileSupport::AtFileCurCP(), Exec, ExtractArgs, II));
954+
C.addCommand(std::make_unique<Command>(JA, T,
955+
ResponseFileSupport::AtFileCurCP(),
956+
Exec, ExtractArgs, II, Output));
956957

957958
// Then remove them from the original .o file.
958959
C.addCommand(std::make_unique<Command>(
959-
JA, T, ResponseFileSupport::AtFileCurCP(), Exec, StripArgs, II));
960+
JA, T, ResponseFileSupport::AtFileCurCP(), Exec, StripArgs, II, Output));
960961
}
961962

962963
// Claim options we don't want to warn if they are unused. We do this for

clang/lib/Driver/ToolChains/CrossWindows.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void tools::CrossWindows::Assembler::ConstructJob(
5858
Exec = Args.MakeArgString(Assembler);
5959

6060
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
61-
Exec, CmdArgs, Inputs));
61+
Exec, CmdArgs, Inputs, Output));
6262
}
6363

6464
void tools::CrossWindows::Linker::ConstructJob(
@@ -203,8 +203,9 @@ void tools::CrossWindows::Linker::ConstructJob(
203203

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

206-
C.addCommand(std::make_unique<Command>(
207-
JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs));
206+
C.addCommand(std::make_unique<Command>(JA, *this,
207+
ResponseFileSupport::AtFileUTF8(),
208+
Exec, CmdArgs, Inputs, Output));
208209
}
209210

210211
CrossWindowsToolChain::CrossWindowsToolChain(const Driver &D,

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
427427
JA, *this,
428428
ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,
429429
"--options-file"},
430-
Exec, CmdArgs, Inputs));
430+
Exec, CmdArgs, Inputs, Output));
431431
}
432432

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

502502
void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA,
@@ -577,7 +577,7 @@ void NVPTX::OpenMPLinker::ConstructJob(Compilation &C, const JobAction &JA,
577577
JA, *this,
578578
ResponseFileSupport{ResponseFileSupport::RF_Full, llvm::sys::WEM_UTF8,
579579
"--options-file"},
580-
Exec, CmdArgs, Inputs));
580+
Exec, CmdArgs, Inputs, Output));
581581
}
582582

583583
/// CUDA toolchain. Our assembler is ptxas, and our "linker" is fatbinary,

0 commit comments

Comments
 (0)