xref: /llvm-project/clang/lib/Basic/Targets/WebAssembly.cpp (revision c3536b263f253a69fb336fb0617ee33a01a5c5dd)
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"}};
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 addBleedingEdgeFeatures = [&]() {
171     addGenericFeatures();
172     Features["atomics"] = true;
173     Features["exception-handling"] = true;
174     Features["extended-const"] = true;
175     Features["fp16"] = true;
176     Features["multimemory"] = true;
177     Features["tail-call"] = true;
178     Features["wide-arithmetic"] = true;
179     setSIMDLevel(Features, RelaxedSIMD, true);
180   };
181   if (CPU == "generic") {
182     addGenericFeatures();
183   } else if (CPU == "bleeding-edge") {
184     addBleedingEdgeFeatures();
185   }
186 
187   return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
188 }
189 
190 bool WebAssemblyTargetInfo::handleTargetFeatures(
191     std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
192   for (const auto &Feature : Features) {
193     if (Feature == "+atomics") {
194       HasAtomics = true;
195       continue;
196     }
197     if (Feature == "-atomics") {
198       HasAtomics = false;
199       continue;
200     }
201     if (Feature == "+bulk-memory") {
202       HasBulkMemory = true;
203       continue;
204     }
205     if (Feature == "-bulk-memory") {
206       HasBulkMemory = false;
207       continue;
208     }
209     if (Feature == "+bulk-memory-opt") {
210       HasBulkMemoryOpt = true;
211       continue;
212     }
213     if (Feature == "-bulk-memory-opt") {
214       HasBulkMemoryOpt = false;
215       continue;
216     }
217     if (Feature == "+call-indirect-overlong") {
218       HasCallIndirectOverlong = true;
219       continue;
220     }
221     if (Feature == "-call-indirect-overlong") {
222       HasCallIndirectOverlong = false;
223       continue;
224     }
225     if (Feature == "+exception-handling") {
226       HasExceptionHandling = true;
227       continue;
228     }
229     if (Feature == "-exception-handling") {
230       HasExceptionHandling = false;
231       continue;
232     }
233     if (Feature == "+extended-const") {
234       HasExtendedConst = true;
235       continue;
236     }
237     if (Feature == "-extended-const") {
238       HasExtendedConst = false;
239       continue;
240     }
241     if (Feature == "+fp16") {
242       SIMDLevel = std::max(SIMDLevel, SIMD128);
243       HasFP16 = true;
244       continue;
245     }
246     if (Feature == "-fp16") {
247       HasFP16 = false;
248       continue;
249     }
250     if (Feature == "+multimemory") {
251       HasMultiMemory = true;
252       continue;
253     }
254     if (Feature == "-multimemory") {
255       HasMultiMemory = false;
256       continue;
257     }
258     if (Feature == "+multivalue") {
259       HasMultivalue = true;
260       continue;
261     }
262     if (Feature == "-multivalue") {
263       HasMultivalue = false;
264       continue;
265     }
266     if (Feature == "+mutable-globals") {
267       HasMutableGlobals = true;
268       continue;
269     }
270     if (Feature == "-mutable-globals") {
271       HasMutableGlobals = false;
272       continue;
273     }
274     if (Feature == "+nontrapping-fptoint") {
275       HasNontrappingFPToInt = true;
276       continue;
277     }
278     if (Feature == "-nontrapping-fptoint") {
279       HasNontrappingFPToInt = false;
280       continue;
281     }
282     if (Feature == "+reference-types") {
283       HasReferenceTypes = true;
284       continue;
285     }
286     if (Feature == "-reference-types") {
287       HasReferenceTypes = false;
288       continue;
289     }
290     if (Feature == "+relaxed-simd") {
291       SIMDLevel = std::max(SIMDLevel, RelaxedSIMD);
292       continue;
293     }
294     if (Feature == "-relaxed-simd") {
295       SIMDLevel = std::min(SIMDLevel, SIMDEnum(RelaxedSIMD - 1));
296       continue;
297     }
298     if (Feature == "+sign-ext") {
299       HasSignExt = true;
300       continue;
301     }
302     if (Feature == "-sign-ext") {
303       HasSignExt = false;
304       continue;
305     }
306     if (Feature == "+simd128") {
307       SIMDLevel = std::max(SIMDLevel, SIMD128);
308       continue;
309     }
310     if (Feature == "-simd128") {
311       SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
312       continue;
313     }
314     if (Feature == "+tail-call") {
315       HasTailCall = true;
316       continue;
317     }
318     if (Feature == "-tail-call") {
319       HasTailCall = false;
320       continue;
321     }
322     if (Feature == "+wide-arithmetic") {
323       HasWideArithmetic = true;
324       continue;
325     }
326     if (Feature == "-wide-arithmetic") {
327       HasWideArithmetic = false;
328       continue;
329     }
330 
331     Diags.Report(diag::err_opt_not_valid_with_opt)
332         << Feature << "-target-feature";
333     return false;
334   }
335 
336   // bulk-memory-opt is a subset of bulk-memory.
337   if (HasBulkMemory) {
338     HasBulkMemoryOpt = true;
339   }
340 
341   // The reference-types feature included the change to `call_indirect`
342   // encodings to support overlong immediates.
343   if (HasReferenceTypes) {
344     HasCallIndirectOverlong = true;
345   }
346 
347   return true;
348 }
349 
350 ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
351   return llvm::ArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
352                                          Builtin::FirstTSBuiltin);
353 }
354 
355 void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags,
356                                    LangOptions &Opts) {
357   TargetInfo::adjust(Diags, Opts);
358   // Turn off POSIXThreads and ThreadModel so that we don't predefine _REENTRANT
359   // or __STDCPP_THREADS__ if we will eventually end up stripping atomics
360   // because they are unsupported.
361   if (!HasAtomics || !HasBulkMemory) {
362     Opts.POSIXThreads = false;
363     Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
364     Opts.ThreadsafeStatics = false;
365   }
366 }
367 
368 void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
369                                                MacroBuilder &Builder) const {
370   WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
371   defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
372 }
373 
374 void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
375                                                MacroBuilder &Builder) const {
376   WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
377   defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
378 }
379