-
Notifications
You must be signed in to change notification settings - Fork 745
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SYCL][SCLA] Add basic
sycl_ext_oneapi_private_alloca
functionality (…
…#12966) After a105055 implementing CodeGen capabilities for `sycl_ext_oneapi_private_alloca`, this patch handles the generated intrinsic in `sycl-post-link` for targets with native specialization constants support. Headers for the new extension are also added, as well as a feature test macro. `multi_ptr` definitions in the SYCL headers are annotated with the `__sycl_detail__::sycl_type` to be detected by the frontend. --------- Signed-off-by: Victor Perez <victor.perez@codeplay.com>
- Loading branch information
1 parent
cf402b8
commit 4ff8fcf
Showing
15 changed files
with
400 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
llvm/test/tools/sycl-post-link/spec-constants/SYCL-alloca.ll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
; RUN: sycl-post-link -spec-const=native < %s -S -o %t.table | ||
; RUN: FileCheck %s -check-prefixes=CHECK-RT < %t_0.ll | ||
; RUN: FileCheck %s --check-prefixes=CHECK-PROPS < %t_0.prop | ||
|
||
; This test checks that the post link tool is able to correctly transform | ||
; SYCL alloca intrinsics in SPIR-V devices. | ||
|
||
%"class.sycl::_V1::specialization_id" = type { i64 } | ||
%"class.sycl::_V1::specialization_id.0" = type { i32 } | ||
%"class.sycl::_V1::specialization_id.1" = type { i16 } | ||
%my_range = type { ptr addrspace(4), ptr addrspace(4) } | ||
|
||
@size_i64 = internal addrspace(1) constant %"class.sycl::_V1::specialization_id" { i64 10 }, align 8 | ||
@size_i32 = internal addrspace(1) constant %"class.sycl::_V1::specialization_id.0" { i32 120 }, align 4 | ||
@size_i16 = internal addrspace(1) constant %"class.sycl::_V1::specialization_id.1" { i16 1 }, align 2 | ||
|
||
; Check that the following globals are preserved: even though they are not used | ||
; in the module anymore, they could still be referenced by debug info metadata | ||
; (specialization_id objects are used as template arguments in SYCL | ||
; specialization constant APIs). | ||
; CHECK: @size_i64 | ||
; CHECK: @size_i32 | ||
; CHECK: @size_i16 | ||
|
||
@size_i64_stable_name = private unnamed_addr constant [36 x i8] c"_ZTS14name_generatorIL_Z8size_i64EE\00", align 1 | ||
@size_i32_stable_name = private unnamed_addr constant [36 x i8] c"_ZTS14name_generatorIL_Z8size_i32EE\00", align 1 | ||
@size_i16_stable_name = private unnamed_addr constant [36 x i8] c"_ZTS14name_generatorIL_Z8size_i16EE\00", align 1 | ||
|
||
; CHECK-LABEL: define dso_local void @private_alloca | ||
define dso_local void @private_alloca() { | ||
; CHECK-RT: [[LENGTH:%.*]] = call i32 @_Z20__spirv_SpecConstantii(i32 1, i32 120) | ||
; CHECK-RT: {{.*}} = alloca double, i32 [[LENGTH]], align 8 | ||
call ptr @llvm.sycl.alloca.p0.p4.p4.p4.f64(ptr addrspace(4) addrspacecast (ptr @size_i32_stable_name to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @size_i32 to ptr addrspace(4)), ptr addrspace(4) null, double 0.000000e+00, i64 8) | ||
; CHECK-RT: [[LENGTH:%.*]] = call i64 @_Z20__spirv_SpecConstantix(i32 0, i64 10) | ||
; CHECK-RT: {{.*}} = alloca float, i64 [[LENGTH]], align 8 | ||
call ptr @llvm.sycl.alloca.p0.p4.p4.p4.f32(ptr addrspace(4) addrspacecast (ptr @size_i64_stable_name to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @size_i64 to ptr addrspace(4)), ptr addrspace(4) null, float 0.000000e+00, i64 8) | ||
call ptr @llvm.sycl.alloca.p0.p4.p4.p4.s_my_range(ptr addrspace(4) addrspacecast (ptr @size_i16_stable_name to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @size_i16 to ptr addrspace(4)), ptr addrspace(4) null, %my_range zeroinitializer, i64 64) | ||
ret void | ||
} | ||
|
||
declare ptr @llvm.sycl.alloca.p0.p4.p4.p4.f32(ptr addrspace(4), ptr addrspace(4), ptr addrspace(4), float, i64) | ||
declare ptr @llvm.sycl.alloca.p0.p4.p4.p4.f64(ptr addrspace(4), ptr addrspace(4), ptr addrspace(4), double, i64) | ||
declare ptr @llvm.sycl.alloca.p0.p4.p4.p4.s_my_range(ptr addrspace(4), ptr addrspace(4), ptr addrspace(4), %my_range, i64) | ||
|
||
; CHECK-RT: !sycl.specialization-constants = !{![[#ID0:]], ![[#ID1:]], ![[#ID2:]]} | ||
; CHECK-RT: !sycl.specialization-constants-default-values = !{![[#DEF0:]], ![[#DEF1:]], ![[#DEF2:]]} | ||
|
||
; CHECK-RT: ![[#ID0]] = !{!"_ZTS14name_generatorIL_Z8size_i64EE", i32 0, i32 0, i32 8} | ||
; CHECK-RT: ![[#ID1]] = !{!"_ZTS14name_generatorIL_Z8size_i32EE", i32 1, i32 0, i32 4} | ||
; CHECK-RT: ![[#ID2]] = !{!"_ZTS14name_generatorIL_Z8size_i16EE", i32 2, i32 0, i32 2} | ||
; CHECK-RT: ![[#DEF0]] = !{i64 10} | ||
; CHECK-RT: ![[#DEF1]] = !{i32 120} | ||
; CHECK-RT: ![[#DEF2]] = !{i16 1} | ||
|
||
; CHECK-PROPS: [SYCL/specialization constants] | ||
; CHECK-PROPS: _ZTS14name_generatorIL_Z8size_i64EE=2| | ||
; CHECK-PROPS: _ZTS14name_generatorIL_Z8size_i32EE=2| | ||
; CHECK-PROPS: _ZTS14name_generatorIL_Z8size_i16EE=2| | ||
; CHECK-PROPS: [SYCL/specialization constants default values] | ||
; CHECK-PROPS: all=2| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//==--- alloca.hpp --- SYCL extension for private memory allocations--------==// | ||
// | ||
// 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/exception.hpp" | ||
#include "sycl/kernel_handler.hpp" | ||
#include "sycl/multi_ptr.hpp" | ||
|
||
namespace sycl { | ||
inline namespace _V1 { | ||
namespace ext::oneapi::experimental { | ||
|
||
#ifdef __SYCL_DEVICE_ONLY__ | ||
|
||
// On the device, this is an alias to __builtin_intel_sycl_alloca. | ||
|
||
/// Function allocating and returning a pointer to an unitialized region of | ||
/// memory capable of hosting `kh.get_specialization_constant<SizeSpecName>()` | ||
/// elements of type \tp ElementType. The pointer will be a `sycl::private_ptr` | ||
/// and will or will not be decorated depending on \tp DecorateAddres. | ||
/// | ||
/// On the host, this function simply throws, as this is not supported there. | ||
/// | ||
/// See sycl_ext_oneapi_private_alloca. | ||
template <typename ElementType, auto &SizeSpecName, | ||
access::decorated DecorateAddress> | ||
__SYCL_BUILTIN_ALIAS(__builtin_intel_sycl_alloca) | ||
private_ptr<ElementType, DecorateAddress> private_alloca(kernel_handler &kh); | ||
|
||
#else | ||
|
||
// On the host, throw, this is not supported. | ||
template <typename ElementType, auto &SizeSpecName, | ||
access::decorated DecorateAddress> | ||
private_ptr<ElementType, DecorateAddress> private_alloca(kernel_handler &kh) { | ||
throw feature_not_supported("sycl::ext::oneapi::experimental::private_alloca " | ||
"is not supported on host", | ||
PI_ERROR_INVALID_OPERATION); | ||
} | ||
|
||
#endif // __SYCL_DEVICE_ONLY__ | ||
|
||
} // namespace ext::oneapi::experimental | ||
} // namespace _V1 | ||
} // namespace sycl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
sycl/test-e2e/PrivateAlloca/Inputs/private_alloca_test.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#pragma once | ||
|
||
// Template for private alloca tests. | ||
|
||
#include <sycl/sycl.hpp> | ||
|
||
#include <sycl/ext/oneapi/experimental/alloca.hpp> | ||
|
||
template <typename ElementType, typename SizeType, | ||
sycl::access::decorated DecorateAddress> | ||
class Kernel; | ||
|
||
template <typename ElementType, auto &Size, | ||
sycl::access::decorated DecorateAddress> | ||
void test() { | ||
std::size_t N; | ||
|
||
std::cin >> N; | ||
|
||
std::vector<std::size_t> v(N); | ||
{ | ||
sycl::queue q; | ||
sycl::buffer<std::size_t> b(v); | ||
q.submit([&](sycl::handler &cgh) { | ||
sycl::accessor acc(b, cgh, sycl::write_only, sycl::no_init); | ||
cgh.set_specialization_constant<Size>(N); | ||
using spec_const_type = std::remove_reference_t<decltype(Size)>; | ||
using size_type = typename spec_const_type::value_type; | ||
cgh.single_task<Kernel<ElementType, size_type, DecorateAddress>>( | ||
[=](sycl::kernel_handler h) { | ||
auto ptr = sycl::ext::oneapi::experimental::private_alloca< | ||
ElementType, Size, DecorateAddress>(h); | ||
const std::size_t M = h.get_specialization_constant<Size>(); | ||
ptr[0] = static_cast<ElementType>(M); | ||
ElementType value{1}; | ||
for (auto begin = ptr.get() + 1, end = ptr.get() + M; begin < end; | ||
++begin, ++value) { | ||
*begin = value; | ||
} | ||
auto accBegin = acc.begin(); | ||
for (auto begin = ptr.get(), end = ptr.get() + M; begin < end; | ||
++begin, ++accBegin) { | ||
*accBegin = *begin; | ||
} | ||
}); | ||
}); | ||
q.wait_and_throw(); | ||
} | ||
assert(static_cast<std::size_t>(v.front()) == N && | ||
"Wrong private alloca length reported"); | ||
for (std::size_t i = 1; i < N; ++i) { | ||
assert(static_cast<std::size_t>(v[i]) == i && | ||
"Wrong value in copied-back sequence"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// RUN: %{build} -w -o %t.out | ||
// RUN: echo 1 | %{run} %t.out | ||
// UNSUPPORTED: cuda || hip | ||
|
||
// Test checking size of 'bool' type. This is not expected to be ever used, but, | ||
// as 'bool' is an integral type, it is a possible scenario. | ||
|
||
#include "Inputs/private_alloca_test.hpp" | ||
|
||
constexpr sycl::specialization_id<bool> size(true); | ||
|
||
int main() { test<int, size, sycl::access::decorated::legacy>(); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// RUN: %{build} -o %t.out | ||
// RUN: echo 1 | %{run} %t.out | ||
// RUN: echo 10 | %{run} %t.out | ||
// RUN: echo 20 | %{run} %t.out | ||
// RUN: echo 30 | %{run} %t.out | ||
// UNSUPPORTED: cuda || hip | ||
|
||
// Simple test filling a SYCL private alloca and copying it back to an output | ||
// accessor using a decorated multi_ptr. | ||
|
||
#include "Inputs/private_alloca_test.hpp" | ||
|
||
constexpr sycl::specialization_id<int> size(10); | ||
|
||
int main() { test<float, size, sycl::access::decorated::yes>(); } |
Oops, something went wrong.