|
| 1 | +//==-------------- test_proxy.hpp - DPC++ Explicit SIMD API ----------------==// |
| 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 | +// Test proxy to differentiate move and copy constructor calls |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +// The test proxy if solely for the test purposes, so it's off by default, with |
| 14 | +// no any code generated. It is enabled only if the __ESIMD_ENABLE_TEST_PROXY |
| 15 | +// macro is defined. |
| 16 | +// It's expected for the proxy class to be available in device code, so that it |
| 17 | +// could be incorporated into the ESIMD API classes. Though there is no reason |
| 18 | +// to limit it to the __SYCL_DEVICE_ONLY__. |
| 19 | +#ifndef __ESIMD_ENABLE_TEST_PROXY |
| 20 | + |
| 21 | +// No code generation by default |
| 22 | +#define __ESIMD_DECLARE_TEST_PROXY |
| 23 | +#define __ESIMD_DECLARE_TEST_PROXY_ACCESS |
| 24 | +#define __esimd_move_test_proxy(other) |
| 25 | + |
| 26 | +#else |
| 27 | + |
| 28 | +// Declare the class attribute |
| 29 | +// |
| 30 | +// We are using non static data member initialization approach to force |
| 31 | +// the value required. Initialization will take place even if no |
| 32 | +// default/copy/move constructor of the test_proxy class was explcitly |
| 33 | +// called by any of the user-defined constructors of the proxy target |
| 34 | +#define __ESIMD_DECLARE_TEST_PROXY \ |
| 35 | + esimd::detail::test::test_proxy M_testProxy = \ |
| 36 | + esimd::detail::test::test_proxy(); |
| 37 | + |
| 38 | +// Declare the getter to access the proxy from the tests |
| 39 | +#define __ESIMD_DECLARE_TEST_PROXY_ACCESS \ |
| 40 | + const auto &get_test_proxy() const { return M_testProxy; } |
| 41 | + |
| 42 | +// Test proxy will be handled in a proper way by default/implicit move |
| 43 | +// constructors and move operators. |
| 44 | +// Still the user-defined constructors or move operators should explicitly state |
| 45 | +// what to do with each of class atributes, so a proper wrapper required |
| 46 | +// |
| 47 | +// We are using a simple do-while trick to make sure no code breakage could |
| 48 | +// possibly occur in case macro becomes multistatement (PRE10-C in SEI CERT C) |
| 49 | +#define __esimd_move_test_proxy(other) \ |
| 50 | + do { \ |
| 51 | + M_testProxy = std::move(other.M_testProxy); \ |
| 52 | + } while (false) |
| 53 | + |
| 54 | +__SYCL_INLINE_NAMESPACE(cl) { |
| 55 | +namespace sycl::ext::intel::experimental::esimd::detail::test { |
| 56 | + |
| 57 | +// The test_proxy class. |
| 58 | +// Being intended solely for the test purposes, it is enabled only if the |
| 59 | +// __ESIMD_ENABLE_TEST_PROXY macro is defined, which is off by default. |
| 60 | +// |
| 61 | +// This is a helper class for tests to differentiate between the copy |
| 62 | +// constructor/assignment and the move constructor/assignment calls, |
| 63 | +// as the copy constructor works as the default fallback for every case with |
| 64 | +// move constructor disabled or not provided |
| 65 | +// |
| 66 | +// It is expected for the class with the test proxy (the class under test) to: |
| 67 | +// - provide the get_test_proxy() method |
| 68 | +// - properly handle moving the test_proxy member in user-defined move |
| 69 | +// constructors and user-defined assignment operators |
| 70 | +// |
| 71 | +// Therefore the following expression is expected to return `true` only if the |
| 72 | +// move constructor or move operator was called for the instance of the class |
| 73 | +// under test: |
| 74 | +// instance.get_test_proxy().was_move_destination() |
| 75 | +// |
| 76 | +class test_proxy { |
| 77 | + // Define the default value to use for every constructor |
| 78 | + bool M_move_destination = false; |
| 79 | + |
| 80 | +public: |
| 81 | + test_proxy() { __esimd_dbg_print(test_proxy()); } |
| 82 | + |
| 83 | + test_proxy(const test_proxy &) { |
| 84 | + __esimd_dbg_print(test_proxy(const test_proxy &other)); |
| 85 | + } |
| 86 | + test_proxy(test_proxy &&) { |
| 87 | + __esimd_dbg_print(test_proxy(test_proxy && other)); |
| 88 | + M_move_destination = true; |
| 89 | + } |
| 90 | + test_proxy &operator=(const test_proxy &) { |
| 91 | + __esimd_dbg_print(test_proxy::operator=(const test_proxy &other)); |
| 92 | + return *this; |
| 93 | + } |
| 94 | + test_proxy &operator=(test_proxy &&) { |
| 95 | + __esimd_dbg_print(test_proxy::operator=(test_proxy &&other)); |
| 96 | + M_move_destination = true; |
| 97 | + return *this; |
| 98 | + } |
| 99 | + bool was_move_destination() const { return M_move_destination; } |
| 100 | +}; |
| 101 | + |
| 102 | +} // namespace sycl::ext::intel::experimental::esimd::detail::test |
| 103 | +} // __SYCL_INLINE_NAMESPACE(cl) |
| 104 | + |
| 105 | +#endif // __ESIMD_ENABLE_TEST_PROXY |
0 commit comments