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-simd", Value: SIMDLevel >= RelaxedSIMD)
70 .Case(S: "sign-ext", Value: HasSignExt)
71 .Case(S: "simd128", Value: SIMDLevel >= SIMD128)
72 .Case(S: "tail-call", Value: HasTailCall)
73 .Case(S: "wide-arithmetic", Value: HasWideArithmetic)
74 .Default(Value: false);
75}
76
77bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
78 return llvm::is_contained(Range: ValidCPUNames, Element: Name);
79}
80
81void WebAssemblyTargetInfo::fillValidCPUList(
82 SmallVectorImpl<StringRef> &Values) const {
83 Values.append(in_start: std::begin(arr: ValidCPUNames), in_end: std::end(arr: ValidCPUNames));
84}
85
86void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
87 MacroBuilder &Builder) const {
88 defineCPUMacros(Builder, CPUName: "wasm", /*Tuning=*/false);
89 if (HasAtomics)
90 Builder.defineMacro(Name: "__wasm_atomics__");
91 if (HasBulkMemory)
92 Builder.defineMacro(Name: "__wasm_bulk_memory__");
93 if (HasBulkMemoryOpt)
94 Builder.defineMacro(Name: "__wasm_bulk_memory_opt__");
95 if (HasExceptionHandling)
96 Builder.defineMacro(Name: "__wasm_exception_handling__");
97 if (HasExtendedConst)
98 Builder.defineMacro(Name: "__wasm_extended_const__");
99 if (HasMultiMemory)
100 Builder.defineMacro(Name: "__wasm_multimemory__");
101 if (HasFP16)
102 Builder.defineMacro(Name: "__wasm_fp16__");
103 if (HasGC)
104 Builder.defineMacro(Name: "__wasm_gc__");
105 if (HasMultivalue)
106 Builder.defineMacro(Name: "__wasm_multivalue__");
107 if (HasMutableGlobals)
108 Builder.defineMacro(Name: "__wasm_mutable_globals__");
109 if (HasNontrappingFPToInt)
110 Builder.defineMacro(Name: "__wasm_nontrapping_fptoint__");
111 if (HasReferenceTypes)
112 Builder.defineMacro(Name: "__wasm_reference_types__");
113 if (SIMDLevel >= RelaxedSIMD)
114 Builder.defineMacro(Name: "__wasm_relaxed_simd__");
115 if (HasSignExt)
116 Builder.defineMacro(Name: "__wasm_sign_ext__");
117 if (SIMDLevel >= SIMD128)
118 Builder.defineMacro(Name: "__wasm_simd128__");
119 if (HasTailCall)
120 Builder.defineMacro(Name: "__wasm_tail_call__");
121 if (HasWideArithmetic)
122 Builder.defineMacro(Name: "__wasm_wide_arithmetic__");
123 // Note that not all wasm features appear here. For example,
124 // HasCompatctImports
125
126 Builder.defineMacro(Name: "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
127 Builder.defineMacro(Name: "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
128 Builder.defineMacro(Name: "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
129 Builder.defineMacro(Name: "__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, Level: SIMD128, Enabled);
164 else if (Name == "relaxed-simd")
165 setSIMDLevel(Features, Level: 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, Level: RelaxedSIMD, Enabled: 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, FeatureVec: 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(a: SIMDLevel, b: 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(a: SIMDLevel, b: RelaxedSIMD);
337 continue;
338 }
339 if (Feature == "-relaxed-simd") {
340 SIMDLevel = std::min(a: SIMDLevel, b: 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(a: SIMDLevel, b: SIMD128);
353 continue;
354 }
355 if (Feature == "-simd128") {
356 SIMDLevel = std::min(a: SIMDLevel, b: 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(DiagID: 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>
403WebAssemblyTargetInfo::getTargetBuiltins() const {
404 return {{.Strings: &BuiltinStrings, .Infos: 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
420void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
421 MacroBuilder &Builder) const {
422 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
423 defineCPUMacros(Builder, CPUName: "wasm32", /*Tuning=*/false);
424}
425
426void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
427 MacroBuilder &Builder) const {
428 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
429 defineCPUMacros(Builder, CPUName: "wasm64", /*Tuning=*/false);
430}
431