diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2828,6 +2828,10 @@ HelpText<"Minimum time granularity (in microseconds) traced by time profiler">, Flags<[CC1Option, CoreOption]>, MarshallingInfoInt, "500u">; +def ftime_trace_path : Joined<["-"], "ftime-trace-path=">, Group, + HelpText<"Directory which stores the output files for -ftime-trace">, + 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 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -499,6 +499,9 @@ /// Minimum time granularity (in microseconds) traced by time profiler. unsigned TimeTraceGranularity; + /// Directory 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 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6183,6 +6183,7 @@ 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_path); 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/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp --- a/clang/tools/driver/cc1_main.cpp +++ b/clang/tools/driver/cc1_main.cpp @@ -255,7 +255,16 @@ if (llvm::timeTraceProfilerEnabled()) { SmallString<128> Path(Clang->getFrontendOpts().OutputFile); - llvm::sys::path::replace_extension(Path, "json"); + if (Clang->getFrontendOpts().TimeTracePath.empty()) { + // replace the suffix to '.json' directly + llvm::sys::path::replace_extension(Path, "json"); + } else { + // change path prefix to the aim path, then add suffix '.json' + Path.assign(Clang->getFrontendOpts().TimeTracePath); + Path.append( + llvm::sys::path::filename(Clang->getFrontendOpts().OutputFile)); + Path.append(".json"); + } if (auto profilerOutput = Clang->createOutputFile( Path.str(), /*Binary=*/false, /*RemoveFileOnSignal=*/false, /*useTemporary=*/false)) {