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

Cameron McInally via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 17 10:57:14 PDT 2025


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

Support -mcpu=native by querying the Host CPU Name.

>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] [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!");



More information about the llvm-commits mailing list