clang 19.0.0git
WebAssembly.cpp
Go to the documentation of this file.
1//===--- WebAssembly.cpp - Implement WebAssembly target feature support ---===//
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// This file implements WebAssembly TargetInfo objects.
10//
11//===----------------------------------------------------------------------===//
12
13#include "WebAssembly.h"
14#include "Targets.h"
18#include "llvm/ADT/StringSwitch.h"
19
20using namespace clang;
21using namespace clang::targets;
22
23static constexpr Builtin::Info BuiltinInfo[] = {
24#define BUILTIN(ID, TYPE, ATTRS) \
25 {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
26#define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
27 {#ID, TYPE, ATTRS, FEATURE, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
28#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
29 {#ID, TYPE, ATTRS, nullptr, HeaderDesc::HEADER, ALL_LANGUAGES},
30#include "clang/Basic/BuiltinsWebAssembly.def"
31};
32
33static constexpr llvm::StringLiteral ValidCPUNames[] = {
34 {"mvp"}, {"bleeding-edge"}, {"generic"}};
35
36StringRef WebAssemblyTargetInfo::getABI() const { return ABI; }
37
38bool WebAssemblyTargetInfo::setABI(const std::string &Name) {
39 if (Name != "mvp" && Name != "experimental-mv")
40 return false;
41
42 ABI = Name;
43 return true;
44}
45
46bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
47 return llvm::StringSwitch<bool>(Feature)
48 .Case("simd128", SIMDLevel >= SIMD128)
49 .Case("relaxed-simd", SIMDLevel >= RelaxedSIMD)
50 .Case("half-precision", HasHalfPrecision)
51 .Case("nontrapping-fptoint", HasNontrappingFPToInt)
52 .Case("sign-ext", HasSignExt)
53 .Case("exception-handling", HasExceptionHandling)
54 .Case("bulk-memory", HasBulkMemory)
55 .Case("atomics", HasAtomics)
56 .Case("mutable-globals", HasMutableGlobals)
57 .Case("multivalue", HasMultivalue)
58 .Case("tail-call", HasTailCall)
59 .Case("reference-types", HasReferenceTypes)
60 .Case("extended-const", HasExtendedConst)
61 .Case("multimemory", HasMultiMemory)
62 .Default(false);
63}
64
65bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
66 return llvm::is_contained(ValidCPUNames, Name);
67}
68
69void WebAssemblyTargetInfo::fillValidCPUList(
70 SmallVectorImpl<StringRef> &Values) const {
71 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
72}
73
75 MacroBuilder &Builder) const {
76 defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
77 if (SIMDLevel >= SIMD128)
78 Builder.defineMacro("__wasm_simd128__");
79 if (SIMDLevel >= RelaxedSIMD)
80 Builder.defineMacro("__wasm_relaxed_simd__");
81 if (HasNontrappingFPToInt)
82 Builder.defineMacro("__wasm_nontrapping_fptoint__");
83 if (HasSignExt)
84 Builder.defineMacro("__wasm_sign_ext__");
85 if (HasExceptionHandling)
86 Builder.defineMacro("__wasm_exception_handling__");
87 if (HasBulkMemory)
88 Builder.defineMacro("__wasm_bulk_memory__");
89 if (HasAtomics)
90 Builder.defineMacro("__wasm_atomics__");
91 if (HasMutableGlobals)
92 Builder.defineMacro("__wasm_mutable_globals__");
93 if (HasMultivalue)
94 Builder.defineMacro("__wasm_multivalue__");
95 if (HasTailCall)
96 Builder.defineMacro("__wasm_tail_call__");
97 if (HasReferenceTypes)
98 Builder.defineMacro("__wasm_reference_types__");
99 if (HasExtendedConst)
100 Builder.defineMacro("__wasm_extended_const__");
101 if (HasMultiMemory)
102 Builder.defineMacro("__wasm_multimemory__");
103 if (HasHalfPrecision)
104 Builder.defineMacro("__wasm_half_precision__");
105
106 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
107 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
108 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
109 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
110}
111
112void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
113 SIMDEnum Level, bool Enabled) {
114 if (Enabled) {
115 switch (Level) {
116 case RelaxedSIMD:
117 Features["relaxed-simd"] = true;
118 [[fallthrough]];
119 case SIMD128:
120 Features["simd128"] = true;
121 [[fallthrough]];
122 case NoSIMD:
123 break;
124 }
125 return;
126 }
127
128 switch (Level) {
129 case NoSIMD:
130 case SIMD128:
131 Features["simd128"] = false;
132 [[fallthrough]];
133 case RelaxedSIMD:
134 Features["relaxed-simd"] = false;
135 break;
136 }
137}
138
139void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
140 StringRef Name,
141 bool Enabled) const {
142 if (Name == "simd128")
143 setSIMDLevel(Features, SIMD128, Enabled);
144 else if (Name == "relaxed-simd")
145 setSIMDLevel(Features, RelaxedSIMD, Enabled);
146 else
147 Features[Name] = Enabled;
148}
149
150bool WebAssemblyTargetInfo::initFeatureMap(
151 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
152 const std::vector<std::string> &FeaturesVec) const {
153 auto addGenericFeatures = [&]() {
154 Features["multivalue"] = true;
155 Features["mutable-globals"] = true;
156 Features["reference-types"] = true;
157 Features["sign-ext"] = true;
158 };
159 auto addBleedingEdgeFeatures = [&]() {
160 addGenericFeatures();
161 Features["atomics"] = true;
162 Features["bulk-memory"] = true;
163 Features["multimemory"] = true;
164 Features["nontrapping-fptoint"] = true;
165 Features["tail-call"] = true;
166 Features["half-precision"] = true;
167 setSIMDLevel(Features, SIMD128, true);
168 };
169 if (CPU == "generic") {
170 addGenericFeatures();
171 } else if (CPU == "bleeding-edge") {
172 addBleedingEdgeFeatures();
173 }
174
175 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
176}
177
178bool WebAssemblyTargetInfo::handleTargetFeatures(
179 std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
180 for (const auto &Feature : Features) {
181 if (Feature == "+simd128") {
182 SIMDLevel = std::max(SIMDLevel, SIMD128);
183 continue;
184 }
185 if (Feature == "-simd128") {
186 SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
187 continue;
188 }
189 if (Feature == "+relaxed-simd") {
190 SIMDLevel = std::max(SIMDLevel, RelaxedSIMD);
191 continue;
192 }
193 if (Feature == "-relaxed-simd") {
194 SIMDLevel = std::min(SIMDLevel, SIMDEnum(RelaxedSIMD - 1));
195 continue;
196 }
197 if (Feature == "+nontrapping-fptoint") {
198 HasNontrappingFPToInt = true;
199 continue;
200 }
201 if (Feature == "-nontrapping-fptoint") {
202 HasNontrappingFPToInt = false;
203 continue;
204 }
205 if (Feature == "+sign-ext") {
206 HasSignExt = true;
207 continue;
208 }
209 if (Feature == "-sign-ext") {
210 HasSignExt = false;
211 continue;
212 }
213 if (Feature == "+exception-handling") {
214 HasExceptionHandling = true;
215 continue;
216 }
217 if (Feature == "-exception-handling") {
218 HasExceptionHandling = false;
219 continue;
220 }
221 if (Feature == "+bulk-memory") {
222 HasBulkMemory = true;
223 continue;
224 }
225 if (Feature == "-bulk-memory") {
226 HasBulkMemory = false;
227 continue;
228 }
229 if (Feature == "+half-precision") {
230 SIMDLevel = std::max(SIMDLevel, SIMD128);
231 HasHalfPrecision = true;
232 continue;
233 }
234 if (Feature == "-half-precision") {
235 HasHalfPrecision = false;
236 continue;
237 }
238 if (Feature == "+atomics") {
239 HasAtomics = true;
240 continue;
241 }
242 if (Feature == "-atomics") {
243 HasAtomics = false;
244 continue;
245 }
246 if (Feature == "+mutable-globals") {
247 HasMutableGlobals = true;
248 continue;
249 }
250 if (Feature == "-mutable-globals") {
251 HasMutableGlobals = false;
252 continue;
253 }
254 if (Feature == "+multivalue") {
255 HasMultivalue = true;
256 continue;
257 }
258 if (Feature == "-multivalue") {
259 HasMultivalue = false;
260 continue;
261 }
262 if (Feature == "+tail-call") {
263 HasTailCall = true;
264 continue;
265 }
266 if (Feature == "-tail-call") {
267 HasTailCall = false;
268 continue;
269 }
270 if (Feature == "+reference-types") {
271 HasReferenceTypes = true;
272 continue;
273 }
274 if (Feature == "-reference-types") {
275 HasReferenceTypes = false;
276 continue;
277 }
278 if (Feature == "+extended-const") {
279 HasExtendedConst = true;
280 continue;
281 }
282 if (Feature == "-extended-const") {
283 HasExtendedConst = false;
284 continue;
285 }
286 if (Feature == "+multimemory") {
287 HasMultiMemory = true;
288 continue;
289 }
290 if (Feature == "-multimemory") {
291 HasMultiMemory = false;
292 continue;
293 }
294
295 Diags.Report(diag::err_opt_not_valid_with_opt)
296 << Feature << "-target-feature";
297 return false;
298 }
299 return true;
300}
301
302ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
305}
306
307void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags,
308 LangOptions &Opts) {
309 TargetInfo::adjust(Diags, Opts);
310 // Turn off POSIXThreads and ThreadModel so that we don't predefine _REENTRANT
311 // or __STDCPP_THREADS__ if we will eventually end up stripping atomics
312 // because they are unsupported.
313 if (!HasAtomics || !HasBulkMemory) {
314 Opts.POSIXThreads = false;
315 Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
316 Opts.ThreadsafeStatics = false;
317 }
318}
319
321 MacroBuilder &Builder) const {
323 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
324}
325
327 MacroBuilder &Builder) const {
329 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
330}
Defines the Diagnostic-related interfaces.
static constexpr llvm::StringLiteral ValidCPUNames[]
Definition: BPF.cpp:69
static constexpr Builtin::Info BuiltinInfo[]
Definition: WebAssembly.cpp:23
static constexpr llvm::StringLiteral ValidCPUNames[]
Definition: WebAssembly.cpp:33
static constexpr Builtin::Info BuiltinInfo[]
Definition: Builtins.cpp:32
Defines enum values for all the target-independent builtin functions.
Enumerates target-specific builtins in their own namespaces within namespace clang.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1547
@ Single
Single Threaded Environment.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:461
virtual void adjust(DiagnosticsEngine &Diags, LangOptions &Opts)
Set forced language options.
Definition: TargetInfo.cpp:392
virtual bool initFeatureMap(llvm::StringMap< bool > &Features, DiagnosticsEngine &Diags, StringRef CPU, const std::vector< std::string > &FeatureVec) const
Initialize the map with the default set of target features for the CPU this should include all legal ...
Definition: TargetInfo.cpp:522
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition: WebAssembly.cpp:74
StringRef getABI() const override
Get the ABI currently in use.
Definition: WebAssembly.cpp:36
bool setABI(const std::string &Name) override
Use the specified ABI.
Definition: WebAssembly.cpp:38
void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning)
Definition: Targets.cpp:76
The JSON file list parser is used to communicate input to InstallAPI.