Skip to content

[mlir][ExecutionEngine] Add LevelZeroRuntimeWrapper. #151038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions mlir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ endif()
set(MLIR_ENABLE_CUDA_RUNNER 0 CACHE BOOL "Enable building the MLIR CUDA runner")
set(MLIR_ENABLE_ROCM_RUNNER 0 CACHE BOOL "Enable building the MLIR ROCm runner")
set(MLIR_ENABLE_SYCL_RUNNER 0 CACHE BOOL "Enable building the MLIR SYCL runner")
set(MLIR_ENABLE_LEVELZERO_RUNNER 0 CACHE BOOL "Enable building the MLIR LevelZero runner")
set(MLIR_ENABLE_SPIRV_CPU_RUNNER 0 CACHE BOOL "Enable building the MLIR SPIR-V cpu runner")
set(MLIR_ENABLE_VULKAN_RUNNER 0 CACHE BOOL "Enable building the MLIR Vulkan runner")
set(MLIR_ENABLE_NVPTXCOMPILER 0 CACHE BOOL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,39 @@ include(FindPackageHandleStandardArgs)
# Search path priority
# 1. CMake Variable LEVEL_ZERO_DIR
# 2. Environment Variable LEVEL_ZERO_DIR

if(NOT LEVEL_ZERO_DIR)
if(DEFINED ENV{LEVEL_ZERO_DIR})
set(LEVEL_ZERO_DIR "$ENV{LEVEL_ZERO_DIR}")
endif()
endif()

if(LEVEL_ZERO_DIR)
find_path(LevelZero_INCLUDE_DIR
find_path(LevelZeroRuntime_INCLUDE_DIR
NAMES level_zero/ze_api.h
PATHS ${LEVEL_ZERO_DIR}/include
NO_DEFAULT_PATH
)

if(LINUX)
find_library(LevelZero_LIBRARY
find_library(LevelZeroRuntime_LIBRARY
NAMES ze_loader
PATHS ${LEVEL_ZERO_DIR}/lib
${LEVEL_ZERO_DIR}/lib/x86_64-linux-gnu
${LEVEL_ZERO_DIR}/lib/x86_64-linux-gnu
NO_DEFAULT_PATH
)
else()
find_library(LevelZero_LIBRARY
find_library(LevelZeroRuntime_LIBRARY
NAMES ze_loader
PATHS ${LEVEL_ZERO_DIR}/lib
NO_DEFAULT_PATH
)
endif()
else()
find_path(LevelZero_INCLUDE_DIR
find_path(LevelZeroRuntime_INCLUDE_DIR
NAMES level_zero/ze_api.h
)

find_library(LevelZero_LIBRARY
find_library(LevelZeroRuntime_LIBRARY
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this renaming feels unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it to make it consistent with SYCL. Since it's a one time thing, planning to keep it if no opposition.

NAMES ze_loader
)
endif()
Expand All @@ -64,12 +63,14 @@ endif()
# lists of equal lengths, with the shorter string getting zero-padded.
function(compare_versions VERSION_STR1 VERSION_STR2 OUTPUT)
# Convert the strings to list
string(REPLACE "." ";" VL1 ${VERSION_STR1})
string(REPLACE "." ";" VL2 ${VERSION_STR2})
string(REPLACE "." ";" VL1 ${VERSION_STR1})
string(REPLACE "." ";" VL2 ${VERSION_STR2})

# get lengths of both lists
list(LENGTH VL1 VL1_LEN)
list(LENGTH VL2 VL2_LEN)
set(LEN ${VL1_LEN})

# If they differ in size pad the shorter list with 0s
if(VL1_LEN GREATER VL2_LEN)
math(EXPR DIFF "${VL1_LEN} - ${VL2_LEN}" OUTPUT_FORMAT DECIMAL)
Expand Down Expand Up @@ -98,12 +99,10 @@ function(compare_versions VERSION_STR1 VERSION_STR2 OUTPUT)
set(${OUTPUT} TRUE PARENT_SCOPE)
endif()
endforeach()

endfunction(compare_versions)
endfunction(compare_versions)

# Creates a small function to run and extract the LevelZero loader version.
function(get_l0_loader_version)

set(L0_VERSIONEER_SRC
[====[
#include <iostream>
Expand Down Expand Up @@ -142,19 +141,20 @@ function(get_l0_loader_version)

# We need both the directories in the include path as ze_loader.h
# includes "ze_api.h" and not "level_zero/ze_api.h".
list(APPEND INCLUDE_DIRS ${LevelZero_INCLUDE_DIR})
list(APPEND INCLUDE_DIRS ${LevelZero_INCLUDE_DIR}/level_zero)
list(APPEND INCLUDE_DIRS ${LevelZeroRuntime_INCLUDE_DIR})
list(APPEND INCLUDE_DIRS ${LevelZeroRuntime_INCLUDE_DIR}/level_zero)
list(JOIN INCLUDE_DIRS ";" INCLUDE_DIRS_STR)
try_run(L0_VERSIONEER_RUN L0_VERSIONEER_COMPILE
"${CMAKE_BINARY_DIR}"
"${L0_VERSIONEER_FILE}"
LINK_LIBRARIES ${LevelZero_LIBRARY}
CMAKE_FLAGS
"-DINCLUDE_DIRECTORIES=${INCLUDE_DIRS_STR}"
RUN_OUTPUT_VARIABLE L0_VERSION
"${CMAKE_BINARY_DIR}"
"${L0_VERSIONEER_FILE}"
LINK_LIBRARIES ${LevelZeroRuntime_LIBRARY}
CMAKE_FLAGS
"-DINCLUDE_DIRECTORIES=${INCLUDE_DIRS_STR}"
RUN_OUTPUT_VARIABLE L0_VERSION
)
if(${L0_VERSIONEER_COMPILE} AND (DEFINED L0_VERSIONEER_RUN))
set(LevelZero_VERSION ${L0_VERSION} PARENT_SCOPE)

if(${L0_VERSIONEER_COMPILE} AND(DEFINED L0_VERSIONEER_RUN))
set(LevelZeroRuntime_VERSION ${L0_VERSION} PARENT_SCOPE)
message(STATUS "Found Level Zero of version: ${L0_VERSION}")
else()
message(FATAL_ERROR
Expand All @@ -163,59 +163,61 @@ function(get_l0_loader_version)
endif()
endfunction(get_l0_loader_version)

if(LevelZero_INCLUDE_DIR AND LevelZero_LIBRARY)
list(APPEND LevelZero_LIBRARIES "${LevelZero_LIBRARY}")
list(APPEND LevelZero_INCLUDE_DIRS ${LevelZero_INCLUDE_DIR})
if(LevelZeroRuntime_INCLUDE_DIR AND LevelZeroRuntime_LIBRARY)
list(APPEND LevelZeroRuntime_LIBRARIES "${LevelZeroRuntime_LIBRARY}")
list(APPEND LevelZeroRuntime_INCLUDE_DIRS ${LevelZeroRuntime_INCLUDE_DIR})

if(OpenCL_FOUND)
list(APPEND LevelZero_INCLUDE_DIRS ${OpenCL_INCLUDE_DIRS})
list(APPEND LevelZeroRuntime_INCLUDE_DIRS ${OpenCL_INCLUDE_DIRS})
endif()

cmake_path(GET LevelZero_LIBRARY PARENT_PATH LevelZero_LIBRARIES_PATH)
set(LevelZero_LIBRARIES_DIR ${LevelZero_LIBRARIES_PATH})

if(NOT TARGET LevelZero::LevelZero)
add_library(LevelZero::LevelZero INTERFACE IMPORTED)
set_target_properties(LevelZero::LevelZero
PROPERTIES INTERFACE_LINK_LIBRARIES "${LevelZero_LIBRARIES}"
)
set_target_properties(LevelZero::LevelZero
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LevelZero_INCLUDE_DIRS}"
)
cmake_path(GET LevelZeroRuntime_LIBRARY PARENT_PATH LevelZeroRuntime_LIBRARIES_PATH)
set(LevelZeroRuntime_LIBRARIES_DIR ${LevelZeroRuntime_LIBRARIES_PATH})

if(NOT TARGET LevelZeroRuntime::LevelZeroRuntime)
add_library(LevelZeroRuntime::LevelZeroRuntime INTERFACE IMPORTED)
set_target_properties(LevelZeroRuntime::LevelZeroRuntime
PROPERTIES INTERFACE_LINK_LIBRARIES "${LevelZeroRuntime_LIBRARIES}"
)
set_target_properties(LevelZeroRuntime::LevelZeroRuntime
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LevelZeroRuntime_INCLUDE_DIRS}"
)
endif()
endif()

# Check if a specific version of Level Zero is required
if(LevelZero_FIND_VERSION)
if(LevelZeroRuntime_FIND_VERSION)
get_l0_loader_version()
set(VERSION_GT_FIND_VERSION FALSE)
compare_versions(
${LevelZero_VERSION}
${LevelZero_FIND_VERSION}
${LevelZeroRuntime_VERSION}
${LevelZeroRuntime_FIND_VERSION}
VERSION_GT_FIND_VERSION
)

if(${VERSION_GT_FIND_VERSION})
set(LevelZero_FOUND TRUE)
set(LevelZeroRuntime_FOUND TRUE)
else()
set(LevelZero_FOUND FALSE)
set(LevelZeroRuntime_FOUND FALSE)
endif()
else()
set(LevelZero_FOUND TRUE)
set(LevelZeroRuntime_FOUND TRUE)
endif()

find_package_handle_standard_args(LevelZero
find_package_handle_standard_args(LevelZeroRuntime
REQUIRED_VARS
LevelZero_FOUND
LevelZero_INCLUDE_DIRS
LevelZero_LIBRARY
LevelZero_LIBRARIES_DIR
LevelZeroRuntime_FOUND
LevelZeroRuntime_INCLUDE_DIRS
LevelZeroRuntime_LIBRARY
LevelZeroRuntime_LIBRARIES_DIR
HANDLE_COMPONENTS
)
mark_as_advanced(LevelZero_LIBRARY LevelZero_INCLUDE_DIRS)
mark_as_advanced(LevelZeroRuntime_LIBRARY LevelZeroRuntime_INCLUDE_DIRS)

if(LevelZero_FOUND)
find_package_message(LevelZero "Found LevelZero: ${LevelZero_LIBRARY}"
"(found version ${LevelZero_VERSION})"
if(LevelZeroRuntime_FOUND)
find_package_message(LevelZeroRuntime "Found LevelZero: ${LevelZeroRuntime_LIBRARY}"
"(found version ${LevelZeroRuntime_VERSION})"
)
else()
find_package_message(LevelZero "Could not find LevelZero" "")
find_package_message(LevelZeroRuntime "Could not find LevelZero" "")
endif()
39 changes: 31 additions & 8 deletions mlir/lib/ExecutionEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(LLVM_OPTIONAL_SOURCES
RunnerUtils.cpp
OptUtils.cpp
JitRunner.cpp
LevelZeroRuntimeWrappers.cpp
SpirvCpuRuntimeWrappers.cpp
SyclRuntimeWrappers.cpp
VulkanRuntimeWrappers.cpp
Expand Down Expand Up @@ -374,19 +375,22 @@ if(LLVM_ENABLE_PIC)
)
endif()

if(MLIR_ENABLE_SYCL_RUNNER OR MLIR_ENABLE_LEVELZERO_RUNNER)
# Both runtimes require LevelZero, so we can find it once.
find_package(LevelZeroRuntime)

if(NOT LevelZeroRuntime_FOUND)
message(FATAL_ERROR "LevelZero not found. Please set LEVEL_ZERO_DIR.")
endif()
endif()

if(MLIR_ENABLE_SYCL_RUNNER)
find_package(SyclRuntime)

if(NOT SyclRuntime_FOUND)
message(FATAL_ERROR "syclRuntime not found. Please set check oneapi installation and run setvars.sh.")
endif()

find_package(LevelZero)

if(NOT LevelZero_FOUND)
message(FATAL_ERROR "LevelZero not found. Please set LEVEL_ZERO_DIR.")
endif()

add_mlir_library(mlir_sycl_runtime
SHARED
SyclRuntimeWrappers.cpp
Expand All @@ -404,9 +408,28 @@ if(LLVM_ENABLE_PIC)
${MLIR_INCLUDE_DIRS}
)

target_link_libraries(mlir_sycl_runtime PRIVATE LevelZero::LevelZero SyclRuntime::SyclRuntime)
target_link_libraries(mlir_sycl_runtime PRIVATE LevelZeroRuntime::LevelZeroRuntime SyclRuntime::SyclRuntime)

set_property(TARGET mlir_sycl_runtime APPEND PROPERTY BUILD_RPATH "${LevelZeroRuntime_LIBRARIES_DIR}" "${SyclRuntime_LIBRARIES_DIR}")
endif()

if(MLIR_ENABLE_LEVELZERO_RUNNER)
add_mlir_library(mlir_levelzero_runtime
SHARED
LevelZeroRuntimeWrappers.cpp

EXCLUDE_FROM_LIBMLIR
)

target_compile_options(mlir_levelzero_runtime PUBLIC -fexceptions -frtti)

target_include_directories(mlir_levelzero_runtime PRIVATE
${MLIR_INCLUDE_DIRS}
)

target_link_libraries(mlir_levelzero_runtime PRIVATE LevelZeroRuntime::LevelZeroRuntime)

set_property(TARGET mlir_sycl_runtime APPEND PROPERTY BUILD_RPATH "${LevelZero_LIBRARIES_DIR}" "${SyclRuntime_LIBRARIES_DIR}")
set_property(TARGET mlir_levelzero_runtime APPEND PROPERTY BUILD_RPATH "${LevelZeroRuntime_LIBRARIES_DIR}")
endif()

if(MLIR_ENABLE_SPIRV_CPU_RUNNER)
Expand Down
Loading