Callum Fare 992b00020f
Reland #2 - [Offload] Introduce offload-tblgen and initial new API implementation (#108413. #117704) (#117894)
Relands #117704, which relanded changes from #108413 - this was reverted
due to build issues. The new offload library did not build with
`LIBOMPTARGET_OMPT_SUPPORT` enabled, which was not picked up by
pre-merge testing.

The last commit contains the fix; everything else is otherwise identical
to the approved PR.
___

### 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-11-28 10:19:37 +00:00

97 lines
3.0 KiB
C++

//===------- Offload API tests - gtest environment ------------------------===//
//
// 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 "Environment.hpp"
#include "Fixtures.hpp"
#include "llvm/Support/CommandLine.h"
#include <OffloadAPI.h>
using namespace llvm;
// Wrapper so we don't have to constantly init and shutdown Offload in every
// test, while having sensible lifetime for the platform environment
struct OffloadInitWrapper {
OffloadInitWrapper() { olInit(); }
~OffloadInitWrapper() { olShutDown(); }
};
static OffloadInitWrapper Wrapper{};
static cl::opt<std::string>
SelectedPlatform("platform", cl::desc("Only test the specified platform"),
cl::value_desc("platform"));
std::ostream &operator<<(std::ostream &Out,
const ol_platform_handle_t &Platform) {
size_t Size;
olGetPlatformInfoSize(Platform, OL_PLATFORM_INFO_NAME, &Size);
std::vector<char> Name(Size);
olGetPlatformInfo(Platform, OL_PLATFORM_INFO_NAME, Size, Name.data());
Out << Name.data();
return Out;
}
std::ostream &operator<<(std::ostream &Out,
const std::vector<ol_platform_handle_t> &Platforms) {
for (auto Platform : Platforms) {
Out << "\n * \"" << Platform << "\"";
}
return Out;
}
const std::vector<ol_platform_handle_t> &TestEnvironment::getPlatforms() {
static std::vector<ol_platform_handle_t> Platforms{};
if (Platforms.empty()) {
uint32_t PlatformCount = 0;
olGetPlatformCount(&PlatformCount);
if (PlatformCount > 0) {
Platforms.resize(PlatformCount);
olGetPlatform(PlatformCount, Platforms.data());
}
}
return Platforms;
}
// Get a single platform, which may be selected by the user.
ol_platform_handle_t TestEnvironment::getPlatform() {
static ol_platform_handle_t Platform = nullptr;
const auto &Platforms = getPlatforms();
if (!Platform) {
if (SelectedPlatform != "") {
for (const auto CandidatePlatform : Platforms) {
std::stringstream PlatformName;
PlatformName << CandidatePlatform;
if (SelectedPlatform == PlatformName.str()) {
Platform = CandidatePlatform;
return Platform;
}
}
std::cout << "No platform found with the name \"" << SelectedPlatform
<< "\". Choose from:" << Platforms << "\n";
std::exit(1);
} else {
// Pick a single platform. We prefer one that has available devices, but
// just pick the first initially in case none have any devices.
Platform = Platforms[0];
for (auto CandidatePlatform : Platforms) {
uint32_t NumDevices = 0;
if (olGetDeviceCount(CandidatePlatform, &NumDevices) == OL_SUCCESS) {
if (NumDevices > 0) {
Platform = CandidatePlatform;
break;
}
}
}
}
}
return Platform;
}