clang 23.0.0git
OpenCLOptions.h
Go to the documentation of this file.
1//===--- OpenCLOptions.h ----------------------------------------*- 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/// Defines the clang::OpenCLOptions class.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_BASIC_OPENCLOPTIONS_H
15#define LLVM_CLANG_BASIC_OPENCLOPTIONS_H
16
18#include "llvm/ADT/StringMap.h"
19
20namespace clang {
21
23class TargetInfo;
24
25namespace {
26// This enum maps OpenCL version(s) into value. These values are used as
27// a mask to indicate in which OpenCL version(s) extension is a core or
28// optional core feature.
29enum OpenCLVersionID : unsigned int {
30 OCL_C_10 = 0x1,
31 OCL_C_11 = 0x2,
32 OCL_C_12 = 0x4,
33 OCL_C_20 = 0x8,
34 OCL_C_30 = 0x10,
35 OCL_C_31 = 0x20,
36 OCL_C_ALL = 0x3f,
37 OCL_C_11P = OCL_C_ALL ^ OCL_C_10, // OpenCL C 1.1+
38 OCL_C_12P = OCL_C_ALL ^ (OCL_C_10 | OCL_C_11), // OpenCL C 1.2+
39};
40
41static inline OpenCLVersionID encodeOpenCLVersion(unsigned OpenCLVersion) {
42 switch (OpenCLVersion) {
43 default:
44 llvm_unreachable("Unknown OpenCL version code");
45 case 100:
46 return OCL_C_10;
47 case 110:
48 return OCL_C_11;
49 case 120:
50 return OCL_C_12;
51 case 200:
52 return OCL_C_20;
53 case 300:
54 return OCL_C_30;
55 case 310:
56 return OCL_C_31;
57 }
58}
59
60// Check if OpenCL C version is contained in a given encoded OpenCL C version
61// mask.
62static inline bool isOpenCLVersionContainedInMask(const LangOptions &LO,
63 unsigned Mask) {
64 auto CLVer = LO.getOpenCLCompatibleVersion();
65 OpenCLVersionID Code = encodeOpenCLVersion(CLVer);
66 return Mask & Code;
67}
68
69} // end anonymous namespace
70
71/// OpenCL supported extensions and optional core features
73
74public:
75 // OpenCL C v1.2 s6.5 - All program scope variables must be declared in the
76 // __constant address space.
77 // OpenCL C v2.0 s6.5.1 - Variables defined at program scope and static
78 // variables inside a function can also be declared in the global
79 // address space.
80 // OpenCL C v3.0 s6.7.1 - Variables at program scope or static or extern
81 // variables inside functions can be declared in global address space if
82 // the __opencl_c_program_scope_global_variables feature is supported
83 // C++ for OpenCL inherits rule from OpenCL C v2.0.
85 return Opts.getOpenCLCompatibleVersion() == 200 ||
86 (Opts.getOpenCLCompatibleVersion() == 300 &&
87 isSupported("__opencl_c_program_scope_global_variables", Opts));
88 }
89
91 // Does this option have pragma.
92 bool WithPragma = false;
93
94 // Option starts to be available in this OpenCL version
95 unsigned Avail = 100U;
96
97 // Option becomes core feature in this OpenCL versions
98 unsigned Core = 0U;
99
100 // Option becomes optional core feature in this OpenCL versions
101 unsigned Opt = 0U;
102
103 // Is this option supported
104 bool Supported = false;
105
106 // Is this option enabled
107 bool Enabled = false;
108
109 OpenCLOptionInfo() = default;
110 OpenCLOptionInfo(bool Pragma, unsigned AvailV, unsigned CoreV,
111 unsigned OptV)
112 : WithPragma(Pragma), Avail(AvailV), Core(CoreV), Opt(OptV) {}
113
114 bool isCore() const { return Core != 0U; }
115
116 bool isOptionalCore() const { return Opt != 0U; }
117
118 // Is option available in OpenCL version \p LO.
119 bool isAvailableIn(const LangOptions &LO) const {
120 // In C++ mode all extensions should work at least as in v2.0.
121 return LO.getOpenCLCompatibleVersion() >= Avail;
122 }
123
124 // Is core option in OpenCL version \p LO.
125 bool isCoreIn(const LangOptions &LO) const {
126 return isAvailableIn(LO) && isOpenCLVersionContainedInMask(LO, Core);
127 }
128
129 // Is optional core option in OpenCL version \p LO.
130 bool isOptionalCoreIn(const LangOptions &LO) const {
131 return isAvailableIn(LO) && isOpenCLVersionContainedInMask(LO, Opt);
132 }
133 };
134
135 bool isKnown(llvm::StringRef Ext) const;
136
137 // For core or optional core feature check that it is supported
138 // by a target, for any other option (extension) check that it is
139 // enabled via pragma
140 bool isAvailableOption(llvm::StringRef Ext, const LangOptions &LO) const;
141
142 bool isWithPragma(llvm::StringRef Ext) const;
143
144 // Is supported as either an extension or an (optional) core feature for
145 // OpenCL version \p LO.
146 bool isSupported(llvm::StringRef Ext, const LangOptions &LO) const;
147
148 // Is supported OpenCL core feature for OpenCL version \p LO.
149 // For supported extension, return false.
150 bool isSupportedCore(llvm::StringRef Ext, const LangOptions &LO) const;
151
152 // Is supported optional core OpenCL feature for OpenCL version \p LO.
153 // For supported extension, return false.
154 bool isSupportedOptionalCore(llvm::StringRef Ext,
155 const LangOptions &LO) const;
156
157 // Is supported optional core or core OpenCL feature for OpenCL version \p
158 // LO. For supported extension, return false.
159 bool isSupportedCoreOrOptionalCore(llvm::StringRef Ext,
160 const LangOptions &LO) const;
161
162 // Is supported OpenCL extension for OpenCL version \p LO.
163 // For supported core or optional core feature, return false.
164 bool isSupportedExtension(llvm::StringRef Ext, const LangOptions &LO) const;
165
166 // FIXME: Whether extension should accept pragma should not
167 // be reset dynamically. But it currently required when
168 // registering new extensions via pragmas.
169 void acceptsPragma(llvm::StringRef Ext, bool V = true);
170
171 void enable(llvm::StringRef Ext, bool V = true);
172
173 /// Enable or disable support for OpenCL extensions
174 /// \param Ext name of the extension (not prefixed with '+' or '-')
175 /// \param V value to set for a extension
176 void support(llvm::StringRef Ext, bool V = true);
177
179
180 // Set supported options based on target settings and language version
181 void addSupport(const llvm::StringMap<bool> &FeaturesMap,
182 const LangOptions &Opts);
183
184 // Disable all extensions
185 void disableAll();
186
187 friend class ASTWriter;
188 friend class ASTReader;
189
190 using OpenCLOptionInfoMap = llvm::StringMap<OpenCLOptionInfo>;
191
192 template <typename... Args>
193 static bool isOpenCLOptionCoreIn(const LangOptions &LO, Args &&... args) {
194 return OpenCLOptionInfo(std::forward<Args>(args)...).isCoreIn(LO);
195 }
196
197 template <typename... Args>
199 Args &&... args) {
200 return OpenCLOptionInfo(std::forward<Args>(args)...).isAvailableIn(LO);
201 }
202
203 // Diagnose feature dependencies for OpenCL C 3.0. Return false if target
204 // doesn't follow these requirements.
206 DiagnosticsEngine &Diags);
207
208 // Diagnose that features and equivalent extension are set to same values.
209 // Return false if target doesn't follow these requirements.
211 DiagnosticsEngine &Diags);
212
213private:
214 // Option is enabled via pragma
215 bool isEnabled(llvm::StringRef Ext) const;
216
217 OpenCLOptionInfoMap OptMap;
218};
219
220} // end namespace clang
221
222#endif
#define V(N, I)
Defines the clang::LangOptions interface.
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
unsigned getOpenCLCompatibleVersion() const
Return the OpenCL version that kernel language is compatible with.
bool isAvailableOption(llvm::StringRef Ext, const LangOptions &LO) const
static bool diagnoseUnsupportedFeatureDependencies(const TargetInfo &TI, DiagnosticsEngine &Diags)
bool isSupportedExtension(llvm::StringRef Ext, const LangOptions &LO) const
void enable(llvm::StringRef Ext, bool V=true)
bool isWithPragma(llvm::StringRef Ext) const
bool isSupportedCore(llvm::StringRef Ext, const LangOptions &LO) const
static bool diagnoseFeatureExtensionDifferences(const TargetInfo &TI, DiagnosticsEngine &Diags)
bool isSupportedCoreOrOptionalCore(llvm::StringRef Ext, const LangOptions &LO) const
bool isSupportedOptionalCore(llvm::StringRef Ext, const LangOptions &LO) const
void addSupport(const llvm::StringMap< bool > &FeaturesMap, const LangOptions &Opts)
llvm::StringMap< OpenCLOptionInfo > OpenCLOptionInfoMap
bool isSupported(llvm::StringRef Ext, const LangOptions &LO) const
void acceptsPragma(llvm::StringRef Ext, bool V=true)
void support(llvm::StringRef Ext, bool V=true)
Enable or disable support for OpenCL extensions.
bool areProgramScopeVariablesSupported(const LangOptions &Opts) const
bool isKnown(llvm::StringRef Ext) const
static bool isOpenCLOptionCoreIn(const LangOptions &LO, Args &&... args)
static bool isOpenCLOptionAvailableIn(const LangOptions &LO, Args &&... args)
Exposes information about the current target.
Definition TargetInfo.h:227
The JSON file list parser is used to communicate input to InstallAPI.
bool isOptionalCoreIn(const LangOptions &LO) const
bool isCoreIn(const LangOptions &LO) const
OpenCLOptionInfo(bool Pragma, unsigned AvailV, unsigned CoreV, unsigned OptV)
bool isAvailableIn(const LangOptions &LO) const