dongjunduo updated this revision to Diff 451874. dongjunduo added a comment.
format cc1_main.cpp Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D131469/new/ https://reviews.llvm.org/D131469 Files: clang/lib/Driver/Driver.cpp clang/test/Driver/check-time-trace.cpp clang/tools/driver/cc1_main.cpp
Index: clang/tools/driver/cc1_main.cpp =================================================================== --- clang/tools/driver/cc1_main.cpp +++ clang/tools/driver/cc1_main.cpp @@ -256,18 +256,13 @@ llvm::TimerGroup::clearAll(); 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 - SmallString<128> TracePath(Clang->getFrontendOpts().TimeTracePath); - if (llvm::sys::fs::is_directory(TracePath)) - llvm::sys::path::append(TracePath, llvm::sys::path::filename(Path)); - Path.assign(TracePath); - } - if (auto profilerOutput = Clang->createOutputFile( - Path.str(), /*Binary=*/false, /*RemoveFileOnSignal=*/false, - /*useTemporary=*/false)) { + SmallString<128> TracePath(Clang->getFrontendOpts().TimeTracePath); + if (TracePath.empty()) { + llvm::errs() << "-ftime-trace=<path> is empty!\n"; + } else if (auto profilerOutput = + Clang->createOutputFile(TracePath.str(), /*Binary=*/false, + /*RemoveFileOnSignal=*/false, + /*useTemporary=*/false)) { llvm::timeTraceProfilerWrite(*profilerOutput); profilerOutput.reset(); llvm::timeTraceProfilerCleanup(); Index: clang/test/Driver/check-time-trace.cpp =================================================================== --- clang/test/Driver/check-time-trace.cpp +++ clang/test/Driver/check-time-trace.cpp @@ -1,3 +1,9 @@ +// RUN: rm -rf %T/exe && mkdir %T/exe +// RUN: %clangxx -ftime-trace -ftime-trace-granularity=0 -o %T/exe/check-time-trace %s -### +// RUN: %clangxx -ftime-trace -ftime-trace-granularity=0 -o %T/exe/check-time-trace %s +// RUN: cat %T/exe/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 -ftime-trace-granularity=0 -o %T/check-time-trace %s // 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)' \ Index: clang/lib/Driver/Driver.cpp =================================================================== --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -4658,6 +4658,87 @@ llvm_unreachable("invalid phase in ConstructPhaseAction"); } +// Infer data storing path of the options `-ftime-trace`, `-ftime-trace=<path>` +void InferTimeTracePath(Compilation &C) { + bool TimeTrace = C.getArgs().getLastArg(options::OPT_ftime_trace) != nullptr; + bool TimeTraceFile = + C.getArgs().getLastArg(options::OPT_ftime_trace_EQ) != nullptr; + // if `-ftime-trace` or `-ftime-trace=<path>` are specified + if (TimeTrace || TimeTraceFile) { + SmallString<128> TracePath(""); + // get the linking executable file's parent path, default is "." + // e.g. executable file's path: /usr/local/a.out + // its parent's path: /usr/local + for (auto &J : C.getJobs()) { + if (J.getSource().getKind() == Action::LinkJobClass && + !J.getOutputFilenames().empty()) { + if (llvm::sys::path::has_parent_path( + SmallString<128>(J.getOutputFilenames()[0].c_str()))) { + TracePath = llvm::sys::path::parent_path( + SmallString<128>(J.getOutputFilenames()[0].c_str())); + } else { + TracePath = SmallString<128>("."); + } + break; + } + } + // Add or replace -ftime-trace<path>` to the correct one to all clang jobs + for (auto &J : C.getJobs()) { + if (J.getSource().getKind() == Action::AssembleJobClass || + J.getSource().getKind() == Action::BackendJobClass) { + SmallString<128> OutputPath(J.getOutputFilenames()[0].c_str()); + std::string arg = std::string("-ftime-trace="); + if (!TimeTraceFile) { + if (TracePath.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(TracePath, + llvm::sys::path::filename(OutputPath)); + llvm::sys::path::replace_extension(TracePath, "json"); + arg += std::string(TracePath.c_str()); + } + } else { + // /full_file_path_specified or /path_specified/yyy.json + TracePath = SmallString<128>( + C.getArgs().getLastArg(options::OPT_ftime_trace_EQ)->getValue()); + if (llvm::sys::fs::is_directory(TracePath)) + llvm::sys::path::append(TracePath, + llvm::sys::path::filename(OutputPath)); + llvm::sys::path::replace_extension(TracePath, "json"); + arg += std::string(TracePath.c_str()); + } + + const std::string::size_type size = arg.size(); + char *buffer = new char[size + 1]; + memcpy(buffer, arg.c_str(), size + 1); + + // replace `-ftime-trace=<path>` + auto &JArgs = J.getArguments(); + bool exist = false; + for (unsigned I = 0; I < JArgs.size(); ++I) { + if (StringRef(JArgs[I]).startswith("-ftime-trace=")) { + ArgStringList NewArgs(JArgs.begin(), JArgs.begin() + I); + NewArgs.push_back(buffer); + NewArgs.append(JArgs.begin() + I + 1, JArgs.end()); + J.replaceArguments(NewArgs); + exist = true; + break; + } + } + // `-ftime-trace=<path>` is not found, add it directly + if (!exist) { + ArgStringList NewArgs(JArgs.begin(), JArgs.end()); + NewArgs.push_back(buffer); + J.replaceArguments(NewArgs); + } + } + } + } +} + void Driver::BuildJobs(Compilation &C) const { llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); @@ -4736,6 +4817,9 @@ /*TargetDeviceOffloadKind*/ Action::OFK_None); } + // set data storing path of the options `-ftime-trace`, `-ftime-trace=<path>` + InferTimeTracePath(C); + // If we have more than one job, then disable integrated-cc1 for now. Do this // also when we need to report process execution statistics. if (C.getJobs().size() > 1 || CCPrintProcessStats)
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits