[Mlir-commits] [mlir] [mlir][ExecutionEngine] Add LevelZeroRuntimeWrapper. (PR #151038)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Jul 28 13:49:08 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-gpu

Author: Md Abdullah Shahneous Bari (mshahneo)

<details>
<summary>Changes</summary>

Adds LevelZeroRuntime wrapper and tests.

---

Patch is 53.34 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/151038.diff


8 Files Affected:

- (renamed) mlir/cmake/modules/FindLevelZeroRuntime.cmake (+60-53) 
- (modified) mlir/lib/ExecutionEngine/CMakeLists.txt (+83-34) 
- (added) mlir/lib/ExecutionEngine/LevelZeroRuntimeWrappers.cpp (+491) 
- (added) mlir/test/Integration/GPU/LEVELZERO/gpu-addf32-to-spirv.mlir (+56) 
- (added) mlir/test/Integration/GPU/LEVELZERO/gpu-addi64-to-spirv.mlir (+54) 
- (added) mlir/test/Integration/GPU/LEVELZERO/gpu-memcpy-addf32-to-spirv.mlir (+53) 
- (added) mlir/test/Integration/GPU/LEVELZERO/gpu-reluf32-to-spirv.mlir (+79) 
- (added) mlir/test/Integration/GPU/LEVELZERO/lit.local.cfg (+2) 


``````````diff
diff --git a/mlir/cmake/modules/FindLevelZero.cmake b/mlir/cmake/modules/FindLevelZeroRuntime.cmake
similarity index 66%
rename from mlir/cmake/modules/FindLevelZero.cmake
rename to mlir/cmake/modules/FindLevelZeroRuntime.cmake
index 012187f0afc0b..b1e8e5b6387f2 100644
--- a/mlir/cmake/modules/FindLevelZero.cmake
+++ b/mlir/cmake/modules/FindLevelZeroRuntime.cmake
@@ -20,7 +20,6 @@ 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}")
@@ -28,32 +27,32 @@ if(NOT LEVEL_ZERO_DIR)
 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
         NAMES ze_loader
     )
 endif()
@@ -64,26 +63,33 @@ 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)
+
         foreach(IDX RANGE 1 ${DIFF} 1)
             list(APPEND VL2 "0")
         endforeach()
     elseif(VL2_LEN GREATER VL2_LEN)
         math(EXPR DIFF "${VL1_LEN} - ${VL2_LEN}" OUTPUT_FORMAT DECIMAL)
+
         foreach(IDX RANGE 1 ${DIFF} 1)
             list(APPEND VL2 "0")
         endforeach()
+
         set(LEN ${VL2_LEN})
     endif()
+
     math(EXPR LEN_SUB_ONE "${LEN}-1")
+
     foreach(IDX RANGE 0 ${LEN_SUB_ONE} 1)
         list(GET VL1 ${IDX} VAL1)
         list(GET VL2 ${IDX} VAL2)
@@ -98,12 +104,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>
@@ -142,19 +146,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
@@ -163,59 +168,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()
diff --git a/mlir/lib/ExecutionEngine/CMakeLists.txt b/mlir/lib/ExecutionEngine/CMakeLists.txt
index dd2ac75b88798..06c879f082926 100644
--- a/mlir/lib/ExecutionEngine/CMakeLists.txt
+++ b/mlir/lib/ExecutionEngine/CMakeLists.txt
@@ -14,12 +14,13 @@ set(LLVM_OPTIONAL_SOURCES
   RunnerUtils.cpp
   OptUtils.cpp
   JitRunner.cpp
+  LevelZeroRuntimeWrappers.cpp
   SpirvCpuRuntimeWrappers.cpp
   SyclRuntimeWrappers.cpp
   VulkanRuntimeWrappers.cpp
   VulkanRuntime.cpp
   VulkanRuntime.h
-  )
+)
 
 # Use a separate library for OptUtils, to avoid pulling in the entire JIT and
 # codegen infrastructure. Unlike MLIRExecutionEngine, this is part of
@@ -45,7 +46,7 @@ add_mlir_library(MLIRExecutionEngineUtils
   IPO
   Passes
   TargetParser
-  )
+)
 
 if(NOT MLIR_ENABLE_EXECUTION_ENGINE)
   return()
@@ -53,12 +54,12 @@ endif()
 
 if(LLVM_USE_INTEL_JITEVENTS)
   set(LLVM_JIT_LISTENER_LIB
-      IntelJITEvents)
+    IntelJITEvents)
 endif(LLVM_USE_INTEL_JITEVENTS)
 
 if(LLVM_USE_PERF)
   set(LLVM_JIT_LISTENER_LIB
-      PerfJITEvents)
+    PerfJITEvents)
 endif(LLVM_USE_PERF)
 
 add_mlir_library(MLIRExecutionEngine
@@ -91,7 +92,7 @@ add_mlir_library(MLIRExecutionEngine
   IPO
   Passes
   ${LLVM_JIT_LISTENER_LIB}
-  )
+)
 
 mlir_target_link_libraries(MLIRExecutionEngine PUBLIC
   MLIRBuiltinToLLVMIRTranslation
@@ -100,9 +101,9 @@ mlir_target_link_libraries(MLIRExecutionEngine PUBLIC
   MLIRLLVMToLLVMIRTranslation
   MLIROpenMPToLLVMIRTranslation
   MLIRTargetLLVMIRExport
-  )
+)
 
-if(LLVM_BUILD_LLVM_DYLIB AND NOT (WIN32 OR MINGW OR CYGWIN)) # Does not build on windows currently, see #106859
+if(LLVM_BUILD_LLVM_DYLIB AND NOT(WIN32 OR MINGW OR CYGWIN)) # Does not build on windows currently, see #106859
   # Build a shared library for the execution engine. Some downstream projects
   # use this library to build their own CPU runners while preserving dynamic
   # linkage.
@@ -122,7 +123,7 @@ if(LLVM_BUILD_LLVM_DYLIB AND NOT (WIN32 OR MINGW OR CYGWIN)) # Does not build on
     LINK_LIBS PUBLIC
     LLVM
     MLIR
-    )
+  )
 endif()
 
 get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
@@ -162,7 +163,7 @@ if(LLVM_ENABLE_PIC)
     Float16bits.cpp
 
     EXCLUDE_FROM_LIBMLIR
-    )
+  )
   set_property(TARGET mlir_float16_utils PROPERTY CXX_STANDARD 17)
   target_compile_definitions(mlir_float16_utils PRIVATE mlir_float16_utils_EXPORTS)
 
@@ -179,7 +180,7 @@ if(LLVM_ENABLE_PIC)
     mlir_float16_utils
     MLIRSparseTensorEnums
     MLIRSparseTensorRuntime
-    )
+  )
   set_property(TARGET mlir_c_runner_utils PROPERTY CXX_STANDARD 17)
   target_compile_definitions(mlir_c_runner_utils PRIVATE mlir_c_runner_utils_EXPORTS)
 
@@ -205,6 +206,7 @@ if(LLVM_ENABLE_PIC)
   )
   set_property(TARGET mlir_async_runtime PROPERTY CXX_VISIBILITY_PRESET hidden)
   target_compile_definitions(mlir_async_runtime PRIVATE mlir_async_runtime_EXPORTS)
+
   if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
     # Don't export symbols from link-time dependencies, these are internal
     # implementation details.
@@ -226,7 +228,8 @@ if(LLVM_ENABLE_PIC)
     # custom error message.
     include(CheckLanguage)
     check_language(CUDA)
-    if (CMAKE_CUDA_COMPILER)
+
+    if(CMAKE_CUDA_COMPILER)
       enable_language(CUDA)
     else()
       message(SEND_ERROR
@@ -290,13 +293,14 @@ if(LLVM_ENABLE_PIC)
 
   if(MLIR_ENABLE_ROCM_RUNNER)
     # Configure ROCm support.
-    if (NOT DEFINED ROCM_PATH)
-      if (NOT DEFINED ENV{ROCM_PATH})
+    if(NOT DEFINED ROCM_PATH)
+      if(NOT DEFINED ENV{ROCM_PATH})
         set(ROCM_PATH "/opt/rocm" CACHE PATH "Path to which ROCm has been installed")
       else()
         set(ROCM_PATH $ENV{ROCM_PATH} CACHE PATH "Path to which ROCm has been installed")
       endif()
     endif()
+
     # A lot of the ROCm CMake files expect to find their own dependencies in
     # CMAKE_PREFIX_PATH and don't respect PATHS or HINTS :( .
     # Therefore, temporarily add the ROCm path to CMAKE_PREFIX_PATH so we can
@@ -306,24 +310,28 @@ if(LLVM_ENABLE_PIC)
     find_package(hip REQUIRED)
     set(CMAKE_PREFIX_PATH "${REAL_CMAKE_PREFIX_PATH}")
 
-    if (NOT DEFINED ROCM_TEST_CHIPSET)
+    if(NOT DEFINED ROCM_TEST_CHIPSET)
       find_program(ROCM_AGENT_ENUMERATOR rocm_agent_enumerator "${ROCM_PATH}/bin" /usr/bin /usr/local/bin)
+
       if(ROCM_AGENT_ENUMERATOR)
-          execute_process(COMMAND "${ROCM_AGENT_ENUMERATOR}"
+        execute_process(COMMAND "${ROCM_AGENT_ENUMERATOR}"
           OUTPUT_VARIABLE AGENTS_STRING
           ERROR_VARIABLE AGENTS_STRING
           RESULT_VARIABLE AGENT_ENUMERATOR_RESULT)
       else()
         message(SEND_ERROR "Could not find rocm_agent_enumerator")
       endif()
-      if (NOT AGENT_ENUMERATOR_RESULT EQUAL 0)
+
+      if(NOT AGENT_ENUMERATOR_RESULT EQUAL 0)
         message(SEND_ERROR "Could not run rocm_agent_enumerator and ROCM_TEST_CHIPSET is not defined")
         set(AGENTS_STRING "")
       endif()
+
       string(STRIP AGENTS_STRING ${AGENTS_STRING})
       string(REPLACE "\n" ";" AGENTS_LIST ${AGENTS_STRING})
       list(FILTER AGENTS_LIST EXCLUDE REGEX "gfx000")
-      if (AGENTS_LIST STREQUAL "")
+
+      if(AGENTS_LIST STREQUAL "")
         message(SEND_ERROR "No non-CPU ROCm agents found on the system, and ROCM_TEST_CHIPSET is not defined")
       else()
         list(GET AGENTS_LIST 0 FIRST_AGENT)
@@ -342,27 +350,34 @@ if(LLVM_ENABLE_PIC)
     # Supress compiler warnings from HIP headers
     check_cxx_compiler_flag(-Wno-c++98-compat-extra-semi
       CXX_SUPPORTS_NO_CXX98_COMPAT_EXTRA_SEMI_FLAG)
-    if (CXX_SUPPORTS_CXX98_COMPAT_EXTRA_SEMI_FLAG)
+
+    if(CXX_SUPPORTS_CXX98_COMPAT_EXTRA_SEMI_FLAG)
       target_compile_options(mlir_rocm_runtime PRIVATE
         "-Wno-c++98-compat-extra-semi")
     endif()
+
     check_cxx_compiler_flag(-Wno-return-type-c-linkage
-        CXX_SUPPORTS_WNO_RETURN_TYPE_C_LINKAGE_FLAG)
-    if (CXX_SUPPORTS_WNO_RETURN_TYPE_C_LINKAGE_FLAG)
+      CXX_SUPPORTS_WNO_RETURN_TYPE_C_LINKAGE_FLAG)
+
+    if(CXX_SUPPORTS_WNO_RETURN_TYPE_C_LINKAGE_FLAG)
       target_compile_options(mlir_rocm_runtime PRIVATE
         "-Wno-return-type-c-linkage")
     endif()
+
     check_cxx_compiler_flag(-Wno-nested-anon-types
       CXX_SUPPORTS_WNO_NESTED_ANON_TYPES_FLAG)
-    if (CXX_SUPPORTS_WNO_NESTED_ANON_TYPES_FLAG)
+
+    if(CXX_SUPPORTS_WNO_NESTED_ANON_TYPES_FLAG)
       target_compile_options(mlir_rocm_runtime PRIVATE
         "-Wno-nested-anon-types")
     endif()
+
     check_cxx_compiler_flag(-Wno-gnu-anonymous-struct
       CXX_SUPPORTS_WNO_GNU_ANONYMOUS_STRUCT_FLAG)
-    if (CXX_SUPPORTS_WNO_GNU_ANONYMOUS_STRUCT_FLAG)
+
+    if(CXX_SUPPORTS_WNO_GNU_ANONYMOUS_STRUCT_FLAG)
       target_compile_options(mlir_rocm_runtime PRIVATE
-      "-Wno-gnu-anonymous-struct")
+        "-Wno-gnu-anonymous-struct")
     endif()
 
     set_property(TARGET mlir_rocm_runtime
@@ -381,9 +396,9 @@ if(LLVM_ENABLE_PIC)
       message(FATAL_ERROR "syclRuntime not found. Please set check oneapi installation and run setvars.sh.")
     endif()
 
-    find_package(LevelZero)
+    find_package(LevelZeroRuntime)
 
-    if(NOT LevelZero_FOUND)
+    if(NOT LevelZeroRuntime_FOUND)
       message(FATAL_ERROR "LevelZero not found. Please set LEVEL_ZERO_DIR.")
     endif()
 
@@ -395,18 +410,51 @@ if(LLVM_ENABLE_PIC)
     )
 
     check_cxx_compiler_flag("-frtti" CXX_HAS_FRTTI_FLAG)
+
     if(NOT CXX_HAS_FRTTI_FLAG)
       message(FATAL_ERROR "CXX compiler does not accept flag -frtti")
     endif()
-    target_compile_options (mlir_sycl_runtime PUBLIC -fexceptions -frtti)
+
+    target_compile_options(mlir_sycl_runtime PUBLIC -fexceptions -frtti)
 
     target_include_directories(mlir_sycl_runtime PRIVATE
       ${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_LEVEL_ZERO_RUNNER)
+    find_package(LevelZeroRuntime)
+
+    if(NOT LevelZeroRuntime_FOUND)
+      message(FATAL_ERROR "LevelZero not found. Please set LEVEL_ZERO_DIR.")
+    endif()
+
+    add_mlir_library(mlir_levelzero_runtime
+      SHARED
+      LevelZeroRuntimeWrappers.cpp
+
+      EXCLUDE_FROM_LIBMLIR
+    )
+
+    check_cxx_compiler_flag("-frtti" CXX_HAS_FRTTI_FLAG)
+
+    if(NOT CXX_HAS_FRTTI_FLAG)
+      message(FATAL_ERROR "CXX compiler does not accept flag -frtti")
+    endif()
+
+    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)
@@ -422,25 +470,26 @@ if(LLVM_ENABLE_PIC)
       mlir_spirv_cpu_runtime_EXPORTS)
   endif()
 
-  if (MLIR_ENABLE_VULKAN_RUNNER)
+  if(MLIR_ENABLE_VULKAN_RUNNER)
     find_package(Vulkan)
 
     # If Vulkan is not found try a path specified by VULKAN_SDK.
-    if (NOT Vulkan_FOUND)
-      if ("$ENV{VULKAN_SDK}" STREQUAL "")
+    if(NOT Vulkan_FOUND)
+      if("$ENV{VULKAN_SDK}" STREQUAL "")
         message(FATAL_ERROR "Vulkan not found through CMake; please provide "
-                            "VULKAN_SDK path as an environment variable")
+          "VULKAN_SDK path as an environment variable")
       endif()
 
       find_library(Vulkan_LIBRARY vulkan HINTS "$ENV{VULKAN_SDK}/lib" REQUIRED)
-      if (Vulkan_LIBRARY)
+
+      if(Vulkan_LIBRARY)
         set(Vulkan_FOUND ON)
         set(Vulkan_INCLUDE_DIR "$ENV{VULKAN_SDK}/include")
         message(STATUS "Found Vulkan: " ${Vulkan_LIBRARY})
       endif()
     endif()
 
-    if (NOT Vulkan_FOUND)
+    if(NOT Vulkan_FOUND)
       message(FATAL_ERROR "Cannot find Vulkan library")
     endif()
 
diff --git a/mlir/lib/ExecutionEngine/LevelZeroRuntimeWrappers.cpp b/mlir/lib/ExecutionEngine/LevelZeroRuntimeWrappers.cpp
new file mode 100644
index 0000000000000..70ac4761dc7fd
--- /dev/null
+++ b/mlir/lib/ExecutionEngine/LevelZeroRuntimeWrappers.cpp
@@ -0,0 +1,491 @@
+//===- LevelZeroRuntimeWrappers.cpp - MLIR Level Zero (L0) wrapper library-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Implements wrappers around the Level Zero (L0) runtime library with C linkage
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/Twine.h"
+
+#include <cassert>
+#include <deque>
+#include <exception>
+#include <functional>
+#include <iostream>
+#include <level_zero/ze_api.h>
+#include <limits>
+#include <unordered_set>
+#include <vector>
+
+namespace {
+
+template <typename F>
+auto catchAll(F &&func) {
+  try {
+    return func();
+  } catch (const std::exception &e) {
+    std::cerr << "An exception was thrown: " << e.what() << std::endl;
+    std::abort();
+  } catch (...) {
+    std::cerr << "An unknown exception was thrown." << std::endl;
+    std::abort();
+  }
+}
+
+#define L0_SAFE_CALL(call)                                                     \
+  {                                                                            \
+    ze_result_t status = (call);                                               \
+    if (status != ZE_RESULT_SUCCESS) {                                         \
+      std::cerr << "L0 error " << status << std::endl;                         \
+      std::abort();                                                            \
+    }                                                                          \
+  }
+
+} // namespace
+
+//===-------...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/151038


More information about the Mlir-commits mailing list