[llvm] [MCA] Ignore host -mcpu when target lacks said CPU (PR #174004)

Amina Chabane via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 30 08:26:17 PST 2025


https://github.com/Amichaxx created https://github.com/llvm/llvm-project/pull/174004

#173399 failed on buildbot llvm-clang-ubuntu-x-aarch64-pauth. This patch aims to rectify this.

--------------

llvm-mca rewrites `-mcpu=native` to `sys::getHostCPUName()` and then immediately instantiates an
`MCSubtargetInfo`. If the build host is x86 but the selected triple is AArch64, then the CPU string (e.g. “znver4”) isn’t valid, so `InitMCProcessorInfo` emits a warning. This broke lit tests on cross compilation.

This patch aims to fix this by creating a default subtarget first, querying `STI->isCPUStringValid(hostCPU)`
and only recreating the subtarget with the detected host CPU when it’s supported. 

`mcpu-help.test` has been moved into the AArch64 directory, as it is target-independent

>From 8920797f8f5845df0ffd717816c4806cf05c4128 Mon Sep 17 00:00:00 2001
From: Amichaxx <amina.chabane at arm.com>
Date: Tue, 30 Dec 2025 16:21:23 +0000
Subject: [PATCH] Ignore host -mcpu when target lacks that CPU

---
 .../llvm-mca/{ => AArch64}/mcpu-help.test     |  0
 llvm/tools/llvm-mca/llvm-mca.cpp              | 44 +++++++++++++++----
 2 files changed, 36 insertions(+), 8 deletions(-)
 rename llvm/test/tools/llvm-mca/{ => AArch64}/mcpu-help.test (100%)

diff --git a/llvm/test/tools/llvm-mca/mcpu-help.test b/llvm/test/tools/llvm-mca/AArch64/mcpu-help.test
similarity index 100%
rename from llvm/test/tools/llvm-mca/mcpu-help.test
rename to llvm/test/tools/llvm-mca/AArch64/mcpu-help.test
diff --git a/llvm/tools/llvm-mca/llvm-mca.cpp b/llvm/tools/llvm-mca/llvm-mca.cpp
index c4aa081a095f8..45fa4973d4399 100644
--- a/llvm/tools/llvm-mca/llvm-mca.cpp
+++ b/llvm/tools/llvm-mca/llvm-mca.cpp
@@ -392,9 +392,6 @@ int main(int argc, char **argv) {
   if (!TheTarget)
     return 1;
 
-  if (MCPU == "native")
-    MCPU = std::string(llvm::sys::getHostCPUName());
-
   // Package up features to be passed to target/subtarget
   std::string FeaturesStr;
   if (MATTRS.size()) {
@@ -404,13 +401,44 @@ int main(int argc, char **argv) {
     FeaturesStr = Features.getString();
   }
 
-  std::unique_ptr<MCSubtargetInfo> STI(
-      TheTarget->createMCSubtargetInfo(TheTriple, MCPU, FeaturesStr));
-  if (!STI) {
-    WithColor::error() << "unable to create subtarget info\n";
-    return 1;
+  auto CreateSubtargetInfo = [&](StringRef CPU) {
+    return std::unique_ptr<MCSubtargetInfo>(
+        TheTarget->createMCSubtargetInfo(TheTriple, CPU, FeaturesStr));
+  };
+
+  std::unique_ptr<MCSubtargetInfo> STI;
+  if (MCPU == "native") {
+    std::string HostCPU = std::string(llvm::sys::getHostCPUName());
+    STI = CreateSubtargetInfo("");
+    if (!STI) {
+      WithColor::error() << "unable to create subtarget info\n";
+      return 1;
+    }
+    if (!HostCPU.empty() && STI->isCPUStringValid(HostCPU)) {
+      // Only utilise the detected host CPU when it exists in the processor
+      // table for the requested triple.
+      STI = CreateSubtargetInfo(HostCPU);
+      if (!STI) {
+        WithColor::error() << "unable to create subtarget info\n";
+        return 1;
+      }
+      MCPU = HostCPU;
+    } else {
+      MCPU.clear();
+    }
+  } else {
+    STI = CreateSubtargetInfo(MCPU);
+    if (!STI) {
+      WithColor::error() << "unable to create subtarget info\n";
+      return 1;
+    }
   }
 
+  if (MCPU.empty())
+    // MCPU being empty here means the target default was selected above,
+    // which avoids forwarding incompatible host CPUs when cross-compiling.
+    MCPU = std::string(STI->getCPU());
+
   if (MCPU == "help")
     return 0;
 



More information about the llvm-commits mailing list