Skip to content

[SR-1788] Add -driver-time-compilation option #4367

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 1 commit into from
Aug 23, 2016
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
7 changes: 6 additions & 1 deletion include/swift/Driver/Compilation.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ class Compilation {
/// True if temporary files should not be deleted.
bool SaveTemps;

/// When true, dumps information on how long each compilation task took to
/// execute.
bool ShowDriverTimeCompilation;

/// When true, dumps information about why files are being scheduled to be
/// rebuilt.
bool ShowIncrementalBuildDecisions = false;
Expand All @@ -145,7 +149,8 @@ class Compilation {
unsigned NumberOfParallelCommands = 1,
bool EnableIncrementalBuild = false,
bool SkipTaskExecution = false,
bool SaveTemps = false);
bool SaveTemps = false,
bool ShowDriverTimeCompilation = false);
~Compilation();

ArrayRefView<std::unique_ptr<const Job>, const Job *, Compilation::unwrap>
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ def output_file_map_EQ : Joined<["-"], "output-file-map=">,

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

def emit_dependencies : Flag<["-"], "emit-dependencies">,
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>,
Expand Down
47 changes: 44 additions & 3 deletions lib/Driver/Compilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Timer.h"
#include "llvm/Support/YAMLParser.h"

using namespace swift;
Expand All @@ -48,15 +49,17 @@ Compilation::Compilation(DiagnosticEngine &Diags, OutputLevel Level,
unsigned NumberOfParallelCommands,
bool EnableIncrementalBuild,
bool SkipTaskExecution,
bool SaveTemps)
bool SaveTemps,
bool ShowDriverTimeCompilation)
: Diags(Diags), Level(Level), RawInputArgs(std::move(InputArgs)),
TranslatedArgs(std::move(TranslatedArgs)),
InputFilesWithTypes(std::move(InputsWithTypes)), ArgsHash(ArgsHash),
BuildStartTime(StartTime),
NumberOfParallelCommands(NumberOfParallelCommands),
SkipTaskExecution(SkipTaskExecution),
EnableIncrementalBuild(EnableIncrementalBuild),
SaveTemps(SaveTemps) {
SaveTemps(SaveTemps),
ShowDriverTimeCompilation(ShowDriverTimeCompilation) {
};

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

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

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

if (ShowDriverTimeCompilation) {
llvm::SmallString<128> TimerName;
llvm::raw_svector_ostream OS(TimerName);

OS << BeganCmd->getSource().getClassName();
for (auto A : BeganCmd->getSource().getInputs()) {
if (const InputAction *IA = dyn_cast<InputAction>(A)) {
OS << " " << IA->getInputArg().getValue();
}
}
for (auto J : BeganCmd->getInputs()) {
for (auto A : J->getSource().getInputs()) {
if (const InputAction *IA = dyn_cast<InputAction>(A)) {
OS << " " << IA->getInputArg().getValue();
}
}
}

DriverTimers.insert({
BeganCmd,
std::unique_ptr<llvm::Timer>(
new llvm::Timer(OS.str(), DriverTimerGroup))
});
DriverTimers[BeganCmd]->startTimer();
}

// For verbose output, print out each command as it begins execution.
if (Level == OutputLevel::Verbose)
BeganCmd->printCommandLine(llvm::errs());
Expand All @@ -445,6 +477,10 @@ int Compilation::performJobsImpl() {
void *Context) -> TaskFinishedResponse {
const Job *FinishedCmd = (const Job *)Context;

if (ShowDriverTimeCompilation) {
DriverTimers[FinishedCmd]->stopTimer();
}

if (Level == OutputLevel::Parseable) {
// Parseable output was requested.
parseable_output::emitFinishedMessage(llvm::errs(), *FinishedCmd, Pid,
Expand Down Expand Up @@ -572,6 +608,10 @@ int Compilation::performJobsImpl() {
void *Context) -> TaskFinishedResponse {
const Job *SignalledCmd = (const Job *)Context;

if (ShowDriverTimeCompilation) {
DriverTimers[SignalledCmd]->stopTimer();
}

if (Level == OutputLevel::Parseable) {
// Parseable output was requested.
parseable_output::emitSignalledMessage(llvm::errs(), *SignalledCmd, Pid,
Expand Down Expand Up @@ -721,6 +761,7 @@ int Compilation::performJobs() {

// If we don't have to do any cleanup work, just exec the subprocess.
if (Level < OutputLevel::Parseable &&
!ShowDriverTimeCompilation &&
(SaveTemps || TempFilePaths.empty()) &&
CompilationRecordPath.empty() &&
Jobs.size() == 1) {
Expand Down
5 changes: 4 additions & 1 deletion lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ std::unique_ptr<Compilation> Driver::buildCompilation(
bool SaveTemps = ArgList->hasArg(options::OPT_save_temps);
bool ContinueBuildingAfterErrors =
ArgList->hasArg(options::OPT_continue_building_after_errors);
bool ShowDriverTimeCompilation =
ArgList->hasArg(options::OPT_driver_time_compilation);

std::unique_ptr<DerivedArgList> TranslatedArgList(
translateInputArgs(*ArgList));
Expand Down Expand Up @@ -503,7 +505,8 @@ std::unique_ptr<Compilation> Driver::buildCompilation(
NumberOfParallelCommands,
Incremental,
DriverSkipExecution,
SaveTemps));
SaveTemps,
ShowDriverTimeCompilation));

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

Expand Down
10 changes: 10 additions & 0 deletions test/Driver/driver-time-compilation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %swiftc_driver -parse -driver-time-compilation %s 2>&1 | %FileCheck %s
// RUN: %swiftc_driver -parse -driver-time-compilation %s %S/../Inputs/empty.swift 2>&1 | %FileCheck -check-prefix CHECK-MULTIPLE %s

// CHECK: Driver Time Compilation
// CHECK: Total Execution Time: {{[0-9]+}}.{{[0-9]+}} seconds ({{[0-9]+}}.{{[0-9]+}} wall clock)
// CHECK: ---Wall Time---
// CHECK: --- Name ---
// CHECK: compile {{.*}}driver-time-compilation.swift
// CHECK-MULTIPLE: compile {{.*}}empty.swift
// CHECK: {{[0-9]+}}.{{[0-9]+}} (100.0%) Total