[clang] 5e4f69e - Revert "[Clang] change default storing path of `-ftime-trace`"

2022-09-03 Thread Junduo Dong via cfe-commits

Author: Junduo Dong
Date: 2022-09-03T01:38:37-07:00
New Revision: 5e4f69edbc1e36c47c62a2c522e9a8a6e7b86d06

URL: 
https://github.com/llvm/llvm-project/commit/5e4f69edbc1e36c47c62a2c522e9a8a6e7b86d06
DIFF: 
https://github.com/llvm/llvm-project/commit/5e4f69edbc1e36c47c62a2c522e9a8a6e7b86d06.diff

LOG: Revert "[Clang] change default storing path of `-ftime-trace`"

This reverts commit 38941da066a7b785ba4771710189172e94e37824.

Added: 


Modified: 
clang/lib/Driver/Driver.cpp
clang/test/Driver/check-time-trace.cpp
clang/tools/driver/cc1_main.cpp

Removed: 




diff  --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 5f5655fbc34a8..ba359a1d31a53 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4510,102 +4510,6 @@ Action *Driver::ConstructPhaseAction(
   llvm_unreachable("invalid phase in ConstructPhaseAction");
 }
 
-// Infer data storing path of the options `-ftime-trace`, `-ftime-trace=`
-void InferTimeTracePath(Compilation ) {
-  bool HasTimeTrace =
-  C.getArgs().getLastArg(options::OPT_ftime_trace) != nullptr;
-  bool HasTimeTraceFile =
-  C.getArgs().getLastArg(options::OPT_ftime_trace_EQ) != nullptr;
-  // Whether `-ftime-trace` or `-ftime-trace=` are specified
-  if (!HasTimeTrace && !HasTimeTraceFile)
-return;
-
-  // If `-ftime-trace=` is specified, TracePath is the .
-  // Else if there is a linking job, TracePath is the parent path of .exe,
-  // then the OutputFile's name may be appended to it.
-  // Else, TracePath is "",
-  // then the full OutputFile's path may be appended to it.
-  SmallString<128> TracePath("");
-
-  if (HasTimeTraceFile) {
-TracePath = SmallString<128>(
-C.getArgs().getLastArg(options::OPT_ftime_trace_EQ)->getValue());
-  } else {
-// Get linking executable file's parent path as TracePath's parent path,
-// default is ".". Filename may be determined and added into TracePath 
then.
-//
-// e.g. executable file's path: /usr/local/a.out
-//  its parent's path:  /usr/local
-for (auto  : C.getJobs()) {
-  if (J.getSource().getKind() == Action::LinkJobClass) {
-assert(!J.getOutputFilenames().empty() &&
-   "linking output filename is empty");
-auto OutputFilePath =
-SmallString<128>(J.getOutputFilenames()[0].c_str());
-if (llvm::sys::path::has_parent_path(OutputFilePath)) {
-  TracePath = llvm::sys::path::parent_path(OutputFilePath);
-} else {
-  TracePath = SmallString<128>(".");
-}
-break;
-  }
-}
-  }
-
-  // Add or replace the modified -ftime-trace=` to all clang jobs
-  for (auto  : C.getJobs()) {
-if (J.getSource().getKind() == Action::AssembleJobClass ||
-J.getSource().getKind() == Action::BackendJobClass ||
-J.getSource().getKind() == Action::CompileJobClass) {
-  SmallString<128> TracePathReal = TracePath;
-  SmallString<128> OutputPath(J.getOutputFilenames()[0].c_str());
-  std::string arg = std::string("-ftime-trace=");
-  if (!HasTimeTraceFile) {
-if (TracePathReal.empty()) {
-  // /xxx/yyy.o => /xxx/yyy.json
-  llvm::sys::path::replace_extension(OutputPath, "json");
-  arg += std::string(OutputPath.c_str());
-} else {
-  // /xxx/yyy.o => /executable_file_parent_path/yyy.json
-  llvm::sys::path::append(TracePathReal,
-  llvm::sys::path::filename(OutputPath));
-  llvm::sys::path::replace_extension(TracePathReal, "json");
-  arg += std::string(TracePathReal.c_str());
-}
-  } else {
-// /full_file_path_specified or /path_specified/yyy.json
-if (llvm::sys::fs::is_directory(TracePathReal))
-  llvm::sys::path::append(TracePathReal,
-  llvm::sys::path::filename(OutputPath));
-llvm::sys::path::replace_extension(TracePathReal, "json");
-arg += std::string(TracePathReal.c_str());
-  }
-
-  assert(arg.size() > strlen("-ftime-trace") &&
- arg.find("-ftime-trace=") == 0 && arg[arg.size() - 1] != '=' &&
- "invalid `-ftime-trace=`");
-
-  const std::string::size_type size = arg.size();
-  char *buffer = new char[size + 1];
-  memcpy(buffer, arg.c_str(), size + 1);
-
-  // Replace `-ftime-trace` or `-ftime-trace=` with the modified
-  // `-ftime-trace=`.
-  auto  = J.getArguments();
-  for (unsigned I = 0; I < JArgs.size(); ++I) {
-if (StringRef(JArgs[I]).startswith("-ftime-trace=") ||
-(StringRef(JArgs[I]).equals("-ftime-trace") && !HasTimeTraceFile)) 
{
-  ArgStringList NewArgs(JArgs.begin(), JArgs.begin() + I);
-  NewArgs.push_back(buffer);
-  NewArgs.append(JArgs.begin() + I + 1, JArgs.end());
-  J.replaceArguments(NewArgs);
-   

[clang] 4ff2250 - Revert "[driver][clang] remove the check-time-trace test on the platform "PS4/PS5/Hexagon""

2022-09-03 Thread Junduo Dong via cfe-commits

Author: Junduo Dong
Date: 2022-09-03T01:37:55-07:00
New Revision: 4ff2250a22ed651facf6dad8f379a3fd24a8f6b1

URL: 
https://github.com/llvm/llvm-project/commit/4ff2250a22ed651facf6dad8f379a3fd24a8f6b1
DIFF: 
https://github.com/llvm/llvm-project/commit/4ff2250a22ed651facf6dad8f379a3fd24a8f6b1.diff

LOG: Revert "[driver][clang] remove the check-time-trace test on the platform 
"PS4/PS5/Hexagon""

This reverts commit 39221ad55752c246bb8448a181847103432e12b2.

Added: 


Modified: 
clang/test/Driver/check-time-trace.cpp

Removed: 




diff  --git a/clang/test/Driver/check-time-trace.cpp 
b/clang/test/Driver/check-time-trace.cpp
index 3ff12981a97c..e0d1e935a733 100644
--- a/clang/test/Driver/check-time-trace.cpp
+++ b/clang/test/Driver/check-time-trace.cpp
@@ -1,5 +1,3 @@
-// UNSUPPORTED: ps4, ps5, hexagon
-
 // RUN: rm -rf %T/exe && mkdir %T/exe
 // RUN: %clangxx -ftime-trace -ftime-trace-granularity=0 -o 
%T/exe/check-time-trace %s
 // RUN: cat %T/exe/check-time-trace*.json \



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 39221ad - [driver][clang] remove the check-time-trace test on the platform "PS4/PS5/Hexagon"

2022-09-02 Thread Junduo Dong via cfe-commits

Author: Junduo Dong
Date: 2022-09-02T20:27:35-07:00
New Revision: 39221ad55752c246bb8448a181847103432e12b2

URL: 
https://github.com/llvm/llvm-project/commit/39221ad55752c246bb8448a181847103432e12b2
DIFF: 
https://github.com/llvm/llvm-project/commit/39221ad55752c246bb8448a181847103432e12b2.diff

LOG: [driver][clang] remove the check-time-trace test on the platform 
"PS4/PS5/Hexagon"

One of the test cases in that test is designed to test the compiling
jobs with a linking stage, but the PS4/PS5/Hexagon platform requires
an external linker that isn't present.

So this test do not support the "PS4/PS5/Hexagon".

Added: 


Modified: 
clang/test/Driver/check-time-trace.cpp

Removed: 




diff  --git a/clang/test/Driver/check-time-trace.cpp 
b/clang/test/Driver/check-time-trace.cpp
index e0d1e935a733..3ff12981a97c 100644
--- a/clang/test/Driver/check-time-trace.cpp
+++ b/clang/test/Driver/check-time-trace.cpp
@@ -1,3 +1,5 @@
+// UNSUPPORTED: ps4, ps5, hexagon
+
 // RUN: rm -rf %T/exe && mkdir %T/exe
 // RUN: %clangxx -ftime-trace -ftime-trace-granularity=0 -o 
%T/exe/check-time-trace %s
 // RUN: cat %T/exe/check-time-trace*.json \



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 38941da - [Clang] change default storing path of `-ftime-trace`

2022-09-02 Thread Junduo Dong via cfe-commits

Author: Junduo Dong
Date: 2022-09-02T18:49:11-07:00
New Revision: 38941da066a7b785ba4771710189172e94e37824

URL: 
https://github.com/llvm/llvm-project/commit/38941da066a7b785ba4771710189172e94e37824
DIFF: 
https://github.com/llvm/llvm-project/commit/38941da066a7b785ba4771710189172e94e37824.diff

LOG: [Clang] change default storing path of `-ftime-trace`

1. This implementation change the default storing behavior of -ftime-trace only.

That is, if the compiling job contains the linking action, the executable file' 
s directory may be seem as the main work directory.
Thus the time trace files would be stored in the same directory of linking 
result.

By this approach, the user can easily get the time-trace files in the main work 
directory. The improved demo results:

```
$ clang++ -ftime-trace -o main.out /demo/main.cpp
$ ls .
main.out   main-[random-string].json
```

2. In addition, the main codes of time-trace files' path inference have been 
refactored.

* The  of -ftime-trace= is infered in clang driver
* After that, -ftime-trace= can be added into clang's options

By this approach, the dirty work of path processing and judging can be 
implemented in driver layer, so that the clang may focus on its main work.

 #   $ clang -ftime-trace -o xxx.out xxx.cpp

Differential Revision: https://reviews.llvm.org/D131469

Added: 


Modified: 
clang/lib/Driver/Driver.cpp
clang/test/Driver/check-time-trace.cpp
clang/tools/driver/cc1_main.cpp

Removed: 




diff  --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index ba359a1d31a53..5f5655fbc34a8 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4510,6 +4510,102 @@ Action *Driver::ConstructPhaseAction(
   llvm_unreachable("invalid phase in ConstructPhaseAction");
 }
 
+// Infer data storing path of the options `-ftime-trace`, `-ftime-trace=`
+void InferTimeTracePath(Compilation ) {
+  bool HasTimeTrace =
+  C.getArgs().getLastArg(options::OPT_ftime_trace) != nullptr;
+  bool HasTimeTraceFile =
+  C.getArgs().getLastArg(options::OPT_ftime_trace_EQ) != nullptr;
+  // Whether `-ftime-trace` or `-ftime-trace=` are specified
+  if (!HasTimeTrace && !HasTimeTraceFile)
+return;
+
+  // If `-ftime-trace=` is specified, TracePath is the .
+  // Else if there is a linking job, TracePath is the parent path of .exe,
+  // then the OutputFile's name may be appended to it.
+  // Else, TracePath is "",
+  // then the full OutputFile's path may be appended to it.
+  SmallString<128> TracePath("");
+
+  if (HasTimeTraceFile) {
+TracePath = SmallString<128>(
+C.getArgs().getLastArg(options::OPT_ftime_trace_EQ)->getValue());
+  } else {
+// Get linking executable file's parent path as TracePath's parent path,
+// default is ".". Filename may be determined and added into TracePath 
then.
+//
+// e.g. executable file's path: /usr/local/a.out
+//  its parent's path:  /usr/local
+for (auto  : C.getJobs()) {
+  if (J.getSource().getKind() == Action::LinkJobClass) {
+assert(!J.getOutputFilenames().empty() &&
+   "linking output filename is empty");
+auto OutputFilePath =
+SmallString<128>(J.getOutputFilenames()[0].c_str());
+if (llvm::sys::path::has_parent_path(OutputFilePath)) {
+  TracePath = llvm::sys::path::parent_path(OutputFilePath);
+} else {
+  TracePath = SmallString<128>(".");
+}
+break;
+  }
+}
+  }
+
+  // Add or replace the modified -ftime-trace=` to all clang jobs
+  for (auto  : C.getJobs()) {
+if (J.getSource().getKind() == Action::AssembleJobClass ||
+J.getSource().getKind() == Action::BackendJobClass ||
+J.getSource().getKind() == Action::CompileJobClass) {
+  SmallString<128> TracePathReal = TracePath;
+  SmallString<128> OutputPath(J.getOutputFilenames()[0].c_str());
+  std::string arg = std::string("-ftime-trace=");
+  if (!HasTimeTraceFile) {
+if (TracePathReal.empty()) {
+  // /xxx/yyy.o => /xxx/yyy.json
+  llvm::sys::path::replace_extension(OutputPath, "json");
+  arg += std::string(OutputPath.c_str());
+} else {
+  // /xxx/yyy.o => /executable_file_parent_path/yyy.json
+  llvm::sys::path::append(TracePathReal,
+  llvm::sys::path::filename(OutputPath));
+  llvm::sys::path::replace_extension(TracePathReal, "json");
+  arg += std::string(TracePathReal.c_str());
+}
+  } else {
+// /full_file_path_specified or /path_specified/yyy.json
+if (llvm::sys::fs::is_directory(TracePathReal))
+  llvm::sys::path::append(TracePathReal,
+  llvm::sys::path::filename(OutputPath));
+llvm::sys::path::replace_extension(TracePathReal, "json");
+arg += 

[clang] f5d9de8 - [Clang] Add a new clang option "-ftime-trace="

2022-07-15 Thread Junduo Dong via cfe-commits

Author: dongjunduo
Date: 2022-07-15T08:55:17-07:00
New Revision: f5d9de8cc33014923fbd1c5682758557887e85ba

URL: 
https://github.com/llvm/llvm-project/commit/f5d9de8cc33014923fbd1c5682758557887e85ba
DIFF: 
https://github.com/llvm/llvm-project/commit/f5d9de8cc33014923fbd1c5682758557887e85ba.diff

LOG: [Clang] Add a new clang option "-ftime-trace="

The time profiler traces the stages during the clang compile
process. Each compiling stage of a single source file
corresponds to a separately .json file which holds its
time tracing data. However, the .json files are stored in the
same path/directory as its corresponding stage's '-o' option.
For example, if we compile the "demo.cc" to "demo.o" with option
"-o /tmp/demo.o", the time trace data file path is "/tmp/demo.json".

A typical c++ project can contain multiple source files in different
path, but all the json files' paths can be a mess.

The option "-ftime-trace=" allows you to specify where the json
files should be stored. This allows the users to place the time trace
data files of interest in the desired location for further data analysis.

Usage:
- clang/clang++ -ftime-trace ...
- clang/clang++ -ftime-trace=the-directory-you-want ...
- clang/clang++ -ftime-trace=the-directory-you-want/ ...
- clang/clang++ -ftime-trace=the-full-file-path-you-want ...

Differential Revision: https://reviews.llvm.org/D128048

Added: 


Modified: 
clang/include/clang/Driver/Options.td
clang/include/clang/Frontend/FrontendOptions.h
clang/lib/Driver/ToolChains/Clang.cpp
clang/test/Driver/check-time-trace.cpp
clang/tools/driver/cc1_main.cpp

Removed: 




diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 404effb4e1de4..6d0736c02bdda 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2857,6 +2857,15 @@ def ftime_trace_granularity_EQ : Joined<["-"], 
"ftime-trace-granularity=">, Grou
   HelpText<"Minimum time granularity (in microseconds) traced by time 
profiler">,
   Flags<[CC1Option, CoreOption]>,
   MarshallingInfoInt, "500u">;
+def ftime_trace_EQ : Joined<["-"], "ftime-trace=">, Group,
+  HelpText<"Turn on time profiler. Generates JSON file based on output 
filename. "
+   "Specify the path which stores the tracing output file.">,
+  DocBrief<[{
+  Turn on time profiler. Generates JSON file based on output filename. Results
+  can be analyzed with chrome://tracing or `Speedscope App
+  `_ for flamegraph visualization.}]>,
+  Flags<[CC1Option, CoreOption]>,
+  MarshallingInfoString>;
 def fproc_stat_report : Joined<["-"], "fproc-stat-report">, Group,
   HelpText<"Print subprocess statistics">;
 def fproc_stat_report_EQ : Joined<["-"], "fproc-stat-report=">, Group,

diff  --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index ff5a9c5c77f4d..b0e719ffcacf5 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -499,6 +499,9 @@ class FrontendOptions {
   /// Minimum time granularity (in microseconds) traced by time profiler.
   unsigned TimeTraceGranularity;
 
+  /// Path which stores the output files for -ftime-trace
+  std::string TimeTracePath;
+
 public:
   FrontendOptions()
   : DisableFree(false), RelocatablePCH(false), ShowHelp(false),

diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 3e36d4def2f33..72a1250f89568 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -6230,6 +6230,7 @@ void Clang::ConstructJob(Compilation , const JobAction 
,
   Args.AddLastArg(CmdArgs, options::OPT_ftime_report_EQ);
   Args.AddLastArg(CmdArgs, options::OPT_ftime_trace);
   Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_granularity_EQ);
+  Args.AddLastArg(CmdArgs, options::OPT_ftime_trace_EQ);
   Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
   Args.AddLastArg(CmdArgs, options::OPT_malign_double);
   Args.AddLastArg(CmdArgs, options::OPT_fno_temp_file);

diff  --git a/clang/test/Driver/check-time-trace.cpp 
b/clang/test/Driver/check-time-trace.cpp
index 1d80748a52331..52b3e71394fba 100644
--- a/clang/test/Driver/check-time-trace.cpp
+++ b/clang/test/Driver/check-time-trace.cpp
@@ -2,6 +2,20 @@
 // RUN: cat %T/check-time-trace.json \
 // RUN:   | %python -c 'import json, sys; 
json.dump(json.loads(sys.stdin.read()), sys.stdout, sort_keys=True, indent=2)' \
 // RUN:   | FileCheck %s
+// RUN: %clangxx -S -ftime-trace=%T/new-name.json -ftime-trace-granularity=0 
-o %T/check-time-trace %s
+// RUN: cat %T/new-name.json \
+// RUN:   | %python -c 'import json, sys; 
json.dump(json.loads(sys.stdin.read()), sys.stdout, sort_keys=True, indent=2)' \
+// RUN:   | FileCheck %s
+// RUN: rm -rf %T/output1 && mkdir %T/output1
+// RUN: