[Mlir-commits] [mlir] [MLIR] Add SyclRuntimeWrapper (PR #69648)
Guray Ozen
llvmlistbot at llvm.org
Tue Oct 24 06:17:52 PDT 2023
================
@@ -0,0 +1,222 @@
+//===- SyclRuntimeWrappers.cpp - MLIR SYCL 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 C wrappers around the sycl runtime library.
+//
+//===----------------------------------------------------------------------===//
+
+#include <algorithm>
+#include <array>
+#include <atomic>
+#include <cassert>
+#include <cfloat>
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+#include <stdexcept>
+#include <tuple>
+#include <vector>
+
+#include <CL/sycl.hpp>
+#include <level_zero/ze_api.h>
+#include <map>
+#include <mutex>
+#include <sycl/ext/oneapi/backend/level_zero.hpp>
+
+#ifdef _WIN32
+#define SYCL_RUNTIME_EXPORT __declspec(dllexport)
+#else
+#define SYCL_RUNTIME_EXPORT
+#endif // _WIN32
+
+namespace {
+
+template <typename F>
+auto catchAll(F &&func) {
+ try {
+ return func();
+ } catch (const std::exception &e) {
+ fprintf(stdout, "An exception was thrown: %s\n", e.what());
+ fflush(stdout);
+ abort();
+ } catch (...) {
+ fprintf(stdout, "An unknown exception was thrown\n");
+ fflush(stdout);
+ abort();
+ }
+}
+
+#define L0_SAFE_CALL(call) \
+ { \
+ ze_result_t status = (call); \
+ if (status != ZE_RESULT_SUCCESS) { \
+ fprintf(stdout, "L0 error %d\n", status); \
+ fflush(stdout); \
+ abort(); \
+ } \
+ }
+
+} // namespace
+
+static sycl::device getDefaultDevice() {
+ static sycl::device syclDevice;
+ static bool isDeviceInitialised = false;
+ if(!isDeviceInitialised) {
+ auto platformList = sycl::platform::get_platforms();
+ for (const auto &platform : platformList) {
+ auto platformName = platform.get_info<sycl::info::platform::name>();
+ bool isLevelZero = platformName.find("Level-Zero") != std::string::npos;
+ if (!isLevelZero)
+ continue;
+
+ syclDevice = platform.get_devices()[0];
+ isDeviceInitialised = true;
+ return syclDevice;
+ }
+ throw std::runtime_error("getDefaultDevice failed");
----------------
grypp wrote:
In our other runtimes, we don't use 'throw' or 'std::runtime_error.' Instead, we use on 'fprintf.' (see [CUDA](https://github.com/llvm/llvm-project/blob/main/mlir/lib/ExecutionEngine/CudaRuntimeWrappers.cpp#L44) and [ROCm](https://github.com/llvm/llvm-project/blob/main/mlir/lib/ExecutionEngine/RocmRuntimeWrappers.cpp#L30)).
>From what I understand, throwing exceptions are heavy and requires a C++ runtime. Do you really want to use exceptions in this context?
Our approach keeps the error lightweight. In the event of an error, the runtime prints the error message, and the program proceeds to run. The compiler can implement fallback if needed (we don't do it right now).
https://github.com/llvm/llvm-project/pull/69648
More information about the Mlir-commits
mailing list