https://github.com/tomershafir created 
https://github.com/llvm/llvm-project/pull/179097

This patch refactors the AArch64 target resolution in Clang driver, extracting 
a new static local function called `getAArch64TargetCPUByTriple` to reduce 
redundant checks at runtime. Previously, `getAArch64TargetFeatures` would 
redundantly double-check `march` and `mcpu` arguments. Also removes the 
un-informative comment for native handling on the way.

>From 2406e10f1b7e29b13eca4eb3e1aff276de021dba Mon Sep 17 00:00:00 2001
From: tomershafir <[email protected]>
Date: Sun, 1 Feb 2026 12:10:04 +0200
Subject: [PATCH] [Clang][AArch64] Extract get target CPU by triple (NFC)

This patch refactors the AArch64 target resolution in Clang driver, extracting 
a new static local function called `getAArch64TargetCPUByTriple` to reduce 
redundant checks at runtime. Previously, `getAArch64TargetFeatures` would 
redundantly double-check `march` and `mcpu` arguments. Also removes the 
un-informative comment for native handling on the way.
---
 clang/lib/Driver/ToolChains/Arch/AArch64.cpp | 63 +++++++++++---------
 1 file changed, 35 insertions(+), 28 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Arch/AArch64.cpp 
b/clang/lib/Driver/ToolChains/Arch/AArch64.cpp
index 4aa3743fb5cd5..d39337b6f4ada 100644
--- a/clang/lib/Driver/ToolChains/Arch/AArch64.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/AArch64.cpp
@@ -25,31 +25,10 @@ static bool isCPUDeterminedByTriple(const llvm::Triple 
&Triple) {
   return Triple.isOSDarwin();
 }
 
-/// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are
-/// targeting. Set \p A to the Arg corresponding to the -mcpu argument if it is
-/// provided, or to nullptr otherwise.
-std::string aarch64::getAArch64TargetCPU(const ArgList &Args,
-                                         const llvm::Triple &Triple, Arg *&A) {
-  std::string CPU;
-  // If we have -mcpu, use that.
-  if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) {
-    StringRef Mcpu = A->getValue();
-    CPU = Mcpu.split("+").first.lower();
-  } else if (const Arg *MArch = Args.getLastArg(options::OPT_march_EQ)) {
-    // Otherwise, use -march=native if specified.
-    StringRef MArchValue = MArch->getValue();
-    if (MArchValue.split("+").first.equals_insensitive("native"))
-      CPU = "native";
-  }
-
-  CPU = llvm::AArch64::resolveCPUAlias(CPU);
-
-  // Handle CPU name is 'native'.
-  if (CPU == "native")
-    return std::string(llvm::sys::getHostCPUName());
-
-  if (CPU.size())
-    return CPU;
+/// \return target CPU based on the target triple.
+static std::string getAArch64TargetCPUByTriple(const llvm::Triple &Triple) {
+  assert(isCPUDeterminedByTriple(Triple) &&
+         "Cannot determine target CPU by triple");
 
   if (Triple.isTargetMachineMac() &&
       Triple.getArch() == llvm::Triple::aarch64) {
@@ -90,7 +69,35 @@ std::string aarch64::getAArch64TargetCPU(const ArgList &Args,
     return Triple.getArch() == llvm::Triple::aarch64_32 ? "apple-s4"
                                                         : "apple-a7";
 
-  return "generic";
+  llvm_unreachable("Unhandled triple");
+}
+
+/// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are
+/// targeting. Set \p A to the Arg corresponding to the -mcpu argument if it is
+/// provided, or to nullptr otherwise.
+std::string aarch64::getAArch64TargetCPU(const ArgList &Args,
+                                         const llvm::Triple &Triple, Arg *&A) {
+  std::string CPU;
+  // If we have -mcpu, use that.
+  if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) {
+    StringRef Mcpu = A->getValue();
+    CPU = Mcpu.split("+").first.lower();
+  } else if (const Arg *MArch = Args.getLastArg(options::OPT_march_EQ)) {
+    // Otherwise, use -march=native if specified.
+    StringRef MArchValue = MArch->getValue();
+    if (MArchValue.split("+").first.equals_insensitive("native"))
+      CPU = "native";
+  }
+
+  CPU = llvm::AArch64::resolveCPUAlias(CPU);
+
+  if (CPU == "native")
+    return std::string(llvm::sys::getHostCPUName());
+
+  if (CPU.size())
+    return CPU;
+
+  return getAArch64TargetCPUByTriple(Triple);
 }
 
 // Decode AArch64 features from string like +[no]featureA+[no]featureB+...
@@ -231,7 +238,7 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
         getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Extensions);
   else if (isCPUDeterminedByTriple(Triple))
     success = getAArch64ArchFeaturesFromMcpu(
-        D, getAArch64TargetCPU(Args, Triple, A), Args, Extensions);
+        D, getAArch64TargetCPUByTriple(Triple), Args, Extensions);
   else
     // Default to 'A' profile if the architecture is not specified.
     success = getAArch64ArchFeaturesFromMarch(D, "armv8-a", Args, Extensions);
@@ -242,7 +249,7 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
     success = getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args);
   else if (success && isCPUDeterminedByTriple(Triple))
     success = getAArch64MicroArchFeaturesFromMcpu(
-        D, getAArch64TargetCPU(Args, Triple, A), Args);
+        D, getAArch64TargetCPUByTriple(Triple), Args);
 
   if (!success) {
     auto Diag = D.Diag(diag::err_drv_unsupported_option_argument);

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to