Просмотр исходного кода

Migrate all CARBON_VLOG to the format string variant. (#4284)

This mostly uses a hilarious set of regular expressions to mechanically
switch all but two uses, and then manually fixed the last two. There
weren't too many.

Also simplifies the `vlog` implementation now that it's all going
through a format string.

This alone has a nice impact on parse and check of about 2% and 1%
respectively. The impact on lex in my timings looks like noise (no
change in instruction count, unlike the other phases).
```
name                                               old cpu/op   new cpu/op   delta
BM_CompileAPIFileDenseDecls<Phase::Lex>/256        39.1µs ± 3%  38.1µs ± 2%  -2.42%  (p=0.000 n=20+19)
BM_CompileAPIFileDenseDecls<Phase::Lex>/1024        187µs ± 3%   183µs ± 1%  -2.30%  (p=0.000 n=20+20)
BM_CompileAPIFileDenseDecls<Phase::Lex>/4096        776µs ± 4%   756µs ± 1%  -2.62%  (p=0.000 n=20+20)
BM_CompileAPIFileDenseDecls<Phase::Lex>/16384      3.36ms ± 1%  3.33ms ± 1%  -0.90%  (p=0.000 n=18+18)
BM_CompileAPIFileDenseDecls<Phase::Lex>/65536      14.4ms ± 2%  14.2ms ± 1%  -1.41%  (p=0.000 n=20+20)
BM_CompileAPIFileDenseDecls<Phase::Lex>/262144     65.7ms ± 1%  65.2ms ± 2%  -0.86%  (p=0.002 n=19+20)
BM_CompileAPIFileDenseDecls<Phase::Parse>/256      87.5µs ± 1%  86.3µs ± 1%  -1.43%  (p=0.000 n=19+20)
BM_CompileAPIFileDenseDecls<Phase::Parse>/1024      438µs ± 2%   431µs ± 1%  -1.54%  (p=0.000 n=19+20)
BM_CompileAPIFileDenseDecls<Phase::Parse>/4096     1.81ms ± 2%  1.77ms ± 1%  -2.12%  (p=0.000 n=20+20)
BM_CompileAPIFileDenseDecls<Phase::Parse>/16384    7.54ms ± 1%  7.43ms ± 1%  -1.44%  (p=0.000 n=19+20)
BM_CompileAPIFileDenseDecls<Phase::Parse>/65536    31.2ms ± 1%  30.6ms ± 1%  -2.03%  (p=0.000 n=20+20)
BM_CompileAPIFileDenseDecls<Phase::Parse>/262144    133ms ± 1%   130ms ± 1%  -1.85%  (p=0.000 n=20+20)
BM_CompileAPIFileDenseDecls<Phase::Check>/256       882µs ± 1%   878µs ± 1%  -0.52%  (p=0.001 n=17+19)
BM_CompileAPIFileDenseDecls<Phase::Check>/1024     1.90ms ± 2%  1.88ms ± 1%  -1.17%  (p=0.000 n=19+19)
BM_CompileAPIFileDenseDecls<Phase::Check>/4096     5.85ms ± 2%  5.76ms ± 1%  -1.43%  (p=0.000 n=20+19)
BM_CompileAPIFileDenseDecls<Phase::Check>/16384    22.2ms ± 2%  21.9ms ± 2%  -1.20%  (p=0.000 n=20+19)
BM_CompileAPIFileDenseDecls<Phase::Check>/65536    91.2ms ± 2%  90.3ms ± 1%  -1.00%  (p=0.000 n=20+19)
BM_CompileAPIFileDenseDecls<Phase::Check>/262144    382ms ± 1%   380ms ± 1%  -0.51%  (p=0.003 n=18+19)
```
Chandler Carruth 1 год назад
Родитель
Сommit
0c8ab663c9

+ 0 - 1
common/BUILD

@@ -570,7 +570,6 @@ cc_library(
 
 cc_library(
     name = "vlog",
-    srcs = ["vlog_internal.h"],
     hdrs = ["vlog.h"],
     deps = [
         ":ostream",

+ 24 - 9
common/vlog.h

@@ -5,9 +5,26 @@
 #ifndef CARBON_COMMON_VLOG_H_
 #define CARBON_COMMON_VLOG_H_
 
-#include "common/vlog_internal.h"
+#include "common/ostream.h"
+#include "common/template_string.h"
+#include "llvm/Support/FormatVariadic.h"
 
-namespace Carbon {
+namespace Carbon::Internal {
+
+// Implements verbose logging.
+//
+// This is designed to minimize the overhead in callers by being a
+// forcibly-outlined routine that takes a minimal number of parameters.
+//
+// Internally uses `llvm::formatv` to render the format string with any value
+// arguments, and streams the result to the provided stream.
+template <TemplateString FormatStr, typename... Ts>
+[[clang::noinline, clang::preserve_most]] auto VLogImpl(
+    llvm::raw_ostream* stream, Ts&&... values) -> void {
+  *stream << llvm::formatv(FormatStr.c_str(), std::forward<Ts>(values)...);
+}
+
+}  // namespace Carbon::Internal
 
 // Logs when verbose logging is enabled (vlog_stream_ is non-null).
 //
@@ -25,12 +42,10 @@ namespace Carbon {
 // However, the streaming syntax has higher overhead and can inhibit inlining.
 // Code should prefer the format string form, and eventually when all code has
 // migrated the streaming interface will be removed.
-#define CARBON_VLOG(...)                                                    \
-  __builtin_expect(vlog_stream_ == nullptr, true)                           \
-      ? (void)0                                                             \
-      : CARBON_VLOG_INTERNAL##__VA_OPT__(_CALL)(vlog_stream_ __VA_OPT__(, ) \
-                                                    __VA_ARGS__)
-
-}  // namespace Carbon
+#define CARBON_VLOG(FormatStr, ...)                                          \
+  __builtin_expect(vlog_stream_ == nullptr, true)                            \
+      ? (void)0                                                              \
+      : Carbon::Internal::VLogImpl<"" FormatStr>(vlog_stream_ __VA_OPT__(, ) \
+                                                     __VA_ARGS__)
 
 #endif  // CARBON_COMMON_VLOG_H_

+ 0 - 75
common/vlog_internal.h

@@ -1,75 +0,0 @@
-// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
-// Exceptions. See /LICENSE for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-#ifndef CARBON_COMMON_VLOG_INTERNAL_H_
-#define CARBON_COMMON_VLOG_INTERNAL_H_
-
-#include "common/ostream.h"
-#include "common/template_string.h"
-#include "llvm/Support/FormatVariadic.h"
-
-namespace Carbon::Internal {
-
-// Wraps a stream and exiting for fatal errors. Should only be used by check.h
-// macros.
-//
-// TODO: Remove this when the last streaming `vlog` is replaced with a function
-// call variant.
-class VLoggingStream {
- public:
-  // Internal type used in macros to dispatch to the `operator|` overload.
-  struct Helper {};
-
-  explicit VLoggingStream(llvm::raw_ostream* stream)
-      // Prefix the buffer with the current bug report message.
-      : stream_(stream) {}
-
-  ~VLoggingStream() = default;
-
-  // If the bool cast occurs, it's because the condition is false. This supports
-  // && short-circuiting the creation of ExitingStream.
-  explicit operator bool() const { return true; }
-
-  // Forward output to llvm::errs.
-  template <typename T>
-  auto operator<<(const T& message) -> VLoggingStream& {
-    *stream_ << message;
-    return *this;
-  }
-
-  // Low-precedence binary operator overload used in vlog.h macros.
-  friend auto operator|(Helper /*helper*/, VLoggingStream& /*stream*/) -> void {
-  }
-
- private:
-  llvm::raw_ostream* stream_;
-};
-
-// Implements verbose logging.
-//
-// This is designed to minimize the overhead in callers by being a
-// forcibly outlined routine that takes a minimal number of parameters.
-//
-// Internally uses `llvm::formatv` to render the format string with any value
-// arguments, and streams the result to the provided stream.
-template <TemplateString FormatStr, typename... Ts>
-[[gnu::cold, clang::noinline, clang::preserve_most]] auto VLogImpl(
-    llvm::raw_ostream* stream, Ts&&... values) -> void {
-  *stream << llvm::formatv(FormatStr.c_str(), std::forward<Ts>(values)...);
-}
-
-}  // namespace Carbon::Internal
-
-// Raw logging stream. This should be used when building the streaming forms of
-// vlog macros.
-#define CARBON_VLOG_INTERNAL(stream)           \
-  Carbon::Internal::VLoggingStream::Helper() | \
-      Carbon::Internal::VLoggingStream(stream)
-
-// Raw logging call. This should be used when building the format-string forms
-// of vlog macros.
-#define CARBON_VLOG_INTERNAL_CALL(stream, FormatStr, ...) \
-  Carbon::Internal::VLogImpl<"" FormatStr>(stream __VA_OPT__(, ) __VA_ARGS__)
-
-#endif  // CARBON_COMMON_VLOG_INTERNAL_H_

+ 0 - 3
common/vlog_test.cpp

@@ -26,7 +26,6 @@ class VLogger {
 
   void VLog() { CARBON_VLOG("Test\n"); }
   void VLogFormatArgs() { CARBON_VLOG("Test {0} {1} {2}\n", 1, 2, 3); }
-  void VLogStream() { CARBON_VLOG() << "Test\n"; }
 
   auto TakeStr() -> std::string { return buffer_.TakeStr(); }
 
@@ -42,8 +41,6 @@ TEST(VLogTest, Enabled) {
   EXPECT_THAT(vlog.TakeStr(), StrEq("Test\n"));
   vlog.VLogFormatArgs();
   EXPECT_THAT(vlog.TakeStr(), StrEq("Test 1 2 3\n"));
-  vlog.VLogStream();
-  EXPECT_THAT(vlog.TakeStr(), StrEq("Test\n"));
 }
 
 TEST(VLogTest, Disabled) {

+ 16 - 18
toolchain/check/check.cpp

@@ -503,8 +503,8 @@ class DeferredDefinitionWorklist {
       -> void {
     worklist_.push_back(CheckSkippedDefinition{
         index, HandleFunctionDefinitionSuspend(context, node_id)});
-    CARBON_VLOG() << VlogPrefix << "Push CheckSkippedDefinition " << index.index
-                  << "\n";
+    CARBON_VLOG("{0}Push CheckSkippedDefinition {1}\n", VlogPrefix,
+                index.index);
   }
 
   // Push a task to re-enter a function scope, so that functions defined within
@@ -519,8 +519,8 @@ class DeferredDefinitionWorklist {
     worklist_.push_back(
         EnterDeferredDefinitionScope{.suspended_name = std::nullopt,
                                      .in_deferred_definition_scope = nested});
-    CARBON_VLOG() << VlogPrefix << "Push EnterDeferredDefinitionScope "
-                  << (nested ? "(nested)" : "(non-nested)") << "\n";
+    CARBON_VLOG("{0}Push EnterDeferredDefinitionScope {1}\n", VlogPrefix,
+                nested ? "(nested)" : "(non-nested)");
   }
 
   // Suspend the current deferred definition scope, which is finished but still
@@ -535,19 +535,18 @@ class DeferredDefinitionWorklist {
       VariantMatch(
           worklist_.back(),
           [&](CheckSkippedDefinition& definition) {
-            CARBON_VLOG() << VlogPrefix << "Handle CheckSkippedDefinition "
-                          << definition.definition_index.index << "\n";
+            CARBON_VLOG("{0}Handle CheckSkippedDefinition {1}\n", VlogPrefix,
+                        definition.definition_index.index);
           },
           [&](EnterDeferredDefinitionScope& enter) {
             CARBON_CHECK(enter.in_deferred_definition_scope);
-            CARBON_VLOG() << VlogPrefix
-                          << "Handle EnterDeferredDefinitionScope (nested)\n";
+            CARBON_VLOG("{0}Handle EnterDeferredDefinitionScope (nested)\n",
+                        VlogPrefix);
           },
           [&](LeaveDeferredDefinitionScope& leave) {
             bool nested = leave.in_deferred_definition_scope;
-            CARBON_VLOG() << VlogPrefix
-                          << "Handle LeaveDeferredDefinitionScope "
-                          << (nested ? "(nested)" : "(non-nested)") << "\n";
+            CARBON_VLOG("{0}Handle LeaveDeferredDefinitionScope {1}\n",
+                        VlogPrefix, nested ? "(nested)" : "(non-nested)");
           });
     }
 
@@ -592,7 +591,7 @@ auto DeferredDefinitionWorklist::SuspendFinishedScopeAndPush(Context& context)
   if (start_index == worklist_.size() - 1) {
     context.decl_name_stack().PopScope();
     worklist_.pop_back();
-    CARBON_VLOG() << VlogPrefix << "Pop EnterDeferredDefinitionScope (empty)\n";
+    CARBON_VLOG("{0}Pop EnterDeferredDefinitionScope (empty)\n", VlogPrefix);
     return false;
   }
 
@@ -607,8 +606,7 @@ auto DeferredDefinitionWorklist::SuspendFinishedScopeAndPush(Context& context)
     // Enqueue a task to leave the nested scope.
     worklist_.push_back(
         LeaveDeferredDefinitionScope{.in_deferred_definition_scope = true});
-    CARBON_VLOG() << VlogPrefix
-                  << "Push LeaveDeferredDefinitionScope (nested)\n";
+    CARBON_VLOG("{0}Push LeaveDeferredDefinitionScope (nested)\n", VlogPrefix);
     return false;
   }
 
@@ -617,8 +615,8 @@ auto DeferredDefinitionWorklist::SuspendFinishedScopeAndPush(Context& context)
   // scope and end checking deferred definitions.
   worklist_.push_back(
       LeaveDeferredDefinitionScope{.in_deferred_definition_scope = false});
-  CARBON_VLOG() << VlogPrefix
-                << "Push LeaveDeferredDefinitionScope (non-nested)\n";
+  CARBON_VLOG("{0}Push LeaveDeferredDefinitionScope (non-nested)\n",
+              VlogPrefix);
 
   // We'll process the worklist in reverse index order, so reverse the part of
   // it we're about to execute so we run our tasks in the order in which they
@@ -632,8 +630,8 @@ auto DeferredDefinitionWorklist::SuspendFinishedScopeAndPush(Context& context)
       holds_alternative<EnterDeferredDefinitionScope>(worklist_.back()))
       << "Unexpected task in worklist.";
   worklist_.pop_back();
-  CARBON_VLOG() << VlogPrefix
-                << "Handle EnterDeferredDefinitionScope (non-nested)\n";
+  CARBON_VLOG("{0}Handle EnterDeferredDefinitionScope (non-nested)\n",
+              VlogPrefix);
   return true;
 }
 

+ 9 - 10
toolchain/check/context.cpp

@@ -101,8 +101,8 @@ auto Context::FinishInst(SemIR::InstId inst_id, SemIR::Inst inst) -> void {
   auto const_id = TryEvalInst(*this, inst_id, inst);
   constant_values().Set(inst_id, const_id);
   if (const_id.is_constant()) {
-    CARBON_VLOG() << "Constant: " << inst << " -> "
-                  << constant_values().GetInstId(const_id) << "\n";
+    CARBON_VLOG("Constant: {0} -> {1}\n", inst,
+                constant_values().GetInstId(const_id));
 
     // If the constant value is symbolic, track that we need to substitute into
     // it.
@@ -150,7 +150,7 @@ auto Context::CheckCompatibleImportedNodeKind(
 auto Context::AddInstInNoBlock(SemIR::LocIdAndInst loc_id_and_inst)
     -> SemIR::InstId {
   auto inst_id = sem_ir().insts().AddInNoBlock(loc_id_and_inst);
-  CARBON_VLOG() << "AddInst: " << loc_id_and_inst.inst << "\n";
+  CARBON_VLOG("AddInst: {0}\n", loc_id_and_inst.inst);
   FinishInst(inst_id, loc_id_and_inst.inst);
   return inst_id;
 }
@@ -164,7 +164,7 @@ auto Context::AddInst(SemIR::LocIdAndInst loc_id_and_inst) -> SemIR::InstId {
 auto Context::AddPlaceholderInstInNoBlock(SemIR::LocIdAndInst loc_id_and_inst)
     -> SemIR::InstId {
   auto inst_id = sem_ir().insts().AddInNoBlock(loc_id_and_inst);
-  CARBON_VLOG() << "AddPlaceholderInst: " << loc_id_and_inst.inst << "\n";
+  CARBON_VLOG("AddPlaceholderInst: {0}\n", loc_id_and_inst.inst);
   constant_values().Set(inst_id, SemIR::ConstantId::Invalid);
   return inst_id;
 }
@@ -179,22 +179,21 @@ auto Context::AddPlaceholderInst(SemIR::LocIdAndInst loc_id_and_inst)
 auto Context::AddConstant(SemIR::Inst inst, bool is_symbolic)
     -> SemIR::ConstantId {
   auto const_id = constants().GetOrAdd(inst, is_symbolic);
-  CARBON_VLOG() << "AddConstant: " << inst << "\n";
+  CARBON_VLOG("AddConstant: {0}\n", inst);
   return const_id;
 }
 
 auto Context::ReplaceLocIdAndInstBeforeConstantUse(
     SemIR::InstId inst_id, SemIR::LocIdAndInst loc_id_and_inst) -> void {
   sem_ir().insts().SetLocIdAndInst(inst_id, loc_id_and_inst);
-  CARBON_VLOG() << "ReplaceInst: " << inst_id << " -> " << loc_id_and_inst.inst
-                << "\n";
+  CARBON_VLOG("ReplaceInst: {0} -> {1}\n", inst_id, loc_id_and_inst.inst);
   FinishInst(inst_id, loc_id_and_inst.inst);
 }
 
 auto Context::ReplaceInstBeforeConstantUse(SemIR::InstId inst_id,
                                            SemIR::Inst inst) -> void {
   sem_ir().insts().Set(inst_id, inst);
-  CARBON_VLOG() << "ReplaceInst: " << inst_id << " -> " << inst << "\n";
+  CARBON_VLOG("ReplaceInst: {0} -> {1}\n", inst_id, inst);
   FinishInst(inst_id, inst);
 }
 
@@ -678,8 +677,8 @@ auto Context::SetBlockArgResultBeforeConstantUse(SemIR::InstId select_id,
   }
 
   if (const_id.is_constant()) {
-    CARBON_VLOG() << "Constant: " << insts().Get(select_id) << " -> "
-                  << constant_values().GetInstId(const_id) << "\n";
+    CARBON_VLOG("Constant: {0} -> {1}\n", insts().Get(select_id),
+                constant_values().GetInstId(const_id));
     constant_values().Set(select_id, const_id);
   }
 }

+ 3 - 3
toolchain/check/inst_block_stack.cpp

@@ -11,7 +11,7 @@
 namespace Carbon::Check {
 
 auto InstBlockStack::Push(SemIR::InstBlockId id) -> void {
-  CARBON_VLOG() << name_ << " Push " << id_stack_.size() << "\n";
+  CARBON_VLOG("{0} Push {1}\n", name_, id_stack_.size());
   CARBON_CHECK(id_stack_.size() < (1 << 20))
       << "Excessive stack size: likely infinite loop";
   id_stack_.push_back(id);
@@ -50,7 +50,7 @@ auto InstBlockStack::Pop() -> SemIR::InstBlockId {
 
   insts_stack_.PopArray();
 
-  CARBON_VLOG() << name_ << " Pop " << id_stack_.size() << ": " << id << "\n";
+  CARBON_VLOG("{0} Pop {1}: {2}\n", name_, id_stack_.size(), id);
   return id.is_valid() ? id : SemIR::InstBlockId::Empty;
 }
 
@@ -58,7 +58,7 @@ auto InstBlockStack::PopAndDiscard() -> void {
   CARBON_CHECK(!empty()) << "no current block";
   id_stack_.pop_back();
   insts_stack_.PopArray();
-  CARBON_VLOG() << name_ << " PopAndDiscard " << id_stack_.size() << "\n";
+  CARBON_VLOG("{0} PopAndDiscard {1}\n", name_, id_stack_.size());
 }
 
 auto InstBlockStack::PrintForStackDump(SemIR::Formatter& formatter, int indent,

+ 7 - 9
toolchain/check/node_stack.h

@@ -81,8 +81,7 @@ class NodeStack {
     auto kind = parse_tree_->node_kind(node_id);
     CARBON_CHECK(NodeKindToIdKind(kind) == Id::Kind::None)
         << "Parse kind expects an Id: " << kind;
-    CARBON_VLOG() << "Node Push " << stack_.size() << ": " << kind
-                  << " -> <none>\n";
+    CARBON_VLOG("Node Push {0}: {1} -> <none>\n", stack_.size(), kind);
     CARBON_CHECK(stack_.size() < (1 << 20))
         << "Excessive stack size: likely infinite loop";
     stack_.push_back({.node_id = node_id, .id = Id()});
@@ -97,8 +96,7 @@ class NodeStack {
         << "\n";
     CARBON_CHECK(id.is_valid())
         << "Push called with invalid id: " << parse_tree_->node_kind(node_id);
-    CARBON_VLOG() << "Node Push " << stack_.size() << ": " << kind << " -> "
-                  << id << "\n";
+    CARBON_VLOG("Node Push {0}: {1} -> {2}\n", stack_.size(), kind, id);
     CARBON_CHECK(stack_.size() < (1 << 20))
         << "Excessive stack size: likely infinite loop";
     stack_.push_back({.node_id = node_id, .id = Id(id)});
@@ -152,8 +150,8 @@ class NodeStack {
   // Pops the top of the stack without any verification.
   auto PopAndIgnore() -> void {
     Entry back = stack_.pop_back_val();
-    CARBON_VLOG() << "Node Pop " << stack_.size() << ": "
-                  << parse_tree_->node_kind(back.node_id) << " -> <ignored>\n";
+    CARBON_VLOG("Node Pop {0}: {1} -> <ignored>\n", stack_.size(),
+                parse_tree_->node_kind(back.node_id));
   }
 
   // Pops the top of the stack and returns the node_id.
@@ -674,9 +672,9 @@ class NodeStack {
   template <typename IdT>
   auto PopEntry() -> Entry {
     Entry back = stack_.pop_back_val();
-    CARBON_VLOG() << "Node Pop " << stack_.size() << ": "
-                  << parse_tree_->node_kind(back.node_id) << " -> "
-                  << back.id.template As<IdT>() << "\n";
+    CARBON_VLOG("Node Pop {0}: {1} -> {2}\n", stack_.size(),
+                parse_tree_->node_kind(back.node_id),
+                back.id.template As<IdT>());
     return back;
   }
 

+ 8 - 8
toolchain/driver/clang_runner.cpp

@@ -52,7 +52,7 @@ auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args) -> bool {
   }
 
   CARBON_CHECK(!args.empty());
-  CARBON_VLOG() << "Running Clang driver with arguments: \n";
+  CARBON_VLOG("Running Clang driver with arguments: \n");
 
   // Render the arguments into null-terminated C-strings for use by the Clang
   // driver. Command lines can get quite long in build systems so this tries to
@@ -82,10 +82,10 @@ auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args) -> bool {
     ++i;
   }
   for (const char* cstr_arg : llvm::ArrayRef(cstr_args)) {
-    CARBON_VLOG() << "    '" << cstr_arg << "'\n";
+    CARBON_VLOG("    '{0}'\n", cstr_arg);
   }
 
-  CARBON_VLOG() << "Preparing Clang driver...\n";
+  CARBON_VLOG("Preparing Clang driver...\n");
 
   // Create the diagnostic options and parse arguments controlling them out of
   // our arguments.
@@ -111,7 +111,7 @@ auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args) -> bool {
   // above. This makes it appear that our binary was in the installed binaries
   // directory, and allows finding tools relative to it.
   driver.Dir = installation_->llvm_install_bin();
-  CARBON_VLOG() << "Setting bin directory to: " << driver.Dir << "\n";
+  CARBON_VLOG("Setting bin directory to: {0}\n", driver.Dir);
 
   // TODO: Directly run in-process rather than using a subprocess. This is both
   // more efficient and makes debugging (much) easier. Needs code like:
@@ -124,7 +124,7 @@ auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args) -> bool {
     return false;
   }
 
-  CARBON_VLOG() << "Running Clang driver...\n";
+  CARBON_VLOG("Running Clang driver...\n");
 
   llvm::SmallVector<std::pair<int, const clang::driver::Command*>>
       failing_commands;
@@ -134,10 +134,10 @@ auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args) -> bool {
   // failures.
   diagnostic_client.finish();
 
-  CARBON_VLOG() << "Execution result code: " << result << "\n";
+  CARBON_VLOG("Execution result code: {0}\n", result);
   for (const auto& [command_result, failing_command] : failing_commands) {
-    CARBON_VLOG() << "Failing command '" << failing_command->getExecutable()
-                  << "' with code '" << command_result << "' was:\n";
+    CARBON_VLOG("Failing command '{0}' with code '{1}' was:\n",
+                failing_command->getExecutable(), command_result);
     if (vlog_stream_) {
       failing_command->Print(*vlog_stream_, "\n\n", /*Quote=*/true);
     }

+ 13 - 14
toolchain/driver/driver.cpp

@@ -547,8 +547,7 @@ class Driver::CompilationUnit {
       success_ = false;
       return;
     }
-    CARBON_VLOG() << "*** SourceBuffer ***\n```\n"
-                  << source_->text() << "\n```\n";
+    CARBON_VLOG("*** SourceBuffer ***\n```\n{0}\n```\n", source_->text());
 
     LogCall("Lex::Lex",
             [&] { tokens_ = Lex::Lex(value_stores_, *source_, *consumer_); });
@@ -559,7 +558,7 @@ class Driver::CompilationUnit {
     if (mem_usage_) {
       mem_usage_->Collect("tokens_", *tokens_);
     }
-    CARBON_VLOG() << "*** Lex::TokenizedBuffer ***\n" << tokens_;
+    CARBON_VLOG("*** Lex::TokenizedBuffer ***\n{0}", tokens_);
     if (tokens_->has_errors()) {
       success_ = false;
     }
@@ -584,7 +583,7 @@ class Driver::CompilationUnit {
     if (mem_usage_) {
       mem_usage_->Collect("parse_tree_", *parse_tree_);
     }
-    CARBON_VLOG() << "*** Parse::Tree ***\n" << parse_tree_;
+    CARBON_VLOG("*** Parse::Tree ***\n{0}", parse_tree_);
     if (parse_tree_->has_errors()) {
       success_ = false;
     }
@@ -618,7 +617,7 @@ class Driver::CompilationUnit {
     }
 
     if (options_.dump_raw_sem_ir && IncludeInDumps()) {
-      CARBON_VLOG() << "*** Raw SemIR::File ***\n" << *sem_ir_ << "\n";
+      CARBON_VLOG("*** Raw SemIR::File ***\n{0}\n", *sem_ir_);
       sem_ir_->Print(driver_->output_stream_, options_.builtin_sem_ir);
       if (options_.dump_sem_ir) {
         driver_->output_stream_ << "\n";
@@ -629,7 +628,7 @@ class Driver::CompilationUnit {
     if (vlog_stream_ || print) {
       SemIR::Formatter formatter(*tokens_, *parse_tree_, *sem_ir_);
       if (vlog_stream_) {
-        CARBON_VLOG() << "*** SemIR::File ***\n";
+        CARBON_VLOG("*** SemIR::File ***\n");
         formatter.Print(*vlog_stream_);
       }
       if (print) {
@@ -655,7 +654,7 @@ class Driver::CompilationUnit {
                                    &inst_namer, vlog_stream_);
     });
     if (vlog_stream_) {
-      CARBON_VLOG() << "*** llvm::Module ***\n";
+      CARBON_VLOG("*** llvm::Module ***\n");
       module_->print(*vlog_stream_, /*AAW=*/nullptr,
                      /*ShouldPreserveUseListOrder=*/false,
                      /*IsForDebug=*/true);
@@ -702,7 +701,7 @@ class Driver::CompilationUnit {
       return false;
     }
     if (vlog_stream_) {
-      CARBON_VLOG() << "*** Assembly ***\n";
+      CARBON_VLOG("*** Assembly ***\n");
       codegen->EmitAssembly(*vlog_stream_);
     }
 
@@ -739,7 +738,7 @@ class Driver::CompilationUnit {
         // Currently each unit overwrites the output from the previous one in
         // this case.
       }
-      CARBON_VLOG() << "Writing output to: " << output_filename << "\n";
+      CARBON_VLOG("Writing output to: {0}\n", output_filename);
 
       std::error_code ec;
       llvm::raw_fd_ostream output_file(output_filename, ec,
@@ -779,9 +778,9 @@ class Driver::CompilationUnit {
   // Wraps a call with log statements to indicate start and end.
   auto LogCall(llvm::StringLiteral label, llvm::function_ref<void()> fn)
       -> void {
-    CARBON_VLOG() << "*** " << label << ": " << input_filename_ << " ***\n";
+    CARBON_VLOG("*** {0}: {1} ***\n", label, input_filename_);
     fn();
-    CARBON_VLOG() << "*** " << label << " done ***\n";
+    CARBON_VLOG("*** {0} done ***\n", label);
   }
 
   // Returns true if the file can be dumped.
@@ -910,10 +909,10 @@ auto Driver::Compile(const CompileOptions& options,
     node_converters.emplace_back(unit.tokens, unit.tokens->source().filename(),
                                  unit.get_parse_tree_and_subtrees);
   }
-  CARBON_VLOG() << "*** Check::CheckParseTrees ***\n";
+  CARBON_VLOG("*** Check::CheckParseTrees ***\n");
   Check::CheckParseTrees(check_units, node_converters, options.prelude_import,
                          vlog_stream_);
-  CARBON_VLOG() << "*** Check::CheckParseTrees done ***\n";
+  CARBON_VLOG("*** Check::CheckParseTrees done ***\n");
   for (auto& unit : units) {
     if (unit->has_source()) {
       unit->PostCheck();
@@ -926,7 +925,7 @@ auto Driver::Compile(const CompileOptions& options,
   // Unlike previous steps, errors block further progress.
   if (std::any_of(units.begin(), units.end(),
                   [&](const auto& unit) { return !unit->success(); })) {
-    CARBON_VLOG() << "*** Stopping before lowering due to errors ***";
+    CARBON_VLOG("*** Stopping before lowering due to errors ***");
     return make_result();
   }
 

+ 1 - 1
toolchain/lower/file_context.cpp

@@ -340,7 +340,7 @@ auto FileContext::BuildFunctionDefinition(SemIR::FunctionId function_id)
 
   // Lower all blocks.
   for (auto block_id : body_block_ids) {
-    CARBON_VLOG() << "Lowering " << block_id << "\n";
+    CARBON_VLOG("Lowering {0}\n", block_id);
     auto* llvm_block = function_lowering.GetBlock(block_id);
     // Keep the LLVM blocks in lexical order.
     llvm_block->moveBefore(llvm_function->end());

+ 1 - 1
toolchain/lower/function_context.cpp

@@ -95,7 +95,7 @@ auto FunctionContext::LowerInst(SemIR::InstId inst_id) -> void {
   }
 
   auto inst = sem_ir().insts().Get(inst_id);
-  CARBON_VLOG() << "Lowering " << inst_id << ": " << inst << "\n";
+  CARBON_VLOG("Lowering {0}: {1}\n", inst_id, inst);
   builder_.getInserter().SetCurrentInstId(inst_id);
   if (di_subprogram_) {
     auto loc = file_context_->GetLocForDI(inst_id);

+ 4 - 4
toolchain/parse/context.h

@@ -225,14 +225,14 @@ class Context {
   // Pops the state and keeps the value for inspection.
   auto PopState() -> StateStackEntry {
     auto back = state_stack_.pop_back_val();
-    CARBON_VLOG() << "Pop " << state_stack_.size() << ": " << back << "\n";
+    CARBON_VLOG("Pop {0}: {1}\n", state_stack_.size(), back);
     return back;
   }
 
   // Pops the state and discards it.
   auto PopAndDiscardState() -> void {
-    CARBON_VLOG() << "PopAndDiscard " << state_stack_.size() - 1 << ": "
-                  << state_stack_.back() << "\n";
+    CARBON_VLOG("PopAndDiscard {0}: {1}\n", state_stack_.size() - 1,
+                state_stack_.back());
     state_stack_.pop_back();
   }
 
@@ -265,7 +265,7 @@ class Context {
 
   // Pushes a constructed state onto the stack.
   auto PushState(StateStackEntry state) -> void {
-    CARBON_VLOG() << "Push " << state_stack_.size() << ": " << state << "\n";
+    CARBON_VLOG("Push {0}: {1}\n", state_stack_.size(), state);
     state_stack_.push_back(state);
     CARBON_CHECK(state_stack_.size() < (1 << 20))
         << "Excessive stack size: likely infinite loop";