[llvm] [Offload] Add GenericProfilerTy abstraction and APITypes extensions (PR #225639)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 23 01:29:05 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-offload
Author: Jan Patrick Lehr (jplehr)
<details>
<summary>Changes</summary>
Introduce GenericProfilerTy alongside the existing OMPT callback dispatch.
The weak profiler factory returns a no-op implementation, so the new hooks
are silent while the established callback path continues to handle OMPT
device events.
Co-Authored-By: Dhruva Chakrabarti <dhruva.chakrabarti@<!-- -->amd.com>
Co-Authored-By: Michael Halkenhauser <michaelgerald.halkenhauser@<!-- -->amd.com>
Assisted-by: Claude Code
---
<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>
---
Patch is 33.68 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/225639.diff
11 Files Affected:
- (modified) offload/include/Shared/APITypes.h (+5)
- (modified) offload/plugins-nextgen/amdgpu/src/rtl.cpp (+2-1)
- (modified) offload/plugins-nextgen/common/CMakeLists.txt (+1)
- (added) offload/plugins-nextgen/common/include/GenericProfiler.h (+197)
- (modified) offload/plugins-nextgen/common/include/PluginInterface.h (+37-19)
- (added) offload/plugins-nextgen/common/src/GenericProfiler.cpp (+36)
- (modified) offload/plugins-nextgen/common/src/PluginInterface.cpp (+78-28)
- (modified) offload/plugins-nextgen/cuda/src/rtl.cpp (+2-1)
- (modified) offload/plugins-nextgen/host/src/rtl.cpp (+4-1)
- (modified) offload/plugins-nextgen/level_zero/include/L0Device.h (+2-1)
- (modified) offload/plugins-nextgen/level_zero/src/L0Device.cpp (+2-1)
``````````diff
diff --git a/offload/include/Shared/APITypes.h b/offload/include/Shared/APITypes.h
index 7a151ab4ee6121..7ded9742942399 100644
--- a/offload/include/Shared/APITypes.h
+++ b/offload/include/Shared/APITypes.h
@@ -88,6 +88,11 @@ struct __tgt_async_info {
/// ensure it is a valid location while the transfer to the device is
/// happening.
KernelLaunchEnvironmentTy KernelLaunchEnvironment;
+
+ /// Opaque handle for profiler-specific data (e.g., OMPT trace record info).
+ /// Owned by the profiler; the runtime threads this pointer through the plugin
+ /// layer to associate async operations with trace records.
+ void *ProfilerData = nullptr;
};
/// This struct contains all of the arguments to a target kernel region launch.
diff --git a/offload/plugins-nextgen/amdgpu/src/rtl.cpp b/offload/plugins-nextgen/amdgpu/src/rtl.cpp
index 1af5d4950a8d88..59b8cac633e3f4 100644
--- a/offload/plugins-nextgen/amdgpu/src/rtl.cpp
+++ b/offload/plugins-nextgen/amdgpu/src/rtl.cpp
@@ -2282,7 +2282,8 @@ struct AMDGPUDeviceTy : public GenericDeviceTy, AMDGenericDeviceTy {
~AMDGPUDeviceTy() {}
/// Initialize the device, its resources and get its properties.
- Error initImpl(GenericPluginTy &Plugin) override {
+ Error initImpl(GenericPluginTy &Plugin,
+ GenericProfilerTy *ProfilerPtr) override {
// First setup all the memory pools.
if (auto Err = initMemoryPools())
return Err;
diff --git a/offload/plugins-nextgen/common/CMakeLists.txt b/offload/plugins-nextgen/common/CMakeLists.txt
index 6ad20796c3ca45..19ce102309a724 100644
--- a/offload/plugins-nextgen/common/CMakeLists.txt
+++ b/offload/plugins-nextgen/common/CMakeLists.txt
@@ -11,6 +11,7 @@ add_public_tablegen_target(PluginErrcodes)
# don't want to export `PluginInterface` while `add_llvm_library` requires that.
add_library(PluginCommon OBJECT
src/PluginInterface.cpp
+ src/GenericProfiler.cpp
src/GlobalHandler.cpp
src/JIT.cpp
src/RecordReplay.cpp
diff --git a/offload/plugins-nextgen/common/include/GenericProfiler.h b/offload/plugins-nextgen/common/include/GenericProfiler.h
new file mode 100644
index 00000000000000..57bb0847f32d91
--- /dev/null
+++ b/offload/plugins-nextgen/common/include/GenericProfiler.h
@@ -0,0 +1,197 @@
+//===- GenericProfiler.h - GenericProfiler interface for use in Plugins ---===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// The GenericProfiler interface allows to implement profiler logic for various
+// backends, such as OMPT or other tracing mechanisms.
+// This enables the plugins to be agnostic of the actual high-level language
+// that is implemented.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef OFFLOAD_PLUGINS_NEXTGEN_COMMON_INCLUDE_GENERICPROFILER_H
+#define OFFLOAD_PLUGINS_NEXTGEN_COMMON_INCLUDE_GENERICPROFILER_H
+
+#include "Shared/APITypes.h"
+
+#include <cstdint>
+#include <functional>
+#include <tuple>
+
+class OmptTracingBufferMgr;
+
+namespace llvm {
+namespace omp {
+namespace target {
+namespace plugin {
+
+struct GenericDeviceTy;
+struct GenericPluginTy;
+class GenericProfilerTy;
+
+template <typename FunT, typename... ArgsT, size_t... IdxSequence>
+void callViaIndexSeq(FunT F, GenericProfilerTy *P, uint64_t StartNanos,
+ uint64_t EndNanos, std::tuple<ArgsT...> Args,
+ std::index_sequence<IdxSequence...>) {
+ F(P, StartNanos, EndNanos, std::get<IdxSequence>(Args)...);
+}
+
+template <typename FunT, typename... ArgsT>
+void callViaUnpack(FunT F, GenericProfilerTy *P, uint64_t StartNanos,
+ uint64_t EndNanos, std::tuple<ArgsT...> Tup) {
+ callViaIndexSeq(F, P, StartNanos, EndNanos, Tup,
+ std::index_sequence_for<ArgsT...>{});
+}
+
+/// Abstraction layer to implement different profiler backends.
+///
+/// The plugins call into the GenericProfilerTy to handle the specific events
+/// with whatever specific backend was instantiated. For now, the supported
+/// backends are limited to an OMPT implementation.
+class GenericProfilerTy {
+public:
+ GenericProfilerTy() = default;
+ virtual ~GenericProfilerTy() = default;
+
+ /// Obtain a pointer to profiler-specific data, if any.
+ virtual void *getProfilerSpecificData() { return nullptr; }
+
+ /// Obtain the trace record manager owned by this profiler, if any. Profilers
+ /// that produce trace records own the manager so that the records handed out
+ /// cannot outlive the buffers they point into.
+ virtual OmptTracingBufferMgr *getTraceRecordManager() { return nullptr; }
+
+ virtual bool isProfilingEnabled() { return false; }
+
+ /// Set the factors which are used to interpolate the device clock compared to
+ /// the host clock. This follows a simple linear interpolation: Slope * <time>
+ /// + Offset.
+ void setTimeConversionFactors(double Slope, double Offset) {
+ HostToDeviceSlope = Slope;
+ HostToDeviceOffset = Offset;
+ setTimeConversionFactorsImpl(HostToDeviceSlope, HostToDeviceOffset);
+ }
+
+ /// Hook that is called when the plugin is initialized.
+ virtual void handleInit(GenericDeviceTy *Device, GenericPluginTy *Plugin) {}
+
+ /// Hook that is called when the plugin is de-initialized.
+ virtual void handleDeinit(GenericDeviceTy *Device, GenericPluginTy *Plugin) {}
+
+ /// Hook that is called when the device image is loaded.
+ virtual void handleLoadBinary(GenericDeviceTy *Device,
+ GenericPluginTy *Plugin,
+ const StringRef InputTgtImage) {}
+
+ /// Hook that is called when memory is allocated on the device.
+ virtual void handleDataAlloc(uint64_t StartNanos, uint64_t EndNanos,
+ void *HostPtr, uint64_t Size, void *Data) {}
+
+ /// Hook that is called when memory is freed on the device.
+ virtual void handleDataDelete(uint64_t StartNanos, uint64_t EndNanos,
+ void *TgtPtr, void *Data) {}
+
+ /// Hook that is called before launching a kernel.
+ virtual void handlePreKernelLaunch(GenericDeviceTy *Device,
+ uint32_t NumBlocks[3],
+ __tgt_async_info *AI) {}
+
+ /// Hook that is called when the kernel is finished to extract the specific
+ /// timing info for that kernel execution.
+ virtual void handleKernelCompletion(uint64_t StartNanos, uint64_t EndNanos,
+ void *Data) {}
+
+ /// Hook that is called when a data transfer happens to extract timing info
+ /// for that transfer.
+ virtual void handleDataTransfer(uint64_t StartNanos, uint64_t EndNanos,
+ void *Data) {}
+
+ /// Allow factors for time conversion between host and device.
+ virtual void setTimeConversionFactorsImpl(double Slope, double Offset) {}
+
+ /// RAII style timer that measures the elapsed time between construction and
+ /// destruction, then invokes a callback with the profiler, start/end times,
+ /// and any captured arguments.
+ template <typename FnT, typename... ArgsT> class ProfTimerTy {
+ public:
+ ProfTimerTy(FnT &&F, GenericProfilerTy *P, GenericDeviceTy *D, ArgsT... As)
+ : Fun(F), Prof(P), Dev(D), Args(As...) {
+ assert(Prof && "GenericProfilerTy is null");
+ assert(Dev && "GenericDeviceTy is null");
+ if (Prof)
+ StartTime = Prof->getDeviceTimeStamp(Dev);
+ }
+
+ ~ProfTimerTy() {
+ assert(Prof && "GenericProfilerTy is null");
+ assert(Dev && "GenericDeviceTy is null");
+ if (Prof) {
+ uint64_t EndTime = Prof->getDeviceTimeStamp(Dev);
+ callViaUnpack(Fun, Prof, StartTime, EndTime, Args);
+ }
+ }
+
+ private:
+ FnT Fun;
+ GenericProfilerTy *Prof;
+ GenericDeviceTy *Dev;
+ uint64_t StartTime = 0;
+ std::tuple<ArgsT...> Args;
+ };
+
+ template <typename FnT, typename... ArgsT>
+ [[maybe_unused]]
+ ProfTimerTy(FnT &&, GenericProfilerTy *, ArgsT...)
+ -> ProfTimerTy<FnT, ArgsT...>;
+
+ template <typename FnT, typename... ArgsT> friend class ProfTimerTy;
+
+ /// Returns an RAII style timer, which will handle data allocation timing.
+ [[nodiscard]] auto getScopedDataAllocTimer(GenericDeviceTy *Dev,
+ void *HostPtr, uint64_t Size,
+ void *ProfData = nullptr) {
+ return ProfTimerTy(
+ [](GenericProfilerTy *P, auto... args) {
+ assert(P && "P was null");
+ P->handleDataAlloc(args...);
+ },
+ this, Dev, HostPtr, Size, ProfData);
+ }
+
+ /// Returns an RAII style timer, which will handle data deletion timing.
+ [[nodiscard]] auto getScopedDataDeleteTimer(GenericDeviceTy *Dev,
+ void *TgtPtr,
+ void *ProfData = nullptr) {
+ return ProfTimerTy(
+ [](GenericProfilerTy *P, auto... args) {
+ assert(P && "P was null");
+ P->handleDataDelete(args...);
+ },
+ this, Dev, TgtPtr, ProfData);
+ }
+
+protected:
+ /// Factors to convert host time to device time. The default values are
+ /// Computed in a way Slope * Time + Offset (inspired by Score-P).
+ double HostToDeviceSlope = 1.0;
+ double HostToDeviceOffset = .0;
+
+private:
+ /// Vendor-specific implementation to obtain device time.
+ uint64_t getDeviceTimeStamp(GenericDeviceTy *D);
+};
+
+/// Null-object used to normalize an absent profiler. Callers that do not have a
+/// profiler pass none, and the receiving method resolves to this instance, so
+/// profiling logic never has to test for a null pointer.
+GenericProfilerTy &getNoOpProfiler();
+} // namespace plugin
+} // namespace target
+} // namespace omp
+} // namespace llvm
+
+#endif // OFFLOAD_PLUGINS_NEXTGEN_COMMON_INCLUDE_GENERICPROFILER_H
diff --git a/offload/plugins-nextgen/common/include/PluginInterface.h b/offload/plugins-nextgen/common/include/PluginInterface.h
index 0881fa3fd76271..db038f767671f3 100644
--- a/offload/plugins-nextgen/common/include/PluginInterface.h
+++ b/offload/plugins-nextgen/common/include/PluginInterface.h
@@ -29,6 +29,7 @@
#include "Shared/Requirements.h"
#include "Shared/Utils.h"
+#include "GenericProfiler.h"
#include "GlobalHandler.h"
#include "JIT.h"
#include "MemoryManager.h"
@@ -493,7 +494,8 @@ struct GenericKernelTy {
/// of it reserved for the kernel launch environment (dyn_ptr); the caller
/// owns the storage it points into.
Error launch(GenericDeviceTy &GenericDevice, KernelLaunchArgsTy &LaunchArgs,
- AsyncInfoWrapperTy &AsyncInfoWrapper) const;
+ AsyncInfoWrapperTy &AsyncInfoWrapper,
+ GenericProfilerTy *ProfilerPtr = nullptr) const;
virtual Error launchImpl(GenericDeviceTy &GenericDevice,
uint32_t NumThreads[3], uint32_t NumBlocks[3],
uint32_t DynBlockMemSize,
@@ -980,20 +982,23 @@ struct GenericDeviceTy : public DeviceAllocatorTy {
/// Initialize the device. After this call, the device should be already
/// working and ready to accept queries or modifications.
- Error init(GenericPluginTy &Plugin);
- virtual Error initImpl(GenericPluginTy &Plugin) = 0;
+ Error init(GenericPluginTy &Plugin, GenericProfilerTy *ProfilerPtr = nullptr);
+ virtual Error initImpl(GenericPluginTy &Plugin,
+ GenericProfilerTy *ProfilerPtr) = 0;
/// Deinitialize the device and free all its resources. After this call, the
/// device is no longer considered ready, so no queries or modifications are
/// allowed.
- Error deinit(GenericPluginTy &Plugin);
+ Error deinit(GenericPluginTy &Plugin,
+ GenericProfilerTy *ProfilerPtr = nullptr);
virtual Error deinitImpl() = 0;
/// Load the binary image into the device and return the target table. When
/// \p Context is null the plugin's driver-scoped default context is used.
- Expected<DeviceImageTy *> loadBinary(GenericPluginTy &Plugin,
- StringRef TgtImage,
- PluginContextTy *Context);
+ Expected<DeviceImageTy *>
+ loadBinary(GenericPluginTy &Plugin, StringRef TgtImage,
+ PluginContextTy *Context,
+ GenericProfilerTy *ProfilerPtr = nullptr);
virtual Expected<DeviceImageTy *>
loadBinaryImpl(std::unique_ptr<MemoryBuffer> &&TgtImage, int32_t ImageId,
PluginContextTy *Context) = 0;
@@ -1053,10 +1058,12 @@ struct GenericDeviceTy : public DeviceAllocatorTy {
/// Allocate data on the device or involving the device.
Expected<void *> dataAlloc(int64_t Size, void *HostPtr, TargetAllocTy Kind,
- size_t Alignment);
+ size_t Alignment,
+ GenericProfilerTy *ProfilerPtr = nullptr);
/// Deallocate data from the device or involving the device.
- Error dataDelete(void *TgtPtr, TargetAllocTy Kind);
+ Error dataDelete(void *TgtPtr, TargetAllocTy Kind,
+ GenericProfilerTy *ProfilerPtr = nullptr);
/// Pin or register host memory to optimize transfers and return the device
/// accessible pointer that devices should use for memory transfers involving
@@ -1160,7 +1167,8 @@ struct GenericDeviceTy : public DeviceAllocatorTy {
/// Run the kernel associated with \p EntryPtr
Error launchKernel(void *EntryPtr, KernelLaunchArgsTy &LaunchArgs,
- __tgt_async_info *AsyncInfo);
+ __tgt_async_info *AsyncInfo,
+ GenericProfilerTy *ProfilerPtr = nullptr);
/// Enqueue a host call to AsyncInfo
Error enqueueHostCall(void (*Callback)(void *), void *UserData,
@@ -1239,6 +1247,11 @@ struct GenericDeviceTy : public DeviceAllocatorTy {
uint32_t getDebugKind() const { return OMPX_DebugKind; }
virtual uint64_t getClockFrequency() const { return CLOCKS_PER_SEC; }
+ /// Get a device-specific timestamp in nanoseconds, used by the profiler
+ /// for timing device operations. Subclasses should override this to provide
+ /// hardware-accurate timestamps (e.g., via HSA system info).
+ virtual uint64_t getDeviceTimeStamp() { return 0; }
+
/// Get target compute unit kind (e.g., sm_80, or gfx908).
virtual std::string getComputeUnitKind() const { return "unknown"; }
@@ -1547,7 +1560,7 @@ struct GenericPluginTy {
virtual Expected<int32_t> initImpl() = 0;
/// Deinitialize the plugin and release the resources.
- Error deinit();
+ Error deinit(GenericProfilerTy *ProfilerPtr = nullptr);
virtual Error deinitImpl() = 0;
/// Create a new device for the underlying plugin.
@@ -1627,12 +1640,12 @@ struct GenericPluginTy {
/// Tear down any target-specific doorbell resources.
virtual Error deinitRPCDoorbell() { return Plugin::success(); }
- /// Get a reference to the record and replay interface for the plugin.
/// Initialize a device within the plugin.
- Error initDevice(int32_t DeviceId);
+ Error initDevice(int32_t DeviceId, GenericProfilerTy *ProfilerPtr = nullptr);
/// Deinitialize a device within the plugin and release its resources.
- Error deinitDevice(int32_t DeviceId);
+ Error deinitDevice(int32_t DeviceId,
+ GenericProfilerTy *ProfilerPtr = nullptr);
/// Indicate whether data can be exchanged directly between two devices under
/// this same plugin. If this function returns true, it's safe to call the
@@ -1714,7 +1727,8 @@ struct GenericPluginTy {
int32_t is_device_initialized(int32_t DeviceId) const;
/// Initialize the device inside of the plugin.
- int32_t init_device(int32_t DeviceId);
+ int32_t init_device(int32_t DeviceId,
+ GenericProfilerTy *ProfilerPtr = nullptr);
/// Return the number of devices this plugin can support.
int32_t number_of_devices();
@@ -1731,13 +1745,16 @@ struct GenericPluginTy {
/// Loads the associated binary into the plugin and returns a handle to it.
int32_t load_binary(int32_t DeviceId, __tgt_device_image *TgtImage,
- __tgt_device_binary *Binary);
+ __tgt_device_binary *Binary,
+ GenericProfilerTy *ProfilerPtr = nullptr);
/// Allocates memory that is accessively to the given device.
- void *data_alloc(int32_t DeviceId, int64_t Size, void *HostPtr, int32_t Kind);
+ void *data_alloc(int32_t DeviceId, int64_t Size, void *HostPtr, int32_t Kind,
+ GenericProfilerTy *ProfilerPtr = nullptr);
/// Deallocates memory on the given device.
- int32_t data_delete(int32_t DeviceId, void *TgtPtr, int32_t Kind);
+ int32_t data_delete(int32_t DeviceId, void *TgtPtr, int32_t Kind,
+ GenericProfilerTy *ProfilerPtr = nullptr);
/// Locks / pins host memory using the plugin runtime.
int32_t data_lock(int32_t DeviceId, void *Ptr, int64_t Size,
@@ -1784,7 +1801,8 @@ struct GenericPluginTy {
/// Begin executing a kernel on the given device.
int32_t launch_kernel(int32_t DeviceId, void *TgtEntryPtr,
KernelLaunchArgsTy &LaunchArgs,
- __tgt_async_info *AsyncInfoPtr);
+ __tgt_async_info *AsyncInfoPtr,
+ GenericProfilerTy *ProfilerPtr = nullptr);
/// Synchronize an asyncrhonous queue with the plugin runtime.
int32_t synchronize(int32_t DeviceId, __tgt_async_info *AsyncInfoPtr);
diff --git a/offload/plugins-nextgen/common/src/GenericProfiler.cpp b/offload/plugins-nextgen/common/src/GenericProfiler.cpp
new file mode 100644
index 00000000000000..5486c40a7a9d2f
--- /dev/null
+++ b/offload/plugins-nextgen/common/src/GenericProfiler.cpp
@@ -0,0 +1,36 @@
+//===- GenericProfiler.cpp - GenericProfiler implementation ---------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+//===----------------------------------------------------------------------===//
+
+#include "GenericProfiler.h"
+#include "PluginInterface.h"
+
+#include <cstdint>
+#include <memory>
+
+namespace llvm {
+namespace omp {
+namespace target {
+namespace plugin {
+
+uint64_t GenericProfilerTy::getDeviceTimeStamp(GenericDeviceTy *D) {
+ if (!D)
+ return 0;
+
+ return D->getDeviceTimeStamp();
+}
+
+GenericProfilerTy &getNoOpProfiler() {
+ static GenericProfilerTy NoOpProfiler;
+ return NoOpProfiler;
+}
+} // namespace plugin
+} // namespace target
+} // namespace omp
+} // namespace llvm
diff --git a/offload/plugins-nextgen/common/src/PluginInterface.cpp b/offload/plugins-nextgen/common/src/PluginInterface.cpp
index e555321e1b96bc..1f23f62ab0852b 100644
--- a/offload/plugins-nextgen/common/src/PluginInterface.cpp
+++ b/offload/plugins-nextgen/common/src/PluginInterface.cpp
@@ -26,6 +26,8 @@
#include "omp-tools.h"
#endif
+#include "GenericProfiler.h"
+
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Frontend/OpenMP/OMPConstants.h"
#include "llvm/Support/Error.h"
@@ -47,7 +49,9 @@ using namespace llvm::offload::debug;
AsyncInfoWrapperTy::AsyncInfoWrapperTy(GenericDeviceTy &Device,
__tgt_async_info *AsyncInfoPtr)
: Device(Device),
- AsyncInfoPtr(AsyncInfoPtr ? AsyncInfoPtr : &LocalAsyncInfo) {}
+ AsyncInfoPtr(AsyncInfoPtr ? AsyncInfoPtr : &LocalAsyncInfo) {
+ LocalAsyncInfo.ProfilerData = nullptr;
+}
Error AsyncInfoWrapperTy::synchronize() {
assert(AsyncInfoPtr && "AsyncInfoWrapperTy already finalized");
@@ -170,9 +174,15 @@ GenericKernelTy::getKernelLaunchEnvironment(
DPxPTR(&LocalKLE), DPxPTR(*AllocOrErr),
sizeof(KernelLaunchEnvironmentTy));
+ // Temporarily suppress ProfilerData so the KLE upload is not traced as
+ // a user data operation.
+ __tgt_async_info *AI = AsyncInfoWrapper;
+ void *SavedProfilerData = AI->ProfilerData;
+ AI->ProfilerData = nullptr;
auto Err = GenericDevice.dataSubmit(*AllocOr...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/225639
More information about the llvm-commits
mailing list