Skip to content

Commit 8009a5b

Browse files
mcinallyCameron McInally
andauthored
[MC] Add support for -mcpu=native. (llvm#159414)
Support -mcpu=native by querying the Host CPU Name and Feature flags. --------- Co-authored-by: Cameron McInally <[email protected]>
1 parent c256966 commit 8009a5b

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

llvm/test/MC/AsmParser/native.s

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# RUN: llvm-mc -filetype=obj -o %t -mcpu=native %s 2> %t.stderr
2+
# RUN: FileCheck --allow-empty %s < %t.stderr
3+
4+
# CHECK-NOT: {{.+}}

llvm/tools/llvm-mc/llvm-mc.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,14 +459,23 @@ int main(int argc, char **argv) {
459459
MAI->setCommentColumn(CommentColumn);
460460

461461
// Package up features to be passed to target/subtarget
462+
SubtargetFeatures Features;
462463
std::string FeaturesStr;
463-
if (MAttrs.size()) {
464-
SubtargetFeatures Features;
465-
for (unsigned i = 0; i != MAttrs.size(); ++i)
466-
Features.AddFeature(MAttrs[i]);
467-
FeaturesStr = Features.getString();
464+
465+
// Replace -mcpu=native with Host CPU and features.
466+
if (MCPU == "native") {
467+
MCPU = std::string(llvm::sys::getHostCPUName());
468+
469+
llvm::StringMap<bool> TargetFeatures = llvm::sys::getHostCPUFeatures();
470+
for (auto const &[FeatureName, IsSupported] : TargetFeatures)
471+
Features.AddFeature(FeatureName, IsSupported);
468472
}
469473

474+
// Handle features passed to target/subtarget.
475+
for (unsigned i = 0; i != MAttrs.size(); ++i)
476+
Features.AddFeature(MAttrs[i]);
477+
FeaturesStr = Features.getString();
478+
470479
std::unique_ptr<MCSubtargetInfo> STI(
471480
TheTarget->createMCSubtargetInfo(TheTriple, MCPU, FeaturesStr));
472481
if (!STI) {

0 commit comments

Comments
 (0)