|
| 1 | +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t1.out |
| 2 | +// RUN: %HOST_RUN_PLACEHOLDER %t1.out |
| 3 | +// RUN: %CPU_RUN_PLACEHOLDER %t1.out |
| 4 | +// RUN: %GPU_RUN_PLACEHOLDER %t1.out |
| 5 | +// RUN: %ACC_RUN_PLACEHOLDER %t1.out |
| 6 | + |
| 7 | +//==------------------------------------------------------------------------==// |
| 8 | +// |
| 9 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 10 | +// See https://llvm.org/LICENSE.txt for license information. |
| 11 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#include <sycl.hpp> |
| 16 | + |
| 17 | +#include <iostream> |
| 18 | + |
| 19 | +using namespace sycl; |
| 20 | + |
| 21 | +// Allocator has a minimum internal alignment of 64, so use higher values for |
| 22 | +// the purpose of this test. |
| 23 | +struct alignas(256) Aligned256 { |
| 24 | + int x; |
| 25 | +}; |
| 26 | +struct alignas(128) Aligned128 { |
| 27 | + int x; |
| 28 | +}; |
| 29 | + |
| 30 | +template <class Allocator> |
| 31 | +void test(Allocator alloc, size_t Align, int Line = __builtin_LINE()) { |
| 32 | + decltype(alloc.allocate(1)) Ptrs[10]; |
| 33 | + for (auto *&Elem : Ptrs) |
| 34 | + Elem = alloc.allocate(1); |
| 35 | + |
| 36 | + for (auto *Ptr : Ptrs) { |
| 37 | + if ((reinterpret_cast<uintptr_t>(Ptr) & (Align - 1)) != 0) { |
| 38 | + std::cout << "Failed at line " << Line << std::endl; |
| 39 | + assert(false && "Not properly aligned!"); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + for (auto *Ptr : Ptrs) |
| 44 | + alloc.deallocate(Ptr, 1); |
| 45 | +} |
| 46 | + |
| 47 | +int main() { |
| 48 | + queue q; |
| 49 | + auto dev = q.get_device(); |
| 50 | + auto ctx = q.get_context(); |
| 51 | + |
| 52 | + if (!dev.get_info<info::device::usm_host_allocations>()) |
| 53 | + return 0; |
| 54 | + |
| 55 | + // There was a bug that we didn't re-adjust allocator's alignment when we |
| 56 | + // rebound it to another type. |
| 57 | + { |
| 58 | + // Test default value of Alignment template parameter. |
| 59 | + usm_allocator<Aligned128, usm::alloc::host> alloc(ctx, dev); |
| 60 | + test(alloc, 128); |
| 61 | + |
| 62 | + using traits_t = std::allocator_traits<decltype(alloc)>; |
| 63 | + using rebind_t = typename traits_t::template rebind_alloc<Aligned256>; |
| 64 | + rebind_t alloc_rebound = alloc; |
| 65 | + test(alloc_rebound, 256); |
| 66 | + } |
| 67 | + |
| 68 | + { |
| 69 | + // Test explicit value of Alignment template parameter. |
| 70 | + usm_allocator<Aligned256, usm::alloc::host, 256> alloc(ctx, dev); |
| 71 | + test(alloc, 256); |
| 72 | + using traits_t = std::allocator_traits<decltype(alloc)>; |
| 73 | + using rebind_t = typename traits_t::template rebind_alloc<Aligned128>; |
| 74 | + rebind_t alloc_rebound = alloc; |
| 75 | + // Rebound allocator must use the higher alignment from the explicit |
| 76 | + // template parameter, not the type's alignment. |
| 77 | + test(alloc_rebound, 256); |
| 78 | + } |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
0 commit comments