Address Space Qualifier.

__constant
constant

Description

The constant or constant address space name is used to describe variables allocated in global memory and which are accessed inside a kernel(s) as read-only variables. These read-only variables can be accessed by all (global) work-items of the kernel during its execution. Pointers to the constant address space are allowed as arguments to functions (including kernel functions) and for variables declared inside functions.

All string literal storage shall be in the __constant address space.

Note
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 CL_DEVICE_MAX_CONSTANT_ARGS are described in the table for clGetDeviceInfo.

Variables in the program scope must be declared in the constant address space. Variables in the outermost scope of kernel functions can be declared in the constant address space. These variables are required to be initialized and the values used to initialize these variables must be a compile time constant. Writing to such a variable results in a compile-time error.

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.

Notes

General information about address space qualifiers

OpenCL implements the following disjoint named address spaces: global, local, constant, and private. The address space qualifier may be used in variable declarations to specify the region of memory that is used to allocate the object. The C syntax for type qualifiers is extended in OpenCL to include an address space name as a valid type qualifier. If the type of an object is qualified by an address space name, the object is allocated in the specified address name; otherwise, the object is allocated in the generic address space.

The address space names without the prefix i.e. global, local, constant and private may be substituted for the corresponding address space names with the prefix.

The address space name for arguments to a function in a program, or local variables of a function is private. All function arguments shall be in the private address space. The address space for a variable at program scope or a static variable inside a function can either be global or constant, but defaults to __global if not specified.

OpenCL 2.0 adds support for an unnamed generic address space. Pointers that are declared without pointing to a named address space point to the generic address space. Before referring to the region pointed to, the pointer must be associated with a named address space. Functions may be written with arguments and return values that point to the generic address space.

kernel function arguments declared to be a pointer or an array of a type must point to one of the named address spaces global, local or __constant.

The named address spaces are a subset of the generic address space except for the constant address space.

A pointer to address space A can only be assigned to a pointer to the same address space A or a pointer to the generic address space. Casting a pointer to address space A to a pointer to address space B is illegal if A and B are named address spaces and A is not the same as B.

The global, constant, local, private, generic, global, constant, local, and private names are reserved for use as address space qualifiers and shall not be used otherwise. The generic and generic names are reserved for future use.

The size of pointers to different address spaces may differ. It is not correct to assume that, for example, sizeof(global int *) always equals sizeof(local int *).

Example

General qualifier examples follow:

// declares a pointer p in the private address space that
// points to an object in address space global
global int *p;

void foo (...)
{
    // declares an array of 4 floats in the private address space
    float x[4];
    ...
}

Example:

private int f() { ... } // should generate an error
local int *f() { ... } // allowed
local int * private f() { ... }; // should generate an error.

Also see

Specification