[Openmp-commits] [llvm] [openmp] [Offload] Add logger in Plugin API functions for OpenMP (PR #215582)
Nick Sarnie via Openmp-commits
openmp-commits at lists.llvm.org
Wed Aug 12 11:49:45 PDT 2026
================
@@ -0,0 +1,167 @@
+//===-- Shared/APITrace.h - Tracing of offload API calls --------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Prints one line per offload API call while tracing is enabled:
+//
+// ---> init_device(.DeviceId = 0)-> OFFLOAD_SUCCESS (1053 us)
+//
+// Tracing is off until the runtime above the plugins turns it on, see
+// GenericPluginTy::set_api_trace.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef OMPTARGET_SHARED_API_TRACE_H
+#define OMPTARGET_SHARED_API_TRACE_H
+
+#include "APITypes.h"
+#include "omptarget.h"
+
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include <atomic>
+#include <chrono>
+#include <type_traits>
+#include <utility>
+
+namespace llvm::offload::trace {
+
+inline std::atomic<bool> &getTraceEnabledFlag() {
+ static std::atomic<bool> Enabled = false;
+ return Enabled;
+}
+
+/// The flag is read on every call so that tracing can be toggled around a
+/// region of interest.
+inline bool isTraceEnabled() {
+ return getTraceEnabledFlag().load(std::memory_order_relaxed);
+}
+
+inline void setTraceEnabled(bool Enable) {
+ getTraceEnabledFlag().store(Enable, std::memory_order_relaxed);
+}
+
+inline raw_ostream &operator<<(raw_ostream &OS, __tgt_device_binary Binary) {
+ return OS << reinterpret_cast<void *>(Binary.handle);
+}
+
+inline raw_ostream &operator<<(raw_ostream &OS, OffloadStatusTy Status) {
+ switch (Status) {
+ case OFFLOAD_SUCCESS:
+ return OS << "OFFLOAD_SUCCESS";
+ case OFFLOAD_FAIL:
+ return OS << "OFFLOAD_FAIL";
+ }
+ return OS << static_cast<int32_t>(Status);
+}
+
+inline raw_ostream &operator<<(raw_ostream &OS, TargetAllocTy Kind) {
+ switch (Kind) {
+ case TARGET_ALLOC_DEVICE:
+ return OS << "TARGET_ALLOC_DEVICE";
+ case TARGET_ALLOC_HOST:
+ return OS << "TARGET_ALLOC_HOST";
+ case TARGET_ALLOC_SHARED:
+ return OS << "TARGET_ALLOC_SHARED";
+ case TARGET_ALLOC_DEFAULT:
+ return OS << "TARGET_ALLOC_DEFAULT";
+ }
+ return OS << static_cast<int32_t>(Kind);
+}
+
+template <typename Ty, typename = void> struct IsPrintable : std::false_type {};
+template <typename Ty>
+struct IsPrintable<Ty, std::void_t<decltype(std::declval<raw_ostream &>()
+ << std::declval<const Ty &>())>>
+ : std::true_type {};
+
+/// Types with no raw_ostream overload are shown as an address, so that adding a
+/// parameter to a traced entry point never breaks the build.
+template <typename Ty> void printArg(raw_ostream &OS, const Ty &Arg) {
+ using DecayedTy = std::decay_t<Ty>;
----------------
sarnex wrote:
nit: we could add on a `std::remove_const` and then be able to do a simpler check in the next `if`
https://github.com/llvm/llvm-project/pull/215582
More information about the Openmp-commits
mailing list