clang 22.0.0git
AArch64.cpp
Go to the documentation of this file.
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"
11#include "clang/Driver/Driver.h"
13#include "llvm/Option/ArgList.h"
14#include "llvm/TargetParser/AArch64TargetParser.h"
15#include "llvm/TargetParser/Host.h"
16
17using namespace clang::driver;
18using namespace clang::driver::tools;
19using namespace clang;
20using namespace llvm::opt;
21
22/// \returns true if the given triple can determine the default CPU type even
23/// if -arch is not specified.
24static 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.
31std::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 CPU = llvm::AArch64::resolveCPUAlias(CPU);
41
42 // Handle CPU name is 'native'.
43 if (CPU == "native")
44 return std::string(llvm::sys::getHostCPUName());
45
46 if (CPU.size())
47 return CPU;
48
49 if (Triple.isTargetMachineMac() &&
50 Triple.getArch() == llvm::Triple::aarch64) {
51 // Apple Silicon macs default to M1 CPUs.
52 return "apple-m1";
53 }
54
55 if (Triple.getOS() == llvm::Triple::IOS) {
56 assert(!Triple.isSimulatorEnvironment() && "iossim should be mac-like");
57 // iOS 26 only runs on apple-a12 and later CPUs.
58 if (!Triple.isOSVersionLT(26))
59 return "apple-a12";
60 }
61
62 if (Triple.isWatchOS()) {
63 assert(!Triple.isSimulatorEnvironment() && "watchossim should be mac-like");
64 // arm64_32/arm64e watchOS requires S4 before watchOS 26, S6 after.
65 if (Triple.getArch() == llvm::Triple::aarch64_32 || Triple.isArm64e())
66 return Triple.isOSVersionLT(26) ? "apple-s4" : "apple-s6";
67 // arm64 (non-e, non-32) watchOS comes later, and requires S6 anyway.
68 return "apple-s6";
69 }
70
71 if (Triple.isXROS()) {
72 // The xrOS simulator runs on M1 as well, it should have been covered above.
73 assert(!Triple.isSimulatorEnvironment() && "xrossim should be mac-like");
74 return "apple-a12";
75 }
76 // arm64e requires v8.3a and only runs on apple-a12 and later CPUs.
77 if (Triple.isArm64e())
78 return "apple-a12";
79
80 // Make sure we pick the appropriate Apple CPU when targetting a Darwin OS.
81 if (Triple.isOSDarwin())
82 return Triple.getArch() == llvm::Triple::aarch64_32 ? "apple-s4"
83 : "apple-a7";
84
85 return "generic";
86}
87
88// Decode AArch64 features from string like +[no]featureA+[no]featureB+...
89static bool DecodeAArch64Features(const Driver &D, StringRef text,
90 llvm::AArch64::ExtensionSet &Extensions) {
92 text.split(Split, StringRef("+"), -1, false);
93
94 for (StringRef Feature : Split) {
95 if (Feature == "neon" || Feature == "noneon") {
96 D.Diag(clang::diag::err_drv_no_neon_modifier);
97 continue;
98 }
99 if (!Extensions.parseModifier(Feature))
100 return false;
101 }
102
103 return true;
104}
105
106// Check if the CPU name and feature modifiers in -mcpu are legal. If yes,
107// decode CPU and feature.
108static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU,
109 llvm::AArch64::ExtensionSet &Extensions) {
110 std::pair<StringRef, StringRef> Split = Mcpu.split("+");
111 CPU = Split.first;
112
113 if (CPU == "native")
114 CPU = llvm::sys::getHostCPUName();
115
116 const std::optional<llvm::AArch64::CpuInfo> CpuInfo =
117 llvm::AArch64::parseCpu(CPU);
118 if (!CpuInfo)
119 return false;
120
121 Extensions.addCPUDefaults(*CpuInfo);
122
123 if (Split.second.size() &&
124 !DecodeAArch64Features(D, Split.second, Extensions))
125 return false;
126
127 return true;
128}
129
130static bool
131getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
132 const ArgList &Args,
133 llvm::AArch64::ExtensionSet &Extensions) {
134 std::string MarchLowerCase = March.lower();
135 std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
136
137 const llvm::AArch64::ArchInfo *ArchInfo =
138 llvm::AArch64::parseArch(Split.first);
139 if (Split.first == "native")
140 ArchInfo = llvm::AArch64::getArchForCpu(llvm::sys::getHostCPUName().str());
141 if (!ArchInfo)
142 return false;
143
144 Extensions.addArchDefaults(*ArchInfo);
145
146 if ((Split.second.size() &&
147 !DecodeAArch64Features(D, Split.second, Extensions)))
148 return false;
149
150 return true;
151}
152
154 const Driver &D, StringRef Mcpu, const ArgList &Args,
155 llvm::AArch64::ExtensionSet &Extensions, std::vector<StringRef> &Features) {
156 StringRef CPU;
157 std::string McpuLowerCase = Mcpu.lower();
158 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, Extensions))
159 return false;
160
161 if (Mcpu == "native") {
162 llvm::StringMap<bool> HostFeatures = llvm::sys::getHostCPUFeatures();
163 for (auto &[Feature, Enabled] : HostFeatures) {
164 Features.push_back(Args.MakeArgString((Enabled ? "+" : "-") + Feature));
165 }
166 }
167
168 return true;
169}
170
171static bool
173 const ArgList &Args,
174 std::vector<StringRef> &Features) {
175 // Check CPU name is valid, but ignore any extensions on it.
176 std::string MtuneLowerCase = Mtune.lower();
177 llvm::AArch64::ExtensionSet Extensions;
178 StringRef Tune;
179 return DecodeAArch64Mcpu(D, MtuneLowerCase, Tune, Extensions);
180}
181
182static bool
184 const ArgList &Args,
185 std::vector<StringRef> &Features) {
186 return getAArch64MicroArchFeaturesFromMtune(D, Mcpu, Args, Features);
187}
188
190 const llvm::Triple &Triple,
191 const ArgList &Args,
192 std::vector<StringRef> &Features,
193 bool ForAS, bool ForMultilib) {
194 Arg *A;
195 bool success = true;
196 llvm::StringRef WaMArch;
197 llvm::AArch64::ExtensionSet Extensions;
198 if (ForAS)
199 for (const auto *A :
200 Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler))
201 for (StringRef Value : A->getValues())
202 if (Value.starts_with("-march="))
203 WaMArch = Value.substr(7);
204 // Call getAArch64ArchFeaturesFromMarch only if "-Wa,-march=" or
205 // "-Xassembler -march" is detected. Otherwise it may return false
206 // and causes Clang to error out.
207 if (!WaMArch.empty())
208 success = getAArch64ArchFeaturesFromMarch(D, WaMArch, Args, Extensions);
209 else if ((A = Args.getLastArg(options::OPT_march_EQ)))
210 success =
211 getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Extensions);
212 else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
213 success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Extensions,
214 Features);
215 else if (isCPUDeterminedByTriple(Triple))
217 D, getAArch64TargetCPU(Args, Triple, A), Args, Extensions, Features);
218 else
219 // Default to 'A' profile if the architecture is not specified.
220 success = getAArch64ArchFeaturesFromMarch(D, "armv8-a", Args, Extensions);
221
222 if (success && (A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)))
223 success =
224 getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
225 else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
226 success =
227 getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
228 else if (success && isCPUDeterminedByTriple(Triple))
230 D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
231
232 if (!success) {
233 auto Diag = D.Diag(diag::err_drv_unsupported_option_argument);
234 // If "-Wa,-march=" is used, 'WaMArch' will contain the argument's value,
235 // while 'A' is uninitialized. Only dereference 'A' in the other case.
236 if (!WaMArch.empty())
237 Diag << "-march=" << WaMArch;
238 else
239 Diag << A->getSpelling() << A->getValue();
240 }
241
242 // -mgeneral-regs-only disables all floating-point features.
243 if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
244 Extensions.disable(llvm::AArch64::AEK_FP);
245 }
246
247 // En/disable crc
248 if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
249 if (A->getOption().matches(options::OPT_mcrc))
250 Extensions.enable(llvm::AArch64::AEK_CRC);
251 else
252 Extensions.disable(llvm::AArch64::AEK_CRC);
253 }
254
255 // At this point all hardware features are decided, so convert the extensions
256 // set to a feature list.
257 Extensions.toLLVMFeatureList(Features);
258
259 if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) {
260 StringRef Mtp = A->getValue();
261 if (Mtp == "el3" || Mtp == "tpidr_el3")
262 Features.push_back("+tpidr-el3");
263 else if (Mtp == "el2" || Mtp == "tpidr_el2")
264 Features.push_back("+tpidr-el2");
265 else if (Mtp == "el1" || Mtp == "tpidr_el1")
266 Features.push_back("+tpidr-el1");
267 else if (Mtp == "tpidrro_el0")
268 Features.push_back("+tpidrro-el0");
269 else if (Mtp != "el0" && Mtp != "tpidr_el0")
270 D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
271 }
272
273 // Enable/disable straight line speculation hardening.
274 if (Arg *A = Args.getLastArg(options::OPT_mharden_sls_EQ)) {
275 StringRef Scope = A->getValue();
276 bool EnableRetBr = false;
277 bool EnableBlr = false;
278 bool DisableComdat = false;
279 if (Scope != "none") {
281 Scope.split(Opts, ",");
282 for (auto Opt : Opts) {
283 Opt = Opt.trim();
284 if (Opt == "all") {
285 EnableBlr = true;
286 EnableRetBr = true;
287 continue;
288 }
289 if (Opt == "retbr") {
290 EnableRetBr = true;
291 continue;
292 }
293 if (Opt == "blr") {
294 EnableBlr = true;
295 continue;
296 }
297 if (Opt == "comdat") {
298 DisableComdat = false;
299 continue;
300 }
301 if (Opt == "nocomdat") {
302 DisableComdat = true;
303 continue;
304 }
305 D.Diag(diag::err_drv_unsupported_option_argument)
306 << A->getSpelling() << Scope;
307 break;
308 }
309 }
310
311 if (EnableRetBr)
312 Features.push_back("+harden-sls-retbr");
313 if (EnableBlr)
314 Features.push_back("+harden-sls-blr");
315 if (DisableComdat) {
316 Features.push_back("+harden-sls-nocomdat");
317 }
318 }
319
320 if (Arg *A = Args.getLastArg(
321 options::OPT_mstrict_align, options::OPT_mno_strict_align,
322 options::OPT_mno_unaligned_access, options::OPT_munaligned_access)) {
323 if (A->getOption().matches(options::OPT_mstrict_align) ||
324 A->getOption().matches(options::OPT_mno_unaligned_access))
325 Features.push_back("+strict-align");
326 } else if (Triple.isOSOpenBSD())
327 Features.push_back("+strict-align");
328
329 // Generate execute-only output (no data access to code sections).
330 // This only makes sense for the compiler, not for the assembler.
331 // It's not needed for multilib selection and may hide an unused
332 // argument diagnostic if the code is always run.
333 if (!ForAS && !ForMultilib) {
334 if (Arg *A = Args.getLastArg(options::OPT_mexecute_only,
335 options::OPT_mno_execute_only)) {
336 if (A->getOption().matches(options::OPT_mexecute_only)) {
337 Features.push_back("+execute-only");
338 }
339 }
340 }
341
342 if (Args.hasArg(options::OPT_ffixed_x1))
343 Features.push_back("+reserve-x1");
344
345 if (Args.hasArg(options::OPT_ffixed_x2))
346 Features.push_back("+reserve-x2");
347
348 if (Args.hasArg(options::OPT_ffixed_x3))
349 Features.push_back("+reserve-x3");
350
351 if (Args.hasArg(options::OPT_ffixed_x4))
352 Features.push_back("+reserve-x4");
353
354 if (Args.hasArg(options::OPT_ffixed_x5))
355 Features.push_back("+reserve-x5");
356
357 if (Args.hasArg(options::OPT_ffixed_x6))
358 Features.push_back("+reserve-x6");
359
360 if (Args.hasArg(options::OPT_ffixed_x7))
361 Features.push_back("+reserve-x7");
362
363 if (Args.hasArg(options::OPT_ffixed_x9))
364 Features.push_back("+reserve-x9");
365
366 if (Args.hasArg(options::OPT_ffixed_x10))
367 Features.push_back("+reserve-x10");
368
369 if (Args.hasArg(options::OPT_ffixed_x11))
370 Features.push_back("+reserve-x11");
371
372 if (Args.hasArg(options::OPT_ffixed_x12))
373 Features.push_back("+reserve-x12");
374
375 if (Args.hasArg(options::OPT_ffixed_x13))
376 Features.push_back("+reserve-x13");
377
378 if (Args.hasArg(options::OPT_ffixed_x14))
379 Features.push_back("+reserve-x14");
380
381 if (Args.hasArg(options::OPT_ffixed_x15))
382 Features.push_back("+reserve-x15");
383
384 if (Args.hasArg(options::OPT_ffixed_x18))
385 Features.push_back("+reserve-x18");
386
387 if (Args.hasArg(options::OPT_ffixed_x20))
388 Features.push_back("+reserve-x20");
389
390 if (Args.hasArg(options::OPT_ffixed_x21))
391 Features.push_back("+reserve-x21");
392
393 if (Args.hasArg(options::OPT_ffixed_x22))
394 Features.push_back("+reserve-x22");
395
396 if (Args.hasArg(options::OPT_ffixed_x23))
397 Features.push_back("+reserve-x23");
398
399 if (Args.hasArg(options::OPT_ffixed_x24))
400 Features.push_back("+reserve-x24");
401
402 if (Args.hasArg(options::OPT_ffixed_x25))
403 Features.push_back("+reserve-x25");
404
405 if (Args.hasArg(options::OPT_ffixed_x26))
406 Features.push_back("+reserve-x26");
407
408 if (Args.hasArg(options::OPT_ffixed_x27))
409 Features.push_back("+reserve-x27");
410
411 if (Args.hasArg(options::OPT_ffixed_x28))
412 Features.push_back("+reserve-x28");
413
414 if (Args.hasArg(options::OPT_mlr_for_calls_only))
415 Features.push_back("+reserve-lr-for-ra");
416
417 if (Args.hasArg(options::OPT_fcall_saved_x8))
418 Features.push_back("+call-saved-x8");
419
420 if (Args.hasArg(options::OPT_fcall_saved_x9))
421 Features.push_back("+call-saved-x9");
422
423 if (Args.hasArg(options::OPT_fcall_saved_x10))
424 Features.push_back("+call-saved-x10");
425
426 if (Args.hasArg(options::OPT_fcall_saved_x11))
427 Features.push_back("+call-saved-x11");
428
429 if (Args.hasArg(options::OPT_fcall_saved_x12))
430 Features.push_back("+call-saved-x12");
431
432 if (Args.hasArg(options::OPT_fcall_saved_x13))
433 Features.push_back("+call-saved-x13");
434
435 if (Args.hasArg(options::OPT_fcall_saved_x14))
436 Features.push_back("+call-saved-x14");
437
438 if (Args.hasArg(options::OPT_fcall_saved_x15))
439 Features.push_back("+call-saved-x15");
440
441 if (Args.hasArg(options::OPT_fcall_saved_x18))
442 Features.push_back("+call-saved-x18");
443
444 if (Args.hasArg(options::OPT_mno_neg_immediates))
445 Features.push_back("+no-neg-immediates");
446
447 if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a53_835769,
448 options::OPT_mno_fix_cortex_a53_835769)) {
449 if (A->getOption().matches(options::OPT_mfix_cortex_a53_835769))
450 Features.push_back("+fix-cortex-a53-835769");
451 else
452 Features.push_back("-fix-cortex-a53-835769");
453 } else if (Triple.isAndroid() || Triple.isOHOSFamily()) {
454 // Enabled A53 errata (835769) workaround by default on android
455 Features.push_back("+fix-cortex-a53-835769");
456 } else if (Triple.isOSFuchsia()) {
457 std::string CPU = getCPUName(D, Args, Triple);
458 if (CPU.empty() || CPU == "generic" || CPU == "cortex-a53")
459 Features.push_back("+fix-cortex-a53-835769");
460 }
461
462 if (Args.getLastArg(options::OPT_mno_bti_at_return_twice))
463 Features.push_back("+no-bti-at-return-twice");
464}
465
466void aarch64::setPAuthABIInTriple(const Driver &D, const ArgList &Args,
467 llvm::Triple &Triple) {
468 Arg *ABIArg = Args.getLastArg(options::OPT_mabi_EQ);
469 bool HasPAuthABI =
470 ABIArg ? (StringRef(ABIArg->getValue()) == "pauthtest") : false;
471
472 switch (Triple.getEnvironment()) {
473 case llvm::Triple::UnknownEnvironment:
474 if (HasPAuthABI)
475 Triple.setEnvironment(llvm::Triple::PAuthTest);
476 break;
477 case llvm::Triple::PAuthTest:
478 break;
479 default:
480 if (HasPAuthABI)
481 D.Diag(diag::err_drv_unsupported_opt_for_target)
482 << ABIArg->getAsString(Args) << Triple.getTriple();
483 break;
484 }
485}
486
487/// Is the triple {aarch64.aarch64_be}-none-elf?
488bool aarch64::isAArch64BareMetal(const llvm::Triple &Triple) {
489 if (Triple.getArch() != llvm::Triple::aarch64 &&
490 Triple.getArch() != llvm::Triple::aarch64_be)
491 return false;
492
493 if (Triple.getVendor() != llvm::Triple::UnknownVendor)
494 return false;
495
496 if (Triple.getOS() != llvm::Triple::UnknownOS)
497 return false;
498
499 return Triple.getEnvironmentName() == "elf";
500}
static bool getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March, const ArgList &Args, llvm::AArch64::ExtensionSet &Extensions)
Definition AArch64.cpp:131
static bool getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu, const ArgList &Args, llvm::AArch64::ExtensionSet &Extensions, std::vector< StringRef > &Features)
Definition AArch64.cpp:153
static bool getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu, const ArgList &Args, std::vector< StringRef > &Features)
Definition AArch64.cpp:183
static bool getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune, const ArgList &Args, std::vector< StringRef > &Features)
Definition AArch64.cpp:172
static bool DecodeAArch64Features(const Driver &D, StringRef text, llvm::AArch64::ExtensionSet &Extensions)
Definition AArch64.cpp:89
static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU, llvm::AArch64::ExtensionSet &Extensions)
Definition AArch64.cpp:108
static bool isCPUDeterminedByTriple(const llvm::Triple &Triple)
Definition AArch64.cpp:24
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:99
DiagnosticBuilder Diag(unsigned DiagID) const
Definition Driver.h:169
bool isAArch64BareMetal(const llvm::Triple &Triple)
Is the triple {aarch64.aarch64_be}-none-elf?
Definition AArch64.cpp:488
void getAArch64TargetFeatures(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args, std::vector< llvm::StringRef > &Features, bool ForAS, bool ForMultilib=false)
std::string getAArch64TargetCPU(const llvm::opt::ArgList &Args, const llvm::Triple &Triple, llvm::opt::Arg *&A)
void setPAuthABIInTriple(const Driver &D, const llvm::opt::ArgList &Args, llvm::Triple &triple)
std::string getCPUName(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &T, bool FromAs=false)
The JSON file list parser is used to communicate input to InstallAPI.
#define false
Definition stdbool.h:26