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