Skip to content

Commit cfdc105

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 llvm-svn: 371437
1 parent 85305ea commit cfdc105

File tree

7 files changed

+67
-12
lines changed

7 files changed

+67
-12
lines changed

clang/include/clang/Driver/Options.td

+2
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,8 @@ def fopenmp_optimistic_collapse : Flag<["-"], "fopenmp-optimistic-collapse">, Gr
15981598
Flags<[CC1Option, NoArgumentUnused, HelpHidden]>;
15991599
def fno_openmp_optimistic_collapse : Flag<["-"], "fno-openmp-optimistic-collapse">, Group<f_Group>,
16001600
Flags<[NoArgumentUnused, HelpHidden]>;
1601+
def static_openmp: Flag<["-"], "static-openmp">,
1602+
HelpText<"Use the static host OpenMP runtime while linking.">;
16011603
def fno_optimize_sibling_calls : Flag<["-"], "fno-optimize-sibling-calls">, Group<f_Group>;
16021604
def foptimize_sibling_calls : Flag<["-"], "foptimize-sibling-calls">, Group<f_Group>;
16031605
def fno_escaping_block_tail_calls : Flag<["-"], "fno-escaping-block-tail-calls">, Group<f_Group>, Flags<[CC1Option]>;

clang/lib/Driver/ToolChains/CommonArgs.cpp

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

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

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

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

clang/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);

clang/lib/Driver/ToolChains/FreeBSD.cpp

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

264264
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
265-
addOpenMPRuntime(CmdArgs, ToolChain, Args);
265+
// Use the static OpenMP runtime with -static-openmp
266+
bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
267+
!Args.hasArg(options::OPT_static);
268+
addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
269+
266270
if (D.CCCIsCXX()) {
267271
if (ToolChain.ShouldLinkCXXStdlib(Args))
268272
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);

clang/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.

clang/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);

clang/test/Driver/fopenmp.c

+31
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
// 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
2525
// RUN: %clang -target x86_64-linux-gnu -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-IOMP5
2626
//
27+
// RUN: %clang -target x86_64-linux-gnu -fopenmp=libomp -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-OMP
28+
// 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
29+
// RUN: %clang -target x86_64-linux-gnu -fopenmp=libiomp5 -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-IOMP5
30+
// 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
31+
//
2732
// RUN: %clang -nostdlib -target x86_64-linux-gnu -fopenmp=libomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-OMP
2833
// RUN: %clang -nostdlib -target x86_64-linux-gnu -fopenmp=libgomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-GOMP
2934
// RUN: %clang -nostdlib -target x86_64-linux-gnu -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-IOMP5
@@ -40,6 +45,11 @@
4045
// 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
4146
// RUN: %clang -target x86_64-freebsd -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-IOMP5
4247
//
48+
// RUN: %clang -target x86_64-freebsd -fopenmp=libomp -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-OMP
49+
// 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
50+
// RUN: %clang -target x86_64-freebsd -fopenmp=libiomp5 -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-IOMP5
51+
// 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
52+
//
4353
// RUN: %clang -nostdlib -target x86_64-freebsd -fopenmp=libomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-OMP
4454
// RUN: %clang -nostdlib -target x86_64-freebsd -fopenmp=libgomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-GOMP
4555
// RUN: %clang -nostdlib -target x86_64-freebsd -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-IOMP5
@@ -48,6 +58,11 @@
4858
// 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
4959
// RUN: %clang -target x86_64-netbsd -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-IOMP5
5060
//
61+
// RUN: %clang -target x86_64-netbsd -fopenmp=libomp -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-OMP
62+
// 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
63+
// RUN: %clang -target x86_64-netbsd -fopenmp=libiomp5 -static-openmp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-STATIC-IOMP5
64+
// 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
65+
//
5166
// RUN: %clang -nostdlib -target x86_64-netbsd -fopenmp=libomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-OMP
5267
// RUN: %clang -nostdlib -target x86_64-netbsd -fopenmp=libgomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-GOMP
5368
// RUN: %clang -nostdlib -target x86_64-netbsd -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-NO-IOMP5
@@ -86,6 +101,22 @@
86101
// CHECK-NO-IOMP5MD: "{{.*}}ld{{(.exe)?}}"
87102
// CHECK-NO-IOMP5MD-NOT: "-liomp5md"
88103
//
104+
// CHECK-LD-STATIC-OMP: "{{.*}}ld{{(.exe)?}}"
105+
// CHECK-LD-STATIC-OMP: "-Bstatic" "-lomp" "-Bdynamic"
106+
//
107+
// CHECK-LD-STATIC-GOMP: "{{.*}}ld{{(.exe)?}}"
108+
// CHECK-LD-STATIC-GOMP: "-Bstatic" "-lgomp" "-Bdynamic"
109+
// CHECK-LD-STATIC-GOMP-RT: "-lrt"
110+
// CHECK-LD-STATIC-NO-GOMP-RT-NOT: "-lrt"
111+
//
112+
// CHECK-LD-STATIC-IOMP5: "{{.*}}ld{{(.exe)?}}"
113+
// CHECK-LD-STATIC-IOMP5: "-Bstatic" "-liomp5" "-Bdynamic"
114+
//
115+
// CHECK-LD-STATIC-IOMP5-NO-BDYNAMIC: "{{.*}}ld{{(.exe)?}}"
116+
// For x86 Gnu, the driver passes -static, while NetBSD and FreeBSD pass -Bstatic
117+
// CHECK-LD-STATIC-IOMP5-NO-BDYNAMIC: "-{{B?}}static" {{.*}} "-liomp5"
118+
// CHECK-LD-STATIC-IOMP5-NO-BDYNAMIC-NOT: "-Bdynamic"
119+
//
89120
// We'd like to check that the default is sane, but until we have the ability
90121
// to *always* semantically analyze OpenMP without always generating runtime
91122
// calls (in the event of an unsupported runtime), we don't have a good way to

0 commit comments

Comments
 (0)