Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions llvm/test/MC/AsmParser/native.s
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is too specific for a not check. Better to verify the output is empty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like FileCheck doesn't like an empty input file (really stderr piped to stdout):

FileCheck error: '<stdin>' is empty.

I could make the CHECK-NOT more narrow. Maybe something like:

# CHECK-NOT: native

Does that seem acceptable, @arsenm?

Copy link
Contributor

Choose a reason for hiding this comment

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

I know there's some trick to do this. I ran into this recently but forget what I did

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess checking native works though

Copy link
Contributor

Choose a reason for hiding this comment

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

--allow-empty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in latest patch.

4 changes: 4 additions & 0 deletions llvm/tools/llvm-mc/llvm-mc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

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.

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