Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/test/MC/AsmParser/native.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# RUN: llvm-mc -filetype=obj -o %t -mcpu=native %s 2> %t.stderr
# RUN: FileCheck --allow-empty %s < %t.stderr

# CHECK-NOT: {{.+}}
19 changes: 14 additions & 5 deletions llvm/tools/llvm-mc/llvm-mc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,14 +459,23 @@ int main(int argc, char **argv) {
MAI->setCommentColumn(CommentColumn);

// Package up features to be passed to target/subtarget
SubtargetFeatures Features;
std::string FeaturesStr;
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 and features.
if (MCPU == "native") {
MCPU = std::string(llvm::sys::getHostCPUName());

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to pass call getHostCPUFeatures too and pass that information to MAttr?

I know on X86, getHostCPUName can return "haswell" on Pentium CPUs that don't support AVX2, but use the haswell microarchitecture. "haswell" implies AVX2. getHostCPUFeatures will return -avx2 for these CPUs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that's a good point.

It doesn't look like X86 supports -mcpu= in Clang.

I do see that -march= is supported, but Clang just maps to llvm::sys::getHostCPUName(), like this patch, but with the obvious FIXME.

clang/lib/Driver/ToolChains/Arch/X86.cpp:

std::string x86::getX86TargetCPU(const Driver &D, const ArgList &Args,
                                 const llvm::Triple &Triple) {
  if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {
    StringRef CPU = A->getValue();
    if (CPU != "native")
      return std::string(CPU);

    // FIXME: Reject attempts to use -march=native unless the target matches
    // the host.
    CPU = llvm::sys::getHostCPUName();
    if (!CPU.empty() && CPU != "generic")
      return std::string(CPU);

I don't have a strong opinion here, but do have a strong opinion that MC needs to support -mcpu=native in some way. So whatever you suggest is fine with me.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clang does use getHostCPUFeatures with -mcpu=native. I wrote the code originally. It's here.

void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,      
                               const ArgList &Args,                              
                               std::vector<StringRef> &Features) {               
  // Claim and report unsupported -mabi=. Note: we don't support "sysv_abi" or   
  // "ms_abi" as default function attributes.                                    
  if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mabi_EQ)) {     
    StringRef DefaultAbi =                                                       
        (Triple.isOSWindows() || Triple.isUEFI()) ? "ms" : "sysv";               
    if (A->getValue() != DefaultAbi)                                             
      D.Diag(diag::err_drv_unsupported_opt_for_target)                           
          << A->getSpelling() << Triple.getTriple();                             
  }                                                                              
                                                                                 
  // If -march=native, autodetect the feature list.                              
  if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {    
    if (StringRef(A->getValue()) == "native") {                                  
      for (auto &F : llvm::sys::getHostCPUFeatures())                            
        Features.push_back(                                                      
            Args.MakeArgString((F.second ? "+" : "-") + F.first()));             
    }                                                                            
  }  

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do see that -march= is supported, but Clang just maps to llvm::sys::getHostCPUName(), like this patch, but with the obvious FIXME.

I think that fixme is maybe saying we shouldn't call getHostCPU if you explicitly pass --target=x86_64 on a non-x86 machine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clang does use getHostCPUFeatures with -mcpu=native. I wrote the code originally. It's here.

That's -march now. Maybe changed when -mcpu was deprecated on X86.

$ clang test.c -mcpu=native
clang: error: unsupported option '-mcpu=' for target 'x86_64-unknown-linux-gnu'

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clang does use getHostCPUFeatures with -mcpu=native. I wrote the code originally. It's here.

That's -march now. Maybe changed when -mcpu was deprecated on X86.

$ clang test.c -mcpu=native
clang: error: unsupported option '-mcpu=' for target 'x86_64-unknown-linux-gnu'

Sorry I meant -march=native. For all intents and purpose -march on X86 is -mcpu. I've been on RISC-V for too long now so I my brain thinks -mcpu.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest patch addresses this. I think it's best to allow the command line -mattr's to override the default with native, but I don't feel strongly about it. Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest patch addresses this. I think it's best to allow the command line -mattr's to override the default with native, but I don't feel strongly about it. Thoughts?

I agree.

llvm::StringMap<bool> TargetFeatures = llvm::sys::getHostCPUFeatures();
for (auto const &[FeatureName, IsSupported] : TargetFeatures)
Features.AddFeature(FeatureName, IsSupported);
}

// Handle features passed to target/subtarget.
for (unsigned i = 0; i != MAttrs.size(); ++i)
Features.AddFeature(MAttrs[i]);
FeaturesStr = Features.getString();

std::unique_ptr<MCSubtargetInfo> STI(
TheTarget->createMCSubtargetInfo(TheTriple, MCPU, FeaturesStr));
assert(STI && "Unable to create subtarget info!");
Expand Down