Skip to content

Commit f539674

Browse files
authored
[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. This partially addresses requests from #89888
1 parent e5ba117 commit f539674

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5896,6 +5896,7 @@ def print_enabled_extensions : Flag<["-", "--"], "print-enabled-extensions">,
58965896
def : Flag<["-"], "mcpu=help">, Alias<print_supported_cpus>;
58975897
def : Flag<["-"], "mtune=help">, Alias<print_supported_cpus>;
58985898
def time : Flag<["-"], "time">,
5899+
Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>,
58995900
HelpText<"Time individual commands">;
59005901
def traditional_cpp : Flag<["-", "--"], "traditional-cpp">,
59015902
Visibility<[ClangOption, CC1Option]>,

clang/lib/Driver/Compilation.cpp

Lines changed: 20 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,28 @@ 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(/*Start=*/true);
206+
197207
std::string Error;
198208
bool ExecutionFailed;
199209
int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
200210
if (PostCallback)
201211
PostCallback(C, Res);
212+
213+
if (getArgs().hasArg(options::OPT_time)) {
214+
llvm::TimeRecord Time = llvm::TimeRecord::getCurrentTime(/*Start=*/false);
215+
Time -= StartTime;
216+
llvm::StringRef Name = llvm::sys::path::filename(C.getExecutable());
217+
llvm::errs() << "# " << Name << " "
218+
<< llvm::format("%0.2f", Time.getUserTime()) << " "
219+
<< llvm::format("%0.2f", Time.getSystemTime()) << "\n";
220+
}
221+
202222
if (!Error.empty()) {
203223
assert(Res && "Error string set with 0 result code!");
204224
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
@@ -1315,6 +1315,9 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
13151315
// Ignore -pipe.
13161316
Args.ClaimAllArgs(options::OPT_pipe);
13171317

1318+
// Ignore -time.
1319+
Args.ClaimAllArgs(options::OPT_time);
1320+
13181321
// Extract -ccc args.
13191322
//
13201323
// FIXME: We need to figure out where this behavior should live. Most of it

clang/test/Driver/time.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
// Ideally, this should be tested on various platforms, but that requires the
9+
// the full toolchain, including a linker to be present. The initial author of
10+
// the test only had access to Linux on x86 which is why this is only enabled
11+
// there. More platforms ought to be added if possible.
12+
13+
// REQUIRES: x86-registered-target
14+
// REQUIRES: x86_64-linux
15+
16+
// RUN: %clang --target=x86_64-pc-linux -time -c -o /dev/null %s 2>&1 \
17+
// RUN: | FileCheck %s --check-prefix=COMPILE-ONLY
18+
// RUN: %clang --target=x86_64-pc-linux -time -S -emit-llvm -o /dev/null %s 2>&1 \
19+
// RUN: | FileCheck %s --check-prefix=COMPILE-ONLY
20+
// RUN: %clang --target=x86_64-pc-linux -time -S -o /dev/null %s 2>&1 \
21+
// RUN: | FileCheck %s --check-prefix=COMPILE-ONLY
22+
// RUN: %clang --target=x86_64-pc-linux -time -o /dev/null %s 2>&1 \
23+
// RUN: | FileCheck %s --check-prefix=COMPILE-AND-LINK
24+
25+
// COMPILE-ONLY: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}}
26+
// COMPILE-ONLY-NOT: {{.}}
27+
28+
// COMPILE-AND-LINK: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}}
29+
// COMPILE-AND-LINK: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}}
30+
31+
int main() {
32+
return 0;
33+
}

flang/test/Driver/time.f90

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
! TODO: For some reason, on Windows, nothing is printed to stderr which causes
2+
! the checks to fail. It is not clear why this is, so disable this on Windows
3+
! until the root cause can be determined.
4+
!
5+
! UNSUPPORTED: system-windows
6+
7+
! The -time option prints timing information for the various subcommands in a
8+
! format similar to that used by gfortran. When compiling and linking, this will
9+
! include the time to call flang-${LLVM_VERSION_MAJOR} and the linker. Since the
10+
! name of the linker could vary across platforms, and the flang name could also
11+
! potentially be something different, just check that whatever is printed to
12+
! stderr looks like timing information.
13+
14+
! Ideally, this should be tested on various platforms, but that requires the
15+
! the full toolchain, including a linker to be present. The initial author of
16+
! the test only had access to Linux on x86 which is why this is only enabled
17+
! there. More platforms ought to be added if possible.
18+
19+
! REQUIRES: x86_64-linux
20+
21+
! RUN: %flang --target=x86_64-linux -time -c -o /dev/null %s 2>&1 \
22+
! RUN: | FileCheck %s --check-prefix=COMPILE-ONLY
23+
! RUN: %flang --target=x86_64-linux -time -S -emit-llvm -O3 -o /dev/null %s 2>&1 \
24+
! RUN: | FileCheck %s --check-prefix=COMPILE-ONLY
25+
! RUN: %flang --target=x86_64-linux -time -S -o /dev/null %s 2>&1 \
26+
! RUN: | FileCheck %s --check-prefix=COMPILE-ONLY
27+
! RUN: %flang --target=x86_64-linux -time -o /dev/null %s 2>&1 \
28+
! RUN: | FileCheck %s --check-prefix=COMPILE-AND-LINK
29+
30+
! COMPILE-ONLY: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}}
31+
! COMPILE-ONLY-NOT: {{.}}
32+
33+
! COMPILE-AND-LINK: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}}
34+
! COMPILE-AND-LINK: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}}
35+
36+
end program

0 commit comments

Comments
 (0)