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"
15#include "clang/Basic/Builtins.h"
16#include "clang/Basic/Diagnostic.h"
17#include "clang/Basic/TargetBuiltins.h"
18#include "llvm/ADT/StringSwitch.h"
19
20using namespace clang;
21using namespace clang::targets;
22
23static constexpr int NumBuiltins =
24 clang::WebAssembly::LastTSBuiltin - Builtin::FirstTSBuiltin;
25
26static constexpr llvm::StringTable BuiltinStrings =
27 CLANG_BUILTIN_STR_TABLE_START
28#define BUILTIN CLANG_BUILTIN_STR_TABLE
29#define TARGET_BUILTIN CLANG_TARGET_BUILTIN_STR_TABLE
30#include "clang/Basic/BuiltinsWebAssembly.def"
31 ;
32
33static constexpr auto BuiltinInfos = Builtin::MakeInfos<NumBuiltins>(Infos: {
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(S: "atomics", Value: HasAtomics)
56 .Case(S: "bulk-memory", Value: HasBulkMemory)
57 .Case(S: "bulk-memory-opt", Value: HasBulkMemoryOpt)
58 .Case(S: "call-indirect-overlong", Value: HasCallIndirectOverlong)
59 .Case(S: "compact-imports", Value: HasCompactImports)
60 .Case(S: "exception-handling", Value: HasExceptionHandling)
61 .Case(S: "extended-const", Value: HasExtendedConst)
62 .Case(S: "fp16", Value: HasFP16)
63 .Case(S: "gc", Value: HasGC)
64 .Case(S: "multimemory", Value: HasMultiMemory)
65 .Case(S: "multivalue", Value: HasMultivalue)
66 .Case(S: "mutable-globals", Value: HasMutableGlobals)
67 .Case(S: "nontrapping-fptoint", Value: HasNontrappingFPToInt)
68 .Case(S: "reference-types", Value: HasReferenceTypes)
69 .Case(S: "relaxed-atomics", Value: HasRelaxedAtomics)
70 .Case(S: "relaxed-simd", Value: SIMDLevel >= RelaxedSIMD)
71 .Case(S: "sign-ext", Value: HasSignExt)
72 .Case(S: "simd128", Value: SIMDLevel >= SIMD128)
73 .Case(S: "tail-call", Value: HasTailCall)
74 .Case(S: "wide-arithmetic", Value: HasWideArithmetic)
75 .Default(Value: false);
76}
77
78bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
79 return llvm::is_contained(Range: ValidCPUNames, Element: Name);
80}
81
82void WebAssemblyTargetInfo::fillValidCPUList(
83 SmallVectorImpl<StringRef> &Values) const {
84 Values.append(in_start: std::begin(arr: ValidCPUNames), in_end: std::end(arr: ValidCPUNames));
85}
86
87void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
88 MacroBuilder &Builder) const {
89 defineCPUMacros(Builder, CPUName: "wasm", /*Tuning=*/false);
90 if (HasAtomics)
91 Builder.defineMacro(Name: "__wasm_atomics__");
92 if (HasBulkMemory)
93 Builder.defineMacro(Name: "__wasm_bulk_memory__");
94 if (HasBulkMemoryOpt)
95 Builder.defineMacro(Name: "__wasm_bulk_memory_opt__");
96 if (HasExceptionHandling)
97 Builder.defineMacro(Name: "__wasm_exception_handling__");
98 if (HasExtendedConst)
99 Builder.defineMacro(Name: "__wasm_extended_const__");
100 if (HasMultiMemory)
101 Builder.defineMacro(Name: "__wasm_multimemory__");
102 if (HasFP16)
103 Builder.defineMacro(Name: "__wasm_fp16__");
104 if (HasGC)
105 Builder.defineMacro(Name: "__wasm_gc__");
106 if (HasMultivalue)
107 Builder.defineMacro(Name: "__wasm_multivalue__");
108 if (HasMutableGlobals)
109 Builder.defineMacro(Name: "__wasm_mutable_globals__");
110 if (HasNontrappingFPToInt)
111 Builder.defineMacro(Name: "__wasm_nontrapping_fptoint__");
112 if (HasReferenceTypes)
113 Builder.defineMacro(Name: "__wasm_reference_types__");
114 if (HasRelaxedAtomics)
115 Builder.defineMacro(Name: "__wasm_relaxed_atomics__");
116 if (SIMDLevel >= RelaxedSIMD)
117 Builder.defineMacro(Name: "__wasm_relaxed_simd__");
118 if (HasSignExt)
119 Builder.defineMacro(Name: "__wasm_sign_ext__");
120 if (SIMDLevel >= SIMD128)
121 Builder.defineMacro(Name: "__wasm_simd128__");
122 if (HasTailCall)
123 Builder.defineMacro(Name: "__wasm_tail_call__");
124 if (HasWideArithmetic)
125 Builder.defineMacro(Name: "__wasm_wide_arithmetic__");
126 // Note that not all wasm features appear here. For example,
127 // HasCompatctImports
128
129 Builder.defineMacro(Name: "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
130 Builder.defineMacro(Name: "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
131 Builder.defineMacro(Name: "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
132 Builder.defineMacro(Name: "__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, Level: SIMD128, Enabled);
167 else if (Name == "relaxed-simd")
168 setSIMDLevel(Features, Level: 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, Level: RelaxedSIMD, Enabled: 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, FeatureVec: 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(a: SIMDLevel, b: 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(a: SIMDLevel, b: RelaxedSIMD);
349 continue;
350 }
351 if (Feature == "-relaxed-simd") {
352 SIMDLevel = std::min(a: SIMDLevel, b: 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(a: SIMDLevel, b: SIMD128);
365 continue;
366 }
367 if (Feature == "-simd128") {
368 SIMDLevel = std::min(a: SIMDLevel, b: 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(DiagID: 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>
415WebAssemblyTargetInfo::getTargetBuiltins() const {
416 return {{.Strings: &BuiltinStrings, .Infos: 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
432void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
433 MacroBuilder &Builder) const {
434 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
435 defineCPUMacros(Builder, CPUName: "wasm32", /*Tuning=*/false);
436}
437
438void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
439 MacroBuilder &Builder) const {
440 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
441 defineCPUMacros(Builder, CPUName: "wasm64", /*Tuning=*/false);
442}
443