Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
133 commits
Select commit Hold shift + click to select a range
0c42764
[OFFLOAD] Add plugin with support for Intel Level Zero
adurang Sep 16, 2025
e8c471e
Merge branch 'main' into l0_plugin
adurang Sep 16, 2025
f8956cd
Update offload/CMakeLists.txt
adurang Sep 16, 2025
d3fc4d7
Update offload/plugins-nextgen/level_zero/CMakeLists.txt
adurang Sep 16, 2025
4b38386
Update offload/plugins-nextgen/level_zero/include/L0Plugin.h
adurang Sep 16, 2025
6c1c820
Replace pragma once
adurang Sep 17, 2025
fd91c47
Address review comments
adurang Sep 17, 2025
84665dc
Fix makefile format
adurang Sep 17, 2025
a2217db
change to StringRef in multiple places
adurang Sep 17, 2025
08880a6
remove tokenize
adurang Sep 18, 2025
9a3088c
remove unused code
adurang Sep 18, 2025
24d0645
fix format
adurang Sep 18, 2025
0eb5712
Remove environment variable
adurang Sep 18, 2025
f491f3d
fix getAsInteger conditions
adurang Sep 18, 2025
9bbdae3
Merge branch 'main' into l0_plugin
adurang Sep 18, 2025
fe633f4
Don't use __tgt_device_image
adurang Sep 19, 2025
e36a8fc
fix image checking
adurang Sep 19, 2025
53bce7e
changes from review comments
adurang Sep 21, 2025
69df388
remove more CMake messages
adurang Sep 21, 2025
91fa069
format
adurang Sep 21, 2025
561e4ca
remove unnecessary check
adurang Sep 21, 2025
ad07ee8
Merge branch 'main' into l0_plugin
adurang Sep 23, 2025
5b90ccb
Add level_zero to liboffload platforms
adurang Sep 23, 2025
6b8280d
fix different issues
adurang Sep 23, 2025
f983135
Fix ELF bits from #159623
adurang Sep 23, 2025
b6c393a
format & add more Deviceinfo tags
adurang Sep 23, 2025
b226c70
tidy up Makefile
adurang Sep 23, 2025
af1945f
format & renaming
adurang Sep 23, 2025
3dba3c3
Remove ompt_device_t reference
adurang Sep 24, 2025
ac62231
address reviews
adurang Sep 25, 2025
17c8e31
remove unused argument
adurang Sep 25, 2025
e550bf1
Merge branch 'main' into l0_plugin
adurang Sep 26, 2025
b859179
cleanup
adurang Sep 26, 2025
f1d1aad
[OFFLOAD] Enable level zero testing
adurang Sep 26, 2025
c141f9d
address review
adurang Sep 30, 2025
dd774b6
fix & remove dead code
adurang Sep 30, 2025
7fa9dda
remove support for target ICVs
adurang Sep 30, 2025
96411ce
typo & format
adurang Sep 30, 2025
a4cd814
Merge branch 'main' into l0_plugin
adurang Sep 30, 2025
6c5278f
Adjust interfaces after merge
adurang Sep 30, 2025
44b2f48
push some error checking down
adurang Sep 30, 2025
b4e238f
Update offload/plugins-nextgen/level_zero/include/L0Device.h
adurang Oct 9, 2025
166028d
address review comments
adurang Oct 9, 2025
6629159
format
adurang Oct 9, 2025
c00c288
more format :/
adurang Oct 9, 2025
93d3948
add missed wrapper; remove unused macro
adurang Oct 9, 2025
4a2140e
Remove level_zero from default plugin list
adurang Oct 15, 2025
94335ac
Merge branch 'main' into l0_plugin
adurang Oct 15, 2025
bd43212
remove removed plugin interface
adurang Oct 15, 2025
2be5a5a
Merge branch 'l0_plugin' into l0_tests
adurang Oct 15, 2025
7f9c0c8
update spirv target
adurang Oct 15, 2025
2d7cc66
address some reviews
adurang Oct 20, 2025
987f44c
[OFFLOAD] Add support for indexed per-thread containers
adurang Oct 20, 2025
91e51ed
address more reviews
adurang Oct 20, 2025
12ab478
address more comments
adurang Oct 20, 2025
3287b1a
change some containers
adurang Oct 21, 2025
69abec0
more container changes
adurang Oct 21, 2025
b7e95ab
move unsafe code out of destructors
adurang Oct 21, 2025
91f1033
Remove legacy error checking Pt 1
adurang Oct 21, 2025
1221006
Remove legacy error checking Pt 2
adurang Oct 21, 2025
45d38a2
Remove legacy error checking Pt 3
adurang Oct 21, 2025
6ec68e7
Remove legacy error checking Pt 4
adurang Oct 21, 2025
ef8d794
Add more error checking
adurang Oct 21, 2025
19e888e
fix format
adurang Oct 21, 2025
a591b0e
Move DeviceRange to PluginInterface and remove internal Device list
adurang Oct 22, 2025
fc61165
refactor MemPool options field
adurang Oct 22, 2025
40f2626
change auto returns
adurang Oct 22, 2025
f04fef7
Remove link copy queues
adurang Oct 22, 2025
e95cab5
Merge branch 'main' into extend_ptt
jhuber6 Oct 22, 2025
e0c46a6
Change container
adurang Oct 23, 2025
0d3d6dc
Add deinit method
adurang Oct 23, 2025
2de9273
renaming variables, remove some locks
adurang Oct 23, 2025
f4e89a9
format
adurang Oct 23, 2025
5ebf0a4
more renaming
adurang Oct 23, 2025
8bff29d
forgot one mutex
adurang Oct 23, 2025
d29eac0
missed one container
adurang Oct 23, 2025
4a6b196
debug build fixes
adurang Oct 23, 2025
caedd4d
remove auto
adurang Oct 23, 2025
01f9c44
missed one
adurang Oct 23, 2025
e80dcf9
Some renaming
adurang Oct 23, 2025
5451f22
Add asserts
adurang Oct 23, 2025
120e3d4
fixes and cleanup
adurang Oct 27, 2025
40ad342
format :/
adurang Oct 27, 2025
8bb25ad
fixes
adurang Oct 29, 2025
08d5f32
fix incorrect memory logging and MemPool small refactor
adurang Nov 3, 2025
ba08709
Refactor code from comments
adurang Nov 3, 2025
69c0db3
Restructure kernel launching code
adurang Nov 3, 2025
ba0ac45
Remove unused code
adurang Nov 3, 2025
32b9b59
Use correct thread information
adurang Nov 3, 2025
b44f992
format
adurang Nov 3, 2025
a14ee62
Fix asserts to not be undefined
adurang Nov 4, 2025
e1dfd79
Fix env var parsing issue
adurang Nov 5, 2025
21effca
Merge branch 'extend_ptt' into l0_plugin
adurang Nov 5, 2025
581b952
Merge branch 'main' into l0_plugin
adurang Nov 5, 2025
045c94c
add isAccessiblePtrImpl to L0Device
adurang Nov 5, 2025
fd15e04
fixes
adurang Nov 7, 2025
827b28c
format
adurang Nov 7, 2025
26a1231
fix
adurang Nov 7, 2025
e2b8523
make kernel properties mutable; remove unused field
adurang Nov 12, 2025
bd364c5
Implement a few additional services
adurang Nov 17, 2025
0f59337
minor change
adurang Nov 17, 2025
579264a
minor refactoring
adurang Nov 19, 2025
26fbcc0
Refactor container concepts checks
adurang Nov 19, 2025
32819df
remove clearAll
adurang Nov 19, 2025
2d4f364
Simplify PerThreadData
adurang Nov 19, 2025
9c602be
Merge branch 'extend_ptt' into l0_plugin
adurang Nov 19, 2025
bc0ccaf
fix
adurang Nov 19, 2025
fd38f8d
Address comments
adurang Nov 21, 2025
9c5942e
fix comments
adurang Nov 25, 2025
8160235
Merge branch 'extend_ptt' into l0_plugin
adurang Nov 25, 2025
1cadfb8
Merge branch 'main' into l0_plugin
adurang Nov 26, 2025
6f850c2
adjust to removal of memory pool
adurang Nov 26, 2025
3f78a5a
cleanup L0ThreadTLS
adurang Dec 5, 2025
1b98333
use std::bitset to track AsyncQueues
adurang Dec 5, 2025
f2c2c32
address comments to changes in PluginInterface
adurang Dec 5, 2025
2623a9f
change usages of *INT_MAX to use numeric_limits
adurang Dec 5, 2025
b76ef59
remove level_zero from pluginNameToBackend
adurang Dec 5, 2025
8caa36d
remove level_zero from liboffload
adurang Dec 5, 2025
7ad1982
fix typo
adurang Dec 12, 2025
7e9e2f4
Make a constant for timeout expression
adurang Dec 12, 2025
2e820be
address comments
adurang Dec 12, 2025
e613b16
address comments
adurang Dec 12, 2025
9a10b71
format
adurang Dec 12, 2025
2b5b460
address comments
adurang Dec 12, 2025
6556a10
Address comments
adurang Dec 15, 2025
0aa85d5
remove some macros
adurang Dec 15, 2025
88b8091
remove unusued macro
adurang Dec 15, 2025
be26aac
remove str macro
adurang Dec 15, 2025
7093e7b
Fix comments punctuation Pt.1
adurang Dec 15, 2025
2d5430c
Fix comments punctuation Pt.2
adurang Dec 15, 2025
12e4bda
Simplify getDevicesRange
adurang Dec 15, 2025
45a62df
space
adurang Dec 15, 2025
bc28628
Fix level zero library name
adurang Dec 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion offload/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ endif()

set(LIBOMPTARGET_ALL_PLUGIN_TARGETS amdgpu cuda host)
set(LIBOMPTARGET_PLUGINS_TO_BUILD "all" CACHE STRING
"Semicolon-separated list of plugins to use: cuda, amdgpu, host or \"all\".")
"Semicolon-separated list of plugins to use: cuda, amdgpu, level_zero, host or \"all\".")

if(LIBOMPTARGET_PLUGINS_TO_BUILD STREQUAL "all")
set(LIBOMPTARGET_PLUGINS_TO_BUILD ${LIBOMPTARGET_ALL_PLUGIN_TARGETS})
Expand All @@ -176,6 +176,18 @@ if(NOT (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(ppc64le)|(aarch64)$"
list(REMOVE_ITEM LIBOMPTARGET_PLUGINS_TO_BUILD "cuda")
endif()
endif()
if(NOT (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64)$" AND
CMAKE_SYSTEM_NAME MATCHES "Linux|Windows"))
if("level_zero" IN_LIST LIBOMPTARGET_PLUGINS_TO_BUILD)
message(STATUS "Not building Level Zero plugin: it is only supported on "
"Linux/Windows x86_64 or ppc64le hosts")
list(REMOVE_ITEM LIBOMPTARGET_PLUGINS_TO_BUILD "level_zero")
endif()
endif()
if("level_zero" IN_LIST LIBOMPTARGET_PLUGINS_TO_BUILD AND
NOT LIBOMPTARGET_DEP_LEVEL_ZERO_FOUND)
list(REMOVE_ITEM LIBOMPTARGET_PLUGINS_TO_BUILD "level_zero")
endif()
message(STATUS "Building the offload library with support for "
"the \"${LIBOMPTARGET_PLUGINS_TO_BUILD}\" plugins")

Expand Down Expand Up @@ -214,6 +226,7 @@ set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} riscv64-unknown-linux
set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} riscv64-unknown-linux-gnu-LTO")
set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} loongarch64-unknown-linux-gnu")
set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} loongarch64-unknown-linux-gnu-LTO")
set (LIBOMPTARGET_ALL_TARGETS "${LIBOMPTARGET_ALL_TARGETS} spirv64-intel")

# Once the plugins for the different targets are validated, they will be added to
# the list of supported targets in the current system.
Expand Down
33 changes: 33 additions & 0 deletions offload/cmake/Modules/LibomptargetGetDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ endif()
find_package(FFI QUIET)
set(LIBOMPTARGET_DEP_LIBFFI_FOUND ${FFI_FOUND})

################################################################################
# Looking for offload-arch...
################################################################################
if(TARGET offload-arch)
get_property(LIBOMPTARGET_OFFLOAD_ARCH TARGET offload-arch PROPERTY LOCATION)
else()
find_program(LIBOMPTARGET_OFFLOAD_ARCH NAMES offload-arch
PATHS ${LLVM_TOOLS_BINARY_DIR})
endif()

################################################################################
# Looking for NVIDIA GPUs...
################################################################################
Expand Down Expand Up @@ -89,4 +99,27 @@ if(LIBOMPTARGET_AMDGPU_ARCH)
endif()
endif()

################################################################################
# Looking for Level0
################################################################################
find_path(LIBOMPTARGET_DEP_LEVEL_ZERO_INCLUDE_DIR NAMES level_zero/ze_api.h)

if(NOT LIBOMPTARGET_DEP_LEVEL_ZERO_INCLUDE_DIR)
set(LIBOMPTARGET_DEP_LEVEL_ZERO_FOUND FALSE)
else()
set(LIBOMPTARGET_DEP_LEVEL_ZERO_FOUND TRUE)
find_library(LIBOMPTARGET_DEP_LEVEL_ZERO_LIBRARY NAMES ze_loader)
endif()

if(LIBOMPTARGET_OFFLOAD_ARCH)
execute_process(COMMAND ${LIBOMPTARGET_OFFLOAD_ARCH} "--only=intel"
OUTPUT_VARIABLE LIBOMPTARGET_INTELGPU_ARCH_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REPLACE "\n" ";" intelgpu_arch_list "${LIBOMPTARGET_INTELGPU_ARCH_OUTPUT}")
if(intelgpu_arch_list)
set(LIBOMPTARGET_FOUND_INTELGPU_GPU TRUE)
set(LIBOMPTARGET_INTELGPU_DETECTED_ARCH_LIST "${intelgpu_arch_list}")
endif()
endif()

set(OPENMP_PTHREAD_LIB ${LLVM_PTHREAD_LIB})
1 change: 1 addition & 0 deletions offload/include/omptarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ enum TargetAllocTy : int32_t {
TARGET_ALLOC_HOST,
TARGET_ALLOC_SHARED,
TARGET_ALLOC_DEFAULT,
TARGET_ALLOC_LAST = TARGET_ALLOC_DEFAULT
};

struct DeviceTy;
Expand Down
16 changes: 16 additions & 0 deletions offload/plugins-nextgen/common/include/DLWrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,21 @@ template <size_t Requested, size_t Required> constexpr void verboseAssert() {
return dlwrap::SYM_USE##_Trait::get()(x0, x1, x2, x3, x4, x5, x6, x7, x8, \
x9, x10); \
}
#define DLWRAP_INSTANTIATE_12(SYM_DEF, SYM_USE, T) \
T::ReturnType SYM_DEF(typename T::template arg<0>::type x0, \
typename T::template arg<1>::type x1, \
typename T::template arg<2>::type x2, \
typename T::template arg<3>::type x3, \
typename T::template arg<4>::type x4, \
typename T::template arg<5>::type x5, \
typename T::template arg<6>::type x6, \
typename T::template arg<7>::type x7, \
typename T::template arg<8>::type x8, \
typename T::template arg<9>::type x9, \
typename T::template arg<10>::type x10, \
typename T::template arg<11>::type x11) { \
return dlwrap::SYM_USE##_Trait::get()(x0, x1, x2, x3, x4, x5, x6, x7, x8, \
x9, x10, x11); \
}

#endif // OMPTARGET_SHARED_DLWRAP_H
15 changes: 15 additions & 0 deletions offload/plugins-nextgen/common/include/PluginInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,14 @@ struct GenericPluginTy {
/// Create a new global handler for the underlying plugin.
virtual GenericGlobalHandlerTy *createGlobalHandler() = 0;

/// Get the reference to the device with a certain device id.
const GenericDeviceTy &getDevice(int32_t DeviceId) const {
assert(isValidDeviceId(DeviceId) && "Invalid device id");
assert(Devices[DeviceId] && "Device is uninitialized");

return *Devices[DeviceId];
}

/// Get the reference to the device with a certain device id.
GenericDeviceTy &getDevice(int32_t DeviceId) {
assert(isValidDeviceId(DeviceId) && "Invalid device id");
Expand Down Expand Up @@ -1558,6 +1566,13 @@ struct GenericPluginTy {
/// object and return immediately.
int32_t async_barrier(omp_interop_val_t *Interop);

/// Returns a Range over all the devices in the plugin that can be
/// used in a for loop:
/// for (&Device : GenericPluginRef.getDevicesRange()) {
auto getDevicesRange() {
return llvm::make_range(Devices.begin(), Devices.end());
}

private:
/// Indicates if the platform runtime has been fully initialized.
bool Initialized = false;
Expand Down
61 changes: 61 additions & 0 deletions offload/plugins-nextgen/level_zero/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Create the library and add the default arguments.
add_target_library(omptarget.rtl.level_zero LEVEL_ZERO)

set(LEVEL_ZERO_SRC_FILES
src/L0Context.cpp
src/L0Device.cpp
src/L0Kernel.cpp
src/L0Memory.cpp
src/L0Program.cpp
src/L0Plugin.cpp
src/L0Program.cpp
src/L0Options.cpp
)

target_sources(omptarget.rtl.level_zero PRIVATE
${LEVEL_ZERO_SRC_FILES}
)

target_include_directories(omptarget.rtl.level_zero PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
)

target_include_directories(omptarget.rtl.level_zero PRIVATE
${LIBOMPTARGET_INCLUDE_DIR}
${LIBOMPTARGET_DEP_LEVEL_ZERO_INCLUDE_DIR}
${LIBOMPTARGET_LLVM_INCLUDE_DIRS}
${LIBOMPTARGET_OMP_HEADER_DIR}
)

if (EXISTS ${LIBOMPTARGET_DEP_LEVEL_ZERO_LIBRARY} AND NOT "level_zero" IN_LIST LIBOMPTARGET_DLOPEN_PLUGINS)
message(STATUS "Building Level Zero NG plugin linked against level_zero library")
if(UNIX)
target_link_libraries(omptarget.rtl.level_zero PRIVATE
${LIBOMPTARGET_DEP_LEVEL_ZERO_LIBRARY})
elseif(WIN32)
# Full path to the Level Zero library is recognized as a linker option, so we
# separate directory and file name
cmake_path(GET LIBOMPTARGET_DEP_LEVEL_ZERO_LIBRARY FILENAME LEVEL_ZERO_LIBRARY_NAME)
cmake_path(GET LIBOMPTARGET_DEP_LEVEL_ZERO_LIBRARY PARENT_PATH LEVEL_ZERO_LIBRARY_PATH)
target_link_libraries(omptarget.rtl.level_zero PRIVATE ${LEVEL_ZERO_LIBRARY_NAME}
${LIBOMP_LIB_FILE})
target_link_directories(omptarget.rtl.level_zero PRIVATE ${LEVEL_ZERO_LIBRARY_PATH})
else()
message(FATAL_ERROR "Missing platform support")
endif()
else()
message(STATUS "Building Level Zero NG plugin for dlopened level_zero")
target_sources(omptarget.rtl.level_zero PRIVATE src/L0DynWrapper.cpp)
endif()

option(LIBOMPTARGET_FORCE_LEVELZERO_TESTS "Build Level Zero libomptarget tests" OFF)
if (LIBOMPTARGET_FOUND_INTELGPU_GPU OR LIBOMPTARGET_FORCE_LEVELZERO_TESTS)
# Report to the parent scope that we are building a plugin for intelgpu
set(LIBOMPTARGET_SYSTEM_TARGETS
"${LIBOMPTARGET_SYSTEM_TARGETS} spirv64-intel" PARENT_SCOPE)
list(APPEND LIBOMPTARGET_TESTED_PLUGINS "omptarget.rtl.level_zero")
set(LIBOMPTARGET_TESTED_PLUGINS "${LIBOMPTARGET_TESTED_PLUGINS}" PARENT_SCOPE)
else()
message(STATUS "Not generating Level Zero tests, no supported devices detected."
" Use 'LIBOMPTARGET_FORCE_INTELGPU_TESTS' to override.")
endif()
44 changes: 44 additions & 0 deletions offload/plugins-nextgen/level_zero/include/AsyncQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===--- 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
//
//===----------------------------------------------------------------------===//
//
// Async Queue wrapper for Level Zero.
//
//===----------------------------------------------------------------------===//

#ifndef OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_ASYNCQUEUE_H
#define OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_ASYNCQUEUE_H

#include <tuple>

#include "L0Memory.h"

namespace llvm::omp::target::plugin {

/// Abstract queue that supports asynchronous command submission.
struct AsyncQueueTy {
/// List of events attached to submitted commands.
llvm::SmallVector<ze_event_handle_t> WaitEvents;
/// Pending staging buffer to host copies.
llvm::SmallVector<std::tuple<void *, void *, size_t>> H2MList;
/// Pending USM memory copy commands that must wait for kernel completion.
llvm::SmallVector<std::tuple<const void *, void *, size_t>> USM2MList;
/// Kernel event not signaled.
ze_event_handle_t KernelEvent = nullptr;
/// Clear data.
void reset() {
WaitEvents.clear();
H2MList.clear();
USM2MList.clear();
KernelEvent = nullptr;
}
};

using AsyncQueuePoolTy = ObjPool<AsyncQueueTy>;

} // namespace llvm::omp::target::plugin
#endif // OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_ASYNCQUEUE_H
142 changes: 142 additions & 0 deletions offload/plugins-nextgen/level_zero/include/L0Context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//===--- 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
//
//===----------------------------------------------------------------------===//
//
// Level Zero Context abstraction.
//
//===----------------------------------------------------------------------===//

#ifndef OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0CONTEXT_H
#define OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0CONTEXT_H

#include "L0Memory.h"
#include "PerThreadTable.h"

namespace llvm::omp::target::plugin {

class LevelZeroPluginTy;

class L0ContextTLSTy {
StagingBufferTy StagingBuffer;

public:
StagingBufferTy &getStagingBuffer() { return StagingBuffer; }
const StagingBufferTy &getStagingBuffer() const { return StagingBuffer; }

Error deinit() { return StagingBuffer.clear(); }
};

struct L0ContextTLSTableTy
: public PerThreadContainer<
std::unordered_map<ze_context_handle_t, L0ContextTLSTy>> {
Error deinit() {
return PerThreadTable::deinit(
[](L0ContextTLSTy &Entry) -> auto { return Entry.deinit(); });
}
};

/// Driver and context-specific resources. We assume a single context per
/// driver.
class L0ContextTy {
/// The plugin that created this context.
LevelZeroPluginTy &Plugin;

/// Level Zero Driver handle.
ze_driver_handle_t zeDriver = nullptr;

/// Common Level Zero context.
ze_context_handle_t zeContext = nullptr;

/// API version supported by the Level Zero driver.
ze_api_version_t APIVersion = ZE_API_VERSION_CURRENT;

/// Imported external pointers. Track this only for user-directed
/// imports/releases.
llvm::DenseMap<uintptr_t, size_t> ImportedPtrs;

/// Common event pool.
EventPoolTy EventPool;

/// Host Memory allocator for this driver.
MemAllocatorTy HostMemAllocator;

public:
/// Named constants for checking the imported external pointer regions.
static constexpr int32_t ImportNotExist = -1;
static constexpr int32_t ImportUnknown = 0;
static constexpr int32_t ImportExist = 1;

/// Create context, initialize event pool and extension functions.
L0ContextTy(LevelZeroPluginTy &Plugin, ze_driver_handle_t zeDriver,
int32_t DriverId)
: Plugin(Plugin), zeDriver(zeDriver) {}

L0ContextTy(const L0ContextTy &) = delete;
L0ContextTy(L0ContextTy &&) = delete;
L0ContextTy &operator=(const L0ContextTy &) = delete;
L0ContextTy &operator=(const L0ContextTy &&) = delete;

/// Release resources.
~L0ContextTy() = default;

Error init();
Error deinit();

LevelZeroPluginTy &getPlugin() const { return Plugin; }

StagingBufferTy &getStagingBuffer();

/// Add imported external pointer region.
void addImported(void *Ptr, size_t Size) {
(void)ImportedPtrs.try_emplace(reinterpret_cast<uintptr_t>(Ptr), Size);
}

/// Remove imported external pointer region.
void removeImported(void *Ptr) {
(void)ImportedPtrs.erase(reinterpret_cast<uintptr_t>(Ptr));
}
/// Check if imported regions contain the specified region.
int32_t checkImported(void *Ptr, size_t Size) const {
uintptr_t LB = reinterpret_cast<uintptr_t>(Ptr);
uintptr_t UB = LB + Size;
// We do not expect a large number of user-directed imports, so use simple
// logic.
for (auto &I : ImportedPtrs) {
uintptr_t ILB = I.first;
uintptr_t IUB = ILB + I.second;
if (LB >= ILB && UB <= IUB)
return ImportExist;
if ((LB >= ILB && LB < IUB) || (UB > ILB && UB <= IUB))
return ImportUnknown;
}
return ImportNotExist;
}

ze_driver_handle_t getZeDriver() const { return zeDriver; }

/// Return context associated with the driver.
ze_context_handle_t getZeContext() const { return zeContext; }

/// Return driver API version.
ze_api_version_t getDriverAPIVersion() const { return APIVersion; }

/// Return the event pool of this driver.
EventPoolTy &getEventPool() { return EventPool; }
const EventPoolTy &getEventPool() const { return EventPool; }

bool supportsLargeMem() const {
// Large memory support is available since API version 1.1.
return getDriverAPIVersion() >= ZE_API_VERSION_1_1;
}

const MemAllocatorTy &getHostMemAllocator() const { return HostMemAllocator; }
MemAllocatorTy &getHostMemAllocator() { return HostMemAllocator; }
};

} // namespace llvm::omp::target::plugin

#endif // OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0CONTEXT_H
Loading
Loading