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.
isCPUDeterminedByTriple(const llvm::Triple & Triple)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.
getAArch64TargetCPU(const ArgList & Args,const llvm::Triple & Triple,Arg * & A)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+...
DecodeAArch64Features(const Driver & D,StringRef text,std::vector<StringRef> & Features,llvm::AArch64::ArchKind ArchKind)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.
DecodeAArch64Mcpu(const Driver & D,StringRef Mcpu,StringRef & CPU,std::vector<StringRef> & Features)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
getAArch64ArchFeaturesFromMarch(const Driver & D,StringRef March,const ArgList & Args,std::vector<StringRef> & Features)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
getAArch64ArchFeaturesFromMcpu(const Driver & D,StringRef Mcpu,const ArgList & Args,std::vector<StringRef> & Features)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
getAArch64MicroArchFeaturesFromMtune(const Driver & D,StringRef Mtune,const ArgList & Args,std::vector<StringRef> & Features)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
getAArch64MicroArchFeaturesFromMcpu(const Driver & D,StringRef Mcpu,const ArgList & Args,std::vector<StringRef> & Features)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
getAArch64TargetFeatures(const Driver & D,const llvm::Triple & Triple,const ArgList & Args,std::vector<StringRef> & Features,bool ForAS)185 void aarch64::getAArch64TargetFeatures(const Driver &D,
186 const llvm::Triple &Triple,
187 const ArgList &Args,
188 std::vector<StringRef> &Features,
189 bool ForAS) {
190 Arg *A;
191 bool success = true;
192 // Enable NEON by default.
193 Features.push_back("+neon");
194 llvm::StringRef WaMArch;
195 if (ForAS)
196 for (const auto *A :
197 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler))
198 for (StringRef Value : A->getValues())
199 if (Value.startswith("-march="))
200 WaMArch = Value.substr(7);
201 // Call getAArch64ArchFeaturesFromMarch only if "-Wa,-march=" or
202 // "-Xassembler -march" is detected. Otherwise it may return false
203 // and causes Clang to error out.
204 if (!WaMArch.empty())
205 success = getAArch64ArchFeaturesFromMarch(D, WaMArch, Args, Features);
206 else if ((A = Args.getLastArg(options::OPT_march_EQ)))
207 success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
208 else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
209 success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
210 else if (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple))
211 success = getAArch64ArchFeaturesFromMcpu(
212 D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
213
214 if (success && (A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)))
215 success =
216 getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
217 else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
218 success =
219 getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
220 else if (success &&
221 (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple)))
222 success = getAArch64MicroArchFeaturesFromMcpu(
223 D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
224
225 if (!success) {
226 auto Diag = D.Diag(diag::err_drv_clang_unsupported);
227 // If "-Wa,-march=" is used, 'WaMArch' will contain the argument's value,
228 // while 'A' is uninitialized. Only dereference 'A' in the other case.
229 if (!WaMArch.empty())
230 Diag << "-march=" + WaMArch.str();
231 else
232 Diag << A->getAsString(Args);
233 }
234
235 if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
236 Features.push_back("-fp-armv8");
237 Features.push_back("-crypto");
238 Features.push_back("-neon");
239 }
240
241 if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) {
242 StringRef Mtp = A->getValue();
243 if (Mtp == "el3")
244 Features.push_back("+tpidr-el3");
245 else if (Mtp == "el2")
246 Features.push_back("+tpidr-el2");
247 else if (Mtp == "el1")
248 Features.push_back("+tpidr-el1");
249 else if (Mtp != "el0")
250 D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
251 }
252
253 // Enable/disable straight line speculation hardening.
254 if (Arg *A = Args.getLastArg(options::OPT_mharden_sls_EQ)) {
255 StringRef Scope = A->getValue();
256 bool EnableRetBr = false;
257 bool EnableBlr = false;
258 bool DisableComdat = false;
259 if (Scope != "none") {
260 SmallVector<StringRef, 4> Opts;
261 Scope.split(Opts, ",");
262 for (auto Opt : Opts) {
263 Opt = Opt.trim();
264 if (Opt == "all") {
265 EnableBlr = true;
266 EnableRetBr = true;
267 continue;
268 }
269 if (Opt == "retbr") {
270 EnableRetBr = true;
271 continue;
272 }
273 if (Opt == "blr") {
274 EnableBlr = true;
275 continue;
276 }
277 if (Opt == "comdat") {
278 DisableComdat = false;
279 continue;
280 }
281 if (Opt == "nocomdat") {
282 DisableComdat = true;
283 continue;
284 }
285 D.Diag(diag::err_invalid_sls_hardening)
286 << Scope << A->getAsString(Args);
287 break;
288 }
289 }
290
291 if (EnableRetBr)
292 Features.push_back("+harden-sls-retbr");
293 if (EnableBlr)
294 Features.push_back("+harden-sls-blr");
295 if (DisableComdat) {
296 Features.push_back("+harden-sls-nocomdat");
297 }
298 }
299
300 // En/disable crc
301 if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
302 if (A->getOption().matches(options::OPT_mcrc))
303 Features.push_back("+crc");
304 else
305 Features.push_back("-crc");
306 }
307
308 // Handle (arch-dependent) fp16fml/fullfp16 relationship.
309 // FIXME: this fp16fml option handling will be reimplemented after the
310 // TargetParser rewrite.
311 const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16");
312 const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml");
313 if (llvm::is_contained(Features, "+v8.4a")) {
314 const auto ItRFullFP16 = std::find(Features.rbegin(), Features.rend(), "+fullfp16");
315 if (ItRFullFP16 < ItRNoFullFP16 && ItRFullFP16 < ItRFP16FML) {
316 // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml.
317 // Only append the +fp16fml if there is no -fp16fml after the +fullfp16.
318 if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16)
319 Features.push_back("+fp16fml");
320 }
321 else
322 goto fp16_fml_fallthrough;
323 } else {
324 fp16_fml_fallthrough:
325 // In both of these cases, putting the 'other' feature on the end of the vector will
326 // result in the same effect as placing it immediately after the current feature.
327 if (ItRNoFullFP16 < ItRFP16FML)
328 Features.push_back("-fp16fml");
329 else if (ItRNoFullFP16 > ItRFP16FML)
330 Features.push_back("+fullfp16");
331 }
332
333 // FIXME: this needs reimplementation too after the TargetParser rewrite
334 //
335 // Context sensitive meaning of Crypto:
336 // 1) For Arch >= ARMv8.4a: crypto = sm4 + sha3 + sha2 + aes
337 // 2) For Arch <= ARMv8.3a: crypto = sha2 + aes
338 const auto ItBegin = Features.begin();
339 const auto ItEnd = Features.end();
340 const auto ItRBegin = Features.rbegin();
341 const auto ItREnd = Features.rend();
342 const auto ItRCrypto = std::find(ItRBegin, ItREnd, "+crypto");
343 const auto ItRNoCrypto = std::find(ItRBegin, ItREnd, "-crypto");
344 const auto HasCrypto = ItRCrypto != ItREnd;
345 const auto HasNoCrypto = ItRNoCrypto != ItREnd;
346 const ptrdiff_t PosCrypto = ItRCrypto - ItRBegin;
347 const ptrdiff_t PosNoCrypto = ItRNoCrypto - ItRBegin;
348
349 bool NoCrypto = false;
350 if (HasCrypto && HasNoCrypto) {
351 if (PosNoCrypto < PosCrypto)
352 NoCrypto = true;
353 }
354
355 if (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd) {
356 if (HasCrypto && !NoCrypto) {
357 // Check if we have NOT disabled an algorithm with something like:
358 // +crypto, -algorithm
359 // And if "-algorithm" does not occur, we enable that crypto algorithm.
360 const bool HasSM4 = (std::find(ItBegin, ItEnd, "-sm4") == ItEnd);
361 const bool HasSHA3 = (std::find(ItBegin, ItEnd, "-sha3") == ItEnd);
362 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
363 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
364 if (HasSM4)
365 Features.push_back("+sm4");
366 if (HasSHA3)
367 Features.push_back("+sha3");
368 if (HasSHA2)
369 Features.push_back("+sha2");
370 if (HasAES)
371 Features.push_back("+aes");
372 } else if (HasNoCrypto) {
373 // Check if we have NOT enabled a crypto algorithm with something like:
374 // -crypto, +algorithm
375 // And if "+algorithm" does not occur, we disable that crypto algorithm.
376 const bool HasSM4 = (std::find(ItBegin, ItEnd, "+sm4") != ItEnd);
377 const bool HasSHA3 = (std::find(ItBegin, ItEnd, "+sha3") != ItEnd);
378 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
379 const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
380 if (!HasSM4)
381 Features.push_back("-sm4");
382 if (!HasSHA3)
383 Features.push_back("-sha3");
384 if (!HasSHA2)
385 Features.push_back("-sha2");
386 if (!HasAES)
387 Features.push_back("-aes");
388 }
389 } else {
390 if (HasCrypto && !NoCrypto) {
391 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
392 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
393 if (HasSHA2)
394 Features.push_back("+sha2");
395 if (HasAES)
396 Features.push_back("+aes");
397 } else if (HasNoCrypto) {
398 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
399 const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
400 const bool HasV82a = (std::find(ItBegin, ItEnd, "+v8.2a") != ItEnd);
401 const bool HasV83a = (std::find(ItBegin, ItEnd, "+v8.3a") != ItEnd);
402 const bool HasV84a = (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd);
403 if (!HasSHA2)
404 Features.push_back("-sha2");
405 if (!HasAES)
406 Features.push_back("-aes");
407 if (HasV82a || HasV83a || HasV84a) {
408 Features.push_back("-sm4");
409 Features.push_back("-sha3");
410 }
411 }
412 }
413
414 auto V8_6Pos = llvm::find(Features, "+v8.6a");
415 if (V8_6Pos != std::end(Features))
416 V8_6Pos = Features.insert(std::next(V8_6Pos), {"+i8mm", "+bf16"});
417
418 if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
419 options::OPT_munaligned_access)) {
420 if (A->getOption().matches(options::OPT_mno_unaligned_access))
421 Features.push_back("+strict-align");
422 } else if (Triple.isOSOpenBSD())
423 Features.push_back("+strict-align");
424
425 if (Args.hasArg(options::OPT_ffixed_x1))
426 Features.push_back("+reserve-x1");
427
428 if (Args.hasArg(options::OPT_ffixed_x2))
429 Features.push_back("+reserve-x2");
430
431 if (Args.hasArg(options::OPT_ffixed_x3))
432 Features.push_back("+reserve-x3");
433
434 if (Args.hasArg(options::OPT_ffixed_x4))
435 Features.push_back("+reserve-x4");
436
437 if (Args.hasArg(options::OPT_ffixed_x5))
438 Features.push_back("+reserve-x5");
439
440 if (Args.hasArg(options::OPT_ffixed_x6))
441 Features.push_back("+reserve-x6");
442
443 if (Args.hasArg(options::OPT_ffixed_x7))
444 Features.push_back("+reserve-x7");
445
446 if (Args.hasArg(options::OPT_ffixed_x9))
447 Features.push_back("+reserve-x9");
448
449 if (Args.hasArg(options::OPT_ffixed_x10))
450 Features.push_back("+reserve-x10");
451
452 if (Args.hasArg(options::OPT_ffixed_x11))
453 Features.push_back("+reserve-x11");
454
455 if (Args.hasArg(options::OPT_ffixed_x12))
456 Features.push_back("+reserve-x12");
457
458 if (Args.hasArg(options::OPT_ffixed_x13))
459 Features.push_back("+reserve-x13");
460
461 if (Args.hasArg(options::OPT_ffixed_x14))
462 Features.push_back("+reserve-x14");
463
464 if (Args.hasArg(options::OPT_ffixed_x15))
465 Features.push_back("+reserve-x15");
466
467 if (Args.hasArg(options::OPT_ffixed_x18))
468 Features.push_back("+reserve-x18");
469
470 if (Args.hasArg(options::OPT_ffixed_x20))
471 Features.push_back("+reserve-x20");
472
473 if (Args.hasArg(options::OPT_ffixed_x21))
474 Features.push_back("+reserve-x21");
475
476 if (Args.hasArg(options::OPT_ffixed_x22))
477 Features.push_back("+reserve-x22");
478
479 if (Args.hasArg(options::OPT_ffixed_x23))
480 Features.push_back("+reserve-x23");
481
482 if (Args.hasArg(options::OPT_ffixed_x24))
483 Features.push_back("+reserve-x24");
484
485 if (Args.hasArg(options::OPT_ffixed_x25))
486 Features.push_back("+reserve-x25");
487
488 if (Args.hasArg(options::OPT_ffixed_x26))
489 Features.push_back("+reserve-x26");
490
491 if (Args.hasArg(options::OPT_ffixed_x27))
492 Features.push_back("+reserve-x27");
493
494 if (Args.hasArg(options::OPT_ffixed_x28))
495 Features.push_back("+reserve-x28");
496
497 if (Args.hasArg(options::OPT_ffixed_x30))
498 Features.push_back("+reserve-x30");
499
500 if (Args.hasArg(options::OPT_fcall_saved_x8))
501 Features.push_back("+call-saved-x8");
502
503 if (Args.hasArg(options::OPT_fcall_saved_x9))
504 Features.push_back("+call-saved-x9");
505
506 if (Args.hasArg(options::OPT_fcall_saved_x10))
507 Features.push_back("+call-saved-x10");
508
509 if (Args.hasArg(options::OPT_fcall_saved_x11))
510 Features.push_back("+call-saved-x11");
511
512 if (Args.hasArg(options::OPT_fcall_saved_x12))
513 Features.push_back("+call-saved-x12");
514
515 if (Args.hasArg(options::OPT_fcall_saved_x13))
516 Features.push_back("+call-saved-x13");
517
518 if (Args.hasArg(options::OPT_fcall_saved_x14))
519 Features.push_back("+call-saved-x14");
520
521 if (Args.hasArg(options::OPT_fcall_saved_x15))
522 Features.push_back("+call-saved-x15");
523
524 if (Args.hasArg(options::OPT_fcall_saved_x18))
525 Features.push_back("+call-saved-x18");
526
527 if (Args.hasArg(options::OPT_mno_neg_immediates))
528 Features.push_back("+no-neg-immediates");
529 }
530