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