[clang] [Frontend] Consolidate frontend timer setup in CompilerInstance::ExecuteAction. NFC (PR #192266)
Vassil Vassilev via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 15 07:28:52 PDT 2026
https://github.com/vgvassilev created https://github.com/llvm/llvm-project/pull/192266
Move the frontend timer creation (-ftime-report) and TimeTraceScope ("ExecuteCompiler") from cc1_main into CompilerInstance::ExecuteAction via a new private PrepareForExecution() method. This ensures all tools that use ExecuteAction (cc1, clang-repl, libclang, etc.) get consistent timing infrastructure without duplicating setup code.
>From 5d0df7a4c6a9e8c9f7dc121797a7b06f3ec907be Mon Sep 17 00:00:00 2001
From: Vassil Vassilev <v.g.vassilev at gmail.com>
Date: Wed, 15 Apr 2026 14:18:11 +0000
Subject: [PATCH] [Frontend] Consolidate frontend timer setup in
CompilerInstance::ExecuteAction. NFC
Move the frontend timer creation (-ftime-report) and TimeTraceScope
("ExecuteCompiler") from cc1_main into CompilerInstance::ExecuteAction via a
new private PrepareForExecution() method. This ensures all tools that use
ExecuteAction (cc1, clang-repl, libclang, etc.) get consistent timing
infrastructure without duplicating setup code.
---
.../include/clang/Frontend/CompilerInstance.h | 14 +++++++++++++
clang/lib/Frontend/CompilerInstance.cpp | 20 +++++++++++++++++++
clang/test/Interpreter/ftime-report.cpp | 5 +++++
clang/tools/driver/cc1_main.cpp | 9 +--------
4 files changed, 40 insertions(+), 8 deletions(-)
create mode 100644 clang/test/Interpreter/ftime-report.cpp
diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h
index 01f83498d8c8e..6d5c083b3af6c 100644
--- a/clang/include/clang/Frontend/CompilerInstance.h
+++ b/clang/include/clang/Frontend/CompilerInstance.h
@@ -220,6 +220,13 @@ class CompilerInstance : public ModuleLoader {
/// @name High-Level Operations
/// @{
+ // FIXME: Add a static InitializeProcess() method to consolidate process-level
+ // setup that is currently scattered across tool entry points (cc1_main,
+ // clang-repl, libclang, etc.). This would include things like AsmParsers and
+ // install_fatal_error_handler.
+ // These are process-global, so a single static method would all clang-based
+ // tools to share them without duplication.
+
/// ExecuteAction - Execute the provided action against the compiler's
/// CompilerInvocation object.
///
@@ -812,6 +819,13 @@ class CompilerInstance : public ModuleLoader {
bool UseTemporary, bool CreateMissingDirectories = false);
private:
+ /// Prepare the CompilerInstance for executing a frontend action.
+ ///
+ /// Called by ExecuteAction. Consolidates instance-level setup that was
+ /// previously duplicated across tool entry points (cc1_main,
+ /// clang-repl/Interpreter, etc.).
+ void PrepareForExecution();
+
/// Create a new output file and add it to the list of tracked output files.
///
/// If \p OutputPath is empty, then createOutputFile will derive an output
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index 0b00ad7128c00..52d5ef0faae67 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -944,11 +944,31 @@ bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input,
// High-Level Operations
+void CompilerInstance::PrepareForExecution() {
+ // Set up the frontend timer for -ftime-report. BackendConsumer uses
+ // getTimerGroup() and getFrontendTimer() when TimePasses is set. In the
+ // cc1 driver path this was done in cc1_main before calling
+ // ExecuteCompilerInvocation; we consolidate it here so that all tools
+ // (cc1, clang-repl, libclang, etc.) get consistent behavior.
+ if (getCodeGenOpts().TimePasses && !FrontendTimer) {
+ createFrontendTimer();
+ getFrontendTimer().startTimer();
+ }
+
+ // FIXME: Consider consolidating additional per-instance setup here:
+ // - llvm::timeTraceProfilerInitialize) when TimeTracePath is set.
+ // - Plugin loading (LoadRequestedPlugins) and -mllvm argument processing.
+}
+
bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
+ llvm::TimeTraceScope TimeScope("ExecuteCompiler");
+
+ PrepareForExecution();
+
// Mark this point as the bottom of the stack if we don't have somewhere
// better. We generally expect frontend actions to be invoked with (nearly)
// DesiredStackSpace available.
diff --git a/clang/test/Interpreter/ftime-report.cpp b/clang/test/Interpreter/ftime-report.cpp
new file mode 100644
index 0000000000000..1c19a9bf0e79b
--- /dev/null
+++ b/clang/test/Interpreter/ftime-report.cpp
@@ -0,0 +1,5 @@
+// Tests that -ftime-report works with clang-repl without crashing.
+// RUN: clang-repl -Xcc -ftime-report "int x = 42;" 2>&1 | FileCheck %s
+// CHECK-NOT: Assertion
+// CHECK-NOT: PLEASE submit a bug report
+// CHECK: Clang time report
diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp
index 0a9ded1cf213a..35405044d8d37 100644
--- a/clang/tools/driver/cc1_main.cpp
+++ b/clang/tools/driver/cc1_main.cpp
@@ -293,14 +293,7 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
return 1;
// Execute the frontend actions.
- {
- llvm::TimeTraceScope TimeScope("ExecuteCompiler");
- bool TimePasses = Clang->getCodeGenOpts().TimePasses;
- if (TimePasses)
- Clang->createFrontendTimer();
- llvm::TimeRegion Timer(TimePasses ? &Clang->getFrontendTimer() : nullptr);
- Success = ExecuteCompilerInvocation(Clang.get());
- }
+ Success = ExecuteCompilerInvocation(Clang.get());
// If any timers were active but haven't been destroyed yet, print their
// results now. This happens in -disable-free mode.
More information about the cfe-commits
mailing list