From cbe6dbfbaa425636c834ae1839450f790685fc7f Mon Sep 17 00:00:00 2001 From: pbalcer Date: Wed, 17 Jan 2024 09:33:55 +0100 Subject: [PATCH] [UR] remove duplicate ur files The ur.hpp and ur.cpp files were moved to the unified runtime repo. --- sycl/plugins/unified_runtime/CMakeLists.txt | 5 + sycl/plugins/unified_runtime/ur/ur.cpp | 28 -- sycl/plugins/unified_runtime/ur/ur.hpp | 298 -------------------- 3 files changed, 5 insertions(+), 326 deletions(-) delete mode 100644 sycl/plugins/unified_runtime/ur/ur.cpp delete mode 100644 sycl/plugins/unified_runtime/ur/ur.hpp diff --git a/sycl/plugins/unified_runtime/CMakeLists.txt b/sycl/plugins/unified_runtime/CMakeLists.txt index 1572bb8ff3e4e..46c10df8e2fa8 100644 --- a/sycl/plugins/unified_runtime/CMakeLists.txt +++ b/sycl/plugins/unified_runtime/CMakeLists.txt @@ -120,6 +120,8 @@ message(STATUS "Using Unified Runtime source directory: ${UNIFIED_RUNTIME_SOURCE_DIR}") set(UNIFIED_RUNTIME_INCLUDE_DIR "${UNIFIED_RUNTIME_SOURCE_DIR}/include") +set(UNIFIED_RUNTIME_SRC_INCLUDE_DIR "${UNIFIED_RUNTIME_SOURCE_DIR}/source") +set(UNIFIED_RUNTIME_COMMON_INCLUDE_DIR "${UNIFIED_RUNTIME_SOURCE_DIR}/source/common") add_library(UnifiedRuntimeLoader ALIAS ur_loader) add_library(UnifiedRuntimeCommon ALIAS ur_common) @@ -149,6 +151,9 @@ set(UNIFIED_RUNTIME_PLUGIN_ARGS Threads::Threads UnifiedRuntimeLoader UnifiedRuntime-Headers + INCLUDE_DIRS + "${UNIFIED_RUNTIME_SRC_INCLUDE_DIR}" + "${UNIFIED_RUNTIME_COMMON_INCLUDE_DIR}" ) # We need for #include in common.h diff --git a/sycl/plugins/unified_runtime/ur/ur.cpp b/sycl/plugins/unified_runtime/ur/ur.cpp deleted file mode 100644 index 01dacd4eb6d47..0000000000000 --- a/sycl/plugins/unified_runtime/ur/ur.cpp +++ /dev/null @@ -1,28 +0,0 @@ - -//===--------- ur.cpp - Unified Runtime ----------------------------------===// -// -// 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 "ur.hpp" -#include - -// Controls tracing UR calls from within the UR itself. -bool PrintTrace = [] { - const char *PiRet = std::getenv("SYCL_PI_TRACE"); - const char *Trace = PiRet ? PiRet : nullptr; - const int TraceValue = Trace ? std::stoi(Trace) : 0; - if (TraceValue == -1 || TraceValue == 2) { // Means print all traces - return true; - } - return false; -}(); - -// Apparatus for maintaining immutable cache of platforms. -std::vector *URPlatformsCache = - new std::vector; -SpinLock *URPlatformsCacheMutex = new SpinLock; -bool URPlatformCachePopulated = false; diff --git a/sycl/plugins/unified_runtime/ur/ur.hpp b/sycl/plugins/unified_runtime/ur/ur.hpp deleted file mode 100644 index 7dbf33a1dc6d3..0000000000000 --- a/sycl/plugins/unified_runtime/ur/ur.hpp +++ /dev/null @@ -1,298 +0,0 @@ -//===--------- ur.hpp - Unified Runtime ----------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -template To ur_cast(From Value) { - // TODO: see if more sanity checks are possible. - assert(sizeof(From) == sizeof(To)); - return (To)(Value); -} - -template <> uint32_t inline ur_cast(uint64_t Value) { - // Cast value and check that we don't lose any information. - uint32_t CastedValue = (uint32_t)(Value); - assert((uint64_t)CastedValue == Value); - return CastedValue; -} - -// TODO: promote all of the below extensions to the Unified Runtime -// and get rid of these ZER_EXT constants. -const ur_device_info_t UR_EXT_DEVICE_INFO_OPENCL_C_VERSION = - (ur_device_info_t)0x103D; - -const ur_command_t UR_EXT_COMMAND_TYPE_USER = - (ur_command_t)((uint32_t)UR_COMMAND_FORCE_UINT32 - 1); - -/// Program metadata tags recognized by the UR adapters. For kernels the tag -/// must appear after the kernel name. -#define __SYCL_UR_PROGRAM_METADATA_TAG_REQD_WORK_GROUP_SIZE \ - "@reqd_work_group_size" -#define __SYCL_UR_PROGRAM_METADATA_GLOBAL_ID_MAPPING "@global_id_mapping" -#define __SYCL_UR_PROGRAM_METADATA_TAG_NEED_FINALIZATION "Requires finalization" - -// Terminates the process with a catastrophic error message. -[[noreturn]] inline void die(const char *Message) { - std::cerr << "die: " << Message << std::endl; - std::terminate(); -} - -// A single-threaded app has an opportunity to enable this mode to avoid -// overhead from mutex locking. Default value is 0 which means that single -// thread mode is disabled. -static const bool SingleThreadMode = [] { - const char *UrRet = std::getenv("UR_L0_SINGLE_THREAD_MODE"); - const char *PiRet = std::getenv("SYCL_PI_LEVEL_ZERO_SINGLE_THREAD_MODE"); - const bool RetVal = UrRet ? std::stoi(UrRet) : (PiRet ? std::stoi(PiRet) : 0); - return RetVal; -}(); - -// Class which acts like shared_mutex if SingleThreadMode variable is not set. -// If SingleThreadMode variable is set then mutex operations are turned into -// nop. -class ur_shared_mutex { - std::shared_mutex Mutex; - -public: - void lock() { - if (!SingleThreadMode) - Mutex.lock(); - } - bool try_lock() { return SingleThreadMode ? true : Mutex.try_lock(); } - void unlock() { - if (!SingleThreadMode) - Mutex.unlock(); - } - - void lock_shared() { - if (!SingleThreadMode) - Mutex.lock_shared(); - } - bool try_lock_shared() { - return SingleThreadMode ? true : Mutex.try_lock_shared(); - } - void unlock_shared() { - if (!SingleThreadMode) - Mutex.unlock_shared(); - } -}; - -// Class which acts like std::mutex if SingleThreadMode variable is not set. -// If SingleThreadMode variable is set then mutex operations are turned into -// nop. -class ur_mutex { - std::mutex Mutex; - friend class ur_lock; - -public: - void lock() { - if (!SingleThreadMode) - Mutex.lock(); - } - bool try_lock() { return SingleThreadMode ? true : Mutex.try_lock(); } - void unlock() { - if (!SingleThreadMode) - Mutex.unlock(); - } -}; - -class ur_lock { - std::unique_lock Lock; - -public: - explicit ur_lock(ur_mutex &Mutex) { - if (!SingleThreadMode) { - Lock = std::unique_lock(Mutex.Mutex); - } - } -}; - -/// SpinLock is a synchronization primitive, that uses atomic variable and -/// causes thread trying acquire lock wait in loop while repeatedly check if -/// the lock is available. -/// -/// One important feature of this implementation is that std::atomic can -/// be zero-initialized. This allows SpinLock to have trivial constructor and -/// destructor, which makes it possible to use it in global context (unlike -/// std::mutex, that doesn't provide such guarantees). -class SpinLock { -public: - void lock() { - while (MLock.test_and_set(std::memory_order_acquire)) - std::this_thread::yield(); - } - void unlock() { MLock.clear(std::memory_order_release); } - -private: - std::atomic_flag MLock = ATOMIC_FLAG_INIT; -}; - -// The wrapper for immutable data. -// The data is initialized only once at first access (via ->) with the -// initialization function provided in Init. All subsequent access to -// the data just returns the already stored data. -// -template struct ZeCache : private T { - // The initialization function takes a reference to the data - // it is going to initialize, since it is private here in - // order to disallow access other than through "->". - // - using InitFunctionType = std::function; - InitFunctionType Compute{nullptr}; - std::once_flag Computed; - - ZeCache() : T{} {} - - // Access to the fields of the original T data structure. - T *operator->() { - std::call_once(Computed, Compute, static_cast(*this)); - return this; - } -}; - -// Helper for one-liner validation -#define UR_ASSERT(condition, error) \ - if (!(condition)) \ - return error; - -// TODO: populate with target agnostic handling of UR platforms -struct _ur_platform {}; - -// Controls tracing UR calls from within the UR itself. -extern bool PrintTrace; - -// Apparatus for maintaining immutable cache of platforms. -// -// Note we only create a simple pointer variables such that C++ RT won't -// deallocate them automatically at the end of the main program. -// The heap memory allocated for these global variables reclaimed only at -// explicit tear-down. -extern std::vector *URPlatformsCache; -extern SpinLock *URPlatformsCacheMutex; -extern bool URPlatformCachePopulated; - -// The getInfo*/ReturnHelper facilities provide shortcut way of -// writing return bytes for the various getInfo APIs. -namespace ur { -template -ur_result_t getInfoImpl(size_t param_value_size, void *param_value, - size_t *param_value_size_ret, T value, - size_t value_size, Assign &&assign_func) { - if (!param_value && !param_value_size_ret) { - return UR_RESULT_ERROR_INVALID_NULL_POINTER; - } - - if (param_value != nullptr) { - - if (param_value_size < value_size) { - return UR_RESULT_ERROR_INVALID_SIZE; - } - - assign_func(param_value, value, value_size); - } - - if (param_value_size_ret != nullptr) { - *param_value_size_ret = value_size; - } - - return UR_RESULT_SUCCESS; -} - -template -ur_result_t getInfo(size_t param_value_size, void *param_value, - size_t *param_value_size_ret, T value) { - - auto assignment = [](void *param_value, T value, size_t value_size) { - std::ignore = value_size; - *static_cast(param_value) = value; - }; - - return getInfoImpl(param_value_size, param_value, param_value_size_ret, value, - sizeof(T), assignment); -} - -template -ur_result_t getInfoArray(size_t array_length, size_t param_value_size, - void *param_value, size_t *param_value_size_ret, - const T *value) { - return getInfoImpl(param_value_size, param_value, param_value_size_ret, value, - array_length * sizeof(T), memcpy); -} - -template -ur_result_t getInfoArray(size_t array_length, size_t param_value_size, - void *param_value, size_t *param_value_size_ret, - const T *value) { - if (param_value) { - memset(param_value, 0, param_value_size); - for (uint32_t I = 0; I < array_length; I++) - ((RetType *)param_value)[I] = (RetType)value[I]; - } - if (param_value_size_ret) - *param_value_size_ret = array_length * sizeof(RetType); - return UR_RESULT_SUCCESS; -} - -template <> -inline ur_result_t -getInfo(size_t param_value_size, void *param_value, - size_t *param_value_size_ret, const char *value) { - return getInfoArray(strlen(value) + 1, param_value_size, param_value, - param_value_size_ret, value); -} -} // namespace ur - -class UrReturnHelper { -public: - UrReturnHelper(size_t param_value_size, void *param_value, - size_t *param_value_size_ret) - : param_value_size(param_value_size), param_value(param_value), - param_value_size_ret(param_value_size_ret) {} - - // A version where in/out info size is represented by a single pointer - // to a value which is updated on return - UrReturnHelper(size_t *param_value_size, void *param_value) - : param_value_size(*param_value_size), param_value(param_value), - param_value_size_ret(param_value_size) {} - - // Scalar return value - template ur_result_t operator()(const T &t) { - return ur::getInfo(param_value_size, param_value, param_value_size_ret, t); - } - - // Array return value - template ur_result_t operator()(const T *t, size_t s) { - return ur::getInfoArray(s, param_value_size, param_value, - param_value_size_ret, t); - } - - // Array return value where element type is different from T - template - ur_result_t operator()(const T *t, size_t s) { - return ur::getInfoArray(s, param_value_size, param_value, - param_value_size_ret, t); - } - -protected: - size_t param_value_size; - void *param_value; - size_t *param_value_size_ret; -};