Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/CL/cl_ext_pocl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 33 additions & 12 deletions lib/CL/devices/common_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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
}

Expand Down Expand Up @@ -383,13 +393,24 @@ 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;
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);
Expand All @@ -411,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;
}
Expand Down
14 changes: 11 additions & 3 deletions tests/runtime/test_cl_pocl_host_kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
Expand All @@ -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. */
Expand Down
Loading