clang 23.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 int NumBuiltins =
25
26static constexpr llvm::StringTable BuiltinStrings =
28#define BUILTIN CLANG_BUILTIN_STR_TABLE
29#define TARGET_BUILTIN CLANG_TARGET_BUILTIN_STR_TABLE
30#include "clang/Basic/BuiltinsWebAssembly.def"
31 ;
32
34#define BUILTIN CLANG_BUILTIN_ENTRY
35#define TARGET_BUILTIN CLANG_TARGET_BUILTIN_ENTRY
36#define LIBBUILTIN CLANG_LIBBUILTIN_ENTRY
37#include "clang/Basic/BuiltinsWebAssembly.def"
38});
39
40static constexpr llvm::StringLiteral ValidCPUNames[] = {
41 {"mvp"}, {"bleeding-edge"}, {"generic"}, {"lime1"}};
42
43StringRef WebAssemblyTargetInfo::getABI() const { return ABI; }
44
45bool WebAssemblyTargetInfo::setABI(const std::string &Name) {
46 if (Name != "mvp" && Name != "experimental-mv")
47 return false;
48
49 ABI = Name;
50 return true;
51}
52
53bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
54 return llvm::StringSwitch<bool>(Feature)
55 .Case("atomics", HasAtomics)
56 .Case("bulk-memory", HasBulkMemory)
57 .Case("bulk-memory-opt", HasBulkMemoryOpt)
58 .Case("call-indirect-overlong", HasCallIndirectOverlong)
59 .Case("compact-imports", HasCompactImports)
60 .Case("exception-handling", HasExceptionHandling)
61 .Case("extended-const", HasExtendedConst)
62 .Case("fp16", HasFP16)
63 .Case("gc", HasGC)
64 .Case("multimemory", HasMultiMemory)
65 .Case("multivalue", HasMultivalue)
66 .Case("mutable-globals", HasMutableGlobals)
67 .Case("nontrapping-fptoint", HasNontrappingFPToInt)
68 .Case("reference-types", HasReferenceTypes)
69 .Case("relaxed-atomics", HasRelaxedAtomics)
70 .Case("relaxed-simd", SIMDLevel >= RelaxedSIMD)
71 .Case("sign-ext", HasSignExt)
72 .Case("simd128", SIMDLevel >= SIMD128)
73 .Case("tail-call", HasTailCall)
74 .Case("wide-arithmetic", HasWideArithmetic)
75 .Default(false);
76}
77
78bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
79 return llvm::is_contained(ValidCPUNames, Name);
80}
81
82void WebAssemblyTargetInfo::fillValidCPUList(
83 SmallVectorImpl<StringRef> &Values) const {
84 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
85}
86
88 MacroBuilder &Builder) const {
89 defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
90 if (HasAtomics)
91 Builder.defineMacro("__wasm_atomics__");
92 if (HasBulkMemory)
93 Builder.defineMacro("__wasm_bulk_memory__");
94 if (HasBulkMemoryOpt)
95 Builder.defineMacro("__wasm_bulk_memory_opt__");
96 if (HasExceptionHandling)
97 Builder.defineMacro("__wasm_exception_handling__");
98 if (HasExtendedConst)
99 Builder.defineMacro("__wasm_extended_const__");
100 if (HasMultiMemory)
101 Builder.defineMacro("__wasm_multimemory__");
102 if (HasFP16)
103 Builder.defineMacro("__wasm_fp16__");
104 if (HasGC)
105 Builder.defineMacro("__wasm_gc__");
106 if (HasMultivalue)
107 Builder.defineMacro("__wasm_multivalue__");
108 if (HasMutableGlobals)
109 Builder.defineMacro("__wasm_mutable_globals__");
110 if (HasNontrappingFPToInt)
111 Builder.defineMacro("__wasm_nontrapping_fptoint__");
112 if (HasReferenceTypes)
113 Builder.defineMacro("__wasm_reference_types__");
114 if (HasRelaxedAtomics)
115 Builder.defineMacro("__wasm_relaxed_atomics__");
116 if (SIMDLevel >= RelaxedSIMD)
117 Builder.defineMacro("__wasm_relaxed_simd__");
118 if (HasSignExt)
119 Builder.defineMacro("__wasm_sign_ext__");
120 if (SIMDLevel >= SIMD128)
121 Builder.defineMacro("__wasm_simd128__");
122 if (HasTailCall)
123 Builder.defineMacro("__wasm_tail_call__");
124 if (HasWideArithmetic)
125 Builder.defineMacro("__wasm_wide_arithmetic__");
126 // Note that not all wasm features appear here. For example,
127 // HasCompatctImports
128
129 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
130 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
131 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
132 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
133}
134
135void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
136 SIMDEnum Level, bool Enabled) {
137 if (Enabled) {
138 switch (Level) {
139 case RelaxedSIMD:
140 Features["relaxed-simd"] = true;
141 [[fallthrough]];
142 case SIMD128:
143 Features["simd128"] = true;
144 [[fallthrough]];
145 case NoSIMD:
146 break;
147 }
148 return;
149 }
150
151 switch (Level) {
152 case NoSIMD:
153 case SIMD128:
154 Features["simd128"] = false;
155 [[fallthrough]];
156 case RelaxedSIMD:
157 Features["relaxed-simd"] = false;
158 break;
159 }
160}
161
162void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
163 StringRef Name,
164 bool Enabled) const {
165 if (Name == "simd128")
166 setSIMDLevel(Features, SIMD128, Enabled);
167 else if (Name == "relaxed-simd")
168 setSIMDLevel(Features, RelaxedSIMD, Enabled);
169 else
170 Features[Name] = Enabled;
171}
172
173bool WebAssemblyTargetInfo::initFeatureMap(
174 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
175 const std::vector<std::string> &FeaturesVec) const {
176 auto addGenericFeatures = [&]() {
177 Features["bulk-memory"] = true;
178 Features["bulk-memory-opt"] = true;
179 Features["call-indirect-overlong"] = true;
180 Features["multivalue"] = true;
181 Features["mutable-globals"] = true;
182 Features["nontrapping-fptoint"] = true;
183 Features["reference-types"] = true;
184 Features["sign-ext"] = true;
185 };
186 auto addLime1Features = [&]() {
187 // Lime1:
188 // <https://github.com/WebAssembly/tool-conventions/blob/main/Lime.md#lime1>
189 Features["bulk-memory-opt"] = true;
190 Features["call-indirect-overlong"] = true;
191 Features["extended-const"] = true;
192 Features["multivalue"] = true;
193 Features["mutable-globals"] = true;
194 Features["nontrapping-fptoint"] = true;
195 Features["sign-ext"] = true;
196 };
197 auto addBleedingEdgeFeatures = [&]() {
198 addGenericFeatures();
199 Features["atomics"] = true;
200 Features["compact-imports"] = true;
201 Features["exception-handling"] = true;
202 Features["extended-const"] = true;
203 Features["fp16"] = true;
204 Features["gc"] = true;
205 Features["multimemory"] = true;
206 Features["relaxed-atomics"] = true;
207 Features["tail-call"] = true;
208 Features["wide-arithmetic"] = true;
209 setSIMDLevel(Features, RelaxedSIMD, true);
210 };
211 if (CPU == "generic") {
212 addGenericFeatures();
213 } else if (CPU == "lime1") {
214 addLime1Features();
215 } else if (CPU == "bleeding-edge") {
216 addBleedingEdgeFeatures();
217 }
218
219 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
220}
221
222bool WebAssemblyTargetInfo::handleTargetFeatures(
223 std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
224 HasMustTail = false;
225 for (const auto &Feature : Features) {
226 if (Feature == "+atomics") {
227 HasAtomics = true;
228 continue;
229 }
230 if (Feature == "-atomics") {
231 HasAtomics = false;
232 continue;
233 }
234 if (Feature == "+bulk-memory") {
235 HasBulkMemory = true;
236 continue;
237 }
238 if (Feature == "-bulk-memory") {
239 HasBulkMemory = false;
240 continue;
241 }
242 if (Feature == "+bulk-memory-opt") {
243 HasBulkMemoryOpt = true;
244 continue;
245 }
246 if (Feature == "-bulk-memory-opt") {
247 HasBulkMemoryOpt = false;
248 continue;
249 }
250 if (Feature == "+call-indirect-overlong") {
251 HasCallIndirectOverlong = true;
252 continue;
253 }
254 if (Feature == "-call-indirect-overlong") {
255 HasCallIndirectOverlong = false;
256 continue;
257 }
258 if (Feature == "+compact-imports") {
259 HasCompactImports = true;
260 continue;
261 }
262 if (Feature == "-compact-imports") {
263 HasCompactImports = false;
264 continue;
265 }
266 if (Feature == "+exception-handling") {
267 HasExceptionHandling = true;
268 continue;
269 }
270 if (Feature == "-exception-handling") {
271 HasExceptionHandling = false;
272 continue;
273 }
274 if (Feature == "+extended-const") {
275 HasExtendedConst = true;
276 continue;
277 }
278 if (Feature == "-extended-const") {
279 HasExtendedConst = false;
280 continue;
281 }
282 if (Feature == "+fp16") {
283 SIMDLevel = std::max(SIMDLevel, SIMD128);
284 HasFP16 = true;
285 continue;
286 }
287 if (Feature == "-fp16") {
288 HasFP16 = false;
289 continue;
290 }
291 if (Feature == "+gc") {
292 HasGC = true;
293 continue;
294 }
295 if (Feature == "-gc") {
296 HasGC = false;
297 continue;
298 }
299 if (Feature == "+multimemory") {
300 HasMultiMemory = true;
301 continue;
302 }
303 if (Feature == "-multimemory") {
304 HasMultiMemory = false;
305 continue;
306 }
307 if (Feature == "+multivalue") {
308 HasMultivalue = true;
309 continue;
310 }
311 if (Feature == "-multivalue") {
312 HasMultivalue = false;
313 continue;
314 }
315 if (Feature == "+mutable-globals") {
316 HasMutableGlobals = true;
317 continue;
318 }
319 if (Feature == "-mutable-globals") {
320 HasMutableGlobals = false;
321 continue;
322 }
323 if (Feature == "+nontrapping-fptoint") {
324 HasNontrappingFPToInt = true;
325 continue;
326 }
327 if (Feature == "-nontrapping-fptoint") {
328 HasNontrappingFPToInt = false;
329 continue;
330 }
331 if (Feature == "+reference-types") {
332 HasReferenceTypes = true;
333 continue;
334 }
335 if (Feature == "-reference-types") {
336 HasReferenceTypes = false;
337 continue;
338 }
339 if (Feature == "+relaxed-atomics") {
340 HasRelaxedAtomics = true;
341 continue;
342 }
343 if (Feature == "-relaxed-atomics") {
344 HasRelaxedAtomics = false;
345 continue;
346 }
347 if (Feature == "+relaxed-simd") {
348 SIMDLevel = std::max(SIMDLevel, RelaxedSIMD);
349 continue;
350 }
351 if (Feature == "-relaxed-simd") {
352 SIMDLevel = std::min(SIMDLevel, SIMDEnum(RelaxedSIMD - 1));
353 continue;
354 }
355 if (Feature == "+sign-ext") {
356 HasSignExt = true;
357 continue;
358 }
359 if (Feature == "-sign-ext") {
360 HasSignExt = false;
361 continue;
362 }
363 if (Feature == "+simd128") {
364 SIMDLevel = std::max(SIMDLevel, SIMD128);
365 continue;
366 }
367 if (Feature == "-simd128") {
368 SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
369 continue;
370 }
371 if (Feature == "+tail-call") {
372 HasTailCall = true;
373 HasMustTail = true;
374 continue;
375 }
376 if (Feature == "-tail-call") {
377 HasTailCall = false;
378 HasMustTail = false;
379 continue;
380 }
381 if (Feature == "+wide-arithmetic") {
382 HasWideArithmetic = true;
383 continue;
384 }
385 if (Feature == "-wide-arithmetic") {
386 HasWideArithmetic = false;
387 continue;
388 }
389
390 Diags.Report(diag::err_opt_not_valid_with_opt)
391 << Feature << "-target-feature";
392 return false;
393 }
394
395 // gc implies reference-types
396 if (HasGC) {
397 HasReferenceTypes = true;
398 }
399
400 // bulk-memory-opt is a subset of bulk-memory.
401 if (HasBulkMemory) {
402 HasBulkMemoryOpt = true;
403 }
404
405 // The reference-types feature included the change to `call_indirect`
406 // encodings to support overlong immediates.
407 if (HasReferenceTypes) {
408 HasCallIndirectOverlong = true;
409 }
410
411 return true;
412}
413
414llvm::SmallVector<Builtin::InfosShard>
416 return {{&BuiltinStrings, BuiltinInfos}};
417}
418
419void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts,
420 const TargetInfo *Aux) {
421 TargetInfo::adjust(Diags, Opts, Aux);
422 // Turn off POSIXThreads and ThreadModel so that we don't predefine _REENTRANT
423 // or __STDCPP_THREADS__ if we will eventually end up stripping atomics
424 // because they are unsupported.
425 if (!HasAtomics || !HasBulkMemory) {
426 Opts.POSIXThreads = false;
427 Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
428 Opts.ThreadsafeStatics = false;
429 }
430}
431
433 MacroBuilder &Builder) const {
435 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
436}
437
439 MacroBuilder &Builder) const {
441 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
442}
Defines the Diagnostic-related interfaces.
static constexpr llvm::StringTable BuiltinStrings
Definition ARM.cpp:1115
static constexpr llvm::StringLiteral ValidCPUNames[]
Definition BPF.cpp:82
static constexpr Builtin::Info BuiltinInfos[]
Definition Builtins.cpp:38
static constexpr unsigned NumBuiltins
Definition Builtins.cpp:32
Defines enum values for all the target-independent builtin functions.
#define CLANG_BUILTIN_STR_TABLE_START
Definition Builtins.h:165
Enumerates target-specific builtins in their own namespaces within namespace clang.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
@ Single
Single Threaded Environment.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
virtual llvm::SmallVector< Builtin::InfosShard > getTargetBuiltins() const =0
Return information about target-specific builtins for the current primary target, and info about whic...
virtual void adjust(DiagnosticsEngine &Diags, LangOptions &Opts, const TargetInfo *Aux)
Set forced language options.
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 ...
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 -----------------------—===//
StringRef getABI() const override
Get the ABI currently in use.
bool setABI(const std::string &Name) override
Use the specified ABI.
static constexpr std::array< Info, N > MakeInfos(std::array< Info, N > Infos)
A constexpr function to construct an infos array from X-macros.
Definition Builtins.h:114
LLVM_LIBRARY_VISIBILITY void defineCPUMacros(clang::MacroBuilder &Builder, llvm::StringRef CPUName, bool Tuning=true)
The JSON file list parser is used to communicate input to InstallAPI.