Files
clang-p2996/offload/liboffload/src/Helpers.hpp
Callum Fare fd3907ccb5 Reland #118503: [Offload] Introduce offload-tblgen and initial new API implementation (#118614)
Reland #118503. Added a fix for builds with `-DBUILD_SHARED_LIBS=ON`
(see last commit). Otherwise the changes are identical.

---


### New API

Previous discussions at the LLVM/Offload meeting have brought up the
need for a new API for exposing the functionality of the plugins. This
change introduces a very small subset of a new API, which is primarily
for testing the offload tooling and demonstrating how a new API can fit
into the existing code base without being too disruptive. Exact designs
for these entry points and future additions can be worked out over time.

The new API does however introduce the bare minimum functionality to
implement device discovery for Unified Runtime and SYCL. This means that
the `urinfo` and `sycl-ls` tools can be used on top of Offload. A
(rough) implementation of a Unified Runtime adapter (aka plugin) for
Offload is available
[here](https://github.com/callumfare/unified-runtime/tree/offload_adapter).
Our intention is to maintain this and use it to implement and test
Offload API changes with SYCL.

### Demoing the new API

```sh
# From the runtime build directory
$ ninja LibomptUnitTests
$ OFFLOAD_TRACE=1 ./offload/unittests/OffloadAPI/offload.unittests 
```


### Open questions and future work
* Only some of the available device info is exposed, and not all the
possible device queries needed for SYCL are implemented by the plugins.
A sensible next step would be to refactor and extend the existing device
info queries in the plugins. The existing info queries are all strings,
but the new API introduces the ability to return any arbitrary type.
* It may be sensible at some point for the plugins to implement the new
API directly, and the higher level code on top of it could be made
generic, but this is more of a long-term possibility.
2024-12-05 09:34:04 +01:00

96 lines
3.2 KiB
C++

//===- helpers.hpp- GetInfo return helpers for the new LLVM/Offload API ---===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// The getInfo*/ReturnHelper facilities provide shortcut way of writing return
// data + size for the various getInfo APIs. Based on the equivalent
// implementations in Unified Runtime.
//
//===----------------------------------------------------------------------===//
#include "OffloadAPI.h"
#include <cstring>
template <typename T, typename Assign>
ol_errc_t getInfoImpl(size_t ParamValueSize, void *ParamValue,
size_t *ParamValueSizeRet, T Value, size_t ValueSize,
Assign &&AssignFunc) {
if (!ParamValue && !ParamValueSizeRet) {
return OL_ERRC_INVALID_NULL_POINTER;
}
if (ParamValue != nullptr) {
if (ParamValueSize < ValueSize) {
return OL_ERRC_INVALID_SIZE;
}
AssignFunc(ParamValue, Value, ValueSize);
}
if (ParamValueSizeRet != nullptr) {
*ParamValueSizeRet = ValueSize;
}
return OL_ERRC_SUCCESS;
}
template <typename T>
ol_errc_t getInfo(size_t ParamValueSize, void *ParamValue,
size_t *ParamValueSizeRet, T Value) {
auto Assignment = [](void *ParamValue, T Value, size_t) {
*static_cast<T *>(ParamValue) = Value;
};
return getInfoImpl(ParamValueSize, ParamValue, ParamValueSizeRet, Value,
sizeof(T), Assignment);
}
template <typename T>
ol_errc_t getInfoArray(size_t array_length, size_t ParamValueSize,
void *ParamValue, size_t *ParamValueSizeRet,
const T *Value) {
return getInfoImpl(ParamValueSize, ParamValue, ParamValueSizeRet, Value,
array_length * sizeof(T), memcpy);
}
template <>
inline ol_errc_t getInfo<const char *>(size_t ParamValueSize, void *ParamValue,
size_t *ParamValueSizeRet,
const char *Value) {
return getInfoArray(strlen(Value) + 1, ParamValueSize, ParamValue,
ParamValueSizeRet, Value);
}
class ReturnHelper {
public:
ReturnHelper(size_t ParamValueSize, void *ParamValue,
size_t *ParamValueSizeRet)
: ParamValueSize(ParamValueSize), ParamValue(ParamValue),
ParamValueSizeRet(ParamValueSizeRet) {}
// A version where in/out info size is represented by a single pointer
// to a value which is updated on return
ReturnHelper(size_t *ParamValueSize, void *ParamValue)
: ParamValueSize(*ParamValueSize), ParamValue(ParamValue),
ParamValueSizeRet(ParamValueSize) {}
// Scalar return Value
template <class T> ol_errc_t operator()(const T &t) {
return getInfo(ParamValueSize, ParamValue, ParamValueSizeRet, t);
}
// Array return Value
template <class T> ol_errc_t operator()(const T *t, size_t s) {
return getInfoArray(s, ParamValueSize, ParamValue, ParamValueSizeRet, t);
}
protected:
size_t ParamValueSize;
void *ParamValue;
size_t *ParamValueSizeRet;
};