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