diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -654,6 +654,17 @@ return args.hasArg(OPT_pie); } +static void parseClangOption(StringRef opt, const Twine &msg) { + std::string err; + raw_string_ostream os(err); + + const char *argv[] = {"lld", opt.data()}; + if (cl::ParseCommandLineOptions(2, argv, "", &os)) + return; + os.flush(); + error(msg + ": " + StringRef(err).trim()); +} + bool macho::link(llvm::ArrayRef argsArr, bool canExitEarly, raw_ostream &stdoutOS, raw_ostream &stderrOS) { lld::stdoutOS = &stdoutOS; @@ -788,6 +799,11 @@ error("-sub_library " + searchName + " does not match a supplied dylib"); } + // Parse LTO options. + if (auto *arg = args.getLastArg(OPT_mcpu)) + parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())), + arg->getSpelling()); + initLLVM(); compileBitcodeFiles(); replaceCommonSymbols(); diff --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp --- a/lld/MachO/LTO.cpp +++ b/lld/MachO/LTO.cpp @@ -27,6 +27,7 @@ static lto::Config createConfig() { lto::Config c; c.Options = initTargetOptionsFromCodeGenFlags(); + c.CPU = getCPUStr(); return c; } diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -878,6 +878,9 @@ HelpText<"Options to pass to LLVM">, Flags<[HelpHidden]>, Group; +def mcpu : Separate<["-"], "mcpu">, + HelpText<"Processor family target for LTO code generation">, + Group; def grp_deprecated : OptionGroup<"deprecated">, HelpText<"DEPRECATED">; @@ -1210,10 +1213,6 @@ HelpText<"Alias for -maccatalyst_version_min">, Flags<[HelpHidden]>, Group; -def mcpu : Flag<["-"], "mcpu">, - HelpText<"This option is undocumented in ld64">, - Flags<[HelpHidden]>, - Group; def no_compact_unwind : Flag<["-"], "no_compact_unwind">, HelpText<"This option is undocumented in ld64">, Flags<[HelpHidden]>, diff --git a/lld/test/MachO/cpu-string.ll b/lld/test/MachO/cpu-string.ll new file mode 100644 --- /dev/null +++ b/lld/test/MachO/cpu-string.ll @@ -0,0 +1,23 @@ +; REQUIRES: x86 +; RUN: llvm-as %s -o %t.o + +; RUN: %lld %t.o -o %t.dylib -dylib +; RUN: llvm-objdump -d --section="__text" --no-leading-addr --no-show-raw-insn %t.dylib | FileCheck %s +; CHECK: nop{{$}} + +; RUN: %lld -mcpu znver1 %t.o -o %t.znver1.dylib -dylib +; RUN: llvm-objdump -d --section="__text" --no-leading-addr --no-show-raw-insn %t.znver1.dylib | FileCheck %s --check-prefix=ZNVER1 + +; ZNVER1: nopw +; ZNVER1-NOT: nop{{$}} + +target triple = "x86_64-apple-darwin" +target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" + +define void @foo() #0 { +entry: + call void asm sideeffect ".p2align 4, 0x90", "~{dirflag},~{fpsr},~{flags}"() + ret void +} + +attributes #0 = { "frame-pointer"="all" }