clang 22.0.0git
WebAssembly.cpp
Go to the documentation of this file.
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"
18#include "llvm/ADT/StringSwitch.h"
19
20using namespace clang;
21using namespace clang::targets;
22
23static constexpr int NumBuiltins =
25
26static constexpr llvm::StringTable BuiltinStrings =
28#define BUILTIN CLANG_BUILTIN_STR_TABLE
29#define TARGET_BUILTIN CLANG_TARGET_BUILTIN_STR_TABLE
30#include "clang/Basic/BuiltinsWebAssembly.def"
31 ;
32
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("atomics", HasAtomics)
56 .Case("bulk-memory", HasBulkMemory)
57 .Case("bulk-memory-opt", HasBulkMemoryOpt)
58 .Case("call-indirect-overlong", HasCallIndirectOverlong)
59 .Case("exception-handling", HasExceptionHandling)
60 .Case("extended-const", HasExtendedConst)
61 .Case("fp16", HasFP16)
62 .Case("gc", HasGC)
63 .Case("multimemory", HasMultiMemory)
64 .Case("multivalue", HasMultivalue)
65 .Case("mutable-globals", HasMutableGlobals)
66 .Case("nontrapping-fptoint", HasNontrappingFPToInt)
67 .Case("reference-types", HasReferenceTypes)
68 .Case("relaxed-simd", SIMDLevel >= RelaxedSIMD)
69 .Case("sign-ext", HasSignExt)
70 .Case("simd128", SIMDLevel >= SIMD128)
71 .Case("tail-call", HasTailCall)
72 .Case("wide-arithmetic", HasWideArithmetic)
73 .Default(false);
74}
75
76bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
77 return llvm::is_contained(ValidCPUNames, Name);
78}
79
80void WebAssemblyTargetInfo::fillValidCPUList(
81 SmallVectorImpl<StringRef> &Values) const {
82 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
83}
84
86 MacroBuilder &Builder) const {
87 defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
88 if (HasAtomics)
89 Builder.defineMacro("__wasm_atomics__");
90 if (HasBulkMemory)
91 Builder.defineMacro("__wasm_bulk_memory__");
92 if (HasBulkMemoryOpt)
93 Builder.defineMacro("__wasm_bulk_memory_opt__");
94 if (HasExceptionHandling)
95 Builder.defineMacro("__wasm_exception_handling__");
96 if (HasExtendedConst)
97 Builder.defineMacro("__wasm_extended_const__");
98 if (HasMultiMemory)
99 Builder.defineMacro("__wasm_multimemory__");
100 if (HasFP16)
101 Builder.defineMacro("__wasm_fp16__");
102 if (HasGC)
103 Builder.defineMacro("__wasm_gc__");
104 if (HasMultivalue)
105 Builder.defineMacro("__wasm_multivalue__");
106 if (HasMutableGlobals)
107 Builder.defineMacro("__wasm_mutable_globals__");
108 if (HasNontrappingFPToInt)
109 Builder.defineMacro("__wasm_nontrapping_fptoint__");
110 if (HasReferenceTypes)
111 Builder.defineMacro("__wasm_reference_types__");
112 if (SIMDLevel >= RelaxedSIMD)
113 Builder.defineMacro("__wasm_relaxed_simd__");
114 if (HasSignExt)
115 Builder.defineMacro("__wasm_sign_ext__");
116 if (SIMDLevel >= SIMD128)
117 Builder.defineMacro("__wasm_simd128__");
118 if (HasTailCall)
119 Builder.defineMacro("__wasm_tail_call__");
120 if (HasWideArithmetic)
121 Builder.defineMacro("__wasm_wide_arithmetic__");
122
123 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
124 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
125 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
126 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
127}
128
129void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
130 SIMDEnum Level, bool Enabled) {
131 if (Enabled) {
132 switch (Level) {
133 case RelaxedSIMD:
134 Features["relaxed-simd"] = true;
135 [[fallthrough]];
136 case SIMD128:
137 Features["simd128"] = true;
138 [[fallthrough]];
139 case NoSIMD:
140 break;
141 }
142 return;
143 }
144
145 switch (Level) {
146 case NoSIMD:
147 case SIMD128:
148 Features["simd128"] = false;
149 [[fallthrough]];
150 case RelaxedSIMD:
151 Features["relaxed-simd"] = false;
152 break;
153 }
154}
155
156void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
157 StringRef Name,
158 bool Enabled) const {
159 if (Name == "simd128")
160 setSIMDLevel(Features, SIMD128, Enabled);
161 else if (Name == "relaxed-simd")
162 setSIMDLevel(Features, RelaxedSIMD, Enabled);
163 else
164 Features[Name] = Enabled;
165}
166
167bool WebAssemblyTargetInfo::initFeatureMap(
168 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
169 const std::vector<std::string> &FeaturesVec) const {
170 auto addGenericFeatures = [&]() {
171 Features["bulk-memory"] = true;
172 Features["bulk-memory-opt"] = true;
173 Features["call-indirect-overlong"] = true;
174 Features["multivalue"] = true;
175 Features["mutable-globals"] = true;
176 Features["nontrapping-fptoint"] = true;
177 Features["reference-types"] = true;
178 Features["sign-ext"] = true;
179 };
180 auto addLime1Features = [&]() {
181 // Lime1:
182 // <https://github.com/WebAssembly/tool-conventions/blob/main/Lime.md#lime1>
183 Features["bulk-memory-opt"] = true;
184 Features["call-indirect-overlong"] = true;
185 Features["extended-const"] = true;
186 Features["multivalue"] = true;
187 Features["mutable-globals"] = true;
188 Features["nontrapping-fptoint"] = true;
189 Features["sign-ext"] = true;
190 };
191 auto addBleedingEdgeFeatures = [&]() {
192 addGenericFeatures();
193 Features["atomics"] = true;
194 Features["exception-handling"] = true;
195 Features["extended-const"] = true;
196 Features["fp16"] = true;
197 Features["gc"] = true;
198 Features["multimemory"] = true;
199 Features["tail-call"] = true;
200 Features["wide-arithmetic"] = true;
201 setSIMDLevel(Features, RelaxedSIMD, true);
202 };
203 if (CPU == "generic") {
204 addGenericFeatures();
205 } else if (CPU == "lime1") {
206 addLime1Features();
207 } else if (CPU == "bleeding-edge") {
208 addBleedingEdgeFeatures();
209 }
210
211 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
212}
213
214bool WebAssemblyTargetInfo::handleTargetFeatures(
215 std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
216 for (const auto &Feature : Features) {
217 if (Feature == "+atomics") {
218 HasAtomics = true;
219 continue;
220 }
221 if (Feature == "-atomics") {
222 HasAtomics = false;
223 continue;
224 }
225 if (Feature == "+bulk-memory") {
226 HasBulkMemory = true;
227 continue;
228 }
229 if (Feature == "-bulk-memory") {
230 HasBulkMemory = false;
231 continue;
232 }
233 if (Feature == "+bulk-memory-opt") {
234 HasBulkMemoryOpt = true;
235 continue;
236 }
237 if (Feature == "-bulk-memory-opt") {
238 HasBulkMemoryOpt = false;
239 continue;
240 }
241 if (Feature == "+call-indirect-overlong") {
242 HasCallIndirectOverlong = true;
243 continue;
244 }
245 if (Feature == "-call-indirect-overlong") {
246 HasCallIndirectOverlong = false;
247 continue;
248 }
249 if (Feature == "+exception-handling") {
250 HasExceptionHandling = true;
251 continue;
252 }
253 if (Feature == "-exception-handling") {
254 HasExceptionHandling = false;
255 continue;
256 }
257 if (Feature == "+extended-const") {
258 HasExtendedConst = true;
259 continue;
260 }
261 if (Feature == "-extended-const") {
262 HasExtendedConst = false;
263 continue;
264 }
265 if (Feature == "+fp16") {
266 SIMDLevel = std::max(SIMDLevel, SIMD128);
267 HasFP16 = true;
268 continue;
269 }
270 if (Feature == "-fp16") {
271 HasFP16 = false;
272 continue;
273 }
274 if (Feature == "+gc") {
275 HasGC = true;
276 continue;
277 }
278 if (Feature == "-gc") {
279 HasGC = false;
280 continue;
281 }
282 if (Feature == "+multimemory") {
283 HasMultiMemory = true;
284 continue;
285 }
286 if (Feature == "-multimemory") {
287 HasMultiMemory = false;
288 continue;
289 }
290 if (Feature == "+multivalue") {
291 HasMultivalue = true;
292 continue;
293 }
294 if (Feature == "-multivalue") {
295 HasMultivalue = false;
296 continue;
297 }
298 if (Feature == "+mutable-globals") {
299 HasMutableGlobals = true;
300 continue;
301 }
302 if (Feature == "-mutable-globals") {
303 HasMutableGlobals = false;
304 continue;
305 }
306 if (Feature == "+nontrapping-fptoint") {
307 HasNontrappingFPToInt = true;
308 continue;
309 }
310 if (Feature == "-nontrapping-fptoint") {
311 HasNontrappingFPToInt = false;
312 continue;
313 }
314 if (Feature == "+reference-types") {
315 HasReferenceTypes = true;
316 continue;
317 }
318 if (Feature == "-reference-types") {
319 HasReferenceTypes = false;
320 continue;
321 }
322 if (Feature == "+relaxed-simd") {
323 SIMDLevel = std::max(SIMDLevel, RelaxedSIMD);
324 continue;
325 }
326 if (Feature == "-relaxed-simd") {
327 SIMDLevel = std::min(SIMDLevel, SIMDEnum(RelaxedSIMD - 1));
328 continue;
329 }
330 if (Feature == "+sign-ext") {
331 HasSignExt = true;
332 continue;
333 }
334 if (Feature == "-sign-ext") {
335 HasSignExt = false;
336 continue;
337 }
338 if (Feature == "+simd128") {
339 SIMDLevel = std::max(SIMDLevel, SIMD128);
340 continue;
341 }
342 if (Feature == "-simd128") {
343 SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
344 continue;
345 }
346 if (Feature == "+tail-call") {
347 HasTailCall = true;
348 continue;
349 }
350 if (Feature == "-tail-call") {
351 HasTailCall = false;
352 continue;
353 }
354 if (Feature == "+wide-arithmetic") {
355 HasWideArithmetic = true;
356 continue;
357 }
358 if (Feature == "-wide-arithmetic") {
359 HasWideArithmetic = false;
360 continue;
361 }
362
363 Diags.Report(diag::err_opt_not_valid_with_opt)
364 << Feature << "-target-feature";
365 return false;
366 }
367
368 // gc implies reference-types
369 if (HasGC) {
370 HasReferenceTypes = true;
371 }
372
373 // bulk-memory-opt is a subset of bulk-memory.
374 if (HasBulkMemory) {
375 HasBulkMemoryOpt = true;
376 }
377
378 // The reference-types feature included the change to `call_indirect`
379 // encodings to support overlong immediates.
380 if (HasReferenceTypes) {
381 HasCallIndirectOverlong = true;
382 }
383
384 return true;
385}
386
387llvm::SmallVector<Builtin::InfosShard>
389 return {{&BuiltinStrings, BuiltinInfos}};
390}
391
392void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts,
393 const TargetInfo *Aux) {
394 TargetInfo::adjust(Diags, Opts, Aux);
395 // Turn off POSIXThreads and ThreadModel so that we don't predefine _REENTRANT
396 // or __STDCPP_THREADS__ if we will eventually end up stripping atomics
397 // because they are unsupported.
398 if (!HasAtomics || !HasBulkMemory) {
399 Opts.POSIXThreads = false;
400 Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
401 Opts.ThreadsafeStatics = false;
402 }
403}
404
406 MacroBuilder &Builder) const {
408 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
409}
410
412 MacroBuilder &Builder) const {
414 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
415}
Defines the Diagnostic-related interfaces.
static constexpr llvm::StringTable BuiltinStrings
Definition AMDGPU.cpp:101
static constexpr llvm::StringLiteral ValidCPUNames[]
Definition BPF.cpp:81
static constexpr Builtin::Info BuiltinInfos[]
Definition Builtins.cpp:38
static constexpr unsigned NumBuiltins
Definition Builtins.cpp:32
Defines enum values for all the target-independent builtin functions.
#define CLANG_BUILTIN_STR_TABLE_START
Definition Builtins.h:158
Enumerates target-specific builtins in their own namespaces within namespace clang.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
@ Single
Single Threaded Environment.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
virtual llvm::SmallVector< Builtin::InfosShard > getTargetBuiltins() const =0
Return information about target-specific builtins for the current primary target, and info about whic...
virtual void adjust(DiagnosticsEngine &Diags, LangOptions &Opts, const TargetInfo *Aux)
Set forced language options.
virtual bool initFeatureMap(llvm::StringMap< bool > &Features, DiagnosticsEngine &Diags, StringRef CPU, const std::vector< std::string > &FeatureVec) const
Initialize the map with the default set of target features for the CPU this should include all legal ...
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
StringRef getABI() const override
Get the ABI currently in use.
bool setABI(const std::string &Name) override
Use the specified ABI.
static constexpr std::array< Info, N > MakeInfos(std::array< Info, N > Infos)
A constexpr function to construct an infos array from X-macros.
Definition Builtins.h:107
LLVM_LIBRARY_VISIBILITY void defineCPUMacros(clang::MacroBuilder &Builder, llvm::StringRef CPUName, bool Tuning=true)
The JSON file list parser is used to communicate input to InstallAPI.