Files
llvm/offload/plugins-nextgen/level_zero/include/L0CmdListManager.h
T
blazej-smorawski dfcd28df1d [offload] Add dlwrap::loaded function to check for optional symbols (#210737)
This PR adds a new template into `dlwrap` namespace that can be used to
check if a symbol was correctly loaded. It adds and easy way to see if
version of shared object in a system has required capability. We could
use it to improve prefetch in CUDA backend as noted
[here](https://github.com/llvm/llvm-project/blob/main/offload/plugins-nextgen/cuda/src/rtl.cpp#L912)
without breaking compatibility with older platforms using CUDA older
than 13.

In the case of prefetch, the new `dlwrap` API could be used like: 
```cpp
bool BatchedPrefetchAvailable = dlwrap::loaded<cuMemPrefetchBatchAsync>();
if (BatchedPrefetchAvailable)
    cuMemPrefetchAsync(....)
else
    // Current implementation
```

Small note, to implement the prefetch we will also have to make loading
of the symbols optional inside plugins too. This would require to change
it
[here](https://github.com/llvm/llvm-project/blob/main/offload/plugins-nextgen/cuda/dynamic_cuda/cuda.cpp#L176),
but I think that the change can be a part of a PR that implements
prefetch.
2026-08-03 16:57:19 +02:00

196 lines
8.0 KiB
C++

//===--- Level Zero Target RTL 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
//
//===----------------------------------------------------------------------===//
//
// Command List Manager for Level Zero.
//
//===----------------------------------------------------------------------===//
#ifndef OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0CMDLISTMANAGER_H
#define OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0CMDLISTMANAGER_H
#include "L0Compat.h"
#include "L0Context.h"
#include "L0Defs.h"
#include "L0Trace.h"
#include "PluginInterface.h"
#include <mutex>
namespace llvm::omp::target::plugin {
/// Manager for Level Zero command list operations.
/// Encapsulates all zeCommandListAppend* calls with thread safety.
class L0CmdListManagerTy {
/// Underlying immediate command list.
ze_command_list_handle_t CmdList;
/// Owning context (provides driver-loaded extension function pointers).
L0ContextTy &Context;
/// Mutex to protect L0 operations that are not thread safe.
std::mutex Mtx;
public:
L0CmdListManagerTy(ze_command_list_handle_t CmdList, L0ContextTy &Context)
: CmdList(CmdList), Context(Context) {}
ze_command_list_handle_t getCmdList() const { return CmdList; }
Error hostSynchronize(uint64_t TimeoutNs = L0DefaultTimeout) {
CALL_ZE_RET_ERROR(zeCommandListHostSynchronize, CmdList, TimeoutNs);
return Plugin::success();
}
Expected<bool> queryPendingWork() {
ze_result_t RC;
CALL_ZE(RC, zeCommandListHostSynchronize, CmdList, 0);
if (RC == ZE_RESULT_SUCCESS)
return false;
if (RC == ZE_RESULT_NOT_READY)
return true;
return Plugin::error(ErrorCode::UNKNOWN,
"zeCommandListHostSynchronize query failed: %s",
getZeErrorName(RC));
}
Error eventHostSynchronize(ze_event_handle_t Event,
uint64_t TimeoutNs = L0DefaultTimeout) {
CALL_ZE_RET_ERROR(zeEventHostSynchronize, Event, TimeoutNs);
return Plugin::success();
}
Error appendMemoryCopy(void *Dst, const void *Src, size_t Size,
ze_event_handle_t SignalEvent = nullptr,
uint32_t NumWaitEvents = 0,
ze_event_handle_t *WaitEvents = nullptr) {
std::lock_guard<std::mutex> Lock(Mtx);
CALL_ZE_RET_ERROR(zeCommandListAppendMemoryCopy, CmdList, Dst, Src, Size,
SignalEvent, NumWaitEvents, WaitEvents);
return Plugin::success();
}
Error appendMemoryCopyRegion(void *Dst, const ze_copy_region_t *DstRegion,
uint32_t DstPitch, uint32_t DstSlicePitch,
const void *Src,
const ze_copy_region_t *SrcRegion,
uint32_t SrcPitch, uint32_t SrcSlicePitch,
ze_event_handle_t SignalEvent = nullptr,
uint32_t NumWaitEvents = 0,
ze_event_handle_t *WaitEvents = nullptr) {
std::lock_guard<std::mutex> Lock(Mtx);
CALL_ZE_RET_ERROR(zeCommandListAppendMemoryCopyRegion, CmdList, Dst,
DstRegion, DstPitch, DstSlicePitch, Src, SrcRegion,
SrcPitch, SrcSlicePitch, SignalEvent, NumWaitEvents,
WaitEvents);
return Plugin::success();
}
Error appendMemoryFill(void *Ptr, const void *Pattern, size_t PatternSize,
size_t Size, ze_event_handle_t SignalEvent = nullptr,
uint32_t NumWaitEvents = 0,
ze_event_handle_t *WaitEvents = nullptr) {
std::lock_guard<std::mutex> Lock(Mtx);
CALL_ZE_RET_ERROR(zeCommandListAppendMemoryFill, CmdList, Ptr, Pattern,
PatternSize, Size, SignalEvent, NumWaitEvents,
WaitEvents);
return Plugin::success();
}
Error appendMemoryPrefetch(const void *Ptr, size_t Size) {
std::lock_guard<std::mutex> Lock(Mtx);
CALL_ZE_RET_ERROR(zeCommandListAppendMemoryPrefetch, CmdList, Ptr, Size);
return Plugin::success();
}
Error appendLaunchKernel(ze_kernel_handle_t Kernel,
const ze_group_count_t *pLaunchFuncArgs,
ze_event_handle_t SignalEvent = nullptr,
uint32_t NumWaitEvents = 0,
ze_event_handle_t *WaitEvents = nullptr,
bool Cooperative = false) {
std::lock_guard<std::mutex> Lock(Mtx);
if (Cooperative) {
CALL_ZE_RET_ERROR(zeCommandListAppendLaunchCooperativeKernel, CmdList,
Kernel, pLaunchFuncArgs, SignalEvent, NumWaitEvents,
WaitEvents);
} else {
CALL_ZE_RET_ERROR(zeCommandListAppendLaunchKernel, CmdList, Kernel,
pLaunchFuncArgs, SignalEvent, NumWaitEvents,
WaitEvents);
}
return Plugin::success();
}
Error appendLaunchKernelWithArgs(
ze_kernel_handle_t Kernel, const ze_group_count_t *GroupCounts,
const ze_group_size_t *GroupSizes, void **ArgPtrs,
ze_event_handle_t SignalEvent = nullptr, uint32_t NumWaitEvents = 0,
ze_event_handle_t *WaitEvents = nullptr, bool IsCooperative = false) {
if (!api_helper::canCall<zeCommandListAppendLaunchKernelWithArguments>())
return Plugin::error(
ErrorCode::UNSUPPORTED,
"zeCommandListAppendLaunchKernelWithArguments is not "
"available on this driver");
ze_command_list_append_launch_kernel_param_cooperative_desc_t CoopDesc = {
ZE_STRUCTURE_TYPE_COMMAND_LIST_APPEND_PARAM_COOPERATIVE_DESC, nullptr,
static_cast<ze_bool_t>(IsCooperative)};
std::lock_guard<std::mutex> Lock(Mtx);
CALL_ZE_RET_ERROR(zeCommandListAppendLaunchKernelWithArguments, CmdList,
Kernel, *GroupCounts, *GroupSizes, ArgPtrs,
IsCooperative ? &CoopDesc : nullptr, SignalEvent,
NumWaitEvents, WaitEvents);
return Plugin::success();
}
Error appendSignalEvent(ze_event_handle_t Event) {
std::lock_guard<std::mutex> Lock(Mtx);
CALL_ZE_RET_ERROR(zeCommandListAppendSignalEvent, CmdList, Event);
return Plugin::success();
}
Error appendWaitOnEvents(size_t NumWaitEvents,
ze_event_handle_t *WaitEvents) {
std::lock_guard<std::mutex> Lock(Mtx);
CALL_ZE_RET_ERROR(zeCommandListAppendWaitOnEvents, CmdList, NumWaitEvents,
WaitEvents);
return Plugin::success();
}
Error appendWaitOnEvent(ze_event_handle_t Event) {
return appendWaitOnEvents(1, &Event);
}
Error appendBarrier(ze_event_handle_t SignalEvent, uint32_t NumWaitEvents,
ze_event_handle_t *WaitEvents) {
std::lock_guard<std::mutex> Lock(Mtx);
CALL_ZE_RET_ERROR(zeCommandListAppendBarrier, CmdList, SignalEvent,
NumWaitEvents, WaitEvents);
return Plugin::success();
}
Error appendHostFunction(void (*Callback)(void *), void *UserData,
ze_event_handle_t SignalEvent = nullptr,
uint32_t NumWaitEvents = 0,
ze_event_handle_t *WaitEvents = nullptr) {
auto zeCommandListAppendHost = Context.zeCommandListAppendHostFunction;
if (!zeCommandListAppendHost)
return Plugin::error(ErrorCode::UNSUPPORTED,
"zeCommandListAppendHostFunction extension is not "
"available on this driver");
std::lock_guard<std::mutex> Lock(Mtx);
CALL_ZE_RET_ERROR(zeCommandListAppendHost, CmdList,
reinterpret_cast<void *>(Callback), UserData,
/*pReserved*/ nullptr, SignalEvent, NumWaitEvents,
WaitEvents);
return Plugin::success();
}
};
} // namespace llvm::omp::target::plugin
#endif // OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0CMDLISTMANAGER_H