================
@@ -4055,6 +4055,135 @@ template<typename T> constexpr T *addressof(T &value) {
 }
 ```
 
+### `__addrspaceof`
+
+`__addrspaceof` returns a Clang address-space identifier for a type, object, or
+expression.
+
+This is useful for performance-sensitive code that needs a compile-time value
+to choose an address-space-specific operation, overload, or template
+specialization. For example, GPU code may select different loads for global,
+shared/local, or constant memory.
+
+**Syntax**:
+
+```c++
+int __addrspaceof(type-or-expression)
+```
+
+The parentheses are required. The operand is not evaluated. The result is an
+integer constant expression.
+Reference types are adjusted to their referenced type before the address space
+is queried.
+
+For a type operand, the operator returns its top-level explicit address space,
+or the default address space if it has none.
+
+For an unparenthesized id-expression or unparenthesized member access, the
+operator queries the directly named variable, structured binding, or data
+member. It first uses the top-level explicit address space of the entity's
+type. For a CUDA/HIP entity without a top-level explicit address space, it uses
+the storage address space specified by `__device__`, `__shared__`, or
+`__constant__`. Otherwise, it returns the default address space. An explicit
+`__device__` `const` global or static data member that is promoted to constant
+memory is reported as the CUDA or HIP constant address space.
+
+For other expression operands, the operator returns the top-level explicit
+address space of the expression's type, or the default address space if it has
+none. It does not follow pointers or look through expression wrappers to find
+an underlying object. Enumerators and non-type template parameters use this
+expression-type rule. As with `decltype`, extra parentheses can change a direct
+entity query into an expression query.
+
+```c++
+__device__ int *p;
+
+static_assert(__addrspaceof(p) == __ADDRSPACE_GLOBAL);
+static_assert(__addrspaceof(*p) == __ADDRSPACE_DEFAULT);
+static_assert(__addrspaceof((p)) == __ADDRSPACE_DEFAULT);
+```
+
+The first query reports where the pointer variable is stored. The second
+reports the address space of the pointed-to object type. The third treats `p`
+as an lvalue expression because of the extra parentheses.
+
+Language address spaces come from a language or Clang extension. Matching
+memory kinds in OpenCL, CUDA, HIP, SYCL, and HLSL get the same value. Clang
+predefines these macros for the common kinds and the HLSL-specific kinds:
+
+| Macro | Value | Typical use |
+| --- | ---: | --- |
+| `__ADDRSPACE_DEFAULT` | 0 | Unqualified type or expression |
----------------
jhuber6 wrote:

Yeah, I guess my questioning is mostly how deep we need to make this, since to 
me it seems like a version of `__builtin_amdgcn_is_shared` that needs to work 
constexpr. I think you could make it work with numerical address spaces if you 
use the target library information. Just trying to hedge if this is a general 
solution to a specific problem or not, but I don't know what the other clang 
maintainers would prioritize.

https://github.com/llvm/llvm-project/pull/210242
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to