r284798 - [Driver][Darwin] Pass -no_deduplicate to ld64

Bruno Cardoso Lopes via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 20 18:49:15 PDT 2016


Author: bruno
Date: Thu Oct 20 20:49:14 2016
New Revision: 284798

URL: http://llvm.org/viewvc/llvm-project?rev=284798&view=rev
Log:
[Driver][Darwin] Pass -no_deduplicate to ld64

Recent versions of ld64 run a deduplicate pass, which is on by default.
Disable the pass by using -no_deduplicate in certain condition and
enhance total compile time.

rdar://problem/25455336

Added:
    cfe/trunk/test/Driver/darwin-ld-dedup.c
Modified:
    cfe/trunk/include/clang/Driver/Job.h
    cfe/trunk/lib/Driver/Tools.cpp

Modified: cfe/trunk/include/clang/Driver/Job.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Job.h?rev=284798&r1=284797&r2=284798&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/Job.h (original)
+++ cfe/trunk/include/clang/Driver/Job.h Thu Oct 20 20:49:14 2016
@@ -175,6 +175,7 @@ public:
 
   const list_type &getJobs() const { return Jobs; }
 
+  bool empty() const { return Jobs.empty(); }
   size_type size() const { return Jobs.size(); }
   iterator begin() { return Jobs.begin(); }
   const_iterator begin() const { return Jobs.begin(); }

Modified: cfe/trunk/lib/Driver/Tools.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=284798&r1=284797&r2=284798&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Tools.cpp (original)
+++ cfe/trunk/lib/Driver/Tools.cpp Thu Oct 20 20:49:14 2016
@@ -7882,6 +7882,29 @@ bool darwin::Linker::NeedsTempPath(const
   return false;
 }
 
+/// \brief Pass -no_deduplicate to ld64 under certain conditions:
+///
+/// - Either -O0 or -O1 is explicitly specified
+/// - No -O option is specified *and* this is a compile+link (implicit -O0)
+///
+/// Also do *not* add -no_deduplicate when no -O option is specified and this
+/// is just a link (we can't imply -O0)
+static bool shouldLinkerNotDedup(bool IsLinkerOnlyAction, const ArgList &Args) {
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
+    if (A->getOption().matches(options::OPT_O0))
+      return true;
+    if (A->getOption().matches(options::OPT_O))
+      return llvm::StringSwitch<bool>(A->getValue())
+                    .Case("1", true)
+                    .Default(false);
+    return false; // OPT_Ofast & OPT_O4
+  }
+
+  if (!IsLinkerOnlyAction) // Implicit -O0 for compile+linker only.
+    return true;
+  return false;
+}
+
 void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args,
                                  ArgStringList &CmdArgs,
                                  const InputInfoList &Inputs) const {
@@ -7938,6 +7961,10 @@ void darwin::Linker::AddLinkArgs(Compila
     }
   }
 
+  // ld64 version 262 and above run the deduplicate pass by default.
+  if (Version[0] >= 262 && shouldLinkerNotDedup(C.getJobs().empty(), Args))
+    CmdArgs.push_back("-no_deduplicate");
+
   // Derived from the "link" spec.
   Args.AddAllArgs(CmdArgs, options::OPT_static);
   if (!Args.hasArg(options::OPT_static))

Added: cfe/trunk/test/Driver/darwin-ld-dedup.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/darwin-ld-dedup.c?rev=284798&view=auto
==============================================================================
--- cfe/trunk/test/Driver/darwin-ld-dedup.c (added)
+++ cfe/trunk/test/Driver/darwin-ld-dedup.c Thu Oct 20 20:49:14 2016
@@ -0,0 +1,46 @@
+// REQUIRES: system-darwin
+
+// -no_deduplicate is only present from ld64 version 262 and later.
+// RUN: %clang -target x86_64-apple-darwin10 -### %s \
+// RUN:   -mlinker-version=261 -O0 2>&1 | FileCheck -check-prefix=LINK_DEDUP %s
+
+// Add -no_deduplicate when either -O0 or -O1 is explicitly specified
+// RUN: %clang -target x86_64-apple-darwin10 -### %s \
+// RUN:   -mlinker-version=262 -O0 2>&1 | FileCheck -check-prefix=LINK_NODEDUP %s
+// RUN: %clang -target x86_64-apple-darwin10 -### %s \
+// RUN:   -mlinker-version=262 -O1 2>&1 | FileCheck -check-prefix=LINK_NODEDUP %s
+
+// RUN: %clang -target x86_64-apple-darwin10 -### %s \
+// RUN:   -mlinker-version=262 -O2 2>&1 | FileCheck -check-prefix=LINK_DEDUP %s
+// RUN: %clang -target x86_64-apple-darwin10 -### %s \
+// RUN:   -mlinker-version=262 -O3 2>&1 | FileCheck -check-prefix=LINK_DEDUP %s
+// RUN: %clang -target x86_64-apple-darwin10 -### %s \
+// RUN:   -mlinker-version=262 -Os 2>&1 | FileCheck -check-prefix=LINK_DEDUP %s
+// RUN: %clang -target x86_64-apple-darwin10 -### %s \
+// RUN:   -mlinker-version=262 -O4 2>&1 | FileCheck -check-prefix=LINK_DEDUP %s
+// RUN: %clang -target x86_64-apple-darwin10 -### %s \
+// RUN:   -mlinker-version=262 -Ofast 2>&1 | FileCheck -check-prefix=LINK_DEDUP %s
+
+// Add -no_deduplicate when no -O option is specified *and* this is a compile+link
+// (implicit -O0)
+// RUN: %clang -target x86_64-apple-darwin10 -### %s \
+// RUN:   -mlinker-version=262 2>&1 | FileCheck -check-prefix=LINK_NODEDUP %s
+
+// Do *not* add -no_deduplicate when no -O option is specified and this is just a link
+// (since we can't imply -O0)
+// RUN: rm -f %t.o %t.bin
+// RUN: %clang -target x86_64-apple-darwin10 -c -o %t.o %s
+// RUN: %clang -target x86_64-apple-darwin10 %t.o -### -mlinker-version=262 \
+// RUN:   -o %t.bin 2>&1 | FileCheck -check-prefix=LINK_DEDUP %s
+// RUN: %clang -target x86_64-apple-darwin10 %t.o -### -mlinker-version=262 \
+// RUN:   -O0 -o %t.bin 2>&1 | FileCheck -check-prefix=LINK_NODEDUP %s
+// RUN: %clang -target x86_64-apple-darwin10 %t.o -### -mlinker-version=262 \
+// RUN:   -O1 -o %t.bin 2>&1 | FileCheck -check-prefix=LINK_NODEDUP %s
+// RUN: %clang -target x86_64-apple-darwin10 %t.o -### -mlinker-version=262 \
+// RUN:   -O2 -o %t.bin 2>&1 | FileCheck -check-prefix=LINK_DEDUP %s
+
+// LINK_NODEDUP: {{ld(.exe)?"}}
+// LINK_NODEDUP: "-no_deduplicate"
+
+// LINK_DEDUP: {{ld(.exe)?"}}
+// LINK_DEDUP-NOT: "-no_deduplicate"




More information about the cfe-commits mailing list