|
| 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 <CL/sycl.hpp> |
| 16 | + |
| 17 | +using namespace cl::sycl; |
| 18 | + |
| 19 | +// Allocator has a minimum internal alignment of 64, so use higher values for |
| 20 | +// the purpose of this test. |
| 21 | +struct alignas(256) Aligned256 { |
| 22 | + int x; |
| 23 | +}; |
| 24 | +struct alignas(128) Aligned128 { |
| 25 | + int x; |
| 26 | +}; |
| 27 | + |
| 28 | +template <class T, class Allocator> |
| 29 | +void test_align(Allocator alloc, size_t Align) { |
| 30 | + std::vector<T *> Ptrs; |
| 31 | + for (int i =0; i < 10; ++i) |
| 32 | + Ptrs.push_back(alloc.allocate(1)); |
| 33 | + |
| 34 | + int NumExactlyAligned = 0; |
| 35 | + |
| 36 | + for (T *Ptr : Ptrs) { |
| 37 | + auto Val = reinterpret_cast<uintptr_t>(Ptr); |
| 38 | + assert((Val & (Align - 1)) == 0 && "Not properly aligned!"); |
| 39 | + if ((Val & (Align*2 - 1)) != 0) |
| 40 | + ++NumExactlyAligned; |
| 41 | + } |
| 42 | + |
| 43 | + // There was a bug that we didn't lower allocator's alignment when we rebound |
| 44 | + // it to less-aligned type. Here, we verify that Aligned128's alignment is in |
| 45 | + // fact 128 and not 256 (we also check that Aligned256's alignment isn't 512 |
| 46 | + // but that is less important). |
| 47 | + // |
| 48 | + // Technically, the underlying runtime might do overalignment itself (as it |
| 49 | + // does for int, for example - always aligning for 64). In such case, the test |
| 50 | + // needs to be updated to find configuration where we still test our runtime |
| 51 | + // by ensuring that underlying allocations' alignment differs between |
| 52 | + // Aligned128/Aligned256. |
| 53 | + assert(NumExactlyAligned != 0 && "All allocations are over-aligned!"); |
| 54 | + |
| 55 | + for (T *Ptr : Ptrs) |
| 56 | + alloc.deallocate(Ptr, 1); |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +int main() { |
| 61 | + queue q; |
| 62 | + auto dev = q.get_device(); |
| 63 | + auto ctx = q.get_context(); |
| 64 | + { |
| 65 | + // Test default value of Alignment template parameter. |
| 66 | + usm_allocator<Aligned256, usm::alloc::host> alloc(ctx, dev); |
| 67 | + test_align<Aligned256>(alloc, 256); |
| 68 | + |
| 69 | + using traits_t = std::allocator_traits<decltype(alloc)>; |
| 70 | + using rebind_t = typename traits_t::template rebind_alloc<Aligned128>; |
| 71 | + rebind_t alloc_rebound = alloc; |
| 72 | + test_align<Aligned128>(alloc_rebound, 128); |
| 73 | + } |
| 74 | + |
| 75 | + { |
| 76 | + // Test explicit value of Alignment template parameter. |
| 77 | + usm_allocator<Aligned256, usm::alloc::host, 256> alloc(ctx, dev); |
| 78 | + test_align<Aligned256>(alloc, 256); |
| 79 | + using traits_t = std::allocator_traits<decltype(alloc)>; |
| 80 | + using rebind_t = typename traits_t::template rebind_alloc<Aligned128>; |
| 81 | + rebind_t alloc_rebound = alloc; |
| 82 | + // Rebound allocator must use the higher alignment from the explicit |
| 83 | + // template parameter, not the type's alignment. |
| 84 | + test_align<Aligned128>(alloc_rebound, 256); |
| 85 | + } |
| 86 | + |
| 87 | + return 0; |
| 88 | +} |
0 commit comments