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 opencl_global_device,
43 opencl_global_host,
44
45 // CUDA specific address spaces.
46 cuda_device,
47 cuda_constant,
48 cuda_shared,
49
50 // SYCL specific address spaces.
51 sycl_global,
52 sycl_global_device,
53 sycl_global_host,
54 sycl_local,
55 sycl_private,
56
57 // Pointer size and extension address spaces.
58 ptr32_sptr,
59 ptr32_uptr,
60 ptr64,
61
62 // HLSL specific address spaces.
63 hlsl_groupshared,
64 hlsl_constant,
65 hlsl_private,
66 hlsl_device,
67 hlsl_input,
68 hlsl_output,
69 hlsl_push_constant,
70
71 // Wasm specific address spaces.
72 wasm_funcref,
73
74 // AMDGPU address spaces
75 amdgpu_barrier,
76
77 // This denotes the count of language-specific address spaces and also
78 // the offset added to the target-specific address spaces, which are usually
79 // specified by address space attributes __attribute__(address_space(n))).
80 FirstTargetAddressSpace
81};
82
83/// The type of a lookup table which maps from language-specific address spaces
84/// to target-specific ones.
85class LangASMap {
86 std::array<unsigned, (unsigned)LangAS::FirstTargetAddressSpace> Map{};
87
88public:
89 constexpr LangASMap() = default;
90
91 constexpr LangASMap(
92 std::initializer_list<std::pair<LangAS, unsigned>> Mappings) {
93 for (auto [LanguageAS, TargetAS] : Mappings)
94 Map[(unsigned)LanguageAS] = TargetAS;
95 }
96
97 constexpr unsigned operator[](LangAS AS) const { return Map[(unsigned)AS]; }
98};
99
100/// \return whether \p AS is a target-specific address space rather than a
101/// clang AST address space
102inline bool isTargetAddressSpace(LangAS AS) {
103 return (unsigned)AS >= (unsigned)LangAS::FirstTargetAddressSpace;
104}
105
106inline unsigned toTargetAddressSpace(LangAS AS) {
107 assert(isTargetAddressSpace(AS));
108 return (unsigned)AS - (unsigned)LangAS::FirstTargetAddressSpace;
109}
110
111inline LangAS getLangASFromTargetAS(unsigned TargetAS) {
112 return static_cast<LangAS>((TargetAS) +
113 (unsigned)LangAS::FirstTargetAddressSpace);
114}
115
116inline bool isPtrSizeAddressSpace(LangAS AS) {
117 return (AS == LangAS::ptr32_sptr || AS == LangAS::ptr32_uptr ||
118 AS == LangAS::ptr64);
119}
120
121} // namespace clang
122
123#endif // LLVM_CLANG_BASIC_ADDRESSSPACES_H
124