Skip to content

Commit 9bb4351

Browse files
author
Tarun Prabhu
committed
[clang][flang] Support -time in both clang and flang
The -time option prints timing information for the subcommands (compiler, linker) in a format similar to that used by gcc/gfortran (we use more digits of precision). This partially addresses requests from #89888
1 parent e584278 commit 9bb4351

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5844,6 +5844,7 @@ def print_enabled_extensions : Flag<["-", "--"], "print-enabled-extensions">,
58445844
def : Flag<["-"], "mcpu=help">, Alias<print_supported_cpus>;
58455845
def : Flag<["-"], "mtune=help">, Alias<print_supported_cpus>;
58465846
def time : Flag<["-"], "time">,
5847+
Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>,
58475848
HelpText<"Time individual commands">;
58485849
def traditional_cpp : Flag<["-", "--"], "traditional-cpp">,
58495850
Visibility<[ClangOption, CC1Option]>,

clang/lib/Driver/Compilation.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include "llvm/Option/OptSpecifier.h"
2222
#include "llvm/Option/Option.h"
2323
#include "llvm/Support/FileSystem.h"
24+
#include "llvm/Support/Format.h"
25+
#include "llvm/Support/Path.h"
26+
#include "llvm/Support/Timer.h"
2427
#include "llvm/Support/raw_ostream.h"
2528
#include "llvm/TargetParser/Triple.h"
2629
#include <cassert>
@@ -194,11 +197,29 @@ int Compilation::ExecuteCommand(const Command &C,
194197
if (LogOnly)
195198
return 0;
196199

200+
// We don't use any timers or llvm::TimeGroup's because those are tied into
201+
// the global static timer list which, in principle, could be cleared without
202+
// us knowing about it.
203+
llvm::TimeRecord StartTime;
204+
if (getArgs().hasArg(options::OPT_time)) {
205+
StartTime = llvm::TimeRecord::getCurrentTime(true);
206+
}
207+
197208
std::string Error;
198209
bool ExecutionFailed;
199210
int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
200211
if (PostCallback)
201212
PostCallback(C, Res);
213+
214+
if (getArgs().hasArg(options::OPT_time)) {
215+
llvm::TimeRecord Time = llvm::TimeRecord::getCurrentTime(false);
216+
Time -= StartTime;
217+
llvm::StringRef Name = llvm::sys::path::filename(C.getExecutable());
218+
llvm::errs() << "# " << Name << " "
219+
<< llvm::format("%0.5f", Time.getUserTime()) << " "
220+
<< llvm::format("%0.5f", Time.getSystemTime()) << "\n";
221+
}
222+
202223
if (!Error.empty()) {
203224
assert(Res && "Error string set with 0 result code!");
204225
getDriver().Diag(diag::err_drv_command_failure) << Error;

clang/lib/Driver/Driver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,9 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
12891289
// Ignore -pipe.
12901290
Args.ClaimAllArgs(options::OPT_pipe);
12911291

1292+
// Ignore -time.
1293+
Args.ClaimAllArgs(options::OPT_time);
1294+
12921295
// Extract -ccc args.
12931296
//
12941297
// FIXME: We need to figure out where this behavior should live. Most of it

clang/test/Driver/time.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// The -time option prints timing information for the various subcommands in a
2+
// format similar to that used by gcc. When compiling and linking, this will
3+
// include the time to call clang-${LLVM_VERSION_MAJOR} and the linker. Since
4+
// the name of the linker could vary across platforms, and name of the compiler
5+
// could be something different, just check that whatever is printed to stderr
6+
// looks like timing information.
7+
8+
// RUN: %clang -time -c -O3 -o /dev/null %s 2>&1 \
9+
// RUN: | FileCheck %s --check-prefix=COMPILE-ONLY
10+
// RUN: %clang -time -S -emit-llvm -O3 -o /dev/null %s 2>&1 \
11+
// RUN: | FileCheck %s --check-prefix=COMPILE-ONLY
12+
// RUN: %clang -time -S -O3 -o /dev/null %s 2>&1 \
13+
// RUN: | FileCheck %s --check-prefix=COMPILE-ONLY
14+
// RUN: %clang -time -O3 -o /dev/null %s 2>&1 \
15+
// RUN: | FileCheck %s --check-prefix=COMPILE-AND-LINK
16+
17+
// COMPILE-ONLY: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}}
18+
19+
// COMPILE-AND-LINK: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}}
20+
// COMPILE-AND-LINK: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}}
21+
22+
int main() {
23+
return 0;
24+
}

flang/test/Driver/time.f90

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
! The -time option prints timing information for the various subcommands in a
2+
! format similar to that used by gfortran. When compiling and linking, this will
3+
! include the time to call flang-${LLVM_VERSION_MAJOR} and the linker. Since the
4+
! name of the linker could vary across platforms, and the flang name could also
5+
! potentially be something different, just check that whatever is printed to
6+
! stderr looks like timing information.
7+
8+
! RUN: %flang -time -c -O3 -o /dev/null %s 2>&1 \
9+
! RUN: | FileCheck %s --check-prefix=COMPILE-ONLY
10+
! RUN: %flang -time -S -emit-llvm -O3 -o /dev/null %s 2>&1 \
11+
! RUN: | FileCheck %s --check-prefix=COMPILE-ONLY
12+
! RUN: %flang -time -S -O3 -o /dev/null %s 2>&1 \
13+
! RUN: | FileCheck %s --check-prefix=COMPILE-ONLY
14+
! RUN: %flang -time -O3 -o /dev/null %s 2>&1 \
15+
! RUN: | FileCheck %s --check-prefix=COMPILE-AND-LINK
16+
17+
! COMPILE-ONLY: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}}
18+
19+
! COMPILE-AND-LINK: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}}
20+
! COMPILE-AND-LINK: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}}
21+
22+
end program

0 commit comments

Comments
 (0)