xref: /llvm-project/clang/lib/Basic/Targets/WebAssembly.cpp (revision d9fd0ddef38bb9d5cce7300ff820272183c09fcd)
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("simd128", SIMDLevel >= SIMD128)
49       .Case("relaxed-simd", SIMDLevel >= RelaxedSIMD)
50       .Case("half-precision", HasHalfPrecision)
51       .Case("nontrapping-fptoint", HasNontrappingFPToInt)
52       .Case("sign-ext", HasSignExt)
53       .Case("exception-handling", HasExceptionHandling)
54       .Case("bulk-memory", HasBulkMemory)
55       .Case("atomics", HasAtomics)
56       .Case("mutable-globals", HasMutableGlobals)
57       .Case("multivalue", HasMultivalue)
58       .Case("tail-call", HasTailCall)
59       .Case("reference-types", HasReferenceTypes)
60       .Case("extended-const", HasExtendedConst)
61       .Case("multimemory", HasMultiMemory)
62       .Default(false);
63 }
64 
65 bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
66   return llvm::is_contained(ValidCPUNames, Name);
67 }
68 
69 void WebAssemblyTargetInfo::fillValidCPUList(
70     SmallVectorImpl<StringRef> &Values) const {
71   Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
72 }
73 
74 void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
75                                              MacroBuilder &Builder) const {
76   defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
77   if (SIMDLevel >= SIMD128)
78     Builder.defineMacro("__wasm_simd128__");
79   if (SIMDLevel >= RelaxedSIMD)
80     Builder.defineMacro("__wasm_relaxed_simd__");
81   if (HasNontrappingFPToInt)
82     Builder.defineMacro("__wasm_nontrapping_fptoint__");
83   if (HasSignExt)
84     Builder.defineMacro("__wasm_sign_ext__");
85   if (HasExceptionHandling)
86     Builder.defineMacro("__wasm_exception_handling__");
87   if (HasBulkMemory)
88     Builder.defineMacro("__wasm_bulk_memory__");
89   if (HasAtomics)
90     Builder.defineMacro("__wasm_atomics__");
91   if (HasMutableGlobals)
92     Builder.defineMacro("__wasm_mutable_globals__");
93   if (HasMultivalue)
94     Builder.defineMacro("__wasm_multivalue__");
95   if (HasTailCall)
96     Builder.defineMacro("__wasm_tail_call__");
97   if (HasReferenceTypes)
98     Builder.defineMacro("__wasm_reference_types__");
99   if (HasExtendedConst)
100     Builder.defineMacro("__wasm_extended_const__");
101   if (HasMultiMemory)
102     Builder.defineMacro("__wasm_multimemory__");
103 
104   Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
105   Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
106   Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
107   Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
108 }
109 
110 void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
111                                          SIMDEnum Level, bool Enabled) {
112   if (Enabled) {
113     switch (Level) {
114     case RelaxedSIMD:
115       Features["relaxed-simd"] = true;
116       [[fallthrough]];
117     case SIMD128:
118       Features["simd128"] = true;
119       [[fallthrough]];
120     case NoSIMD:
121       break;
122     }
123     return;
124   }
125 
126   switch (Level) {
127   case NoSIMD:
128   case SIMD128:
129     Features["simd128"] = false;
130     [[fallthrough]];
131   case RelaxedSIMD:
132     Features["relaxed-simd"] = false;
133     break;
134   }
135 }
136 
137 void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
138                                               StringRef Name,
139                                               bool Enabled) const {
140   if (Name == "simd128")
141     setSIMDLevel(Features, SIMD128, Enabled);
142   else if (Name == "relaxed-simd")
143     setSIMDLevel(Features, RelaxedSIMD, Enabled);
144   else
145     Features[Name] = Enabled;
146 }
147 
148 bool WebAssemblyTargetInfo::initFeatureMap(
149     llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
150     const std::vector<std::string> &FeaturesVec) const {
151   if (CPU == "bleeding-edge") {
152     Features["atomics"] = true;
153     Features["bulk-memory"] = true;
154     Features["multimemory"] = true;
155     Features["mutable-globals"] = true;
156     Features["nontrapping-fptoint"] = true;
157     Features["reference-types"] = true;
158     Features["sign-ext"] = true;
159     Features["tail-call"] = true;
160     Features["half-precision"] = true;
161     setSIMDLevel(Features, SIMD128, true);
162   } else if (CPU == "generic") {
163     Features["mutable-globals"] = true;
164     Features["sign-ext"] = true;
165   }
166 
167   return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
168 }
169 
170 bool WebAssemblyTargetInfo::handleTargetFeatures(
171     std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
172   for (const auto &Feature : Features) {
173     if (Feature == "+simd128") {
174       SIMDLevel = std::max(SIMDLevel, SIMD128);
175       continue;
176     }
177     if (Feature == "-simd128") {
178       SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
179       continue;
180     }
181     if (Feature == "+relaxed-simd") {
182       SIMDLevel = std::max(SIMDLevel, RelaxedSIMD);
183       continue;
184     }
185     if (Feature == "-relaxed-simd") {
186       SIMDLevel = std::min(SIMDLevel, SIMDEnum(RelaxedSIMD - 1));
187       continue;
188     }
189     if (Feature == "+nontrapping-fptoint") {
190       HasNontrappingFPToInt = true;
191       continue;
192     }
193     if (Feature == "-nontrapping-fptoint") {
194       HasNontrappingFPToInt = false;
195       continue;
196     }
197     if (Feature == "+sign-ext") {
198       HasSignExt = true;
199       continue;
200     }
201     if (Feature == "-sign-ext") {
202       HasSignExt = false;
203       continue;
204     }
205     if (Feature == "+exception-handling") {
206       HasExceptionHandling = true;
207       continue;
208     }
209     if (Feature == "-exception-handling") {
210       HasExceptionHandling = false;
211       continue;
212     }
213     if (Feature == "+bulk-memory") {
214       HasBulkMemory = true;
215       continue;
216     }
217     if (Feature == "-bulk-memory") {
218       HasBulkMemory = false;
219       continue;
220     }
221     if (Feature == "+half-precision") {
222       SIMDLevel = std::max(SIMDLevel, SIMD128);
223       HasHalfPrecision = true;
224       continue;
225     }
226     if (Feature == "-half-precision") {
227       HasHalfPrecision = false;
228       continue;
229     }
230     if (Feature == "+atomics") {
231       HasAtomics = true;
232       continue;
233     }
234     if (Feature == "-atomics") {
235       HasAtomics = false;
236       continue;
237     }
238     if (Feature == "+mutable-globals") {
239       HasMutableGlobals = true;
240       continue;
241     }
242     if (Feature == "-mutable-globals") {
243       HasMutableGlobals = false;
244       continue;
245     }
246     if (Feature == "+multivalue") {
247       HasMultivalue = true;
248       continue;
249     }
250     if (Feature == "-multivalue") {
251       HasMultivalue = false;
252       continue;
253     }
254     if (Feature == "+tail-call") {
255       HasTailCall = true;
256       continue;
257     }
258     if (Feature == "-tail-call") {
259       HasTailCall = false;
260       continue;
261     }
262     if (Feature == "+reference-types") {
263       HasReferenceTypes = true;
264       continue;
265     }
266     if (Feature == "-reference-types") {
267       HasReferenceTypes = 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 == "+multimemory") {
279       HasMultiMemory = true;
280       continue;
281     }
282     if (Feature == "-multimemory") {
283       HasMultiMemory = false;
284       continue;
285     }
286 
287     Diags.Report(diag::err_opt_not_valid_with_opt)
288         << Feature << "-target-feature";
289     return false;
290   }
291   return true;
292 }
293 
294 ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
295   return llvm::ArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
296                                          Builtin::FirstTSBuiltin);
297 }
298 
299 void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags,
300                                    LangOptions &Opts) {
301   TargetInfo::adjust(Diags, Opts);
302   // Turn off POSIXThreads and ThreadModel so that we don't predefine _REENTRANT
303   // or __STDCPP_THREADS__ if we will eventually end up stripping atomics
304   // because they are unsupported.
305   if (!HasAtomics || !HasBulkMemory) {
306     Opts.POSIXThreads = false;
307     Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
308     Opts.ThreadsafeStatics = false;
309   }
310 }
311 
312 void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
313                                                MacroBuilder &Builder) const {
314   WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
315   defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
316 }
317 
318 void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
319                                                MacroBuilder &Builder) const {
320   WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
321   defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
322 }
323