[Mlir-commits] [mlir] 0502d83 - Minor fixes on the MLIR ActionProfiler (NFC)
Mehdi Amini
llvmlistbot at llvm.org
Mon Oct 2 20:11:24 PDT 2023
Author: Mehdi Amini
Date: 2023-10-02T20:11:18-07:00
New Revision: 0502d83470202de6412d4b310692c7b06f1791cb
URL: https://github.com/llvm/llvm-project/commit/0502d83470202de6412d4b310692c7b06f1791cb
DIFF: https://github.com/llvm/llvm-project/commit/0502d83470202de6412d4b310692c7b06f1791cb.diff
LOG: Minor fixes on the MLIR ActionProfiler (NFC)
Ensure the stream flushed to the string before acquiring the mutext.
No need to flush the output stream, the goal of the mutex is to sync
ahead before content is added to the stream.
Added:
Modified:
mlir/include/mlir/Debug/Observers/ActionProfiler.h
mlir/lib/Debug/Observers/ActionProfiler.cpp
Removed:
################################################################################
diff --git a/mlir/include/mlir/Debug/Observers/ActionProfiler.h b/mlir/include/mlir/Debug/Observers/ActionProfiler.h
index 50ee4f0baf2b096..8b324ea46eaba09 100644
--- a/mlir/include/mlir/Debug/Observers/ActionProfiler.h
+++ b/mlir/include/mlir/Debug/Observers/ActionProfiler.h
@@ -41,7 +41,7 @@ struct ActionProfiler : public ExecutionContext::Observer {
std::chrono::time_point<std::chrono::steady_clock> startTime;
bool printComma = false;
- /// A mutex used to guard profiling.
+ /// A mutex used to guard printing from multiple threads.
std::mutex mutex;
};
diff --git a/mlir/lib/Debug/Observers/ActionProfiler.cpp b/mlir/lib/Debug/Observers/ActionProfiler.cpp
index 07bf9fd0ccc7db8..b08d5a24526b6a9 100644
--- a/mlir/lib/Debug/Observers/ActionProfiler.cpp
+++ b/mlir/lib/Debug/Observers/ActionProfiler.cpp
@@ -36,6 +36,8 @@ void ActionProfiler::print(const ActionActiveStack *action,
// Create the event.
std::string str;
llvm::raw_string_ostream event(str);
+ if (printComma)
+ event << ",\n";
event << "{";
event << R"("name": ")" << action->getAction().getTag() << "\", ";
event << R"("cat": "PERF", )";
@@ -52,12 +54,11 @@ void ActionProfiler::print(const ActionActiveStack *action,
event << "\"}";
}
event << "}";
+ event.flush();
- // Print the event.
+ // Print the event, guard with a mutex to ensure the stream is correctly
+ // formed.
std::lock_guard<std::mutex> guard(mutex);
- if (printComma)
- os << ",\n";
printComma = true;
os << event.str();
- os.flush();
}
More information about the Mlir-commits
mailing list