[Mlir-commits] [mlir] [MLIR] Restore std::cout formatting state in the Vulkan runtime (PR #214671)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Aug 7 01:58:02 PDT 2026


https://github.com/LouisLu060211 created https://github.com/llvm/llvm-project/pull/214671

`std::setprecision` modifies the stream itself rather than a single insertion, so it stays in effect until something changes it back. The Vulkan runtime sets it to 3 while printing the compute shader execution time and never restores it, so every float printed afterwards, including by RunnerUtils, comes out with three significant digits. A memref holding 858.5 prints as 858 after a kernel launch even though the stored bits are unchanged.

Save the formatting state with `copyfmt` before the timing line and restore it after.

Fixes #211992

>From e647f0fb64f0e07fbd2a7339c0478bdcc1af1fff Mon Sep 17 00:00:00 2001
From: LouisLu0602 <yaolu0602 at gmail.com>
Date: Fri, 7 Aug 2026 16:57:34 +0800
Subject: [PATCH] [MLIR] Restore std::cout formatting state in the Vulkan
 runtime

`std::setprecision` modifies the stream itself rather than a single insertion,
so it stays in effect until something changes it back. The Vulkan runtime sets
it to 3 while printing the compute shader execution time and never restores it,
so every float printed afterwards, including by RunnerUtils, comes out with
three significant digits. A memref holding 858.5 prints as 858 after a kernel
launch even though the stored bits are unchanged.

Save the formatting state with `copyfmt` before the timing line and restore it
after.

Fixes #211992
---
 mlir/lib/ExecutionEngine/VulkanRuntime.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/mlir/lib/ExecutionEngine/VulkanRuntime.cpp b/mlir/lib/ExecutionEngine/VulkanRuntime.cpp
index 9452a561ad2f1..9daf1fa2f4e43 100644
--- a/mlir/lib/ExecutionEngine/VulkanRuntime.cpp
+++ b/mlir/lib/ExecutionEngine/VulkanRuntime.cpp
@@ -211,8 +211,13 @@ LogicalResult VulkanRuntime::run() {
             VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT),
         "vkGetQueryPoolResults");
     float microsec = (timestamps[1] - timestamps[0]) * timestampPeriod / 1000;
+    // std::setprecision is sticky on the stream, so save and restore the
+    // caller's formatting state rather than leaving it changed.
+    std::ios formatState(nullptr);
+    formatState.copyfmt(std::cout);
     std::cout << "Compute shader execution time: " << std::setprecision(3)
               << microsec << "us\n";
+    std::cout.copyfmt(formatState);
   }
 
   std::cout << "Command buffer submit time: " << submitDuration.count()



More information about the Mlir-commits mailing list