Skip to content

Commit 0502d83

Browse files
committed
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.
1 parent f33afea commit 0502d83

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

mlir/include/mlir/Debug/Observers/ActionProfiler.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct ActionProfiler : public ExecutionContext::Observer {
4141
std::chrono::time_point<std::chrono::steady_clock> startTime;
4242
bool printComma = false;
4343

44-
/// A mutex used to guard profiling.
44+
/// A mutex used to guard printing from multiple threads.
4545
std::mutex mutex;
4646
};
4747

mlir/lib/Debug/Observers/ActionProfiler.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ void ActionProfiler::print(const ActionActiveStack *action,
3636
// Create the event.
3737
std::string str;
3838
llvm::raw_string_ostream event(str);
39+
if (printComma)
40+
event << ",\n";
3941
event << "{";
4042
event << R"("name": ")" << action->getAction().getTag() << "\", ";
4143
event << R"("cat": "PERF", )";
@@ -52,12 +54,11 @@ void ActionProfiler::print(const ActionActiveStack *action,
5254
event << "\"}";
5355
}
5456
event << "}";
57+
event.flush();
5558

56-
// Print the event.
59+
// Print the event, guard with a mutex to ensure the stream is correctly
60+
// formed.
5761
std::lock_guard<std::mutex> guard(mutex);
58-
if (printComma)
59-
os << ",\n";
6062
printComma = true;
6163
os << event.str();
62-
os.flush();
6364
}

0 commit comments

Comments
 (0)