1 //===--- AArch64.cpp - AArch64 (not ARM) Helpers for Tools ------*- C++ -*-===// 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 #include "AArch64.h" 10 #include "clang/Driver/Driver.h" 11 #include "clang/Driver/DriverDiagnostic.h" 12 #include "clang/Driver/Options.h" 13 #include "llvm/Option/ArgList.h" 14 #include "llvm/Support/TargetParser.h" 15 #include "llvm/Support/Host.h" 16 17 using namespace clang::driver; 18 using namespace clang::driver::tools; 19 using namespace clang; 20 using namespace llvm::opt; 21 22 /// \returns true if the given triple can determine the default CPU type even 23 /// if -arch is not specified. 24 static bool isCPUDeterminedByTriple(const llvm::Triple &Triple) { 25 return Triple.isOSDarwin(); 26 } 27 28 /// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are 29 /// targeting. Set \p A to the Arg corresponding to the -mcpu argument if it is 30 /// provided, or to nullptr otherwise. 31 std::string aarch64::getAArch64TargetCPU(const ArgList &Args, 32 const llvm::Triple &Triple, Arg *&A) { 33 std::string CPU; 34 // If we have -mcpu, use that. 35 if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) { 36 StringRef Mcpu = A->getValue(); 37 CPU = Mcpu.split("+").first.lower(); 38 } 39 40 // Handle CPU name is 'native'. 41 if (CPU == "native") 42 return std::string(llvm::sys::getHostCPUName()); 43 44 if (CPU.size()) 45 return CPU; 46 47 if (Triple.isTargetMachineMac() && 48 Triple.getArch() == llvm::Triple::aarch64) { 49 // Apple Silicon macs default to M1 CPUs. 50 return "apple-m1"; 51 } 52 53 // arm64e requires v8.3a and only runs on apple-a12 and later CPUs. 54 if (Triple.isArm64e()) 55 return "apple-a12"; 56 57 // Make sure we pick the appropriate Apple CPU if -arch is used or when 58 // targetting a Darwin OS. 59 if (Args.getLastArg(options::OPT_arch) || Triple.isOSDarwin()) 60 return Triple.getArch() == llvm::Triple::aarch64_32 ? "apple-s4" 61 : "apple-a7"; 62 63 return "generic"; 64 } 65 66 // Decode AArch64 features from string like +[no]featureA+[no]featureB+... 67 static bool DecodeAArch64Features(const Driver &D, StringRef text, 68 std::vector<StringRef> &Features, 69 llvm::AArch64::ArchKind ArchKind) { 70 SmallVector<StringRef, 8> Split; 71 text.split(Split, StringRef("+"), -1, false); 72 73 for (StringRef Feature : Split) { 74 StringRef FeatureName = llvm::AArch64::getArchExtFeature(Feature); 75 if (!FeatureName.empty()) 76 Features.push_back(FeatureName); 77 else if (Feature == "neon" || Feature == "noneon") 78 D.Diag(clang::diag::err_drv_no_neon_modifier); 79 else 80 return false; 81 82 // +sve implies +f32mm if the base architecture is v8.6A or v8.7A 83 // it isn't the case in general that sve implies both f64mm and f32mm 84 if ((ArchKind == llvm::AArch64::ArchKind::ARMV8_6A || 85 ArchKind == llvm::AArch64::ArchKind::ARMV8_7A) && Feature == "sve") 86 Features.push_back("+f32mm"); 87 } 88 return true; 89 } 90 91 // Check if the CPU name and feature modifiers in -mcpu are legal. If yes, 92 // decode CPU and feature. 93 static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU, 94 std::vector<StringRef> &Features) { 95 std::pair<StringRef, StringRef> Split = Mcpu.split("+"); 96 CPU = Split.first; 97 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::ArchKind::ARMV8A; 98 99 if (CPU == "native") 100 CPU = llvm::sys::getHostCPUName(); 101 102 if (CPU == "generic") { 103 Features.push_back("+neon"); 104 } else { 105 ArchKind = llvm::AArch64::parseCPUArch(CPU); 106 if (!llvm::AArch64::getArchFeatures(ArchKind, Features)) 107 return false; 108 109 uint64_t Extension = llvm::AArch64::getDefaultExtensions(CPU, ArchKind); 110 if (!llvm::AArch64::getExtensionFeatures(Extension, Features)) 111 return false; 112 } 113 114 if (Split.second.size() && 115 !DecodeAArch64Features(D, Split.second, Features, ArchKind)) 116 return false; 117 118 return true; 119 } 120 121 static bool 122 getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March, 123 const ArgList &Args, 124 std::vector<StringRef> &Features) { 125 std::string MarchLowerCase = March.lower(); 126 std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+"); 127 128 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseArch(Split.first); 129 if (ArchKind == llvm::AArch64::ArchKind::INVALID || 130 !llvm::AArch64::getArchFeatures(ArchKind, Features) || 131 (Split.second.size() && 132 !DecodeAArch64Features(D, Split.second, Features, ArchKind))) 133 return false; 134 135 return true; 136 } 137 138 static bool 139 getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu, 140 const ArgList &Args, 141 std::vector<StringRef> &Features) { 142 StringRef CPU; 143 std::string McpuLowerCase = Mcpu.lower(); 144 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, Features)) 145 return false; 146 147 return true; 148 } 149 150 static bool 151 getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune, 152 const ArgList &Args, 153 std::vector<StringRef> &Features) { 154 std::string MtuneLowerCase = Mtune.lower(); 155 // Check CPU name is valid 156 std::vector<StringRef> MtuneFeatures; 157 StringRef Tune; 158 if (!DecodeAArch64Mcpu(D, MtuneLowerCase, Tune, MtuneFeatures)) 159 return false; 160 161 // Handle CPU name is 'native'. 162 if (MtuneLowerCase == "native") 163 MtuneLowerCase = std::string(llvm::sys::getHostCPUName()); 164 if (MtuneLowerCase == "cyclone" || 165 StringRef(MtuneLowerCase).startswith("apple")) { 166 Features.push_back("+zcm"); 167 Features.push_back("+zcz"); 168 } 169 return true; 170 } 171 172 static bool 173 getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu, 174 const ArgList &Args, 175 std::vector<StringRef> &Features) { 176 StringRef CPU; 177 std::vector<StringRef> DecodedFeature; 178 std::string McpuLowerCase = Mcpu.lower(); 179 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, DecodedFeature)) 180 return false; 181 182 return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features); 183 } 184 185 void aarch64::getAArch64TargetFeatures(const Driver &D, 186 const llvm::Triple &Triple, 187 const ArgList &Args, 188 std::vector<StringRef> &Features) { 189 Arg *A; 190 bool success = true; 191 // Enable NEON by default. 192 Features.push_back("+neon"); 193 if ((A = Args.getLastArg(options::OPT_march_EQ))) 194 success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features); 195 else if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) 196 success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features); 197 else if (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple)) 198 success = getAArch64ArchFeaturesFromMcpu( 199 D, getAArch64TargetCPU(Args, Triple, A), Args, Features); 200 201 if (success && (A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ))) 202 success = 203 getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features); 204 else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ))) 205 success = 206 getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features); 207 else if (success && 208 (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple))) 209 success = getAArch64MicroArchFeaturesFromMcpu( 210 D, getAArch64TargetCPU(Args, Triple, A), Args, Features); 211 212 if (!success) 213 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args); 214 215 if (Args.getLastArg(options::OPT_mgeneral_regs_only)) { 216 Features.push_back("-fp-armv8"); 217 Features.push_back("-crypto"); 218 Features.push_back("-neon"); 219 } 220 221 if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) { 222 StringRef Mtp = A->getValue(); 223 if (Mtp == "el3") 224 Features.push_back("+tpidr-el3"); 225 else if (Mtp == "el2") 226 Features.push_back("+tpidr-el2"); 227 else if (Mtp == "el1") 228 Features.push_back("+tpidr-el1"); 229 else if (Mtp != "el0") 230 D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args); 231 } 232 233 // Enable/disable straight line speculation hardening. 234 if (Arg *A = Args.getLastArg(options::OPT_mharden_sls_EQ)) { 235 StringRef Scope = A->getValue(); 236 bool EnableRetBr = false; 237 bool EnableBlr = false; 238 bool DisableComdat = false; 239 if (Scope != "none") { 240 SmallVector<StringRef, 4> Opts; 241 Scope.split(Opts, ","); 242 for (auto Opt : Opts) { 243 Opt = Opt.trim(); 244 if (Opt == "all") { 245 EnableBlr = true; 246 EnableRetBr = true; 247 continue; 248 } 249 if (Opt == "retbr") { 250 EnableRetBr = true; 251 continue; 252 } 253 if (Opt == "blr") { 254 EnableBlr = true; 255 continue; 256 } 257 if (Opt == "comdat") { 258 DisableComdat = false; 259 continue; 260 } 261 if (Opt == "nocomdat") { 262 DisableComdat = true; 263 continue; 264 } 265 D.Diag(diag::err_invalid_sls_hardening) 266 << Scope << A->getAsString(Args); 267 break; 268 } 269 } 270 271 if (EnableRetBr) 272 Features.push_back("+harden-sls-retbr"); 273 if (EnableBlr) 274 Features.push_back("+harden-sls-blr"); 275 if (DisableComdat) { 276 Features.push_back("+harden-sls-nocomdat"); 277 } 278 } 279 280 // En/disable crc 281 if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) { 282 if (A->getOption().matches(options::OPT_mcrc)) 283 Features.push_back("+crc"); 284 else 285 Features.push_back("-crc"); 286 } 287 288 // Handle (arch-dependent) fp16fml/fullfp16 relationship. 289 // FIXME: this fp16fml option handling will be reimplemented after the 290 // TargetParser rewrite. 291 const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16"); 292 const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml"); 293 if (llvm::is_contained(Features, "+v8.4a")) { 294 const auto ItRFullFP16 = std::find(Features.rbegin(), Features.rend(), "+fullfp16"); 295 if (ItRFullFP16 < ItRNoFullFP16 && ItRFullFP16 < ItRFP16FML) { 296 // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml. 297 // Only append the +fp16fml if there is no -fp16fml after the +fullfp16. 298 if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16) 299 Features.push_back("+fp16fml"); 300 } 301 else 302 goto fp16_fml_fallthrough; 303 } else { 304 fp16_fml_fallthrough: 305 // In both of these cases, putting the 'other' feature on the end of the vector will 306 // result in the same effect as placing it immediately after the current feature. 307 if (ItRNoFullFP16 < ItRFP16FML) 308 Features.push_back("-fp16fml"); 309 else if (ItRNoFullFP16 > ItRFP16FML) 310 Features.push_back("+fullfp16"); 311 } 312 313 // FIXME: this needs reimplementation too after the TargetParser rewrite 314 // 315 // Context sensitive meaning of Crypto: 316 // 1) For Arch >= ARMv8.4a: crypto = sm4 + sha3 + sha2 + aes 317 // 2) For Arch <= ARMv8.3a: crypto = sha2 + aes 318 const auto ItBegin = Features.begin(); 319 const auto ItEnd = Features.end(); 320 const auto ItRBegin = Features.rbegin(); 321 const auto ItREnd = Features.rend(); 322 const auto ItRCrypto = std::find(ItRBegin, ItREnd, "+crypto"); 323 const auto ItRNoCrypto = std::find(ItRBegin, ItREnd, "-crypto"); 324 const auto HasCrypto = ItRCrypto != ItREnd; 325 const auto HasNoCrypto = ItRNoCrypto != ItREnd; 326 const ptrdiff_t PosCrypto = ItRCrypto - ItRBegin; 327 const ptrdiff_t PosNoCrypto = ItRNoCrypto - ItRBegin; 328 329 bool NoCrypto = false; 330 if (HasCrypto && HasNoCrypto) { 331 if (PosNoCrypto < PosCrypto) 332 NoCrypto = true; 333 } 334 335 if (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd) { 336 if (HasCrypto && !NoCrypto) { 337 // Check if we have NOT disabled an algorithm with something like: 338 // +crypto, -algorithm 339 // And if "-algorithm" does not occur, we enable that crypto algorithm. 340 const bool HasSM4 = (std::find(ItBegin, ItEnd, "-sm4") == ItEnd); 341 const bool HasSHA3 = (std::find(ItBegin, ItEnd, "-sha3") == ItEnd); 342 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd); 343 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd); 344 if (HasSM4) 345 Features.push_back("+sm4"); 346 if (HasSHA3) 347 Features.push_back("+sha3"); 348 if (HasSHA2) 349 Features.push_back("+sha2"); 350 if (HasAES) 351 Features.push_back("+aes"); 352 } else if (HasNoCrypto) { 353 // Check if we have NOT enabled a crypto algorithm with something like: 354 // -crypto, +algorithm 355 // And if "+algorithm" does not occur, we disable that crypto algorithm. 356 const bool HasSM4 = (std::find(ItBegin, ItEnd, "+sm4") != ItEnd); 357 const bool HasSHA3 = (std::find(ItBegin, ItEnd, "+sha3") != ItEnd); 358 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd); 359 const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd); 360 if (!HasSM4) 361 Features.push_back("-sm4"); 362 if (!HasSHA3) 363 Features.push_back("-sha3"); 364 if (!HasSHA2) 365 Features.push_back("-sha2"); 366 if (!HasAES) 367 Features.push_back("-aes"); 368 } 369 } else { 370 if (HasCrypto && !NoCrypto) { 371 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd); 372 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd); 373 if (HasSHA2) 374 Features.push_back("+sha2"); 375 if (HasAES) 376 Features.push_back("+aes"); 377 } else if (HasNoCrypto) { 378 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd); 379 const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd); 380 const bool HasV82a = (std::find(ItBegin, ItEnd, "+v8.2a") != ItEnd); 381 const bool HasV83a = (std::find(ItBegin, ItEnd, "+v8.3a") != ItEnd); 382 const bool HasV84a = (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd); 383 if (!HasSHA2) 384 Features.push_back("-sha2"); 385 if (!HasAES) 386 Features.push_back("-aes"); 387 if (HasV82a || HasV83a || HasV84a) { 388 Features.push_back("-sm4"); 389 Features.push_back("-sha3"); 390 } 391 } 392 } 393 394 auto V8_6Pos = llvm::find(Features, "+v8.6a"); 395 if (V8_6Pos != std::end(Features)) 396 V8_6Pos = Features.insert(std::next(V8_6Pos), {"+i8mm", "+bf16"}); 397 398 if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access, 399 options::OPT_munaligned_access)) { 400 if (A->getOption().matches(options::OPT_mno_unaligned_access)) 401 Features.push_back("+strict-align"); 402 } else if (Triple.isOSOpenBSD()) 403 Features.push_back("+strict-align"); 404 405 if (Args.hasArg(options::OPT_ffixed_x1)) 406 Features.push_back("+reserve-x1"); 407 408 if (Args.hasArg(options::OPT_ffixed_x2)) 409 Features.push_back("+reserve-x2"); 410 411 if (Args.hasArg(options::OPT_ffixed_x3)) 412 Features.push_back("+reserve-x3"); 413 414 if (Args.hasArg(options::OPT_ffixed_x4)) 415 Features.push_back("+reserve-x4"); 416 417 if (Args.hasArg(options::OPT_ffixed_x5)) 418 Features.push_back("+reserve-x5"); 419 420 if (Args.hasArg(options::OPT_ffixed_x6)) 421 Features.push_back("+reserve-x6"); 422 423 if (Args.hasArg(options::OPT_ffixed_x7)) 424 Features.push_back("+reserve-x7"); 425 426 if (Args.hasArg(options::OPT_ffixed_x9)) 427 Features.push_back("+reserve-x9"); 428 429 if (Args.hasArg(options::OPT_ffixed_x10)) 430 Features.push_back("+reserve-x10"); 431 432 if (Args.hasArg(options::OPT_ffixed_x11)) 433 Features.push_back("+reserve-x11"); 434 435 if (Args.hasArg(options::OPT_ffixed_x12)) 436 Features.push_back("+reserve-x12"); 437 438 if (Args.hasArg(options::OPT_ffixed_x13)) 439 Features.push_back("+reserve-x13"); 440 441 if (Args.hasArg(options::OPT_ffixed_x14)) 442 Features.push_back("+reserve-x14"); 443 444 if (Args.hasArg(options::OPT_ffixed_x15)) 445 Features.push_back("+reserve-x15"); 446 447 if (Args.hasArg(options::OPT_ffixed_x18)) 448 Features.push_back("+reserve-x18"); 449 450 if (Args.hasArg(options::OPT_ffixed_x20)) 451 Features.push_back("+reserve-x20"); 452 453 if (Args.hasArg(options::OPT_ffixed_x21)) 454 Features.push_back("+reserve-x21"); 455 456 if (Args.hasArg(options::OPT_ffixed_x22)) 457 Features.push_back("+reserve-x22"); 458 459 if (Args.hasArg(options::OPT_ffixed_x23)) 460 Features.push_back("+reserve-x23"); 461 462 if (Args.hasArg(options::OPT_ffixed_x24)) 463 Features.push_back("+reserve-x24"); 464 465 if (Args.hasArg(options::OPT_ffixed_x25)) 466 Features.push_back("+reserve-x25"); 467 468 if (Args.hasArg(options::OPT_ffixed_x26)) 469 Features.push_back("+reserve-x26"); 470 471 if (Args.hasArg(options::OPT_ffixed_x27)) 472 Features.push_back("+reserve-x27"); 473 474 if (Args.hasArg(options::OPT_ffixed_x28)) 475 Features.push_back("+reserve-x28"); 476 477 if (Args.hasArg(options::OPT_ffixed_x30)) 478 Features.push_back("+reserve-x30"); 479 480 if (Args.hasArg(options::OPT_fcall_saved_x8)) 481 Features.push_back("+call-saved-x8"); 482 483 if (Args.hasArg(options::OPT_fcall_saved_x9)) 484 Features.push_back("+call-saved-x9"); 485 486 if (Args.hasArg(options::OPT_fcall_saved_x10)) 487 Features.push_back("+call-saved-x10"); 488 489 if (Args.hasArg(options::OPT_fcall_saved_x11)) 490 Features.push_back("+call-saved-x11"); 491 492 if (Args.hasArg(options::OPT_fcall_saved_x12)) 493 Features.push_back("+call-saved-x12"); 494 495 if (Args.hasArg(options::OPT_fcall_saved_x13)) 496 Features.push_back("+call-saved-x13"); 497 498 if (Args.hasArg(options::OPT_fcall_saved_x14)) 499 Features.push_back("+call-saved-x14"); 500 501 if (Args.hasArg(options::OPT_fcall_saved_x15)) 502 Features.push_back("+call-saved-x15"); 503 504 if (Args.hasArg(options::OPT_fcall_saved_x18)) 505 Features.push_back("+call-saved-x18"); 506 507 if (Args.hasArg(options::OPT_mno_neg_immediates)) 508 Features.push_back("+no-neg-immediates"); 509 } 510