[llvm] Mcpu check fix - buildbot failure (PR #173995)

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


https://github.com/Amichaxx updated https://github.com/llvm/llvm-project/pull/173995

>From 8fa1001a2bc103faec8d45b9544463c8f31e0ed1 Mon Sep 17 00:00:00 2001
From: Amichaxx <amina.chabane at arm.com>
Date: Mon, 22 Dec 2025 14:20:00 +0000
Subject: [PATCH 1/4] [MCA] Fix -mcpu=help flag

Previously, using the 'mcpu=help' flag would require an empty stdin to be passed to print the CPU/Features list.

This patch moves the MemoryBuffer::getFileOrSTDIN call below an early return.

A test mcpu-help.test is included which tests the flag with a missing file. Previously, this would have resulted in an error with
no outputted help list, but now provides the help list and ignores the missing file input.
---
 llvm/test/tools/llvm-mca/mcpu-help.test |  9 +++++++++
 llvm/tools/llvm-mca/llvm-mca.cpp        | 17 ++++++++++-------
 2 files changed, 19 insertions(+), 7 deletions(-)
 create mode 100644 llvm/test/tools/llvm-mca/mcpu-help.test

diff --git a/llvm/test/tools/llvm-mca/mcpu-help.test b/llvm/test/tools/llvm-mca/mcpu-help.test
new file mode 100644
index 0000000000000..b1eabaf791d4e
--- /dev/null
+++ b/llvm/test/tools/llvm-mca/mcpu-help.test
@@ -0,0 +1,9 @@
+# REQUIRES: aarch64-registered-target
+
+# RUN: llvm-mca -mtriple=aarch64-unknown-linux-gnu -mcpu=help %t.no-input 2>&1 | FileCheck %s
+
+// Ensures -mcpu=help prints target info without erroring on missing input.
+
+# CHECK: Available CPUs for this target:
+# CHECK: Available features for this target:
+# CHECK-NOT: error:
diff --git a/llvm/tools/llvm-mca/llvm-mca.cpp b/llvm/tools/llvm-mca/llvm-mca.cpp
index a64539c09b81e..c4aa081a095f8 100644
--- a/llvm/tools/llvm-mca/llvm-mca.cpp
+++ b/llvm/tools/llvm-mca/llvm-mca.cpp
@@ -392,13 +392,6 @@ int main(int argc, char **argv) {
   if (!TheTarget)
     return 1;
 
-  ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
-      MemoryBuffer::getFileOrSTDIN(InputFilename);
-  if (std::error_code EC = BufferPtr.getError()) {
-    WithColor::error() << InputFilename << ": " << EC.message() << '\n';
-    return 1;
-  }
-
   if (MCPU == "native")
     MCPU = std::string(llvm::sys::getHostCPUName());
 
@@ -418,9 +411,19 @@ int main(int argc, char **argv) {
     return 1;
   }
 
+  if (MCPU == "help")
+    return 0;
+
   if (!STI->isCPUStringValid(MCPU))
     return 1;
 
+  ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
+      MemoryBuffer::getFileOrSTDIN(InputFilename);
+  if (std::error_code EC = BufferPtr.getError()) {
+    WithColor::error() << InputFilename << ": " << EC.message() << '\n';
+    return 1;
+  }
+
   if (!STI->getSchedModel().hasInstrSchedModel()) {
     WithColor::error()
         << "unable to find instruction-level scheduling information for"

>From c72eb9ab0b1b7d2e39c4ff2844f018f6454ae7f1 Mon Sep 17 00:00:00 2001
From: Amichaxx <amina.chabane at arm.com>
Date: Tue, 23 Dec 2025 17:27:30 +0000
Subject: [PATCH 2/4] wip

---
 llvm/test/tools/llvm-mca/mcpu-help.test | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/tools/llvm-mca/mcpu-help.test b/llvm/test/tools/llvm-mca/mcpu-help.test
index b1eabaf791d4e..2cb1be660f65f 100644
--- a/llvm/test/tools/llvm-mca/mcpu-help.test
+++ b/llvm/test/tools/llvm-mca/mcpu-help.test
@@ -2,7 +2,7 @@
 
 # RUN: llvm-mca -mtriple=aarch64-unknown-linux-gnu -mcpu=help %t.no-input 2>&1 | FileCheck %s
 
-// Ensures -mcpu=help prints target info without erroring on missing input.
+// Ensures -mcpu=help still prints the CPU/features lists even when the input file after the flag is nonexistent.
 
 # CHECK: Available CPUs for this target:
 # CHECK: Available features for this target:

>From ff8eea258e9172f369cc8ff9855f2069c329ffa9 Mon Sep 17 00:00:00 2001
From: Amichaxx <amina.chabane at arm.com>
Date: Wed, 24 Dec 2025 10:09:42 +0000
Subject: [PATCH 3/4] Asserts CPUs in mcpu-help.test

---
 llvm/test/tools/llvm-mca/mcpu-help.test | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/llvm/test/tools/llvm-mca/mcpu-help.test b/llvm/test/tools/llvm-mca/mcpu-help.test
index 2cb1be660f65f..55168b4080c0c 100644
--- a/llvm/test/tools/llvm-mca/mcpu-help.test
+++ b/llvm/test/tools/llvm-mca/mcpu-help.test
@@ -5,5 +5,7 @@
 // Ensures -mcpu=help still prints the CPU/features lists even when the input file after the flag is nonexistent.
 
 # CHECK: Available CPUs for this target:
+# CHECK: a64fx           - Select the a64fx processor.
+# CHECK: ampere1         - Select the ampere1 processor.
 # CHECK: Available features for this target:
 # CHECK-NOT: error:

>From e18923453499a50685aecc04b6e4d5ee6d185667 Mon Sep 17 00:00:00 2001
From: Amichaxx <amina.chabane at arm.com>
Date: Tue, 30 Dec 2025 14:58:30 +0000
Subject: [PATCH 4/4] =?UTF-8?q?llvm-mca=20rewrites=20-mcpu=3Dnative=20to?=
 =?UTF-8?q?=20sys::getHostCPUName()=20and=20then=20immediately=20instantia?=
 =?UTF-8?q?tes=20an=20MCSubtargetInfo.=20If=20the=20build=20host=20is=20x8?=
 =?UTF-8?q?6=20but=20the=20selected=20triple=20is=20AArch64,=20then=20the?=
 =?UTF-8?q?=20CPU=20string=20(e.g.=20=E2=80=9Cznver4=E2=80=9D)=20isn?=
 =?UTF-8?q?=E2=80=99t=20valid,=20so=20InitMCProcessorInfo=20emits=20a=20wa?=
 =?UTF-8?q?rning.=20This=20broke=20lit=20tests=20on=20cross=20compilation.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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.
---
 .../llvm-mca/{ => AArch64}/mcpu-help.test     |  0
 llvm/tools/llvm-mca/llvm-mca.cpp              | 43 +++++++++++++++----
 2 files changed, 35 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..fcd85a489736f 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,43 @@ 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 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