|
| 1 | +// |
| 2 | +// Copyright (c) 2024 The Khronos Group Inc. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +#include "testBase.h" |
| 18 | +#include "spirvInfo.hpp" |
| 19 | +#include "types.hpp" |
| 20 | + |
| 21 | +#include <algorithm> |
| 22 | +#include <cinttypes> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +REGISTER_TEST(spirv15_ptr_bitcast) |
| 26 | +{ |
| 27 | + if (!is_spirv_version_supported(device, "SPIR-V_1.5")) |
| 28 | + { |
| 29 | + log_info("SPIR-V 1.5 not supported; skipping tests.\n"); |
| 30 | + return TEST_SKIPPED_ITSELF; |
| 31 | + } |
| 32 | + |
| 33 | + cl_int error = CL_SUCCESS; |
| 34 | + |
| 35 | + cl_uint address_bits; |
| 36 | + error = clGetDeviceInfo(device, CL_DEVICE_ADDRESS_BITS, sizeof(cl_uint), |
| 37 | + &address_bits, NULL); |
| 38 | + SPIRV_CHECK_ERROR(error, "Failed to get address bits"); |
| 39 | + |
| 40 | + clProgramWrapper prog; |
| 41 | + error = get_program_with_il(prog, device, context, "spv1.5/ptr_bitcast"); |
| 42 | + SPIRV_CHECK_ERROR(error, "Failed to compile spv program"); |
| 43 | + |
| 44 | + clKernelWrapper kernel = clCreateKernel(prog, "ptr_bitcast_test", &error); |
| 45 | + SPIRV_CHECK_ERROR(error, "Failed to create spv kernel"); |
| 46 | + |
| 47 | + cl_ulong result_ulong = |
| 48 | + address_bits == 32 ? 0xAAAAAAAAUL : 0xAAAAAAAAAAAAAAAAUL; |
| 49 | + cl_ulong result_uint2 = |
| 50 | + address_bits == 32 ? 0x55555555UL : 0x5555555555555555UL; |
| 51 | + |
| 52 | + clMemWrapper dst_ulong = |
| 53 | + clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, |
| 54 | + sizeof(result_ulong), &result_ulong, &error); |
| 55 | + SPIRV_CHECK_ERROR(error, "Failed to create dst_ulong buffer"); |
| 56 | + |
| 57 | + clMemWrapper dst_uint2 = |
| 58 | + clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, |
| 59 | + sizeof(result_uint2), &result_uint2, &error); |
| 60 | + SPIRV_CHECK_ERROR(error, "Failed to create dst_uint2 buffer"); |
| 61 | + |
| 62 | + error |= clSetKernelArg(kernel, 0, sizeof(dst_ulong), &dst_ulong); |
| 63 | + error |= clSetKernelArg(kernel, 1, sizeof(dst_uint2), &dst_uint2); |
| 64 | + SPIRV_CHECK_ERROR(error, "Failed to set kernel args"); |
| 65 | + |
| 66 | + size_t global = 1; |
| 67 | + error = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, NULL, 0, |
| 68 | + NULL, NULL); |
| 69 | + SPIRV_CHECK_ERROR(error, "Failed to enqueue kernel"); |
| 70 | + |
| 71 | + error = |
| 72 | + clEnqueueReadBuffer(queue, dst_ulong, CL_TRUE, 0, sizeof(result_ulong), |
| 73 | + &result_ulong, 0, NULL, NULL); |
| 74 | + SPIRV_CHECK_ERROR(error, "Unable to read dst_ulong buffer"); |
| 75 | + |
| 76 | + error = |
| 77 | + clEnqueueReadBuffer(queue, dst_uint2, CL_TRUE, 0, sizeof(result_uint2), |
| 78 | + &result_uint2, 0, NULL, NULL); |
| 79 | + SPIRV_CHECK_ERROR(error, "Unable to read dst_uint2 buffer"); |
| 80 | + |
| 81 | + if (result_ulong != result_uint2) |
| 82 | + { |
| 83 | + log_error("Results mismatch! ulong = 0x%016" PRIx64 |
| 84 | + " vs. uint2 = 0x%016" PRIx64 "\n", |
| 85 | + result_ulong, result_uint2); |
| 86 | + return TEST_FAIL; |
| 87 | + } |
| 88 | + |
| 89 | + return TEST_PASS; |
| 90 | +} |
| 91 | + |
| 92 | +REGISTER_TEST(spirv15_non_uniform_broadcast) |
| 93 | +{ |
| 94 | + if (!is_spirv_version_supported(device, "SPIR-V_1.5")) |
| 95 | + { |
| 96 | + log_info("SPIR-V 1.5 not supported; skipping tests.\n"); |
| 97 | + return TEST_SKIPPED_ITSELF; |
| 98 | + } |
| 99 | + |
| 100 | + if (!is_extension_available(device, "cl_khr_subgroup_ballot")) |
| 101 | + { |
| 102 | + log_info("cl_khr_subgroup_ballot is not supported; skipping tests.\n"); |
| 103 | + return TEST_SKIPPED_ITSELF; |
| 104 | + } |
| 105 | + |
| 106 | + cl_int error = CL_SUCCESS; |
| 107 | + |
| 108 | + clProgramWrapper prog; |
| 109 | + error = get_program_with_il(prog, device, context, |
| 110 | + "spv1.5/non_uniform_broadcast_dynamic_index"); |
| 111 | + SPIRV_CHECK_ERROR(error, "Failed to compile spv program"); |
| 112 | + |
| 113 | + clKernelWrapper kernel = clCreateKernel( |
| 114 | + prog, "non_uniform_broadcast_dynamic_index_test", &error); |
| 115 | + SPIRV_CHECK_ERROR(error, "Failed to create spv kernel"); |
| 116 | + |
| 117 | + // Get the local work-group size for one sub-group per work-group. |
| 118 | + size_t lws = 0; |
| 119 | + size_t one = 1; |
| 120 | + error = clGetKernelSubGroupInfo( |
| 121 | + kernel, device, CL_KERNEL_LOCAL_SIZE_FOR_SUB_GROUP_COUNT, |
| 122 | + sizeof(size_t), &one, sizeof(size_t), &lws, NULL); |
| 123 | + SPIRV_CHECK_ERROR(error, "Failed to get local work size for one sub-group"); |
| 124 | + |
| 125 | + // Use four work-groups, unless the local-group size is less than four. |
| 126 | + size_t wgcount = std::min<size_t>(lws, 4); |
| 127 | + size_t gws = wgcount * lws; |
| 128 | + clMemWrapper dst = clCreateBuffer(context, CL_MEM_READ_WRITE, |
| 129 | + sizeof(cl_int) * gws, NULL, &error); |
| 130 | + SPIRV_CHECK_ERROR(error, "Failed to create dst buffer"); |
| 131 | + |
| 132 | + error |= clSetKernelArg(kernel, 0, sizeof(dst), &dst); |
| 133 | + SPIRV_CHECK_ERROR(error, "Failed to set kernel args"); |
| 134 | + |
| 135 | + error = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &gws, &lws, 0, NULL, |
| 136 | + NULL); |
| 137 | + SPIRV_CHECK_ERROR(error, "Failed to enqueue kernel"); |
| 138 | + |
| 139 | + std::vector<cl_int> results(gws); |
| 140 | + error = clEnqueueReadBuffer(queue, dst, CL_TRUE, 0, sizeof(cl_int) * gws, |
| 141 | + results.data(), 0, NULL, NULL); |
| 142 | + SPIRV_CHECK_ERROR(error, "Unable to read destination buffer"); |
| 143 | + |
| 144 | + // Remember: the test kernel did: |
| 145 | + // sub_group_non_uniform_broadcast(get_global_id(0), get_group_id(0)) |
| 146 | + for (size_t g = 0; g < wgcount; g++) |
| 147 | + { |
| 148 | + for (size_t l = 0; l < lws; l++) |
| 149 | + { |
| 150 | + size_t index = g * lws + l; |
| 151 | + size_t check = g * lws + g; |
| 152 | + if (results[index] != static_cast<cl_int>(check)) |
| 153 | + { |
| 154 | + log_error("Result mismatch at index %zu! Got %d, Wanted %zu\n", |
| 155 | + index, results[index], check); |
| 156 | + return TEST_FAIL; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return TEST_PASS; |
| 162 | +} |
0 commit comments