From a0d0942d2b42d937b9ec41e7526d93c608a2beaf Mon Sep 17 00:00:00 2001 From: Brandon Yates Date: Thu, 23 Mar 2023 22:44:54 -0400 Subject: [PATCH] [SYCL][UR] Link UR PI against UR Loader (#8637) Compile UR L0 adapter as a standalone library Signed-off-by: Brandon Yates --- sycl/plugins/unified_runtime/CMakeLists.txt | 46 +-- sycl/plugins/unified_runtime/pi2ur.hpp | 3 +- .../ur/adapters/level_zero/ur_level_zero.cpp | 16 +- .../level_zero/ur_level_zero_common.cpp | 2 +- .../level_zero/ur_loader_interface.cpp | 286 ++++++++++++++++++ 5 files changed, 329 insertions(+), 24 deletions(-) create mode 100644 sycl/plugins/unified_runtime/ur/adapters/level_zero/ur_loader_interface.cpp diff --git a/sycl/plugins/unified_runtime/CMakeLists.txt b/sycl/plugins/unified_runtime/CMakeLists.txt index 76b3916549a38..664dd7dc51856 100755 --- a/sycl/plugins/unified_runtime/CMakeLists.txt +++ b/sycl/plugins/unified_runtime/CMakeLists.txt @@ -4,7 +4,7 @@ if (NOT DEFINED UNIFIED_RUNTIME_LIBRARY OR NOT DEFINED UNIFIED_RUNTIME_INCLUDE_D include(FetchContent) set(UNIFIED_RUNTIME_REPO "https://github.com/oneapi-src/unified-runtime.git") - set(UNIFIED_RUNTIME_TAG 6fb8e2620f1474428a539ef46d6dd47043c7d59b) + set(UNIFIED_RUNTIME_TAG d6af758779db6eebdc419fd5e249302f566eb5de) message(STATUS "Will fetch Unified Runtime from ${UNIFIED_RUNTIME_REPO}") FetchContent_Declare(unified-runtime @@ -37,16 +37,16 @@ if (NOT DEFINED UNIFIED_RUNTIME_LIBRARY OR NOT DEFINED UNIFIED_RUNTIME_INCLUDE_D # Restore original flags set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_BAK}") - add_library(${PROJECT_NAME}::ur_loader ALIAS loader) - - set(UNIFIED_RUNTIME_LIBRARY ${PROJECT_NAME}::ur_loader) + add_library(UnifiedRuntimeLoader ALIAS loader) set(UNIFIED_RUNTIME_SOURCE_DIR ${unified-runtime_SOURCE_DIR} CACHE PATH "Path to Unified Runtime Headers") set(UNIFIED_RUNTIME_INCLUDE_DIR "${UNIFIED_RUNTIME_SOURCE_DIR}/include") endif() + add_library (UnifiedRuntime-Headers INTERFACE) + target_include_directories(UnifiedRuntime-Headers INTERFACE "${UNIFIED_RUNTIME_INCLUDE_DIR}" @@ -54,11 +54,6 @@ target_include_directories(UnifiedRuntime-Headers find_package(Threads REQUIRED) -# -# NOTE: the Unified Runtime doesn't have the loader [yet]. -# So what we really build is the Unified Runtime with Level Zero Adapter -# together. -# add_sycl_plugin(unified_runtime SOURCES # These are short-term shared with Unified Runtime @@ -69,9 +64,25 @@ add_sycl_plugin(unified_runtime "ur_bindings.hpp" "pi2ur.hpp" "pi2ur.cpp" + # These below belong to Unified Runtime PI Plugin only + "pi_unified_runtime.hpp" + "pi_unified_runtime.cpp" + LIBRARIES + Threads::Threads + UnifiedRuntimeLoader + UnifiedRuntime-Headers +) + +# Build level zero adapter +add_sycl_library("ur_adapter_level_zero" SHARED + SOURCES "ur/ur.hpp" "ur/ur.cpp" + "ur/usm_allocator.hpp" + "ur/usm_allocator.cpp" "ur/adapters/level_zero/ur_level_zero.hpp" + "ur/adapters/level_zero/ur_level_zero.cpp" + "ur/adapters/level_zero/ur_loader_interface.cpp" "ur/adapters/level_zero/ur_level_zero_common.hpp" "ur/adapters/level_zero/ur_level_zero_context.hpp" "ur/adapters/level_zero/ur_level_zero_device.hpp" @@ -93,18 +104,19 @@ add_sycl_plugin(unified_runtime "ur/adapters/level_zero/ur_level_zero_program.cpp" "ur/adapters/level_zero/ur_level_zero_queue.cpp" "ur/adapters/level_zero/ur_level_zero_sampler.cpp" - "ur/usm_allocator.hpp" - "ur/usm_allocator.cpp" - # These below belong to Unified Runtime PI Plugin only - "pi_unified_runtime.hpp" - "pi_unified_runtime.cpp" - LIBRARIES - Threads::Threads + INCLUDE_DIRS + LIBRARIES UnifiedRuntime-Headers LevelZeroLoader-Headers LevelZeroLoader ) -if (TARGET ${PROJECT_NAME}::ur_loader) +set_target_properties("ur_adapter_level_zero" PROPERTIES + VERSION "0.0.0" + SOVERSION "0" +) + + +if (TARGET UnifiedRuntimeLoader) set_target_properties(hello_world PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1) endif() diff --git a/sycl/plugins/unified_runtime/pi2ur.hpp b/sycl/plugins/unified_runtime/pi2ur.hpp index fef607f3ef185..8730866b2942a 100644 --- a/sycl/plugins/unified_runtime/pi2ur.hpp +++ b/sycl/plugins/unified_runtime/pi2ur.hpp @@ -29,7 +29,7 @@ static pi_result ur2piResult(ur_result_t urResult) { {UR_RESULT_ERROR_INVALID_FUNCTION_NAME, PI_ERROR_BUILD_PROGRAM_FAILURE}, {UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE, PI_ERROR_INVALID_WORK_GROUP_SIZE}, - {UR_RESULT_ERROR_MODULE_BUILD_FAILURE, PI_ERROR_BUILD_PROGRAM_FAILURE}, + {UR_RESULT_ERROR_PROGRAM_BUILD_FAILURE, PI_ERROR_BUILD_PROGRAM_FAILURE}, {UR_RESULT_ERROR_OUT_OF_DEVICE_MEMORY, PI_ERROR_OUT_OF_RESOURCES}, {UR_RESULT_ERROR_OUT_OF_HOST_MEMORY, PI_ERROR_OUT_OF_HOST_MEMORY}}; @@ -252,6 +252,7 @@ namespace pi2ur { inline pi_result piPlatformsGet(pi_uint32 num_entries, pi_platform *platforms, pi_uint32 *num_platforms) { + urInit(0); uint32_t Count = num_entries; auto phPlatforms = reinterpret_cast(platforms); HANDLE_ERRORS(urPlatformGet(Count, phPlatforms, num_platforms)); diff --git a/sycl/plugins/unified_runtime/ur/adapters/level_zero/ur_level_zero.cpp b/sycl/plugins/unified_runtime/ur/adapters/level_zero/ur_level_zero.cpp index c859c166c7735..44e6a632444ee 100644 --- a/sycl/plugins/unified_runtime/ur/adapters/level_zero/ur_level_zero.cpp +++ b/sycl/plugins/unified_runtime/ur/adapters/level_zero/ur_level_zero.cpp @@ -286,7 +286,7 @@ ur_result_t _ur_platform_handle_t::initialize() { return UR_RESULT_SUCCESS; } -UR_APIEXPORT ur_result_t UR_APICALL urPlatformGet( +ur_result_t urPlatformGet( uint32_t NumEntries, ///< [in] the number of platforms to be added to ///< phPlatforms. If phPlatforms is not NULL, then ///< NumEntries should be greater than zero, otherwise @@ -403,7 +403,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGet( return UR_RESULT_SUCCESS; } -UR_APIEXPORT ur_result_t UR_APICALL urPlatformGetInfo( +ur_result_t urPlatformGetInfo( ur_platform_handle_t Platform, ///< [in] handle of the platform ur_platform_info_t ParamName, ///< [in] type of the info to retrieve size_t Size, ///< [in] the number of bytes pointed to by pPlatformInfo. @@ -457,7 +457,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGetInfo( return UR_RESULT_SUCCESS; } -UR_APIEXPORT ur_result_t UR_APICALL urDeviceGet( +ur_result_t urDeviceGet( ur_platform_handle_t Platform, ///< [in] handle of the platform instance ur_device_type_t DeviceType, ///< [in] the type of the devices. uint32_t NumEntries, ///< [in] the number of devices to be added to @@ -534,7 +534,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGet( return UR_RESULT_SUCCESS; } -UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo( +ur_result_t urDeviceGetInfo( ur_device_handle_t Device, ///< [in] handle of the device instance ur_device_info_t ParamName, ///< [in] type of the info to retrieve size_t propSize, ///< [in] the number of bytes pointed to by pDeviceInfo. @@ -1660,7 +1660,7 @@ void ZeUSMImportExtension::doZeUSMRelease(ze_driver_handle_t DriverHandle, ZE_CALL_NOCHECK(zexDriverReleaseImportedPointer, (DriverHandle, HostPtr)); } -UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition( +ur_result_t urDevicePartition( ur_device_handle_t Device, ///< [in] handle of the device to partition. const ur_device_partition_property_t *Properties, ///< [in] null-terminated array of <$_device_partition_t @@ -1741,3 +1741,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition( } return UR_RESULT_SUCCESS; } + +ur_result_t urInit(ur_device_init_flags_t device_flags) { + return UR_RESULT_SUCCESS; +} + +ur_result_t urTearDown(void *pParams) { return UR_RESULT_SUCCESS; } diff --git a/sycl/plugins/unified_runtime/ur/adapters/level_zero/ur_level_zero_common.cpp b/sycl/plugins/unified_runtime/ur/adapters/level_zero/ur_level_zero_common.cpp index f108c1de10784..088369e1cb43d 100644 --- a/sycl/plugins/unified_runtime/ur/adapters/level_zero/ur_level_zero_common.cpp +++ b/sycl/plugins/unified_runtime/ur/adapters/level_zero/ur_level_zero_common.cpp @@ -33,7 +33,7 @@ std::unordered_map Ze2UrErrorMapping = { {ZE_RESULT_ERROR_INVALID_GROUP_SIZE_DIMENSION, UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE}, {ZE_RESULT_ERROR_MODULE_BUILD_FAILURE, - UR_RESULT_ERROR_MODULE_BUILD_FAILURE}, + UR_RESULT_ERROR_PROGRAM_BUILD_FAILURE}, {ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY, UR_RESULT_ERROR_OUT_OF_DEVICE_MEMORY}, {ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY, UR_RESULT_ERROR_OUT_OF_HOST_MEMORY}}; diff --git a/sycl/plugins/unified_runtime/ur/adapters/level_zero/ur_loader_interface.cpp b/sycl/plugins/unified_runtime/ur/adapters/level_zero/ur_loader_interface.cpp new file mode 100644 index 0000000000000..cc6e727a43df1 --- /dev/null +++ b/sycl/plugins/unified_runtime/ur/adapters/level_zero/ur_loader_interface.cpp @@ -0,0 +1,286 @@ +//===--------- ur_loader_interface.cpp - Level Zero Adapter----------===// +// +// 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 +#include + +ur_result_t validateProcInputs(ur_api_version_t version, void *pDdiTable) { + if (nullptr == pDdiTable) { + return UR_RESULT_ERROR_INVALID_NULL_POINTER; + } + // Pre 1.0 we enforce loader and adapter must have same version. + // Post 1.0 only major version match should be required. + if (version != UR_API_VERSION_CURRENT) { + return UR_RESULT_ERROR_UNSUPPORTED_VERSION; + } + return UR_RESULT_SUCCESS; +} + +UR_DLLEXPORT ur_result_t UR_APICALL urGetGlobalProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_global_dditable_t + *pDdiTable ///< [in,out] pointer to table of DDI function pointers +) { + auto retVal = validateProcInputs(version, pDdiTable); + if (UR_RESULT_SUCCESS != retVal) { + return retVal; + } + + pDdiTable->pfnInit = urInit; + pDdiTable->pfnGetLastResult = nullptr; + pDdiTable->pfnTearDown = urTearDown; + + return retVal; +} + +UR_DLLEXPORT ur_result_t UR_APICALL urGetContextProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_context_dditable_t + *pDdiTable ///< [in,out] pointer to table of DDI function pointers +) { + auto retVal = validateProcInputs(version, pDdiTable); + if (UR_RESULT_SUCCESS != retVal) { + return retVal; + } + + pDdiTable->pfnCreate = nullptr; + pDdiTable->pfnRetain = nullptr; + pDdiTable->pfnRelease = nullptr; + pDdiTable->pfnGetInfo = nullptr; + pDdiTable->pfnGetNativeHandle = nullptr; + pDdiTable->pfnCreateWithNativeHandle = nullptr; + pDdiTable->pfnSetExtendedDeleter = nullptr; + + return retVal; +} + +UR_DLLEXPORT ur_result_t UR_APICALL urGetEnqueueProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_enqueue_dditable_t + *pDdiTable ///< [in,out] pointer to table of DDI function pointers +) { + auto retVal = validateProcInputs(version, pDdiTable); + if (UR_RESULT_SUCCESS != retVal) { + return retVal; + } + + pDdiTable->pfnKernelLaunch = nullptr; + pDdiTable->pfnEventsWait = nullptr; + pDdiTable->pfnEventsWaitWithBarrier = nullptr; + pDdiTable->pfnMemBufferRead = nullptr; + pDdiTable->pfnMemBufferWrite = nullptr; + pDdiTable->pfnMemBufferReadRect = nullptr; + pDdiTable->pfnMemBufferWriteRect = nullptr; + pDdiTable->pfnMemBufferCopy = nullptr; + pDdiTable->pfnMemBufferCopyRect = nullptr; + pDdiTable->pfnMemBufferFill = nullptr; + pDdiTable->pfnMemImageRead = nullptr; + pDdiTable->pfnMemImageWrite = nullptr; + pDdiTable->pfnMemImageCopy = nullptr; + pDdiTable->pfnMemBufferMap = nullptr; + pDdiTable->pfnMemUnmap = nullptr; + pDdiTable->pfnUSMMemcpy = nullptr; + pDdiTable->pfnUSMPrefetch = nullptr; + pDdiTable->pfnUSMMemAdvise = nullptr; + pDdiTable->pfnUSMFill2D = nullptr; + pDdiTable->pfnUSMMemcpy2D = nullptr; + pDdiTable->pfnDeviceGlobalVariableWrite = nullptr; + pDdiTable->pfnDeviceGlobalVariableRead = nullptr; + + return retVal; +} + +UR_DLLEXPORT ur_result_t UR_APICALL urGetEventProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_event_dditable_t + *pDdiTable ///< [in,out] pointer to table of DDI function pointers +) { + auto retVal = validateProcInputs(version, pDdiTable); + if (UR_RESULT_SUCCESS != retVal) { + return retVal; + } + pDdiTable->pfnGetInfo = nullptr; + pDdiTable->pfnGetProfilingInfo = nullptr; + pDdiTable->pfnWait = nullptr; + pDdiTable->pfnRetain = nullptr; + pDdiTable->pfnRelease = nullptr; + pDdiTable->pfnGetNativeHandle = nullptr; + pDdiTable->pfnCreateWithNativeHandle = nullptr; + pDdiTable->pfnSetCallback = nullptr; + + return retVal; +} + +UR_DLLEXPORT ur_result_t UR_APICALL urGetKernelProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_kernel_dditable_t + *pDdiTable ///< [in,out] pointer to table of DDI function pointers +) { + auto retVal = validateProcInputs(version, pDdiTable); + if (UR_RESULT_SUCCESS != retVal) { + return retVal; + } + pDdiTable->pfnCreate = nullptr; + pDdiTable->pfnGetInfo = nullptr; + pDdiTable->pfnGetGroupInfo = nullptr; + pDdiTable->pfnGetSubGroupInfo = nullptr; + pDdiTable->pfnRetain = nullptr; + pDdiTable->pfnRelease = nullptr; + pDdiTable->pfnGetNativeHandle = nullptr; + pDdiTable->pfnCreateWithNativeHandle = nullptr; + pDdiTable->pfnSetArgValue = nullptr; + pDdiTable->pfnSetArgLocal = nullptr; + pDdiTable->pfnSetArgPointer = nullptr; + pDdiTable->pfnSetExecInfo = nullptr; + pDdiTable->pfnSetArgSampler = nullptr; + pDdiTable->pfnSetArgMemObj = nullptr; + return retVal; +} + +UR_DLLEXPORT ur_result_t UR_APICALL urGetMemProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_mem_dditable_t + *pDdiTable ///< [in,out] pointer to table of DDI function pointers +) { + auto retVal = validateProcInputs(version, pDdiTable); + if (UR_RESULT_SUCCESS != retVal) { + return retVal; + } + pDdiTable->pfnImageCreate = nullptr; + pDdiTable->pfnBufferCreate = nullptr; + pDdiTable->pfnRetain = nullptr; + pDdiTable->pfnRelease = nullptr; + pDdiTable->pfnBufferPartition = nullptr; + pDdiTable->pfnGetNativeHandle = nullptr; + pDdiTable->pfnCreateWithNativeHandle = nullptr; + pDdiTable->pfnGetInfo = nullptr; + pDdiTable->pfnImageGetInfo = nullptr; + + return retVal; +} + +UR_DLLEXPORT ur_result_t UR_APICALL urGetPlatformProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_platform_dditable_t + *pDdiTable ///< [in,out] pointer to table of DDI function pointers +) { + auto retVal = validateProcInputs(version, pDdiTable); + if (UR_RESULT_SUCCESS != retVal) { + return retVal; + } + pDdiTable->pfnGet = urPlatformGet; + pDdiTable->pfnGetInfo = urPlatformGetInfo; + pDdiTable->pfnGetNativeHandle = nullptr; + pDdiTable->pfnCreateWithNativeHandle = nullptr; + pDdiTable->pfnGetApiVersion = nullptr; + + return retVal; +} + +UR_DLLEXPORT ur_result_t UR_APICALL urGetProgramProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_program_dditable_t + *pDdiTable ///< [in,out] pointer to table of DDI function pointers +) { + + auto retVal = validateProcInputs(version, pDdiTable); + if (UR_RESULT_SUCCESS != retVal) { + return retVal; + } + pDdiTable->pfnCreateWithBinary = nullptr; + pDdiTable->pfnRetain = nullptr; + pDdiTable->pfnRelease = nullptr; + pDdiTable->pfnGetFunctionPointer = nullptr; + pDdiTable->pfnGetInfo = nullptr; + pDdiTable->pfnGetBuildInfo = nullptr; + pDdiTable->pfnGetNativeHandle = nullptr; + pDdiTable->pfnCreateWithNativeHandle = nullptr; + + return retVal; +} + +UR_DLLEXPORT ur_result_t UR_APICALL urGetQueueProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_queue_dditable_t + *pDdiTable ///< [in,out] pointer to table of DDI function pointers +) { + auto retVal = validateProcInputs(version, pDdiTable); + if (UR_RESULT_SUCCESS != retVal) { + return retVal; + } + + pDdiTable->pfnGetInfo = nullptr; + pDdiTable->pfnCreate = nullptr; + pDdiTable->pfnRetain = nullptr; + pDdiTable->pfnRelease = nullptr; + pDdiTable->pfnGetNativeHandle = nullptr; + pDdiTable->pfnCreateWithNativeHandle = nullptr; + pDdiTable->pfnFinish = nullptr; + pDdiTable->pfnFlush = nullptr; + + return retVal; +} + +UR_DLLEXPORT ur_result_t UR_APICALL urGetSamplerProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_sampler_dditable_t + *pDdiTable ///< [in,out] pointer to table of DDI function pointers +) { + auto retVal = validateProcInputs(version, pDdiTable); + if (UR_RESULT_SUCCESS != retVal) { + return retVal; + } + pDdiTable->pfnCreate = nullptr; + pDdiTable->pfnRetain = nullptr; + pDdiTable->pfnRelease = nullptr; + pDdiTable->pfnGetInfo = nullptr; + pDdiTable->pfnGetNativeHandle = nullptr; + pDdiTable->pfnCreateWithNativeHandle = nullptr; + + return retVal; +} + +UR_DLLEXPORT ur_result_t UR_APICALL urGetUSMProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_usm_dditable_t + *pDdiTable ///< [in,out] pointer to table of DDI function pointers +) { + auto retVal = validateProcInputs(version, pDdiTable); + if (UR_RESULT_SUCCESS != retVal) { + return retVal; + } + pDdiTable->pfnHostAlloc = nullptr; + pDdiTable->pfnDeviceAlloc = nullptr; + pDdiTable->pfnSharedAlloc = nullptr; + pDdiTable->pfnFree = nullptr; + pDdiTable->pfnGetMemAllocInfo = nullptr; + + return retVal; +} + +UR_DLLEXPORT ur_result_t UR_APICALL urGetDeviceProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_device_dditable_t + *pDdiTable ///< [in,out] pointer to table of DDI function pointers +) { + auto retVal = validateProcInputs(version, pDdiTable); + if (UR_RESULT_SUCCESS != retVal) { + return retVal; + } + pDdiTable->pfnGet = urDeviceGet; + pDdiTable->pfnGetInfo = urDeviceGetInfo; + pDdiTable->pfnRetain = urDeviceRetain; + pDdiTable->pfnRelease = urDeviceRelease; + pDdiTable->pfnPartition = urDevicePartition; + pDdiTable->pfnSelectBinary = nullptr; + pDdiTable->pfnGetNativeHandle = nullptr; + pDdiTable->pfnCreateWithNativeHandle = nullptr; + pDdiTable->pfnGetGlobalTimestamps = nullptr; + + return retVal; +}