From 7757d0b472526c9378ea3da079b21321ec6cef75 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 27 Oct 2026 01:00:16 +0100 Subject: [PATCH 1/2] Skip host kernel coordinate divisions for ranges starting in the first row clRunHostKernelPOCL used two 64-bit divisions to compute the range's first work-group coordinates. Each takes tens of cycles on Intel CPUs before Ice Lake. Ranges starting in the grid's first row, as launches on one thread do, now need neither division. A trivial KernelAbstractions launch on a Xeon E3-1245 v6 takes 518 instead of 566 cycles in a local build. --- lib/CL/devices/common_utils.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/CL/devices/common_utils.c b/lib/CL/devices/common_utils.c index 4aaf52168c..71a2d59fa2 100644 --- a/lib/CL/devices/common_utils.c +++ b/lib/CL/devices/common_utils.c @@ -386,10 +386,21 @@ pocl_cpu_run_host_kernel (cl_host_kernel_pocl hk, enter_kernel_fp_mode (hk->fp_mode, &caller_fp_env); pocl_workgroup_func wg = (pocl_workgroup_func)hk->wg; - size_t x = first_group % pc.num_groups[0]; - size_t yz = first_group / pc.num_groups[0]; - size_t y = yz % pc.num_groups[1]; - size_t z = yz / pc.num_groups[1]; + /* Without dividing when the range starts in the first row or plane, as + small launches do: a 64-bit division takes up to ~90 cycles, against a + few hundred for a whole launch of a trivial kernel. */ + size_t x = first_group, y = 0, z = 0; + if (x >= pc.num_groups[0]) + { + size_t yz = x / pc.num_groups[0]; + x -= yz * pc.num_groups[0]; + y = yz; + if (yz >= pc.num_groups[1]) + { + z = yz / pc.num_groups[1]; + y = yz - z * pc.num_groups[1]; + } + } for (size_t i = 0; i < group_count; ++i) { wg ((uint8_t *)args, (uint8_t *)&pc, x, y, z); From 0caf82517fd11f84bd4aff4a07201085106fb492 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 27 Oct 2026 01:49:36 +0100 Subject: [PATCH 2/2] Skip the second MXCSR read when host kernel FP modes already match When the caller's floating-point environment already matches the kernel's, the kernel can only raise further exception flags, as a function call would. Leaving them avoids a second MXCSR read (STMXCSR) and a store-forwarding stall: a trivial KernelAbstractions launch on a Xeon E3-1245 v6 takes 498 instead of 518 cycles in a local build. The documented contract becomes the C function-call one on x86 and AArch64: the caller's control modes and previously raised flags are preserved, and which further flags the call raises is unspecified. AArch64 still restores FPCR and FPSR. --- include/CL/cl_ext_pocl.h | 6 ++++-- lib/CL/devices/common_utils.c | 26 ++++++++++++++++-------- tests/runtime/test_cl_pocl_host_kernel.c | 14 ++++++++++--- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/include/CL/cl_ext_pocl.h b/include/CL/cl_ext_pocl.h index e10db26051..c27bf656ed 100644 --- a/include/CL/cl_ext_pocl.h +++ b/include/CL/cl_ext_pocl.h @@ -499,8 +499,10 @@ extern CL_API_ENTRY cl_int CL_API_CALL clSetKernelArgDevicePointerEXT ( * CL_HOST_KERNEL_SCRATCH_ALIGNMENT_POCL that no concurrent call uses; it * holds the __local memory and the printf buffer. On x86 and AArch64, the * kernel runs in its own floating-point environment (round to nearest, - * denormal handling per the build options, all exceptions masked), and the - * caller's (including its exception flags) is restored on return. + * denormal handling per the build options, all exceptions masked). The + * caller's control modes and the exception flags it had raised are preserved; + * as with a function call, the call may raise further flags, which ones being + * unspecified. * * Work-groups may be run in any order, from any number of threads and * concurrently, so they must not wait on each other. Only kernels without diff --git a/lib/CL/devices/common_utils.c b/lib/CL/devices/common_utils.c index 71a2d59fa2..487f3eb535 100644 --- a/lib/CL/devices/common_utils.c +++ b/lib/CL/devices/common_utils.c @@ -217,7 +217,8 @@ typedef struct } host_fp_env; #endif -static inline void +/* Returns whether it changed the caller's environment. */ +static inline int enter_kernel_fp_mode (unsigned ftz, host_fp_env *caller) { #if defined(HOST_KERNEL_FP_X86) @@ -227,8 +228,10 @@ enter_kernel_fp_mode (unsigned ftz, host_fp_env *caller) & ~(_MM_FLUSH_ZERO_MASK | _MM_DENORMALS_ZERO_MASK | _MM_ROUND_MASK)) | _MM_MASK_MASK | _MM_ROUND_NEAREST | (ftz ? (_MM_FLUSH_ZERO_ON | _MM_DENORMALS_ZERO_ON) : 0); - if (kernel != caller->mxcsr) - _mm_setcsr (kernel); + if (kernel == caller->mxcsr) + return 0; + _mm_setcsr (kernel); + return 1; #elif defined(HOST_KERNEL_FP_AARCH64) __asm__ volatile ("mrs %0, fpcr" : "=r"(caller->fpcr)); __asm__ volatile ("mrs %0, fpsr" : "=r"(caller->fpsr)); @@ -238,20 +241,26 @@ enter_kernel_fp_mode (unsigned ftz, host_fp_env *caller) | (ftz ? (FPCR_FZ | FPCR_FZ16) : 0); if (kernel != caller->fpcr) __asm__ volatile ("msr fpcr, %0" : : "r"(kernel)); + return 1; #else (void)ftz; (void)caller; + return 0; #endif } -/* Restores the caller's environment, including its exception flags. */ +/* Restores the caller's environment, including its exception flags, if + `enter_kernel_fp_mode` changed it. Otherwise the kernel ran in the caller's + environment and can only have raised further flags, which the contract + allows: leaving them saves reading MXCSR again (a few nanoseconds). */ static inline void -leave_kernel_fp_mode (const host_fp_env *caller) +leave_kernel_fp_mode (const host_fp_env *caller, int changed) { #if defined(HOST_KERNEL_FP_X86) - if (_mm_getcsr () != caller->mxcsr) + if (changed) _mm_setcsr (caller->mxcsr); #elif defined(HOST_KERNEL_FP_AARCH64) + (void)changed; uint64_t fpcr, fpsr; __asm__ volatile ("mrs %0, fpcr" : "=r"(fpcr)); __asm__ volatile ("mrs %0, fpsr" : "=r"(fpsr)); @@ -261,6 +270,7 @@ leave_kernel_fp_mode (const host_fp_env *caller) __asm__ volatile ("msr fpsr, %0" : : "r"(caller->fpsr)); #else (void)caller; + (void)changed; #endif } @@ -383,7 +393,7 @@ pocl_cpu_run_host_kernel (cl_host_kernel_pocl hk, pc.printf_buffer_position = &position; host_fp_env caller_fp_env; - enter_kernel_fp_mode (hk->fp_mode, &caller_fp_env); + int fp_changed = enter_kernel_fp_mode (hk->fp_mode, &caller_fp_env); pocl_workgroup_func wg = (pocl_workgroup_func)hk->wg; /* Without dividing when the range starts in the first row or plane, as @@ -422,7 +432,7 @@ pocl_cpu_run_host_kernel (cl_host_kernel_pocl hk, } } - leave_kernel_fp_mode (&caller_fp_env); + leave_kernel_fp_mode (&caller_fp_env, fp_changed); return pc.execution_failed ? CL_OUT_OF_RESOURCES : CL_SUCCESS; } diff --git a/tests/runtime/test_cl_pocl_host_kernel.c b/tests/runtime/test_cl_pocl_host_kernel.c index 7859cd60de..72af219112 100644 --- a/tests/runtime/test_cl_pocl_host_kernel.c +++ b/tests/runtime/test_cl_pocl_host_kernel.c @@ -250,7 +250,7 @@ test_group_sums (cl_kernel kernel, cl_device_id device) } /* The kernel runs with round-to-nearest, and the caller's rounding mode and - exception flags survive the call. */ + the exception flags it had raised survive the call. */ static int test_fp_env (cl_kernel kernel, cl_device_id device) { @@ -272,7 +272,6 @@ test_fp_env (cl_kernel kernel, cl_device_id device) err = run_hk (hk, args, &num_groups, NULL, 0, 1, scratch); int rounding = fegetround (); int divbyzero = fetestexcept (FE_DIVBYZERO); - int inexact = fetestexcept (FE_INEXACT); fesetround (FE_TONEAREST); feclearexcept (FE_ALL_EXCEPT); CHECK_CL_ERROR (err); @@ -282,7 +281,16 @@ test_fp_env (cl_kernel kernel, cl_device_id device) TEST_ASSERT (out == nearest); TEST_ASSERT (rounding == FE_UPWARD); TEST_ASSERT (divbyzero); - TEST_ASSERT (!inexact); + + /* Also when the caller's environment is the kernel's. */ + feraiseexcept (FE_DIVBYZERO); + err = run_hk (hk, args, &num_groups, NULL, 0, 1, scratch); + rounding = fegetround (); + divbyzero = fetestexcept (FE_DIVBYZERO); + feclearexcept (FE_ALL_EXCEPT); + CHECK_CL_ERROR (err); + TEST_ASSERT (rounding == FE_TONEAREST); + TEST_ASSERT (divbyzero); #ifdef __GLIBC__ /* Exceptions the caller unmasked don't trap in the kernel. */