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-simd", SIMDLevel >= RelaxedSIMD)
70 .Case("sign-ext", HasSignExt)
71 .Case("simd128", SIMDLevel >= SIMD128)
72 .Case("tail-call", HasTailCall)
73 .Case("wide-arithmetic", HasWideArithmetic)
74 .Default(false);
75}
76
77bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
78 return llvm::is_contained(ValidCPUNames, Name);
79}
80
81void WebAssemblyTargetInfo::fillValidCPUList(
82 SmallVectorImpl<StringRef> &Values) const {
83 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
84}
85
87 MacroBuilder &Builder) const {
88 defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
89 if (HasAtomics)
90 Builder.defineMacro("__wasm_atomics__");
91 if (HasBulkMemory)
92 Builder.defineMacro("__wasm_bulk_memory__");
93 if (HasBulkMemoryOpt)
94 Builder.defineMacro("__wasm_bulk_memory_opt__");
95 if (HasExceptionHandling)
96 Builder.defineMacro("__wasm_exception_handling__");
97 if (HasExtendedConst)
98 Builder.defineMacro("__wasm_extended_const__");
99 if (HasMultiMemory)
100 Builder.defineMacro("__wasm_multimemory__");
101 if (HasFP16)
102 Builder.defineMacro("__wasm_fp16__");
103 if (HasGC)
104 Builder.defineMacro("__wasm_gc__");
105 if (HasMultivalue)
106 Builder.defineMacro("__wasm_multivalue__");
107 if (HasMutableGlobals)
108 Builder.defineMacro("__wasm_mutable_globals__");
109 if (HasNontrappingFPToInt)
110 Builder.defineMacro("__wasm_nontrapping_fptoint__");
111 if (HasReferenceTypes)
112 Builder.defineMacro("__wasm_reference_types__");
113 if (SIMDLevel >= RelaxedSIMD)
114 Builder.defineMacro("__wasm_relaxed_simd__");
115 if (HasSignExt)
116 Builder.defineMacro("__wasm_sign_ext__");
117 if (SIMDLevel >= SIMD128)
118 Builder.defineMacro("__wasm_simd128__");
119 if (HasTailCall)
120 Builder.defineMacro("__wasm_tail_call__");
121 if (HasWideArithmetic)
122 Builder.defineMacro("__wasm_wide_arithmetic__");
123 // Note that not all wasm features appear here. For example,
124 // HasCompatctImports
125
126 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
127 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
128 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
129 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
130}
131
132void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
133 SIMDEnum Level, bool Enabled) {
134 if (Enabled) {
135 switch (Level) {
136 case RelaxedSIMD:
137 Features["relaxed-simd"] = true;
138 [[fallthrough]];
139 case SIMD128:
140 Features["simd128"] = true;
141 [[fallthrough]];
142 case NoSIMD:
143 break;
144 }
145 return;
146 }
147
148 switch (Level) {
149 case NoSIMD:
150 case SIMD128:
151 Features["simd128"] = false;
152 [[fallthrough]];
153 case RelaxedSIMD:
154 Features["relaxed-simd"] = false;
155 break;
156 }
157}
158
159void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
160 StringRef Name,
161 bool Enabled) const {
162 if (Name == "simd128")
163 setSIMDLevel(Features, SIMD128, Enabled);
164 else if (Name == "relaxed-simd")
165 setSIMDLevel(Features, RelaxedSIMD, Enabled);
166 else
167 Features[Name] = Enabled;
168}
169
170bool WebAssemblyTargetInfo::initFeatureMap(
171 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
172 const std::vector<std::string> &FeaturesVec) const {
173 auto addGenericFeatures = [&]() {
174 Features["bulk-memory"] = true;
175 Features["bulk-memory-opt"] = true;
176 Features["call-indirect-overlong"] = true;
177 Features["multivalue"] = true;
178 Features["mutable-globals"] = true;
179 Features["nontrapping-fptoint"] = true;
180 Features["reference-types"] = true;
181 Features["sign-ext"] = true;
182 };
183 auto addLime1Features = [&]() {
184 // Lime1:
185 // <https://github.com/WebAssembly/tool-conventions/blob/main/Lime.md#lime1>
186 Features["bulk-memory-opt"] = true;
187 Features["call-indirect-overlong"] = true;
188 Features["extended-const"] = true;
189 Features["multivalue"] = true;
190 Features["mutable-globals"] = true;
191 Features["nontrapping-fptoint"] = true;
192 Features["sign-ext"] = true;
193 };
194 auto addBleedingEdgeFeatures = [&]() {
195 addGenericFeatures();
196 Features["atomics"] = true;
197 Features["compact-imports"] = true;
198 Features["exception-handling"] = true;
199 Features["extended-const"] = true;
200 Features["fp16"] = true;
201 Features["gc"] = true;
202 Features["multimemory"] = true;
203 Features["tail-call"] = true;
204 Features["wide-arithmetic"] = true;
205 setSIMDLevel(Features, RelaxedSIMD, true);
206 };
207 if (CPU == "generic") {
208 addGenericFeatures();
209 } else if (CPU == "lime1") {
210 addLime1Features();
211 } else if (CPU == "bleeding-edge") {
212 addBleedingEdgeFeatures();
213 }
214
215 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
216}
217
218bool WebAssemblyTargetInfo::handleTargetFeatures(
219 std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
220 HasMustTail = false;
221 for (const auto &Feature : Features) {
222 if (Feature == "+atomics") {
223 HasAtomics = true;
224 continue;
225 }
226 if (Feature == "-atomics") {
227 HasAtomics = false;
228 continue;
229 }
230 if (Feature == "+bulk-memory") {
231 HasBulkMemory = true;
232 continue;
233 }
234 if (Feature == "-bulk-memory") {
235 HasBulkMemory = false;
236 continue;
237 }
238 if (Feature == "+bulk-memory-opt") {
239 HasBulkMemoryOpt = true;
240 continue;
241 }
242 if (Feature == "-bulk-memory-opt") {
243 HasBulkMemoryOpt = false;
244 continue;
245 }
246 if (Feature == "+call-indirect-overlong") {
247 HasCallIndirectOverlong = true;
248 continue;
249 }
250 if (Feature == "-call-indirect-overlong") {
251 HasCallIndirectOverlong = false;
252 continue;
253 }
254 if (Feature == "+compact-imports") {
255 HasCompactImports = true;
256 continue;
257 }
258 if (Feature == "-compact-imports") {
259 HasCompactImports = false;
260 continue;
261 }
262 if (Feature == "+exception-handling") {
263 HasExceptionHandling = true;
264 continue;
265 }
266 if (Feature == "-exception-handling") {
267 HasExceptionHandling = false;
268 continue;
269 }
270 if (Feature == "+extended-const") {
271 HasExtendedConst = true;
272 continue;
273 }
274 if (Feature == "-extended-const") {
275 HasExtendedConst = false;
276 continue;
277 }
278 if (Feature == "+fp16") {
279 SIMDLevel = std::max(SIMDLevel, SIMD128);
280 HasFP16 = true;
281 continue;
282 }
283 if (Feature == "-fp16") {
284 HasFP16 = false;
285 continue;
286 }
287 if (Feature == "+gc") {
288 HasGC = true;
289 continue;
290 }
291 if (Feature == "-gc") {
292 HasGC = false;
293 continue;
294 }
295 if (Feature == "+multimemory") {
296 HasMultiMemory = true;
297 continue;
298 }
299 if (Feature == "-multimemory") {
300 HasMultiMemory = false;
301 continue;
302 }
303 if (Feature == "+multivalue") {
304 HasMultivalue = true;
305 continue;
306 }
307 if (Feature == "-multivalue") {
308 HasMultivalue = false;
309 continue;
310 }
311 if (Feature == "+mutable-globals") {
312 HasMutableGlobals = true;
313 continue;
314 }
315 if (Feature == "-mutable-globals") {
316 HasMutableGlobals = false;
317 continue;
318 }
319 if (Feature == "+nontrapping-fptoint") {
320 HasNontrappingFPToInt = true;
321 continue;
322 }
323 if (Feature == "-nontrapping-fptoint") {
324 HasNontrappingFPToInt = false;
325 continue;
326 }
327 if (Feature == "+reference-types") {
328 HasReferenceTypes = true;
329 continue;
330 }
331 if (Feature == "-reference-types") {
332 HasReferenceTypes = false;
333 continue;
334 }
335 if (Feature == "+relaxed-simd") {
336 SIMDLevel = std::max(SIMDLevel, RelaxedSIMD);
337 continue;
338 }
339 if (Feature == "-relaxed-simd") {
340 SIMDLevel = std::min(SIMDLevel, SIMDEnum(RelaxedSIMD - 1));
341 continue;
342 }
343 if (Feature == "+sign-ext") {
344 HasSignExt = true;
345 continue;
346 }
347 if (Feature == "-sign-ext") {
348 HasSignExt = false;
349 continue;
350 }
351 if (Feature == "+simd128") {
352 SIMDLevel = std::max(SIMDLevel, SIMD128);
353 continue;
354 }
355 if (Feature == "-simd128") {
356 SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
357 continue;
358 }
359 if (Feature == "+tail-call") {
360 HasTailCall = true;
361 HasMustTail = true;
362 continue;
363 }
364 if (Feature == "-tail-call") {
365 HasTailCall = false;
366 HasMustTail = false;
367 continue;
368 }
369 if (Feature == "+wide-arithmetic") {
370 HasWideArithmetic = true;
371 continue;
372 }
373 if (Feature == "-wide-arithmetic") {
374 HasWideArithmetic = false;
375 continue;
376 }
377
378 Diags.Report(diag::err_opt_not_valid_with_opt)
379 << Feature << "-target-feature";
380 return false;
381 }
382
383 // gc implies reference-types
384 if (HasGC) {
385 HasReferenceTypes = true;
386 }
387
388 // bulk-memory-opt is a subset of bulk-memory.
389 if (HasBulkMemory) {
390 HasBulkMemoryOpt = true;
391 }
392
393 // The reference-types feature included the change to `call_indirect`
394 // encodings to support overlong immediates.
395 if (HasReferenceTypes) {
396 HasCallIndirectOverlong = true;
397 }
398
399 return true;
400}
401
402llvm::SmallVector<Builtin::InfosShard>
404 return {{&BuiltinStrings, BuiltinInfos}};
405}
406
407void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts,
408 const TargetInfo *Aux) {
409 TargetInfo::adjust(Diags, Opts, Aux);
410 // Turn off POSIXThreads and ThreadModel so that we don't predefine _REENTRANT
411 // or __STDCPP_THREADS__ if we will eventually end up stripping atomics
412 // because they are unsupported.
413 if (!HasAtomics || !HasBulkMemory) {
414 Opts.POSIXThreads = false;
415 Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
416 Opts.ThreadsafeStatics = false;
417 }
418}
419
421 MacroBuilder &Builder) const {
423 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
424}
425
427 MacroBuilder &Builder) const {
429 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
430}
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.