From b9ca8cfc6398f42df316b03dd7b3fed5c1ecbfc6 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Mon, 17 Aug 2026 15:57:39 +0200 Subject: [PATCH 1/2] CanonicalizeBarriers: split reconverging barrier successors Parallel-region formation treats successors of a barrier block as entries of disjoint regions. Bounds-checked Julia kernels can instead branch around a variable-trip loop and reconverge at the next barrier, producing a malformed region and aborting work-group function generation. Detect successors that reach a common block before crossing another barrier and move their branch to a common post-barrier block. Successors ending at distinct barriers retain the existing conditional-barrier handling. Add a reduced SPIR-V regression that builds and executes with the loops, loopvec, and CBS handlers. Co-Authored-By: Claude Fable 5 --- lib/llvmopencl/CanonicalizeBarriers.cc | 46 ++++- tests/regression/CMakeLists.txt | 3 + .../regression/test_barrier_divergent_skip.c | 166 ++++++++++++++++++ .../test_barrier_divergent_skip.spv | Bin 0 -> 1448 bytes 4 files changed, 208 insertions(+), 7 deletions(-) create mode 100644 tests/regression/test_barrier_divergent_skip.c create mode 100644 tests/regression/test_barrier_divergent_skip.spv diff --git a/lib/llvmopencl/CanonicalizeBarriers.cc b/lib/llvmopencl/CanonicalizeBarriers.cc index 9dc1dad192..1cf179c5e5 100644 --- a/lib/llvmopencl/CanonicalizeBarriers.cc +++ b/lib/llvmopencl/CanonicalizeBarriers.cc @@ -29,6 +29,9 @@ IGNORE_COMPILER_WARNING("-Wmaybe-uninitialized") #include POP_COMPILER_DIAGS IGNORE_COMPILER_WARNING("-Wunused-parameter") +#include +#include +#include #include #include #include @@ -60,6 +63,34 @@ static bool processFunction(Function &F, WorkitemHandlerType Handler); using InstructionSet = std::set; +// Check whether two successors can reach a common block without crossing a +// barrier. A shared barrier endpoint counts; other paths may lead elsewhere. +// +// Kernel::createParallelRegionBefore can collect both successors in one region +// but designates only one as its entry. Moving the branch into a common +// post-barrier block prevents outside edges to non-entry blocks. +static bool successorsReconverge(llvm::Instruction *T) { + llvm::SmallPtrSet Reached; + for (unsigned Succ = 0, Num = T->getNumSuccessors(); Succ < Num; ++Succ) { + llvm::SmallPtrSet Visited; + llvm::SmallVector WorkList{T->getSuccessor(Succ)}; + while (!WorkList.empty()) { + llvm::BasicBlock *BB = WorkList.pop_back_val(); + if (!Visited.insert(BB).second) + continue; + // Include barrier blocks in the search, but do not cross them. + if (Reached.count(BB)) + return true; + if (Barrier::hasBarrier(BB)) + continue; + for (llvm::BasicBlock *SuccBB : successors(BB)) + WorkList.push_back(SuccBB); + } + Reached.insert(Visited.begin(), Visited.end()); + } + return false; +} + static bool canonicalizeBarriers(Function &F, WorkitemHandlerType Handler) { bool changed = false; @@ -145,15 +176,16 @@ static bool processFunction(Function &F, WorkitemHandlerType Handler) { // Split post barrier first cause it does not make the barrier // to belong to another basic block. Instruction *t = b->getTerminator(); - // if ((t->getNumSuccessors() > 1) || - // (t->getPrevNode() != *i)) { - // Change: barriers with several successors are all right - // they just start several parallel regions. Simplifies - // loop handling. + // Keep separate entries for disjoint conditional-barrier regions. If the + // successor searches overlap, move the branch into a common entry block. + // CBS requires this split for every multi-successor barrier. + + const bool MustSplitSuccessors = + t->getNumSuccessors() > 1 && + (Handler == WorkitemHandlerType::CBS || successorsReconverge(t)); const bool HasNonBranchInstructionsAfterBarrier = - t->getPrevNode() != *i || - (Handler == WorkitemHandlerType::CBS && t->getNumSuccessors() > 1); + t->getPrevNode() != *i || MustSplitSuccessors; if (HasNonBranchInstructionsAfterBarrier) { BasicBlock *new_b = SplitBlock(b, (*i)->getNextNode()); diff --git a/tests/regression/CMakeLists.txt b/tests/regression/CMakeLists.txt index a17a518440..657c7db67d 100644 --- a/tests/regression/CMakeLists.txt +++ b/tests/regression/CMakeLists.txt @@ -35,6 +35,7 @@ set(C_PROGRAMS_TO_BUILD test_assign_loop_variable_to_privvar_makes_it_local test_rematerialized_alloca_load_with_outside_pr_users test_peeled_wi_global_id test_multi_exit_barrier_region + test_barrier_divergent_skip test_divergent_implicit_barrier_entry test_uniform_implicit_barrier_entry test_localmem_load_across_barrier @@ -203,6 +204,8 @@ add_test_pocl(NAME "regression/multi_exit_barrier_region" WORKITEM_HANDLER "loop add_test_pocl(NAME "regression/spirv_intel_subgroups" WORKITEM_HANDLER "loops;loopvec;cbs;" COMMAND "test_spirv_intel_subgroups" LABELS ${SPIRV_LABELS}) +add_test_pocl(NAME "regression/barrier_divergent_skip" WORKITEM_HANDLER "loops;loopvec;cbs;" COMMAND "test_barrier_divergent_skip" LABELS ${SPIRV_LABELS}) + add_test_pocl(NAME "regression/divergent_implicit_barrier_entry" WORKITEM_HANDLER "loops;loopvec;cbs;" COMMAND "test_divergent_implicit_barrier_entry") add_test_pocl(NAME "regression/uniform_implicit_barrier_entry" WORKITEM_HANDLER "loops;loopvec;cbs;" COMMAND "test_uniform_implicit_barrier_entry") diff --git a/tests/regression/test_barrier_divergent_skip.c b/tests/regression/test_barrier_divergent_skip.c new file mode 100644 index 0000000000..69b1b29fd7 --- /dev/null +++ b/tests/regression/test_barrier_divergent_skip.c @@ -0,0 +1,166 @@ +// Copyright (c) 2026 PoCL developers +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +/* + Reduced from a bounds-checked KernelAbstractions.jl radix-sort kernel. One + arm of a branch after a barrier skips a variable-trip loop, then reconverges + with the other arm at the next barrier: + + barrier(); + br i1 %skip, label %join, label %loop + ... + join: ; reconvergence point + barrier(); + + Treating the successors as disjoint region entries produced a malformed + region ending at the loop latch. Building the work-group function then + aborted in ParallelRegion::chainAfter. + + The crash happened when building the work-group function; completing the + build and a launch with correct results is the test. The launch takes the + skip path in all work-items so the barrier behavior is defined. +*/ + +#include "pocl_opencl.h" + +#include +#include +#ifndef _WIN32 +#include +#endif + +#define CHECK_ERROR(err) \ + if (err != CL_SUCCESS) { \ + printf("OpenCL Error %d at %s:%d\n", err, __FILE__, __LINE__); \ + return 1; \ + } + +int main(int argc, char **argv) { + cl_uint platform_index = argc > 1 ? (cl_uint)atoi(argv[1]) : 0; + cl_int err; + +#ifndef _WIN32 + // Fail quickly instead of tripping the much larger ctest timeout in case + // a regressed build hangs. + alarm(300); +#endif + + cl_uint num_platforms; + CHECK_ERROR(clGetPlatformIDs(0, NULL, &num_platforms)); + if (platform_index >= num_platforms) { + printf("Platform index %u out of range\n", platform_index); + return 1; + } + cl_platform_id *platforms = malloc(sizeof(cl_platform_id) * num_platforms); + CHECK_ERROR(clGetPlatformIDs(num_platforms, platforms, NULL)); + cl_platform_id platform = platforms[platform_index]; + free(platforms); + + cl_device_id device; + CHECK_ERROR(clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL)); + + if (!poclu_device_supports_il(device, "SPIR-V_1.0")) { + printf("SKIP: The test requires support for SPIR-V 1.0\n"); + return 77; + } + + cl_context context = clCreateContext(NULL, 1, &device, NULL, NULL, &err); + CHECK_ERROR(err); + cl_queue_properties props[] = {0}; + cl_command_queue queue = + clCreateCommandQueueWithProperties(context, device, props, &err); + CHECK_ERROR(err); + + FILE *f = fopen(SRCDIR "/test_barrier_divergent_skip.spv", "rb"); + if (!f) { + printf("Failed to open test_barrier_divergent_skip.spv\n"); + return 1; + } + fseek(f, 0, SEEK_END); + size_t size = ftell(f); + fseek(f, 0, SEEK_SET); + unsigned char *binary = malloc(size); + if (fread(binary, 1, size, f) != size) { + printf("Failed to read SPIR-V\n"); + free(binary); + fclose(f); + return 1; + } + fclose(f); + + cl_program program = clCreateProgramWithIL(context, binary, size, &err); + CHECK_ERROR(err); + err = clBuildProgram(program, 1, &device, NULL, NULL, NULL); + if (err != CL_SUCCESS) { + size_t log_size; + clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, + &log_size); + char *log = malloc(log_size); + clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, log, + NULL); + printf("Build error:\n%s\n", log); + return 1; + } + + // Building the work-group function is what used to crash; force it even + // when the driver would otherwise defer compilation to launch time. + size_t binary_size; + CHECK_ERROR(clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, + sizeof(binary_size), &binary_size, NULL)); + + cl_kernel kernel = clCreateKernel(program, "vartrip", &err); + CHECK_ERROR(err); + + // All work-items enter the barrier and take the loop-skipping arm, so the + // barrier behavior is defined and every work-item stores 42. + const size_t local_size = 64, global_size = 128; + const cl_long opaque = 0; + const cl_uchar enter = 1, skip = 1, exitl = 1; + cl_int result[128]; + cl_mem out = + clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(result), NULL, &err); + CHECK_ERROR(err); + CHECK_ERROR(clSetKernelArg(kernel, 0, sizeof(out), &out)); + CHECK_ERROR(clSetKernelArg(kernel, 1, sizeof(opaque), &opaque)); + CHECK_ERROR(clSetKernelArg(kernel, 2, sizeof(enter), &enter)); + CHECK_ERROR(clSetKernelArg(kernel, 3, sizeof(skip), &skip)); + CHECK_ERROR(clSetKernelArg(kernel, 4, sizeof(exitl), &exitl)); + CHECK_ERROR(clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_size, + &local_size, 0, NULL, NULL)); + CHECK_ERROR(clEnqueueReadBuffer(queue, out, CL_TRUE, 0, sizeof(result), + result, 0, NULL, NULL)); + + for (size_t i = 0; i < global_size; ++i) { + if (result[i] != 42) { + printf("FAIL at %zu: got %d, expected 42\n", i, result[i]); + return 1; + } + } + + clReleaseMemObject(out); + clReleaseKernel(kernel); + clReleaseProgram(program); + clReleaseCommandQueue(queue); + clReleaseContext(context); + free(binary); + + printf("OK\n"); + return 0; +} diff --git a/tests/regression/test_barrier_divergent_skip.spv b/tests/regression/test_barrier_divergent_skip.spv new file mode 100644 index 0000000000000000000000000000000000000000..437dc6f5553cced26f0ed6cb307be605a47cbace GIT binary patch literal 1448 zcmZXTTTc@~7={P7TToPvo=+`RP>WTm2;3MG6S*Pb-kYY_NwZjX%XVq}TmA@tl8Kra zeV>^f-RNv4-}laUK5R7Gnlom`TrqX?%=oixW=%~jG%jhqqzj@8dYutH8r$^kVP{ed zjO-(68+R8aM}Jdy+RuwT8Ji`^!S{6|ec{B?B}VW4?r}fQ6Puq1&zTGE?L~KJi{5aQ z9rs7QWN>06t0~rBzp;ER3TNkqlP$rI`)B9Y>n;o1w6M7-wkx8Ok4mV-tD^Q(Qj8pq z&6?J5MnSc_-g0Cd`= z{I-XGF#GaLr>(5eZ|D+-6W&@LmTx}Jh7fhHY9rapru?(h@N6G=UKF3;hA_KL?cg#`_z$kFFLT($xkb{t~t$^$<52lkXLI@r-W4snCdMe#ugKkPP#nCK9b{f5T} z`wdTgc02Iwb>P|QZ_0{Y-Y;XbXT+z!@AQUW_6^_2ekgPNvFq$J_VCz~*F9Zo#2#JZ zvCHf`y8IVERVzI9uceKD=7&9f`CjUwgWXqEL`_Xy>f&E%=o0sf?2)bOB17l1-l&`1 z=e?y%E?{iQ0pIYv(}!w~@S&cE;zK Date: Tue, 13 Oct 2026 10:07:35 +0200 Subject: [PATCH 2/2] tests: cover barrier successors reconverging around an early exit A second shape fixed by splitting reconverging barrier successors: a guarded section between barriers whose early exit, such as the never-taken bounds-check failure path of a Julia kernel, leaves the kernel without reaching the next barrier. Padding work-items branch from the barrier straight to the end of the section, so both barrier successors lie in the same parallel region. Because the early exit makes that region multi-exit, IsolateRegions does not insert a dedicated post-barrier entry. Region formation then took the first such successor as the region entry. Either padding work-items entered the work-item loop past its per-work-item control and executed the guarded code, or a bogus region with a two-successor exit was formed and WorkitemLoops::createLoopAround asserted. Both kernels in the test are written in OpenCL C, and both failed with the loops and loopvec handlers before the fix. Co-Authored-By: Claude Opus 5.5 --- tests/regression/CMakeLists.txt | 3 + tests/regression/test_barrier_early_exit.c | 211 +++++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 tests/regression/test_barrier_early_exit.c diff --git a/tests/regression/CMakeLists.txt b/tests/regression/CMakeLists.txt index 657c7db67d..89d8ba2c99 100644 --- a/tests/regression/CMakeLists.txt +++ b/tests/regression/CMakeLists.txt @@ -36,6 +36,7 @@ set(C_PROGRAMS_TO_BUILD test_assign_loop_variable_to_privvar_makes_it_local test_peeled_wi_global_id test_multi_exit_barrier_region test_barrier_divergent_skip + test_barrier_early_exit test_divergent_implicit_barrier_entry test_uniform_implicit_barrier_entry test_localmem_load_across_barrier @@ -206,6 +207,8 @@ add_test_pocl(NAME "regression/spirv_intel_subgroups" WORKITEM_HANDLER "loops;lo add_test_pocl(NAME "regression/barrier_divergent_skip" WORKITEM_HANDLER "loops;loopvec;cbs;" COMMAND "test_barrier_divergent_skip" LABELS ${SPIRV_LABELS}) +add_test_pocl(NAME "regression/barrier_early_exit" WORKITEM_HANDLER "loops;loopvec;cbs;" COMMAND "test_barrier_early_exit") + add_test_pocl(NAME "regression/divergent_implicit_barrier_entry" WORKITEM_HANDLER "loops;loopvec;cbs;" COMMAND "test_divergent_implicit_barrier_entry") add_test_pocl(NAME "regression/uniform_implicit_barrier_entry" WORKITEM_HANDLER "loops;loopvec;cbs;" COMMAND "test_uniform_implicit_barrier_entry") diff --git a/tests/regression/test_barrier_early_exit.c b/tests/regression/test_barrier_early_exit.c new file mode 100644 index 0000000000..6b46065fb1 --- /dev/null +++ b/tests/regression/test_barrier_early_exit.c @@ -0,0 +1,211 @@ +// Copyright (c) 2026 PoCL developers +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +/* + Kernels in which a guarded section between barriers has an early exit that + leaves the kernel without reaching the remaining barriers, such as the + bounds-check failure path of a Julia kernel. Padding work-items skip the + guarded section: + + barrier(); + br i1 %pad, label %next, label %guarded + guarded: + br i1 %oob, label %exit, label %body + body: + ... + br label %next + next: + barrier(); + + The successors of the first barrier reconverge at %next, but the early exit + makes the region between the barriers multi-exit, so IsolateRegions does not + give it a single entry. Region formation then picked an arbitrary barrier + successor as the entry of the work-item loop: either the padding work-items + jumped into the loop past its per-work-item control and executed the guarded + code (first kernel), or a bogus region was formed whose exit had two + successors and WorkitemLoops aborted with an assertion (second kernel). + + The early exit is never taken at launch, so all work-items reach every + barrier. +*/ + +#include "pocl_opencl.h" + +#include +#include + +#define CHECK_ERROR(err) \ + do { \ + if ((err) != CL_SUCCESS) { \ + fprintf(stderr, "OpenCL error %d at %s:%d\n", (err), __FILE__, \ + __LINE__); \ + goto error; \ + } \ + } while (0) + +static const char KernelSource[] = + "__kernel void skip_padding(__global int *out, int n, int len) {\n" + " int i = get_global_id(0);\n" + " bool pad = i >= n;\n" + " if (!pad) {\n" + " if (i >= len) goto error;\n" + " out[i] += 1;\n" + " }\n" + " barrier(CLK_GLOBAL_MEM_FENCE);\n" + " if (!pad) {\n" + " if (i >= len) goto error;\n" + " out[i] += 2;\n" + " }\n" + " barrier(CLK_GLOBAL_MEM_FENCE);\n" + " return;\n" + "error:\n" + " out[0] = -1;\n" + "}\n" + "\n" + "__kernel void skip_padding_to_exit(__global int *out, int n, int len) {\n" + " int i = get_global_id(0) + 1;\n" + " bool pad = i > n;\n" + " int idx = pad ? 0 : i;\n" + " if (pad) goto sync1;\n" + " if (i > len) goto error;\n" + " out[idx - 1] += 1;\n" + "sync1:\n" + " barrier(CLK_GLOBAL_MEM_FENCE);\n" + " if (pad) goto sync2;\n" + " if (i > len) goto error;\n" + " out[idx - 1] += 2;\n" + "sync2:\n" + " barrier(CLK_GLOBAL_MEM_FENCE);\n" + " goto sync3;\n" + "error:\n" + " out[0] = -1;\n" + " goto done;\n" + "sync3:\n" + " barrier(CLK_GLOBAL_MEM_FENCE);\n" + "done:\n" + " return;\n" + "}\n"; + +static const char *KernelNames[] = {"skip_padding", "skip_padding_to_exit"}; + +/* Two work-groups of 8, of which the last 3 work-items are padding. */ +#define GLOBAL_SIZE 16 +#define LOCAL_SIZE 8 +#define LOGICAL_SIZE 13 + +int main(void) { + const size_t global_size = GLOBAL_SIZE, local_size = LOCAL_SIZE; + const cl_int n = LOGICAL_SIZE, len = GLOBAL_SIZE; + cl_int output[GLOBAL_SIZE]; + cl_int err; + cl_uint num_platforms; + cl_platform_id *platforms = NULL; + cl_context context = NULL; + cl_command_queue queue = NULL; + cl_program program = NULL; + cl_kernel kernel = NULL; + cl_mem output_buffer = NULL; + int result = 1; + + err = clGetPlatformIDs(0, NULL, &num_platforms); + CHECK_ERROR(err); + if (num_platforms == 0) { + puts("SKIP: no OpenCL platforms"); + return 77; + } + platforms = malloc(num_platforms * sizeof(*platforms)); + if (platforms == NULL) + goto error; + err = clGetPlatformIDs(num_platforms, platforms, NULL); + CHECK_ERROR(err); + + cl_device_id device; + err = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_ALL, 1, &device, NULL); + CHECK_ERROR(err); + context = clCreateContext(NULL, 1, &device, NULL, NULL, &err); + CHECK_ERROR(err); + const cl_queue_properties properties[] = {0}; + queue = clCreateCommandQueueWithProperties(context, device, properties, &err); + CHECK_ERROR(err); + + const char *sources[] = {KernelSource}; + program = clCreateProgramWithSource(context, 1, sources, NULL, &err); + CHECK_ERROR(err); + err = clBuildProgram(program, 1, &device, NULL, NULL, NULL); + if (err != CL_SUCCESS) { + size_t log_size = 0; + clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, + &log_size); + char *log = malloc(log_size); + if (log != NULL) { + clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, + log, NULL); + fprintf(stderr, "Build error:\n%s\n", log); + free(log); + } + goto error; + } + output_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(output), + NULL, &err); + CHECK_ERROR(err); + + for (size_t k = 0; k < sizeof(KernelNames) / sizeof(*KernelNames); ++k) { + kernel = clCreateKernel(program, KernelNames[k], &err); + CHECK_ERROR(err); + + const cl_int zero = 0; + CHECK_ERROR(clEnqueueFillBuffer(queue, output_buffer, &zero, sizeof(zero), + 0, sizeof(output), 0, NULL, NULL)); + CHECK_ERROR( + clSetKernelArg(kernel, 0, sizeof(output_buffer), &output_buffer)); + CHECK_ERROR(clSetKernelArg(kernel, 1, sizeof(n), &n)); + CHECK_ERROR(clSetKernelArg(kernel, 2, sizeof(len), &len)); + CHECK_ERROR(clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_size, + &local_size, 0, NULL, NULL)); + CHECK_ERROR(clEnqueueReadBuffer(queue, output_buffer, CL_TRUE, 0, + sizeof(output), output, 0, NULL, NULL)); + + for (size_t i = 0; i < GLOBAL_SIZE; ++i) { + cl_int expected = i < LOGICAL_SIZE ? 3 : 0; + if (output[i] != expected) { + fprintf(stderr, "%s: output[%zu] = %d, expected %d\n", KernelNames[k], + i, output[i], expected); + goto error; + } + } + clReleaseKernel(kernel); + kernel = NULL; + } + puts("OK"); + result = 0; + +error: + free(platforms); + if (output_buffer != NULL) + clReleaseMemObject(output_buffer); + if (kernel != NULL) + clReleaseKernel(kernel); + if (program != NULL) + clReleaseProgram(program); + if (queue != NULL) + clReleaseCommandQueue(queue); + if (context != NULL) + clReleaseContext(context); + return result; +}