Skip to content

[SYCL] Implement sycl_ext_oneapi_address_cast #12382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/lib/Sema/SPIRVBuiltins.td
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,9 @@ foreach Ty = [Void, ConstType<Void>, VolatileType<Void>, VolatileType<ConstType<
def : SPVBuiltin<"GenericCastToPtrExplicit_ToGlobal", [PointerType<Ty, GlobalAS>, PointerType<Ty, DefaultAS>, Int], Attr.Const>;
def : SPVBuiltin<"GenericCastToPtrExplicit_ToLocal", [PointerType<Ty, LocalAS>, PointerType<Ty, DefaultAS>, Int], Attr.Const>;
def : SPVBuiltin<"GenericCastToPtrExplicit_ToPrivate", [PointerType<Ty, PrivateAS>, PointerType<Ty, DefaultAS>, Int], Attr.Const>;
def : SPVBuiltin<"GenericCastToPtr_ToGlobal", [PointerType<Ty, GlobalAS>, PointerType<Ty, DefaultAS>, Int], Attr.Const>;
def : SPVBuiltin<"GenericCastToPtr_ToLocal", [PointerType<Ty, LocalAS>, PointerType<Ty, DefaultAS>, Int], Attr.Const>;
def : SPVBuiltin<"GenericCastToPtr_ToPrivate", [PointerType<Ty, PrivateAS>, PointerType<Ty, DefaultAS>, Int], Attr.Const>;
}

foreach Type = TLFloat.List in {
Expand Down
88 changes: 88 additions & 0 deletions sycl/include/CL/__spirv/spirv_ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,94 @@ __SYCL_GenericCastToPtrExplicit_ToPrivate(const volatile void *Ptr) noexcept {
__spv::StorageClass::Function);
}

template <typename dataT>
extern __attribute__((opencl_global)) dataT *
__SYCL_GenericCastToPtr_ToGlobal(void *Ptr) noexcept {
return (__attribute__((opencl_global)) dataT *)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: no need for cast, the returned type should be already correctly typed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I just copied the existing definitions of __SYCL_GenericCastToPtrExplicit here.

I agree it makes sense to change this, but I'd prefer for it to be done as part of a follow-up PR. I don't really want to touch the existing definitions, in case something breaks unexpectedly.

__spirv_GenericCastToPtr_ToGlobal(Ptr,
__spv::StorageClass::CrossWorkgroup);
}

template <typename dataT>
extern const __attribute__((opencl_global)) dataT *
__SYCL_GenericCastToPtr_ToGlobal(const void *Ptr) noexcept {
return (const __attribute__((opencl_global)) dataT *)
__spirv_GenericCastToPtr_ToGlobal(Ptr,
__spv::StorageClass::CrossWorkgroup);
}

template <typename dataT>
extern volatile __attribute__((opencl_global)) dataT *
__SYCL_GenericCastToPtr_ToGlobal(volatile void *Ptr) noexcept {
return (volatile __attribute__((opencl_global)) dataT *)
__spirv_GenericCastToPtr_ToGlobal(Ptr,
__spv::StorageClass::CrossWorkgroup);
}

template <typename dataT>
extern const volatile __attribute__((opencl_global)) dataT *
__SYCL_GenericCastToPtr_ToGlobal(const volatile void *Ptr) noexcept {
return (const volatile __attribute__((opencl_global)) dataT *)
__spirv_GenericCastToPtr_ToGlobal(Ptr,
__spv::StorageClass::CrossWorkgroup);
}

template <typename dataT>
extern __attribute__((opencl_local)) dataT *
__SYCL_GenericCastToPtr_ToLocal(void *Ptr) noexcept {
return (__attribute__((opencl_local)) dataT *)
__spirv_GenericCastToPtr_ToLocal(Ptr, __spv::StorageClass::Workgroup);
}

template <typename dataT>
extern const __attribute__((opencl_local)) dataT *
__SYCL_GenericCastToPtr_ToLocal(const void *Ptr) noexcept {
return (const __attribute__((opencl_local)) dataT *)
__spirv_GenericCastToPtr_ToLocal(Ptr, __spv::StorageClass::Workgroup);
}

template <typename dataT>
extern volatile __attribute__((opencl_local)) dataT *
__SYCL_GenericCastToPtr_ToLocal(volatile void *Ptr) noexcept {
return (volatile __attribute__((opencl_local)) dataT *)
__spirv_GenericCastToPtr_ToLocal(Ptr, __spv::StorageClass::Workgroup);
}

template <typename dataT>
extern const volatile __attribute__((opencl_local)) dataT *
__SYCL_GenericCastToPtr_ToLocal(const volatile void *Ptr) noexcept {
return (const volatile __attribute__((opencl_local)) dataT *)
__spirv_GenericCastToPtr_ToLocal(Ptr, __spv::StorageClass::Workgroup);
}

template <typename dataT>
extern __attribute__((opencl_private)) dataT *
__SYCL_GenericCastToPtr_ToPrivate(void *Ptr) noexcept {
return (__attribute__((opencl_private)) dataT *)
__spirv_GenericCastToPtr_ToPrivate(Ptr, __spv::StorageClass::Function);
}

template <typename dataT>
extern const __attribute__((opencl_private)) dataT *
__SYCL_GenericCastToPtr_ToPrivate(const void *Ptr) noexcept {
return (const __attribute__((opencl_private)) dataT *)
__spirv_GenericCastToPtr_ToPrivate(Ptr, __spv::StorageClass::Function);
}

template <typename dataT>
extern volatile __attribute__((opencl_private)) dataT *
__SYCL_GenericCastToPtr_ToPrivate(volatile void *Ptr) noexcept {
return (volatile __attribute__((opencl_private)) dataT *)
__spirv_GenericCastToPtr_ToPrivate(Ptr, __spv::StorageClass::Function);
}

template <typename dataT>
extern const volatile __attribute__((opencl_private)) dataT *
__SYCL_GenericCastToPtr_ToPrivate(const volatile void *Ptr) noexcept {
return (const volatile __attribute__((opencl_private)) dataT *)
__spirv_GenericCastToPtr_ToPrivate(Ptr, __spv::StorageClass::Function);
}

template <typename dataT>
__SYCL_CONVERGENT__ extern __DPCPP_SYCL_EXTERNAL dataT
__spirv_SubgroupShuffleINTEL(dataT Data, uint32_t InvocationId) noexcept;
Expand Down
24 changes: 24 additions & 0 deletions sycl/include/sycl/detail/spirv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,30 @@ __SYCL_GROUP_COLLECTIVE_OVERLOAD(BitwiseAndKHR)
__SYCL_GROUP_COLLECTIVE_OVERLOAD(LogicalAndKHR)
__SYCL_GROUP_COLLECTIVE_OVERLOAD(LogicalOrKHR)

template <access::address_space Space, typename T>
auto GenericCastToPtr(T *Ptr) ->
typename multi_ptr<T, Space, access::decorated::yes>::pointer {
if constexpr (Space == access::address_space::global_space) {
return __SYCL_GenericCastToPtr_ToGlobal<T>(Ptr);
} else if constexpr (Space == access::address_space::local_space) {
return __SYCL_GenericCastToPtr_ToLocal<T>(Ptr);
} else if constexpr (Space == access::address_space::private_space) {
return __SYCL_GenericCastToPtr_ToPrivate<T>(Ptr);
}
}

template <access::address_space Space, typename T>
auto GenericCastToPtrExplicit(T *Ptr) ->
typename multi_ptr<T, Space, access::decorated::yes>::pointer {
if constexpr (Space == access::address_space::global_space) {
return __SYCL_GenericCastToPtrExplicit_ToGlobal<T>(Ptr);
} else if constexpr (Space == access::address_space::local_space) {
return __SYCL_GenericCastToPtrExplicit_ToLocal<T>(Ptr);
} else if constexpr (Space == access::address_space::private_space) {
return __SYCL_GenericCastToPtrExplicit_ToPrivate<T>(Ptr);
}
}

} // namespace spirv
} // namespace detail
} // namespace _V1
Expand Down
46 changes: 46 additions & 0 deletions sycl/include/sycl/ext/oneapi/experimental/address_cast.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//==----------- address_cast.hpp - sycl_ext_oneapi_address_cast ------------==//
//
// 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 <sycl/multi_ptr.hpp>

namespace sycl {
inline namespace _V1 {
namespace ext {
namespace oneapi {
namespace experimental {

template <access::address_space Space, access::decorated DecorateAddress,
typename ElementType>
multi_ptr<ElementType, Space, DecorateAddress>
static_address_cast(ElementType *Ptr) {
#ifdef __SYCL_DEVICE_ONLY__
auto CastPtr = sycl::detail::spirv::GenericCastToPtr<Space>(Ptr);
return multi_ptr<ElementType, Space, DecorateAddress>(CastPtr);
#else
return multi_ptr<ElementType, Space, DecorateAddress>(Ptr);
#endif
}

template <access::address_space Space, access::decorated DecorateAddress,
typename ElementType>
multi_ptr<ElementType, Space, DecorateAddress>
dynamic_address_cast(ElementType *Ptr) {
#ifdef __SYCL_DEVICE_ONLY__
auto CastPtr = sycl::detail::spirv::GenericCastToPtrExplicit<Space>(Ptr);
return multi_ptr<ElementType, Space, DecorateAddress>(CastPtr);
#else
return multi_ptr<ElementType, Space, DecorateAddress>(Ptr);
#endif
}

} // namespace experimental
} // namespace oneapi
} // namespace ext
} // namespace _V1
} // namespace sycl
2 changes: 1 addition & 1 deletion sycl/include/sycl/sycl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#include <sycl/ext/oneapi/bindless_images.hpp>
#include <sycl/ext/oneapi/device_global/device_global.hpp>
#include <sycl/ext/oneapi/device_global/properties.hpp>
#include <sycl/ext/oneapi/experimental/address_cast.hpp>
#include <sycl/ext/oneapi/experimental/annotated_arg/annotated_arg.hpp>
#include <sycl/ext/oneapi/experimental/annotated_ptr/annotated_ptr.hpp>
#include <sycl/ext/oneapi/experimental/annotated_usm/alloc_device.hpp>
Expand All @@ -100,7 +101,6 @@
#include <sycl/ext/oneapi/sub_group.hpp>
#include <sycl/ext/oneapi/sub_group_mask.hpp>
#include <sycl/ext/oneapi/weak_object.hpp>

#if !defined(SYCL2020_CONFORMANT_APIS) && \
!defined(__INTEL_PREVIEW_BREAKING_CHANGES)
// We used to include those and some code might be reliant on that.
Expand Down
112 changes: 112 additions & 0 deletions sycl/test-e2e/AddressCast/dynamic_address_cast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//==--------- dynamic_address_cast.cpp - dynamic address_cast test ---------==//
//
// 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
//
//===----------------------------------------------------------------------===//

// Issue with OpenCL CPU runtime implementation of OpGenericCastToPtrExplicit
// OpGenericCastToPtr* intrinsics not implemented on AMD or NVIDIA
// UNSUPPORTED: cpu, hip, cuda
// RUN: %{build} -o %t.out
// RUN: %{run} %t.out
#include <sycl/sycl.hpp>

int main() {

sycl::queue Queue;

sycl::range<1> NItems{1};

sycl::buffer<int, 1> GlobalBuffer{NItems};
sycl::buffer<bool, 1> ResultBuffer{NItems};

Queue
.submit([&](sycl::handler &cgh) {
auto GlobalAccessor =
GlobalBuffer.get_access<sycl::access::mode::read_write>(cgh);
auto LocalAccessor = sycl::local_accessor<int>(1, cgh);
auto ResultAccessor =
ResultBuffer.get_access<sycl::access::mode::write>(cgh);
cgh.parallel_for<class Kernel>(
sycl::nd_range<1>(NItems, 1), [=](sycl::nd_item<1> Item) {
bool Success = true;
size_t Index = Item.get_global_id(0);

int *RawGlobalPointer = &GlobalAccessor[Index];
{
auto GlobalPointer =
sycl::ext::oneapi::experimental::dynamic_address_cast<
sycl::access::address_space::global_space,
sycl::access::decorated::no>(RawGlobalPointer);
auto LocalPointer =
sycl::ext::oneapi::experimental::dynamic_address_cast<
sycl::access::address_space::local_space,
sycl::access::decorated::no>(RawGlobalPointer);
auto PrivatePointer =
sycl::ext::oneapi::experimental::dynamic_address_cast<
sycl::access::address_space::private_space,
sycl::access::decorated::no>(RawGlobalPointer);
Success &= reinterpret_cast<size_t>(RawGlobalPointer) ==
reinterpret_cast<size_t>(GlobalPointer.get_raw());
Success &= LocalPointer.get_raw() == nullptr;
Success &= PrivatePointer.get_raw() == nullptr;
Comment on lines +53 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmoadeli for your awareness, your patch is going to be required to enable this test on cuda and hip

}

int *RawLocalPointer = &LocalAccessor[0];
{
auto GlobalPointer =
sycl::ext::oneapi::experimental::dynamic_address_cast<
sycl::access::address_space::global_space,
sycl::access::decorated::no>(RawLocalPointer);
auto LocalPointer =
sycl::ext::oneapi::experimental::dynamic_address_cast<
sycl::access::address_space::local_space,
sycl::access::decorated::no>(RawLocalPointer);
auto PrivatePointer =
sycl::ext::oneapi::experimental::dynamic_address_cast<
sycl::access::address_space::private_space,
sycl::access::decorated::no>(RawLocalPointer);
Success &= GlobalPointer.get_raw() == nullptr;
Success &= reinterpret_cast<size_t>(RawLocalPointer) ==
reinterpret_cast<size_t>(LocalPointer.get_raw());
Success &= PrivatePointer.get_raw() == nullptr;
}

int PrivateVariable = 0;
int *RawPrivatePointer = &PrivateVariable;
{
auto GlobalPointer =
sycl::ext::oneapi::experimental::dynamic_address_cast<
sycl::access::address_space::global_space,
sycl::access::decorated::no>(RawPrivatePointer);
auto LocalPointer =
sycl::ext::oneapi::experimental::dynamic_address_cast<
sycl::access::address_space::local_space,
sycl::access::decorated::no>(RawPrivatePointer);
auto PrivatePointer =
sycl::ext::oneapi::experimental::dynamic_address_cast<
sycl::access::address_space::private_space,
sycl::access::decorated::no>(RawPrivatePointer);
Success &= GlobalPointer.get_raw() == nullptr;
Success &= LocalPointer.get_raw() == nullptr;
Success &= reinterpret_cast<size_t>(RawPrivatePointer) ==
reinterpret_cast<size_t>(PrivatePointer.get_raw());
}

ResultAccessor[Index] = Success;
});
})
.wait();

bool Success = true;
{
auto ResultAccessor = sycl::host_accessor(ResultBuffer);
for (int i = 0; i < NItems.size(); ++i) {
Success &= ResultAccessor[i];
};
}

return (Success) ? 0 : -1;
}
75 changes: 75 additions & 0 deletions sycl/test-e2e/AddressCast/static_address_cast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//==---------- static_address_cast.cpp - static address_cast test ----------==//
//
// 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
//
//===----------------------------------------------------------------------===//

// OpGenericCastToPtr* intrinsics not implemented on AMD or NVIDIA
// UNSUPPORTED: hip, cuda
// RUN: %{build} -o %t.out
// RUN: %{run} %t.out
#include <sycl/sycl.hpp>

int main() {

sycl::queue Queue;

sycl::range<1> NItems{1};

sycl::buffer<int, 1> GlobalBuffer{NItems};
sycl::buffer<bool, 1> ResultBuffer{NItems};

Queue
.submit([&](sycl::handler &cgh) {
auto GlobalAccessor =
GlobalBuffer.get_access<sycl::access::mode::read_write>(cgh);
auto LocalAccessor = sycl::local_accessor<int>(1, cgh);
auto ResultAccessor =
ResultBuffer.get_access<sycl::access::mode::write>(cgh);
cgh.parallel_for<class Kernel>(
sycl::nd_range<1>(NItems, 1), [=](sycl::nd_item<1> Item) {
bool Success = true;
size_t Index = Item.get_global_id(0);

int *RawGlobalPointer = &GlobalAccessor[Index];
auto GlobalPointer =
sycl::ext::oneapi::experimental::static_address_cast<
sycl::access::address_space::global_space,
sycl::access::decorated::no>(RawGlobalPointer);
Success &= reinterpret_cast<size_t>(RawGlobalPointer) ==
reinterpret_cast<size_t>(GlobalPointer.get_raw());

int *RawLocalPointer = &LocalAccessor[0];
auto LocalPointer =
sycl::ext::oneapi::experimental::static_address_cast<
sycl::access::address_space::local_space,
sycl::access::decorated::no>(RawLocalPointer);
Success &= reinterpret_cast<size_t>(RawLocalPointer) ==
reinterpret_cast<size_t>(LocalPointer.get_raw());

int PrivateVariable = 0;
int *RawPrivatePointer = &PrivateVariable;
auto PrivatePointer =
sycl::ext::oneapi::experimental::static_address_cast<
sycl::access::address_space::private_space,
sycl::access::decorated::no>(RawPrivatePointer);
Success &= reinterpret_cast<size_t>(RawPrivatePointer) ==
reinterpret_cast<size_t>(PrivatePointer.get_raw());

ResultAccessor[Index] = Success;
});
})
.wait();

bool Success = true;
{
auto ResultAccessor = sycl::host_accessor(ResultBuffer);
for (int i = 0; i < NItems.size(); ++i) {
Success &= ResultAccessor[i];
};
}

return (Success) ? 0 : -1;
}