|
| 1 | +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -fsycl-device-code-split=per_kernel %s -o %t.out |
| 2 | +// RUN: %CPU_RUN_PLACEHOLDER %t.out |
| 3 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out |
| 4 | +// FIXME: ACC devices use emulation path, which is not yet supported |
| 5 | + |
| 6 | +// This test checks that specialization constant information is available on |
| 7 | +// kernel bundles produced by sycl::link. |
| 8 | + |
| 9 | +#include <sycl/sycl.hpp> |
| 10 | + |
| 11 | +#include <optional> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +using namespace sycl; |
| 15 | + |
| 16 | +class Kernel1; |
| 17 | +class Kernel2; |
| 18 | + |
| 19 | +struct TestStruct { |
| 20 | + float f; |
| 21 | + long long ll; |
| 22 | +}; |
| 23 | + |
| 24 | +bool operator==(const TestStruct &LHS, const TestStruct &RHS) { |
| 25 | + return LHS.f == RHS.f && LHS.ll == RHS.ll; |
| 26 | +} |
| 27 | +bool operator!=(const TestStruct &LHS, const TestStruct &RHS) { |
| 28 | + return !(LHS == RHS); |
| 29 | +} |
| 30 | + |
| 31 | +constexpr specialization_id<TestStruct> SpecName1; |
| 32 | +constexpr specialization_id<int> SpecName2; |
| 33 | + |
| 34 | +std::optional<device> FindDeviceWithOnlineLinker() { |
| 35 | + std::vector<device> Devices = device::get_devices(); |
| 36 | + auto Device = |
| 37 | + std::find_if(Devices.begin(), Devices.end(), [](const device &D) { |
| 38 | + return D.has(aspect::online_linker); |
| 39 | + }); |
| 40 | + if (Device == Devices.end()) |
| 41 | + return std::nullopt; |
| 42 | + return *Device; |
| 43 | +} |
| 44 | + |
| 45 | +int main() { |
| 46 | + std::optional<device> Device = FindDeviceWithOnlineLinker(); |
| 47 | + |
| 48 | + if (!Device) { |
| 49 | + std::cout << "No device with aspect::online_linker. Skipping test." |
| 50 | + << std::endl; |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + context Ctx{*Device}; |
| 55 | + |
| 56 | + kernel_bundle<bundle_state::input> Bundle1 = |
| 57 | + sycl::get_kernel_bundle<Kernel1, bundle_state::input>(Ctx, {*Device}); |
| 58 | + kernel_bundle<bundle_state::input> Bundle2 = |
| 59 | + sycl::get_kernel_bundle<Kernel2, bundle_state::input>(Ctx, {*Device}); |
| 60 | + |
| 61 | + assert(Bundle1.has_specialization_constant<SpecName1>()); |
| 62 | + assert(Bundle2.has_specialization_constant<SpecName2>()); |
| 63 | + assert(!Bundle1.has_specialization_constant<SpecName2>()); |
| 64 | + assert(!Bundle2.has_specialization_constant<SpecName1>()); |
| 65 | + |
| 66 | + TestStruct Value1{3.14f, 1234ll}; |
| 67 | + int Value2 = 42; |
| 68 | + Bundle1.set_specialization_constant<SpecName1>(Value1); |
| 69 | + Bundle2.set_specialization_constant<SpecName2>(Value2); |
| 70 | + |
| 71 | + kernel_bundle<bundle_state::object> ObjBundle1 = sycl::compile(Bundle1); |
| 72 | + kernel_bundle<bundle_state::object> ObjBundle2 = sycl::compile(Bundle2); |
| 73 | + |
| 74 | + assert(ObjBundle1.has_specialization_constant<SpecName1>()); |
| 75 | + assert(ObjBundle2.has_specialization_constant<SpecName2>()); |
| 76 | + assert(!ObjBundle1.has_specialization_constant<SpecName2>()); |
| 77 | + assert(!ObjBundle2.has_specialization_constant<SpecName1>()); |
| 78 | + |
| 79 | + kernel_bundle<bundle_state::executable> LinkedBundle = |
| 80 | + sycl::link({ObjBundle1, ObjBundle2}); |
| 81 | + |
| 82 | + assert(LinkedBundle.has_specialization_constant<SpecName1>()); |
| 83 | + assert(LinkedBundle.has_specialization_constant<SpecName2>()); |
| 84 | + |
| 85 | + int Failures = 0; |
| 86 | + |
| 87 | + if (LinkedBundle.get_specialization_constant<SpecName1>() != Value1) { |
| 88 | + std::cout |
| 89 | + << "Read value of SpecName1 on host is not the same as was written." |
| 90 | + << std::endl; |
| 91 | + ++Failures; |
| 92 | + } |
| 93 | + |
| 94 | + if (LinkedBundle.get_specialization_constant<SpecName2>() != Value2) { |
| 95 | + std::cout |
| 96 | + << "Read value of SpecName2 on host is not the same as was written." |
| 97 | + << std::endl; |
| 98 | + ++Failures; |
| 99 | + } |
| 100 | + |
| 101 | + TestStruct ReadValue1; |
| 102 | + int ReadValue2; |
| 103 | + |
| 104 | + { |
| 105 | + queue Q{Ctx, *Device}; |
| 106 | + |
| 107 | + buffer<TestStruct> ReadValue1Buffer{&ReadValue1, 1}; |
| 108 | + Q.submit([&](handler &CGH) { |
| 109 | + CGH.use_kernel_bundle(LinkedBundle); |
| 110 | + accessor ReadValue1Acc{ReadValue1Buffer, CGH, sycl::write_only}; |
| 111 | + CGH.single_task<Kernel1>([=](kernel_handler KHR) { |
| 112 | + ReadValue1Acc[0] = KHR.get_specialization_constant<SpecName1>(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + buffer<int> ReadValue2Buffer{&ReadValue2, 1}; |
| 117 | + Q.submit([&](handler &CGH) { |
| 118 | + CGH.use_kernel_bundle(LinkedBundle); |
| 119 | + accessor ReadValue2Acc{ReadValue2Buffer, CGH, sycl::write_only}; |
| 120 | + CGH.single_task<Kernel2>([=](kernel_handler KHR) { |
| 121 | + ReadValue2Acc[0] = KHR.get_specialization_constant<SpecName2>(); |
| 122 | + }); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + if (ReadValue1 != Value1) { |
| 127 | + std::cout |
| 128 | + << "Read value of SpecName1 on device is not the same as was written." |
| 129 | + << std::endl; |
| 130 | + ++Failures; |
| 131 | + } |
| 132 | + |
| 133 | + if (ReadValue2 != Value2) { |
| 134 | + std::cout |
| 135 | + << "Read value of SpecName2 on device is not the same as was written." |
| 136 | + << std::endl; |
| 137 | + ++Failures; |
| 138 | + } |
| 139 | + |
| 140 | + return Failures; |
| 141 | +} |
0 commit comments