[clang] 9ce198d - [Darwin] Respect -fno-unroll-loops during LTO.

Florian Hahn via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 27 15:19:26 PDT 2020


Author: Florian Hahn
Date: 2020-03-27T22:19:03Z
New Revision: 9ce198d6ed371399e9bd9ba8b48fbab0f4e60240

URL: https://github.com/llvm/llvm-project/commit/9ce198d6ed371399e9bd9ba8b48fbab0f4e60240
DIFF: https://github.com/llvm/llvm-project/commit/9ce198d6ed371399e9bd9ba8b48fbab0f4e60240.diff

LOG: [Darwin] Respect -fno-unroll-loops during LTO.

Currently -fno-unroll-loops is ignored when doing LTO on Darwin. This
patch adds a new -lto-no-unroll-loops option to the LTO code generator
and forwards it to the linker if -fno-unroll-loops is passed.

Reviewers: thegameg, steven_wu

Reviewed By: thegameg

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

Added: 
    clang/test/Driver/darwin-ld-lto-fno-unroll-loops.c
    llvm/test/tools/llvm-lto/fno-unroll-loops-option.ll

Modified: 
    clang/lib/Driver/ToolChains/Darwin.cpp
    llvm/lib/LTO/LTOCodeGenerator.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index 451d0d206d07..951c71bff00e 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -543,6 +543,12 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
     CmdArgs.push_back(Args.MakeArgString("-lto-stats-file=" + StatsFile.str()));
   }
 
+  // Forward -fno-unroll-loops to the linker in LTO.
+  if (Args.hasArg(options::OPT_fno_unroll_loops)) {
+    CmdArgs.push_back("-mllvm");
+    CmdArgs.push_back(Args.MakeArgString("-lto-no-unroll-loops"));
+  }
+
   // It seems that the 'e' option is completely ignored for dynamic executables
   // (the default), and with static executables, the last one wins, as expected.
   Args.AddAllArgs(CmdArgs, {options::OPT_d_Flag, options::OPT_s, options::OPT_t,

diff  --git a/clang/test/Driver/darwin-ld-lto-fno-unroll-loops.c b/clang/test/Driver/darwin-ld-lto-fno-unroll-loops.c
new file mode 100644
index 000000000000..b248898a89f5
--- /dev/null
+++ b/clang/test/Driver/darwin-ld-lto-fno-unroll-loops.c
@@ -0,0 +1,17 @@
+// REQUIRES: system-darwin
+
+// RUN: mkdir -p %t/bin
+// RUN: mkdir -p %t/lib
+// RUN: touch %t/lib/libLTO.dylib
+
+// Check that ld gets "-lto-no-unroll-loops" when -fno-unroll-loops is passed.
+//
+// RUN: %clang -target x86_64-apple-darwin10 %s -fno-unroll-loops -flto=full -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=NOUNROLL %s
+
+// NOUNROLL:  "-mllvm" "-lto-no-unroll-loops"
+//
+// RUN: %clang -target x86_64-apple-darwin10 %s -flto=full -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=UNROLL %s
+
+// UNROLL-NOT:  -lto-no-unroll-loops

diff  --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp
index a8a7877f66da..d2ae956b7823 100644
--- a/llvm/lib/LTO/LTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/LTOCodeGenerator.cpp
@@ -109,6 +109,10 @@ cl::opt<std::string> LTOStatsFile(
     cl::Hidden);
 }
 
+cl::opt<bool> LTONoUnrollLoops("lto-no-unroll-loops",
+                               cl::desc("Disable unrolling during LTO."),
+                               cl::Hidden, cl::init(false));
+
 LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
     : Context(Context), MergedModule(new Module("ld-temp.o", Context)),
       TheLinker(new Linker(*MergedModule)) {
@@ -570,6 +574,7 @@ bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
 
   Triple TargetTriple(TargetMach->getTargetTriple());
   PassManagerBuilder PMB;
+  PMB.DisableUnrollLoops = LTONoUnrollLoops;
   PMB.DisableGVNLoadPRE = DisableGVNLoadPRE;
   PMB.LoopVectorize = !DisableVectorization;
   PMB.SLPVectorize = !DisableVectorization;

diff  --git a/llvm/test/tools/llvm-lto/fno-unroll-loops-option.ll b/llvm/test/tools/llvm-lto/fno-unroll-loops-option.ll
new file mode 100644
index 000000000000..3ac4c285ffe7
--- /dev/null
+++ b/llvm/test/tools/llvm-lto/fno-unroll-loops-option.ll
@@ -0,0 +1,34 @@
+; REQUIRES: asserts
+
+; RUN: llvm-as < %s > %t1.bc
+
+; Build with unrolling disabled (-lto-no-unroll-loops).
+; RUN: llvm-lto %t1.bc -o %t.nounroll.o -lto-no-unroll-loops --exported-symbol=foo -save-merged-module
+; RUN: llvm-dis  -o - %t.nounroll.o.merged.bc | FileCheck --check-prefix=NOUNROLL %s
+
+; NOUNROLL: br label %loop
+; NOUNROLL: br i1 %ec, label %exit, label %loop
+
+; Build with unrolling enabled (by not passing -lto-no-unroll-loops). All
+; branches should be gone.
+; RUN: llvm-lto %t1.bc -o %t.nounroll.o --exported-symbol=foo -save-merged-module
+; RUN: llvm-dis  -o - %t.nounroll.o.merged.bc | FileCheck --check-prefix=UNROLL %s
+
+; UNROLL-NOT: br
+
+define void @foo(i32* %ptr) {
+
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i32 [ 0, %entry], [ %iv.next, %loop ]
+  %iv.ptr = getelementptr i32, i32* %ptr, i32 %iv
+  store i32 %iv, i32* %iv.ptr
+  %iv.next = add i32 %iv, 1
+  %ec = icmp eq i32 %iv.next, 10
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  ret void
+}


        


More information about the cfe-commits mailing list