Description

The following rules apply when using pointers that point to the generic address space:

  • A pointer that points to the global, local or private address space can be implicitly converted to a pointer to the unnamed generic address space but not vice-versa.

  • Pointer casts can be used to cast a pointer that points to the global, local or private space to the unnamed generic address space and vice-versa.

  • A pointer that points to the constant address space cannot be cast or implicitly converted to the generic address space.

A few examples follow.

This is the canonical example. In this example, function foo is declared with an argument that is a pointer with no address space qualifier.

void foo(int *a)
{
    *a = *a + 2;
}

kernel void k1(local int *a)
{
    ...
    foo(a);
    ...
}

kernel void k2(global int *a)
{
    ...
    foo(a);
    ...
}

In the example below, var is a pointer to the unnamed generic address space. A pointer to the global or local address space may be assigned to var depending on the result of a conditional expression.

kernel void bar(global int *g, local int *l)
{
    int *var;

    if (is_even(get_global_id(0))
        var = g;
    else
        var = l;
    *var = 42;
    ...
}

In the example below, the same pointer to the unnamed generic address space is used to point to objects allocated in different named address spaces. A pointer to the unnamed generic address space may point to objects in the global, local, and private address spaces, but it is not legal for a pointer to the unnamed generic address to point to an object in the constant address space.

int *ptr;
global int g;
ptr = &g; // legal

local int l;
ptr = &l; // legal

private int p;
ptr = &p; // legal

constant int c;
ptr = &c; // illegal

In the example below, pointers to named address spaces are assigned to a pointer to the unnamed generic address space. It is legal to assign a pointer to the global, local, and private address spaces to a pointer to the unnamed generic address space without an explicit cast. It is not legal to assign a pointer to the constant address space to a pointer to the unnamed generic address space. It is also not legal to assign a pointer to the unnamed generic address space to a pointer to a named address space without a cast.

global int *gp;
local int *lp;
private int *pp;
constant int *cp;

int *p;
p = gp; // legal
p = lp; // legal
p = pp; // legal
p = cp; // illegal

// it is illegal to convert from a generic pointer
// to an explicit address space pointer without a cast:
gp = p; // compile-time error
lp = p; // compile-time error
pp = p; // compile-time error
cp = p; // compile-time error

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 (c) 2014-2020 Khronos Group. This work is licensed under a Creative Commons Attribution 4.0 International License.