[llvm] [Offload] Add support for measuring elapsed time between events (PR #186856)

Leandro Lacerda via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 28 12:07:08 PDT 2026


================
@@ -3453,6 +3586,78 @@ AMDGPUStreamTy::AMDGPUStreamTy(AMDGPUDeviceTy &Device)
       StreamBusyWaitMicroseconds(Device.getStreamBusyWaitMicroseconds()),
       UseMultipleSdmaEngines(Device.useMultipleSdmaEngines()) {}
 
+Error AMDGPUEventTy::releaseTimingSignal() {
+  if (!TimingSignal)
+    return Plugin::success();
+
+  AMDGPUSignalTy *Signal = TimingSignal;
+  TimingSignal = nullptr;
+
+  if (Signal->decreaseUseCount())
+    return Device.getSignalManager().returnResource(Signal);
+
+  return Plugin::success();
+}
+
+Expected<float> AMDGPUEventTy::getElapsedTime(AMDGPUEventTy &EndEvent) {
+  if (this == &EndEvent) {
+    std::lock_guard<std::mutex> Lock(Mutex);
+
+    if (!TimingSignal)
+      return Plugin::error(ErrorCode::INVALID_ARGUMENT,
+                           "event timing is not available");
+
+    if (TimingSignal->load())
+      return Plugin::error(ErrorCode::UNKNOWN, "event timing is not ready");
+
+    return 0.0f;
+  }
+
+  const uint64_t TicksPerSecond = Device.getSystemTimestampFrequency();
+  if (TicksPerSecond == 0)
+    return Plugin::error(ErrorCode::UNSUPPORTED,
+                         "HSA system timestamp frequency is unavailable");
+
+  std::scoped_lock<std::mutex, std::mutex> Lock(Mutex, EndEvent.Mutex);
+
+  if (&Device != &EndEvent.Device)
+    return Plugin::error(ErrorCode::INVALID_ARGUMENT,
+                         "events belong to different devices");
+
+  if (!TimingSignal || !EndEvent.TimingSignal)
+    return Plugin::error(
+        ErrorCode::INVALID_ARGUMENT,
+        "timing information is not available for one or both events");
+
+  if (TimingSignal->load() || EndEvent.TimingSignal->load())
----------------
leandrolcampos wrote:

Yes. This condition is true if either event has not completed yet, i.e. if either timing signal is still non-zero.

https://github.com/llvm/llvm-project/pull/186856


More information about the llvm-commits mailing list