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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-mc

Author: Cameron McInally (mcinally)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/159414.diff


2 Files Affected:

- (added) llvm/test/MC/AsmParser/native.s (+3) 
- (modified) llvm/tools/llvm-mc/llvm-mc.cpp (+4) 


``````````diff
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!");

``````````

</details>


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


More information about the llvm-commits mailing list