[llvm-branch-commits] [lld] 29d3b0e - [lld-macho] Add support for -mcpu, -mattr, -code-model in LTO

Jez Ng via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Dec 10 16:02:57 PST 2020


Author: Jez Ng
Date: 2020-12-10T15:57:51-08:00
New Revision: 29d3b0e47113e011c6fc541fc81c868ef3df21a3

URL: https://github.com/llvm/llvm-project/commit/29d3b0e47113e011c6fc541fc81c868ef3df21a3
DIFF: https://github.com/llvm/llvm-project/commit/29d3b0e47113e011c6fc541fc81c868ef3df21a3.diff

LOG: [lld-macho] Add support for -mcpu, -mattr, -code-model in LTO

`-mcpu` and `-code-model` tests were copied from similar ones in
LLD-ELF.

There doesn't seem to be an equivalent test for `-mattr` in LLD-ELF, so
I've verified our behavior by cribbing a test from
CodeGen/X86/recip-fastmath.ll.

Reviewed By: #lld-macho, compnerd, MaskRay

Differential Revision: https://reviews.llvm.org/D92912

Added: 
    lld/test/MachO/codemodel.ll
    lld/test/MachO/cpu-string.ll
    lld/test/MachO/mattrs.ll

Modified: 
    lld/MachO/Driver.cpp
    lld/MachO/LTO.cpp
    lld/MachO/Options.td

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 48e7e7e8dc0d..432bb81925da 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -654,6 +654,17 @@ static bool isPie(opt::InputArgList &args) {
   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<const char *> argsArr, bool canExitEarly,
                  raw_ostream &stdoutOS, raw_ostream &stderrOS) {
   lld::stdoutOS = &stdoutOS;
@@ -788,6 +799,14 @@ bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly,
       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());
+
+  for (auto *arg : args.filtered(OPT_mllvm))
+    parseClangOption(arg->getValue(), arg->getSpelling());
+
   initLLVM();
   compileBitcodeFiles();
   replaceCommonSymbols();

diff  --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp
index f2a0c683052f..9e74b69ff1ec 100644
--- a/lld/MachO/LTO.cpp
+++ b/lld/MachO/LTO.cpp
@@ -27,6 +27,9 @@ using namespace llvm::sys;
 static lto::Config createConfig() {
   lto::Config c;
   c.Options = initTargetOptionsFromCodeGenFlags();
+  c.CodeModel = getCodeModelFromCMModel();
+  c.CPU = getCPUStr();
+  c.MAttrs = getMAttrs();
   return c;
 }
 

diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index b05bc9fc8073..f00e23bfd791 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -876,7 +876,9 @@ def not_for_dyld_shared_cache : Flag<["-"], "not_for_dyld_shared_cache">,
      Group<grp_rare>;
 def mllvm : Separate<["-"], "mllvm">,
      HelpText<"Options to pass to LLVM">,
-     Flags<[HelpHidden]>,
+     Group<grp_rare>;
+def mcpu : Separate<["-"], "mcpu">,
+     HelpText<"Processor family target for LTO code generation">,
      Group<grp_rare>;
 
 def grp_deprecated : OptionGroup<"deprecated">, HelpText<"DEPRECATED">;
@@ -1210,10 +1212,6 @@ def uikitformac_version_min : Flag<["-"], "uikitformac_version_min">,
      HelpText<"Alias for -maccatalyst_version_min">,
      Flags<[HelpHidden]>,
      Group<grp_undocumented>;
-def mcpu : Flag<["-"], "mcpu">,
-     HelpText<"This option is undocumented in ld64">,
-     Flags<[HelpHidden]>,
-     Group<grp_undocumented>;
 def no_compact_unwind : Flag<["-"], "no_compact_unwind">,
      HelpText<"This option is undocumented in ld64">,
      Flags<[HelpHidden]>,

diff  --git a/lld/test/MachO/codemodel.ll b/lld/test/MachO/codemodel.ll
new file mode 100644
index 000000000000..2c5fec775b25
--- /dev/null
+++ b/lld/test/MachO/codemodel.ll
@@ -0,0 +1,20 @@
+; REQUIRES: x86
+; RUN: llvm-as %s -o %t.o
+; RUN: %lld -lSystem %t.o -o %ts -mllvm -code-model=small
+; RUN: %lld -lSystem %t.o -o %tl -mllvm -code-model=large
+; RUN: llvm-objdump -d %ts | FileCheck %s --check-prefix=CHECK-SMALL
+; RUN: llvm-objdump -d %tl | FileCheck %s --check-prefix=CHECK-LARGE
+
+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"
+
+ at data = internal constant [0 x i32] []
+
+define i32* @main() nounwind readonly {
+entry:
+; CHECK-SMALL-LABEL: <_main>:
+; CHECK-SMALL:       leaq    [[#]](%rip), %rax
+; CHECK-LARGE-LABEL: <_main>:
+; CHECK-LARGE:       movabsq $[[#]], %rax
+  ret i32* getelementptr ([0 x i32], [0 x i32]* @data, i64 0, i64 0)
+}

diff  --git a/lld/test/MachO/cpu-string.ll b/lld/test/MachO/cpu-string.ll
new file mode 100644
index 000000000000..dcd51bef6f44
--- /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" }

diff  --git a/lld/test/MachO/mattrs.ll b/lld/test/MachO/mattrs.ll
new file mode 100644
index 000000000000..66415013093b
--- /dev/null
+++ b/lld/test/MachO/mattrs.ll
@@ -0,0 +1,35 @@
+; REQUIRES: x86
+; RUN: llvm-as %s -o %t.o
+
+;; Verify that LTO behavior can be tweaked using -mattr.
+
+; RUN: %lld -mcpu haswell -mllvm -mattr=+fma %t.o -o %t.dylib -dylib
+; RUN: llvm-objdump -d --section="__text" --no-leading-addr --no-show-raw-insn %t.dylib | FileCheck %s --check-prefix=FMA
+
+; RUN: %lld -mcpu haswell -mllvm -mattr=-fma %t.o -o %t.dylib -dylib
+; RUN: llvm-objdump -d --section="__text" --no-leading-addr --no-show-raw-insn %t.dylib | FileCheck %s --check-prefix=NO-FMA
+
+; FMA:      <_foo>:
+; FMA-NEXT:	vrcpss	%xmm0, %xmm0, %xmm1
+; FMA-NEXT:	vfmsub213ss	7(%rip), %xmm1, %xmm0  # 2d4
+; FMA-NEXT:	vfnmadd132ss	%xmm1, %xmm1, %xmm0
+; FMA-NEXT:	retq
+
+; NO-FMA:      <_foo>:
+; NO-FMA-NEXT: vrcpss	%xmm0, %xmm0, %xmm1
+; NO-FMA-NEXT: vmulss	%xmm1, %xmm0, %xmm0
+; NO-FMA-NEXT: vmovss	16(%rip), %xmm2  # 2e0
+; NO-FMA-NEXT: vsubss	%xmm0, %xmm2, %xmm0
+; NO-FMA-NEXT: vmulss	%xmm0, %xmm1, %xmm0
+; NO-FMA-NEXT: vaddss	%xmm0, %xmm1, %xmm0
+; NO-FMA-NEXT: retq
+
+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 float @foo(float %x) #0 {
+  %div = fdiv fast float 1.0, %x
+  ret float %div
+}
+
+attributes #0 = { "unsafe-fp-math"="true" "reciprocal-estimates"="divf,vec-divf" }


        


More information about the llvm-branch-commits mailing list