Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit dea079e

Browse files
[Driver] Add -static-openmp driver option
Summary: For Gnu, FreeBSD and NetBSD, this option forces linking with the static OpenMP host runtime (similar to -static-libgcc and -static-libstdcxx). Android's NDK will start the shared OpenMP runtime in addition to the static libomp. In this scenario, the linker will prefer to use the shared library by default. Add this option to enable linking with the static libomp. Reviewers: Hahnfeld, danalbert, srhines, joerg, jdoerfert Subscribers: guansong, cfe-commits Tags: #clang Fixes android/ndk#1028 Differential Revision: https://reviews.llvm.org/D67200 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@371437 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent be05121 commit dea079e

File tree

7 files changed

+67
-12
lines changed

7 files changed

+67
-12
lines changed

include/clang/Driver/Options.td

+2
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,8 @@ def fopenmp_optimistic_collapse : Flag<["-"], "fopenmp-optimistic-collapse">, Gr
16161616
Flags<[CC1Option, NoArgumentUnused, HelpHidden]>;
16171617
def fno_openmp_optimistic_collapse : Flag<["-"], "fno-openmp-optimistic-collapse">, Group<f_Group>,
16181618
Flags<[NoArgumentUnused, HelpHidden]>;
1619+
def static_openmp: Flag<["-"], "static-openmp">,
1620+
HelpText<"Use the static host OpenMP runtime while linking.">;
16191621
def fno_optimize_sibling_calls : Flag<["-"], "fno-optimize-sibling-calls">, Group<f_Group>;
16201622
def foptimize_sibling_calls : Flag<["-"], "foptimize-sibling-calls">, Group<f_Group>;
16211623
def fno_escaping_block_tail_calls : Flag<["-"], "fno-escaping-block-tail-calls">, Group<f_Group>, Flags<[CC1Option]>;

lib/Driver/ToolChains/CommonArgs.cpp

+18-9
Original file line numberDiff line numberDiff line change
@@ -500,30 +500,39 @@ void tools::addArchSpecificRPath(const ToolChain &TC, const ArgList &Args,
500500
}
501501

502502
bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
503-
const ArgList &Args, bool IsOffloadingHost,
504-
bool GompNeedsRT) {
503+
const ArgList &Args, bool ForceStaticHostRuntime,
504+
bool IsOffloadingHost, bool GompNeedsRT) {
505505
if (!Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
506506
options::OPT_fno_openmp, false))
507507
return false;
508508

509-
switch (TC.getDriver().getOpenMPRuntime(Args)) {
509+
Driver::OpenMPRuntimeKind RTKind = TC.getDriver().getOpenMPRuntime(Args);
510+
511+
if (RTKind == Driver::OMPRT_Unknown)
512+
// Already diagnosed.
513+
return false;
514+
515+
if (ForceStaticHostRuntime)
516+
CmdArgs.push_back("-Bstatic");
517+
518+
switch (RTKind) {
510519
case Driver::OMPRT_OMP:
511520
CmdArgs.push_back("-lomp");
512521
break;
513522
case Driver::OMPRT_GOMP:
514523
CmdArgs.push_back("-lgomp");
515-
516-
if (GompNeedsRT)
517-
CmdArgs.push_back("-lrt");
518524
break;
519525
case Driver::OMPRT_IOMP5:
520526
CmdArgs.push_back("-liomp5");
521527
break;
522-
case Driver::OMPRT_Unknown:
523-
// Already diagnosed.
524-
return false;
525528
}
526529

530+
if (ForceStaticHostRuntime)
531+
CmdArgs.push_back("-Bdynamic");
532+
533+
if (RTKind == Driver::OMPRT_GOMP && GompNeedsRT)
534+
CmdArgs.push_back("-lrt");
535+
527536
if (IsOffloadingHost)
528537
CmdArgs.push_back("-lomptarget");
529538

lib/Driver/ToolChains/CommonArgs.h

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ void addArchSpecificRPath(const ToolChain &TC, const llvm::opt::ArgList &Args,
8484
/// Returns true, if an OpenMP runtime has been added.
8585
bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC,
8686
const llvm::opt::ArgList &Args,
87+
bool ForceStaticHostRuntime = false,
8788
bool IsOffloadingHost = false, bool GompNeedsRT = false);
8889

8990
llvm::opt::Arg *getLastProfileUseArg(const llvm::opt::ArgList &Args);

lib/Driver/ToolChains/FreeBSD.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,11 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
270270
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
271271

272272
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
273-
addOpenMPRuntime(CmdArgs, ToolChain, Args);
273+
// Use the static OpenMP runtime with -static-openmp
274+
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
275+
!Args.hasArg(options::OPT_static);
276+
addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
277+
274278
if (D.CCCIsCXX()) {
275279
if (ToolChain.ShouldLinkCXXStdlib(Args))
276280
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);

lib/Driver/ToolChains/Gnu.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,13 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
555555
bool WantPthread = Args.hasArg(options::OPT_pthread) ||
556556
Args.hasArg(options::OPT_pthreads);
557557

558+
// Use the static OpenMP runtime with -static-openmp
559+
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
560+
!Args.hasArg(options::OPT_static);
561+
558562
// FIXME: Only pass GompNeedsRT = true for platforms with libgomp that
559563
// require librt. Most modern Linux platforms do, but some may not.
560-
if (addOpenMPRuntime(CmdArgs, ToolChain, Args,
564+
if (addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP,
561565
JA.isHostOffloading(Action::OFK_OpenMP),
562566
/* GompNeedsRT= */ true))
563567
// OpenMP runtimes implies pthreads when using the GNU toolchain.

lib/Driver/ToolChains/NetBSD.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,11 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
289289
}
290290

291291
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
292-
addOpenMPRuntime(CmdArgs, getToolChain(), Args);
292+
// Use the static OpenMP runtime with -static-openmp
293+
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
294+
!Args.hasArg(options::OPT_static);
295+
addOpenMPRuntime(CmdArgs, getToolChain(), Args, StaticOpenMP);
296+
293297
if (D.CCCIsCXX()) {
294298
if (ToolChain.ShouldLinkCXXStdlib(Args))
295299
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);

test/Driver/fopenmp.c

+31
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
// RUN: %clang -target x86_64-linux-gnu -fopenmp=libgomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-GOMP --check-prefix=CHECK-LD-GOMP-RT
3232
// RUN: %clang -target x86_64-linux-gnu -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-IOMP5
3333
//
34+
// RUN: %clang -target x86_64-linux-gnu -fopenmp=libomp -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-OMP
35+
// RUN: %clang -target x86_64-linux-gnu -fopenmp=libgomp -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-GOMP --check-prefix=CHECK-LD-STATIC-GOMP-RT
36+
// RUN: %clang -target x86_64-linux-gnu -fopenmp=libiomp5 -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-IOMP5
37+
// RUN: %clang -target x86_64-linux-gnu -fopenmp=libiomp5 -static -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-IOMP5-NO-BDYNAMIC
38+
//
3439
// RUN: %clang -nostdlib -target x86_64-linux-gnu -fopenmp=libomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-OMP
3540
// RUN: %clang -nostdlib -target x86_64-linux-gnu -fopenmp=libgomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-GOMP
3641
// RUN: %clang -nostdlib -target x86_64-linux-gnu -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-IOMP5
@@ -47,6 +52,11 @@
4752
// RUN: %clang -target x86_64-freebsd -fopenmp=libgomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-GOMP --check-prefix=CHECK-LD-GOMP-NO-RT
4853
// RUN: %clang -target x86_64-freebsd -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-IOMP5
4954
//
55+
// RUN: %clang -target x86_64-freebsd -fopenmp=libomp -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-OMP
56+
// RUN: %clang -target x86_64-freebsd -fopenmp=libgomp -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-GOMP --check-prefix=CHECK-LD-STATIC-GOMP-NO-RT
57+
// RUN: %clang -target x86_64-freebsd -fopenmp=libiomp5 -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-IOMP5
58+
// RUN: %clang -target x86_64-freebsd -fopenmp=libiomp5 -static -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-IOMP5-NO-BDYNAMIC
59+
//
5060
// RUN: %clang -nostdlib -target x86_64-freebsd -fopenmp=libomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-OMP
5161
// RUN: %clang -nostdlib -target x86_64-freebsd -fopenmp=libgomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-GOMP
5262
// RUN: %clang -nostdlib -target x86_64-freebsd -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-IOMP5
@@ -55,6 +65,11 @@
5565
// RUN: %clang -target x86_64-netbsd -fopenmp=libgomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-GOMP --check-prefix=CHECK-LD-GOMP-NO-RT
5666
// RUN: %clang -target x86_64-netbsd -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-IOMP5
5767
//
68+
// RUN: %clang -target x86_64-netbsd -fopenmp=libomp -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-OMP
69+
// RUN: %clang -target x86_64-netbsd -fopenmp=libgomp -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-GOMP --check-prefix=CHECK-LD-STATIC-GOMP-NO-RT
70+
// RUN: %clang -target x86_64-netbsd -fopenmp=libiomp5 -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-IOMP5
71+
// RUN: %clang -target x86_64-netbsd -fopenmp=libiomp5 -static -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-IOMP5-NO-BDYNAMIC
72+
//
5873
// RUN: %clang -nostdlib -target x86_64-netbsd -fopenmp=libomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-OMP
5974
// RUN: %clang -nostdlib -target x86_64-netbsd -fopenmp=libgomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-GOMP
6075
// RUN: %clang -nostdlib -target x86_64-netbsd -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-IOMP5
@@ -93,6 +108,22 @@
93108
// CHECK-NO-IOMP5MD: "{{.*}}ld{{(.exe)?}}"
94109
// CHECK-NO-IOMP5MD-NOT: "-liomp5md"
95110
//
111+
// CHECK-LD-STATIC-OMP: "{{.*}}ld{{(.exe)?}}"
112+
// CHECK-LD-STATIC-OMP: "-Bstatic" "-lomp" "-Bdynamic"
113+
//
114+
// CHECK-LD-STATIC-GOMP: "{{.*}}ld{{(.exe)?}}"
115+
// CHECK-LD-STATIC-GOMP: "-Bstatic" "-lgomp" "-Bdynamic"
116+
// CHECK-LD-STATIC-GOMP-RT: "-lrt"
117+
// CHECK-LD-STATIC-NO-GOMP-RT-NOT: "-lrt"
118+
//
119+
// CHECK-LD-STATIC-IOMP5: "{{.*}}ld{{(.exe)?}}"
120+
// CHECK-LD-STATIC-IOMP5: "-Bstatic" "-liomp5" "-Bdynamic"
121+
//
122+
// CHECK-LD-STATIC-IOMP5-NO-BDYNAMIC: "{{.*}}ld{{(.exe)?}}"
123+
// For x86 Gnu, the driver passes -static, while NetBSD and FreeBSD pass -Bstatic
124+
// CHECK-LD-STATIC-IOMP5-NO-BDYNAMIC: "-{{B?}}static" {{.*}} "-liomp5"
125+
// CHECK-LD-STATIC-IOMP5-NO-BDYNAMIC-NOT: "-Bdynamic"
126+
//
96127
// We'd like to check that the default is sane, but until we have the ability
97128
// to *always* semantically analyze OpenMP without always generating runtime
98129
// calls (in the event of an unsupported runtime), we don't have a good way to

0 commit comments

Comments
 (0)