1//===- AddressSpaces.h - Language-specific address spaces -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// Provides definitions for the various language-specific address
11/// spaces.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_BASIC_ADDRESSSPACES_H
16#define LLVM_CLANG_BASIC_ADDRESSSPACES_H
17
18#include <array>
19#include <cassert>
20#include <initializer_list>
21#include <utility>
22
23namespace clang {
24
25/// Defines the address space values used by the address space qualifier
26/// of QualType.
27///
28enum class LangAS : unsigned {
29 // The default value 0 is the value used in QualType for the situation
30 // where there is no address space qualifier.
31 Default = 0,
32
33 // OpenCL specific address spaces.
34 // In OpenCL each l-value must have certain non-default address space, each
35 // r-value must have no address space (i.e. the default address space). The
36 // pointee of a pointer must have non-default address space.
37 opencl_global,
38 opencl_local,
39 opencl_constant,
40 opencl_private,
41 opencl_generic,
42 // TODO: Remove opencl_global_device and opencl_global_host after
43 // corresponding attributes are deprecated for the required time.
44 // https://discourse.llvm.org/t/rfc-remove-opencl-global-device-and-opencl-global-host-address-space-attributes/90677
45 opencl_global_device,
46 opencl_global_host,
47
48 // CUDA specific address spaces.
49 cuda_device,
50 cuda_constant,
51 cuda_shared,
52
53 // SYCL specific address spaces.
54 sycl_global,
55 // TODO: Remove sycl_global_device and sycl_global_host after corresponding
56 // attributes are deprecated for the required time.
57 // https://discourse.llvm.org/t/rfc-remove-opencl-global-device-and-opencl-global-host-address-space-attributes/90677
58 sycl_global_device,
59 sycl_global_host,
60 sycl_local,
61 sycl_private,
62 sycl_generic,
63 sycl_constant,
64
65 // Pointer size and extension address spaces.
66 ptr32_sptr,
67 ptr32_uptr,
68 ptr64,
69
70 // HLSL specific address spaces.
71 hlsl_groupshared,
72 hlsl_constant,
73 hlsl_private,
74 hlsl_device,
75 hlsl_input,
76 hlsl_output,
77 hlsl_push_constant,
78
79 // Wasm specific address spaces.
80 wasm_funcref,
81
82 // AMDGPU address spaces
83 amdgpu_barrier,
84
85 // This denotes the count of language-specific address spaces and also
86 // the offset added to the target-specific address spaces, which are usually
87 // specified by address space attributes __attribute__(address_space(n))).
88 FirstTargetAddressSpace
89};
90
91/// The type of a lookup table which maps from language-specific address spaces
92/// to target-specific ones.
93class LangASMap {
94 std::array<unsigned, (unsigned)LangAS::FirstTargetAddressSpace> Map{};
95
96public:
97 constexpr LangASMap() = default;
98
99 constexpr LangASMap(
100 std::initializer_list<std::pair<LangAS, unsigned>> Mappings) {
101 for (auto [LanguageAS, TargetAS] : Mappings)
102 Map[(unsigned)LanguageAS] = TargetAS;
103 }
104
105 constexpr unsigned operator[](LangAS AS) const { return Map[(unsigned)AS]; }
106};
107
108/// \return whether \p AS is a target-specific address space rather than a
109/// clang AST address space
110inline bool isTargetAddressSpace(LangAS AS) {
111 return (unsigned)AS >= (unsigned)LangAS::FirstTargetAddressSpace;
112}
113
114inline unsigned toTargetAddressSpace(LangAS AS) {
115 assert(isTargetAddressSpace(AS));
116 return (unsigned)AS - (unsigned)LangAS::FirstTargetAddressSpace;
117}
118
119inline LangAS getLangASFromTargetAS(unsigned TargetAS) {
120 return static_cast<LangAS>((TargetAS) +
121 (unsigned)LangAS::FirstTargetAddressSpace);
122}
123
124inline bool isPtrSizeAddressSpace(LangAS AS) {
125 return (AS == LangAS::ptr32_sptr || AS == LangAS::ptr32_uptr ||
126 AS == LangAS::ptr64);
127}
128
129} // namespace clang
130
131#endif // LLVM_CLANG_BASIC_ADDRESSSPACES_H
132