Skip to content

Commit 998fd91

Browse files
authored
[SYCL][HIP] Add interop header and devcie specialization (#7145)
Fixes #6635 for HIP. Note that it does mean the HIP extension header must be included in user applications in order to get the correct specialization, as is done in the test: ```cpp #include <sycl/ext/oneapi/backend/hip.hpp> ```
1 parent d95e353 commit 998fd91

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//==--------- hip.hpp - SYCL HIP backend -----------------------------------==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#pragma once
10+
11+
#include <sycl/backend.hpp>
12+
13+
namespace sycl {
14+
__SYCL_INLINE_VER_NAMESPACE(_V1) {
15+
16+
template <>
17+
inline backend_return_t<backend::ext_oneapi_hip, device>
18+
get_native<backend::ext_oneapi_hip, device>(const device &Obj) {
19+
// TODO use SYCL 2020 exception when implemented
20+
if (Obj.get_backend() != backend::ext_oneapi_hip) {
21+
throw sycl::runtime_error(errc::backend_mismatch, "Backends mismatch",
22+
PI_ERROR_INVALID_OPERATION);
23+
}
24+
// HIP uses a 32-bit int instead of an opaque pointer like other backends,
25+
// so we need a specialization with static_cast instead of reinterpret_cast.
26+
return static_cast<backend_return_t<backend::ext_oneapi_hip, device>>(
27+
Obj.getNative());
28+
}
29+
30+
} // __SYCL_INLINE_VER_NAMESPACE(_V1)
31+
} // namespace sycl

sycl/test/basic_tests/interop-hip.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// REQUIRES: hip_be
2+
// RUN: %clangxx %fsycl-host-only -fsyntax-only -Xclang -verify -Xclang -verify-ignore-unexpected=note %s -o %t.out
3+
// RUN: %clangxx %fsycl-host-only -fsyntax-only -Xclang -verify -Xclang -verify-ignore-unexpected=note -D__SYCL_INTERNAL_API %s -o %t.out
4+
// expected-no-diagnostics
5+
6+
// Test for HIP interop API
7+
8+
#include <sycl/sycl.hpp>
9+
#include <sycl/ext/oneapi/backend/hip.hpp>
10+
11+
using namespace sycl;
12+
13+
//
14+
// 4.5.1 SYCL application interoperability may be provided for
15+
// platform,
16+
// device,
17+
// context,
18+
// queue,
19+
// event,
20+
// buffer,
21+
// device_image,
22+
// sampled_image,
23+
// unsampled_image.
24+
25+
int main() {
26+
27+
// Create SYCL objects
28+
device Device;
29+
context Context(Device);
30+
queue Queue(Device);
31+
event Event;
32+
33+
// 4.5.1.1 For each SYCL runtime class T which supports SYCL application
34+
// interoperability with the SYCL backend, a specialization of return_type
35+
// must be defined as the type of SYCL application interoperability native
36+
// backend object associated with T for the SYCL backend, specified in the
37+
// SYCL backend specification.
38+
//
39+
// return_type is used when retrieving the backend specific native object from
40+
// a SYCL object. See the relevant backend specification for details.
41+
42+
backend_traits<backend::ext_oneapi_hip>::return_type<device> hip_device;
43+
backend_traits<backend::ext_oneapi_hip>::return_type<context> hip_context;
44+
backend_traits<backend::ext_oneapi_hip>::return_type<event> hip_event;
45+
backend_traits<backend::ext_oneapi_hip>::return_type<queue> hip_queue;
46+
47+
// 4.5.1.2 For each SYCL runtime class T which supports SYCL application
48+
// interoperability, a specialization of get_native must be defined, which
49+
// takes an instance of T and returns a SYCL application interoperability
50+
// native backend object associated with syclObject which can be used for SYCL
51+
// application interoperability. The lifetime of the object returned are
52+
// backend-defined and specified in the backend specification.
53+
54+
hip_device = get_native<backend::ext_oneapi_hip>(Device);
55+
hip_context = get_native<backend::ext_oneapi_hip>(Context);
56+
hip_event = get_native<backend::ext_oneapi_hip>(Event);
57+
hip_queue = get_native<backend::ext_oneapi_hip>(Queue);
58+
59+
return 0;
60+
}

0 commit comments

Comments
 (0)