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