Skip to content

Commit 6c92c78

Browse files
committed
[SR-1788] Add -driver-time-compilation option
Add an option to print the time it takes each driver task to complete. Here's an example of the output: ``` $ swiftc -driver-time-compilation \ -emit-library -module-name Crispix \ Crispix/A.swift Crispix/B.swift Crispix/C.swift ===-------------------------------------------------------------------------=== Driver Time Compilation ===-------------------------------------------------------------------------=== Total Execution Time: 0.0000 seconds (0.0875 wall clock) ---Wall Time--- --- Name --- 0.0245 ( 28.0%) link /path/to/Crispix/A.swift /path/to/Crispix/B.swift /path/to/Crispix/C.swift 0.0211 ( 24.1%) compile /path/to/Crispix/A.swift 0.0209 ( 23.9%) compile /path/to/Crispix/B.swift 0.0176 ( 20.1%) compile /path/to/Crispix/C.swift 0.0035 ( 4.0%) swift-autolink-extract /path/to/Crispix/A.swift /path/to/Crispix/B.swift /path/to/Crispix/C.swift 0.0875 (100.0%) Total ```
1 parent cc32f93 commit 6c92c78

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

include/swift/Driver/Compilation.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ class Compilation {
128128
/// True if temporary files should not be deleted.
129129
bool SaveTemps;
130130

131+
/// When true, dumps information on how long each compilation task took to
132+
/// execute.
133+
bool ShowDriverTimeCompilation;
134+
131135
/// When true, dumps information about why files are being scheduled to be
132136
/// rebuilt.
133137
bool ShowIncrementalBuildDecisions = false;
@@ -145,7 +149,8 @@ class Compilation {
145149
unsigned NumberOfParallelCommands = 1,
146150
bool EnableIncrementalBuild = false,
147151
bool SkipTaskExecution = false,
148-
bool SaveTemps = false);
152+
bool SaveTemps = false,
153+
bool ShowDriverTimeCompilation = false);
149154
~Compilation();
150155

151156
ArrayRefView<std::unique_ptr<const Job>, const Job *, Compilation::unwrap>

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ def output_file_map_EQ : Joined<["-"], "output-file-map=">,
169169

170170
def save_temps : Flag<["-"], "save-temps">, Flags<[NoInteractiveOption]>,
171171
HelpText<"Save intermediate compilation results">;
172+
def driver_time_compilation : Flag<["-"], "driver-time-compilation">,
173+
Flags<[NoInteractiveOption]>,
174+
HelpText<"Prints the total time it took to execute all compilation tasks">;
172175

173176
def emit_dependencies : Flag<["-"], "emit-dependencies">,
174177
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>,

lib/Driver/Compilation.cpp

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/Support/FileSystem.h"
3434
#include "llvm/Support/Path.h"
3535
#include "llvm/Support/raw_ostream.h"
36+
#include "llvm/Support/Timer.h"
3637
#include "llvm/Support/YAMLParser.h"
3738

3839
using namespace swift;
@@ -48,15 +49,17 @@ Compilation::Compilation(DiagnosticEngine &Diags, OutputLevel Level,
4849
unsigned NumberOfParallelCommands,
4950
bool EnableIncrementalBuild,
5051
bool SkipTaskExecution,
51-
bool SaveTemps)
52+
bool SaveTemps,
53+
bool ShowDriverTimeCompilation)
5254
: Diags(Diags), Level(Level), RawInputArgs(std::move(InputArgs)),
5355
TranslatedArgs(std::move(TranslatedArgs)),
5456
InputFilesWithTypes(std::move(InputsWithTypes)), ArgsHash(ArgsHash),
5557
BuildStartTime(StartTime),
5658
NumberOfParallelCommands(NumberOfParallelCommands),
5759
SkipTaskExecution(SkipTaskExecution),
5860
EnableIncrementalBuild(EnableIncrementalBuild),
59-
SaveTemps(SaveTemps) {
61+
SaveTemps(SaveTemps),
62+
ShowDriverTimeCompilation(ShowDriverTimeCompilation) {
6063
};
6164

6265
using CommandSet = llvm::SmallPtrSet<const Job *, 16>;
@@ -421,14 +424,43 @@ int Compilation::performJobsImpl() {
421424
}
422425

423426
int Result = EXIT_SUCCESS;
427+
llvm::TimerGroup DriverTimerGroup("Driver Time Compilation");
428+
llvm::SmallDenseMap<const Job *, std::unique_ptr<llvm::Timer>, 16>
429+
DriverTimers;
424430

425431
// Set up a callback which will be called immediately after a task has
426432
// started. This callback may be used to provide output indicating that the
427433
// task began.
428-
auto taskBegan = [this] (ProcessId Pid, void *Context) {
434+
auto taskBegan = [&] (ProcessId Pid, void *Context) {
429435
// TODO: properly handle task began.
430436
const Job *BeganCmd = (const Job *)Context;
431437

438+
if (ShowDriverTimeCompilation) {
439+
llvm::SmallString<128> TimerName;
440+
llvm::raw_svector_ostream OS(TimerName);
441+
442+
OS << BeganCmd->getSource().getClassName();
443+
for (auto A : BeganCmd->getSource().getInputs()) {
444+
if (const InputAction *IA = dyn_cast<InputAction>(A)) {
445+
OS << " " << IA->getInputArg().getValue();
446+
}
447+
}
448+
for (auto J : BeganCmd->getInputs()) {
449+
for (auto A : J->getSource().getInputs()) {
450+
if (const InputAction *IA = dyn_cast<InputAction>(A)) {
451+
OS << " " << IA->getInputArg().getValue();
452+
}
453+
}
454+
}
455+
456+
DriverTimers.insert({
457+
BeganCmd,
458+
std::unique_ptr<llvm::Timer>(
459+
new llvm::Timer(OS.str(), DriverTimerGroup))
460+
});
461+
DriverTimers[BeganCmd]->startTimer();
462+
}
463+
432464
// For verbose output, print out each command as it begins execution.
433465
if (Level == OutputLevel::Verbose)
434466
BeganCmd->printCommandLine(llvm::errs());
@@ -445,6 +477,10 @@ int Compilation::performJobsImpl() {
445477
void *Context) -> TaskFinishedResponse {
446478
const Job *FinishedCmd = (const Job *)Context;
447479

480+
if (ShowDriverTimeCompilation) {
481+
DriverTimers[FinishedCmd]->stopTimer();
482+
}
483+
448484
if (Level == OutputLevel::Parseable) {
449485
// Parseable output was requested.
450486
parseable_output::emitFinishedMessage(llvm::errs(), *FinishedCmd, Pid,
@@ -572,6 +608,10 @@ int Compilation::performJobsImpl() {
572608
void *Context) -> TaskFinishedResponse {
573609
const Job *SignalledCmd = (const Job *)Context;
574610

611+
if (ShowDriverTimeCompilation) {
612+
DriverTimers[SignalledCmd]->stopTimer();
613+
}
614+
575615
if (Level == OutputLevel::Parseable) {
576616
// Parseable output was requested.
577617
parseable_output::emitSignalledMessage(llvm::errs(), *SignalledCmd, Pid,
@@ -721,6 +761,7 @@ int Compilation::performJobs() {
721761

722762
// If we don't have to do any cleanup work, just exec the subprocess.
723763
if (Level < OutputLevel::Parseable &&
764+
!ShowDriverTimeCompilation &&
724765
(SaveTemps || TempFilePaths.empty()) &&
725766
CompilationRecordPath.empty() &&
726767
Jobs.size() == 1) {

lib/Driver/Driver.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ std::unique_ptr<Compilation> Driver::buildCompilation(
369369
bool SaveTemps = ArgList->hasArg(options::OPT_save_temps);
370370
bool ContinueBuildingAfterErrors =
371371
ArgList->hasArg(options::OPT_continue_building_after_errors);
372+
bool ShowDriverTimeCompilation =
373+
ArgList->hasArg(options::OPT_driver_time_compilation);
372374

373375
std::unique_ptr<DerivedArgList> TranslatedArgList(
374376
translateInputArgs(*ArgList));
@@ -503,7 +505,8 @@ std::unique_ptr<Compilation> Driver::buildCompilation(
503505
NumberOfParallelCommands,
504506
Incremental,
505507
DriverSkipExecution,
506-
SaveTemps));
508+
SaveTemps,
509+
ShowDriverTimeCompilation));
507510

508511
buildJobs(Actions, OI, OFM.get(), *TC, *C);
509512

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %swiftc_driver -parse -driver-time-compilation %s 2>&1 | %FileCheck %s
2+
// RUN: %swiftc_driver -parse -driver-time-compilation %s %S/../Inputs/empty.swift 2>&1 | %FileCheck -check-prefix CHECK-MULTIPLE %s
3+
4+
// CHECK: Driver Time Compilation
5+
// CHECK: Total Execution Time: {{[0-9]+}}.{{[0-9]+}} seconds ({{[0-9]+}}.{{[0-9]+}} wall clock)
6+
// CHECK: ---Wall Time---
7+
// CHECK: --- Name ---
8+
// CHECK: compile {{.*}}driver-time-compilation.swift
9+
// CHECK-MULTIPLE: compile {{.*}}empty.swift
10+
// CHECK: {{[0-9]+}}.{{[0-9]+}} (100.0%) Total

0 commit comments

Comments
 (0)