Description

The __constant or constant address space name is used to describe read-only variables that are accessible globally. They may be declared in program scope or in the outermost kernel scope or inside functions with a static or extern storage class specifier. Such variables can be accessed by all work-items or by different kernels during the program execution.

Each argument to a kernel that is a pointer to the __constant address space is counted separately towards the maximum number of such arguments, defined as the value of the CL_DEVICE_MAX_CONSTANT_ARGS device query.

It is illegal to write to a variable in the constant address space and will result in a compilation error.

Example:

constant int a = 3; // int allocated in the constant address space
kernel void k1(global int *buf)
{
    buf[a] = ...;   // OK. All work items access element with index 3.
}
kernel void k2(global int *buf)
{
    *buf = a;       // OK. All work items store value 3.
    a = 42;         // Error. a is in constant memory.
}

Implementations are not required to aggregate these declarations into the fewest number of constant arguments. This behavior is implementation-defined.

Thus portable code must conservatively assume that each variable declared inside a function or in program scope allocated in the __constant address space counts as a separate constant argument.

See Also

Document Notes

For more information, see the OpenCL C Specification

This page is extracted from the OpenCL C Specification. Fixes and changes should be made to the Specification, not directly.

Copyright 2014-2023 The Khronos Group Inc.

SPDX-License-Identifier: CC-BY-4.0