diff options
author | Tarun Prabhu <tarun@lanl.gov> | 2024-11-12 14:27:22 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-12 14:27:22 -0700 |
commit | f5396748c7da3d9f278fcd42e2a10a3214920d82 (patch) | |
tree | 9348eaa39fce1ac9b07acac6f38efd9f87a36ab5 /clang | |
parent | e5ba11727437456fbab7ce733c07843bf682fa0c (diff) | |
download | llvm-f5396748c7da3d9f278fcd42e2a10a3214920d82.zip llvm-f5396748c7da3d9f278fcd42e2a10a3214920d82.tar.gz llvm-f5396748c7da3d9f278fcd42e2a10a3214920d82.tar.bz2 |
[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
Diffstat (limited to 'clang')
-rw-r--r-- | clang/include/clang/Driver/Options.td | 1 | ||||
-rw-r--r-- | clang/lib/Driver/Compilation.cpp | 20 | ||||
-rw-r--r-- | clang/lib/Driver/Driver.cpp | 3 | ||||
-rw-r--r-- | clang/test/Driver/time.c | 33 |
4 files changed, 57 insertions, 0 deletions
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 9fb7f8b..0bbab21 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -5896,6 +5896,7 @@ def print_enabled_extensions : Flag<["-", "--"], "print-enabled-extensions">, def : Flag<["-"], "mcpu=help">, Alias<print_supported_cpus>; def : Flag<["-"], "mtune=help">, Alias<print_supported_cpus>; def time : Flag<["-"], "time">, + Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>, HelpText<"Time individual commands">; def traditional_cpp : Flag<["-", "--"], "traditional-cpp">, Visibility<[ClangOption, CC1Option]>, diff --git a/clang/lib/Driver/Compilation.cpp b/clang/lib/Driver/Compilation.cpp index ad077d5..6965e59 100644 --- a/clang/lib/Driver/Compilation.cpp +++ b/clang/lib/Driver/Compilation.cpp @@ -21,6 +21,9 @@ #include "llvm/Option/OptSpecifier.h" #include "llvm/Option/Option.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/Timer.h" #include "llvm/Support/raw_ostream.h" #include "llvm/TargetParser/Triple.h" #include <cassert> @@ -194,11 +197,28 @@ int Compilation::ExecuteCommand(const Command &C, if (LogOnly) return 0; + // We don't use any timers or llvm::TimeGroup's because those are tied into + // the global static timer list which, in principle, could be cleared without + // us knowing about it. + llvm::TimeRecord StartTime; + if (getArgs().hasArg(options::OPT_time)) + StartTime = llvm::TimeRecord::getCurrentTime(/*Start=*/true); + std::string Error; bool ExecutionFailed; int Res = C.Execute(Redirects, &Error, &ExecutionFailed); if (PostCallback) PostCallback(C, Res); + + if (getArgs().hasArg(options::OPT_time)) { + llvm::TimeRecord Time = llvm::TimeRecord::getCurrentTime(/*Start=*/false); + Time -= StartTime; + llvm::StringRef Name = llvm::sys::path::filename(C.getExecutable()); + llvm::errs() << "# " << Name << " " + << llvm::format("%0.2f", Time.getUserTime()) << " " + << llvm::format("%0.2f", Time.getSystemTime()) << "\n"; + } + if (!Error.empty()) { assert(Res && "Error string set with 0 result code!"); getDriver().Diag(diag::err_drv_command_failure) << Error; diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 93e85f7..a0fd459 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -1315,6 +1315,9 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) { // Ignore -pipe. Args.ClaimAllArgs(options::OPT_pipe); + // Ignore -time. + Args.ClaimAllArgs(options::OPT_time); + // Extract -ccc args. // // FIXME: We need to figure out where this behavior should live. Most of it diff --git a/clang/test/Driver/time.c b/clang/test/Driver/time.c new file mode 100644 index 0000000..a9a95b0 --- /dev/null +++ b/clang/test/Driver/time.c @@ -0,0 +1,33 @@ +// The -time option prints timing information for the various subcommands in a +// format similar to that used by gcc. When compiling and linking, this will +// include the time to call clang-${LLVM_VERSION_MAJOR} and the linker. Since +// the name of the linker could vary across platforms, and name of the compiler +// could be something different, just check that whatever is printed to stderr +// looks like timing information. + +// Ideally, this should be tested on various platforms, but that requires the +// the full toolchain, including a linker to be present. The initial author of +// the test only had access to Linux on x86 which is why this is only enabled +// there. More platforms ought to be added if possible. + +// REQUIRES: x86-registered-target +// REQUIRES: x86_64-linux + +// RUN: %clang --target=x86_64-pc-linux -time -c -o /dev/null %s 2>&1 \ +// RUN: | FileCheck %s --check-prefix=COMPILE-ONLY +// RUN: %clang --target=x86_64-pc-linux -time -S -emit-llvm -o /dev/null %s 2>&1 \ +// RUN: | FileCheck %s --check-prefix=COMPILE-ONLY +// RUN: %clang --target=x86_64-pc-linux -time -S -o /dev/null %s 2>&1 \ +// RUN: | FileCheck %s --check-prefix=COMPILE-ONLY +// RUN: %clang --target=x86_64-pc-linux -time -o /dev/null %s 2>&1 \ +// RUN: | FileCheck %s --check-prefix=COMPILE-AND-LINK + +// COMPILE-ONLY: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}} +// COMPILE-ONLY-NOT: {{.}} + +// COMPILE-AND-LINK: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}} +// COMPILE-AND-LINK: # {{.+}} {{[0-9]+(.[0-9]+)?}} {{[0-9]+(.[0-9]+)?}} + +int main() { + return 0; +} |