[llvm] [MC] Add support for -mcpu=native. (PR #159414)

Cameron McInally via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 18 11:41:24 PDT 2025


https://github.com/mcinally updated https://github.com/llvm/llvm-project/pull/159414

>From 58240cd4208504aa37c871c8dd61a6cf82716774 Mon Sep 17 00:00:00 2001
From: Cameron McInally <cmcinally at nvidia.com>
Date: Wed, 17 Sep 2025 10:45:51 -0700
Subject: [PATCH 1/3] [MC] Add support for -mcpu=native.

Support -mcpu=native by querying the Host CPU Name.
---
 llvm/test/MC/AsmParser/native.s | 3 +++
 llvm/tools/llvm-mc/llvm-mc.cpp  | 4 ++++
 2 files changed, 7 insertions(+)
 create mode 100644 llvm/test/MC/AsmParser/native.s

diff --git a/llvm/test/MC/AsmParser/native.s b/llvm/test/MC/AsmParser/native.s
new file mode 100644
index 0000000000000..b5228dfd77396
--- /dev/null
+++ b/llvm/test/MC/AsmParser/native.s
@@ -0,0 +1,3 @@
+# RUN: llvm-mc -filetype=obj -mcpu=native %s 2>&1 | FileCheck %s
+
+# CHECK-NOT: 'native' is not a recognized processor for this target (ignoring processor)
diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp
index 136cd69526a3c..cb38a04f249e1 100644
--- a/llvm/tools/llvm-mc/llvm-mc.cpp
+++ b/llvm/tools/llvm-mc/llvm-mc.cpp
@@ -467,6 +467,10 @@ int main(int argc, char **argv) {
     FeaturesStr = Features.getString();
   }
 
+  // Replace -mcpu=native with Host CPU.
+  if (MCPU == "native")
+    MCPU = std::string(llvm::sys::getHostCPUName());
+
   std::unique_ptr<MCSubtargetInfo> STI(
       TheTarget->createMCSubtargetInfo(TheTriple, MCPU, FeaturesStr));
   assert(STI && "Unable to create subtarget info!");

>From c0fa4ef0b020a56231efe9c26df9ba113c480ae5 Mon Sep 17 00:00:00 2001
From: Cameron McInally <cmcinally at nvidia.com>
Date: Thu, 18 Sep 2025 10:14:19 -0700
Subject: [PATCH 2/3] Handle -mcpu=native default Host features.

Also update negative check to look for no stderr output.
---
 llvm/test/MC/AsmParser/native.s |  5 +++--
 llvm/tools/llvm-mc/llvm-mc.cpp  | 18 +++++++++++++-----
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/llvm/test/MC/AsmParser/native.s b/llvm/test/MC/AsmParser/native.s
index b5228dfd77396..4da5b3717913f 100644
--- a/llvm/test/MC/AsmParser/native.s
+++ b/llvm/test/MC/AsmParser/native.s
@@ -1,3 +1,4 @@
-# RUN: llvm-mc -filetype=obj -mcpu=native %s 2>&1 | FileCheck %s
+# RUN: llvm-mc -filetype=obj -o %t -mcpu=native %s 2> %t.stderr
+# RUN: FileCheck --allow-empty %s < %t.stderr
 
-# CHECK-NOT: 'native' is not a recognized processor for this target (ignoring processor)
+# CHECK-NOT: {{.+}}
diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp
index cb38a04f249e1..becd1fd2eefd8 100644
--- a/llvm/tools/llvm-mc/llvm-mc.cpp
+++ b/llvm/tools/llvm-mc/llvm-mc.cpp
@@ -459,17 +459,25 @@ int main(int argc, char **argv) {
   MAI->setCommentColumn(CommentColumn);
 
   // Package up features to be passed to target/subtarget
+  SubtargetFeatures Features;
   std::string FeaturesStr;
+
+  // Replace -mcpu=native with Host CPU and features.
+  if (MCPU == "native") {
+    MCPU = std::string(llvm::sys::getHostCPUName());
+
+    llvm::StringMap<bool> TargetFeatures = llvm::sys::getHostCPUFeatures();
+    for (auto const& [FeatureName, IsSupported] : TargetFeatures)
+      Features.AddFeature(FeatureName, IsSupported);
+  }
+
+  // Handle features passed to target/subtarget.
   if (MAttrs.size()) {
-    SubtargetFeatures Features;
     for (unsigned i = 0; i != MAttrs.size(); ++i)
       Features.AddFeature(MAttrs[i]);
-    FeaturesStr = Features.getString();
   }
 
-  // Replace -mcpu=native with Host CPU.
-  if (MCPU == "native")
-    MCPU = std::string(llvm::sys::getHostCPUName());
+  FeaturesStr = Features.getString();
 
   std::unique_ptr<MCSubtargetInfo> STI(
       TheTarget->createMCSubtargetInfo(TheTriple, MCPU, FeaturesStr));

>From 54aa0fd05ca9f71ed34314b7e9f6fb934b0b83b6 Mon Sep 17 00:00:00 2001
From: Cameron McInally <cmcinally at nvidia.com>
Date: Thu, 18 Sep 2025 11:40:50 -0700
Subject: [PATCH 3/3] Run clang-format.

---
 llvm/tools/llvm-mc/llvm-mc.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp
index becd1fd2eefd8..55d6af3fe1545 100644
--- a/llvm/tools/llvm-mc/llvm-mc.cpp
+++ b/llvm/tools/llvm-mc/llvm-mc.cpp
@@ -467,7 +467,7 @@ int main(int argc, char **argv) {
     MCPU = std::string(llvm::sys::getHostCPUName());
 
     llvm::StringMap<bool> TargetFeatures = llvm::sys::getHostCPUFeatures();
-    for (auto const& [FeatureName, IsSupported] : TargetFeatures)
+    for (auto const &[FeatureName, IsSupported] : TargetFeatures)
       Features.AddFeature(FeatureName, IsSupported);
   }
 



More information about the llvm-commits mailing list