Skip to content

Commit 6d2f57d

Browse files
[FLANG] allow -fopenmp= (#86816)
This enables the -fopenmp=<library> option to the set of options supported by flang. The generated arguments for the FC1 compilation will appear in a slightly different order, so one test had to be updated to be less sensitive to order of the arguments.
1 parent c6e38b9 commit 6d2f57d

File tree

5 files changed

+114
-24
lines changed

5 files changed

+114
-24
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ def warn_drv_unsupported_diag_option_for_flang : Warning<
142142
def warn_drv_unsupported_option_for_processor : Warning<
143143
"ignoring '%0' option as it is not currently supported for processor '%1'">,
144144
InGroup<OptionIgnored>;
145+
def warn_drv_unsupported_openmp_library : Warning<
146+
"The library '%0=%1' is not supported, openmp is not be enabled">,
147+
InGroup<OptionIgnored>;
145148

146149
def err_drv_invalid_thread_model_for_target : Error<
147150
"invalid thread model '%0' in '%1' for this target">;

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3425,7 +3425,8 @@ defm openmp_extensions: BoolFOption<"openmp-extensions",
34253425
"Enable all Clang extensions for OpenMP directives and clauses">,
34263426
NegFlag<SetFalse, [NoArgumentUnused], [ClangOption, CC1Option],
34273427
"Disable all Clang extensions for OpenMP directives and clauses">>;
3428-
def fopenmp_EQ : Joined<["-"], "fopenmp=">, Group<f_Group>;
3428+
def fopenmp_EQ : Joined<["-"], "fopenmp=">, Group<f_Group>,
3429+
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>;
34293430
def fopenmp_use_tls : Flag<["-"], "fopenmp-use-tls">, Group<f_Group>,
34303431
Flags<[NoArgumentUnused, HelpHidden]>;
34313432
def fnoopenmp_use_tls : Flag<["-"], "fnoopenmp-use-tls">, Group<f_Group>,

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,18 @@ static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
3535

3636
void Flang::addFortranDialectOptions(const ArgList &Args,
3737
ArgStringList &CmdArgs) const {
38-
Args.addAllArgs(CmdArgs, {options::OPT_ffixed_form,
39-
options::OPT_ffree_form,
40-
options::OPT_ffixed_line_length_EQ,
41-
options::OPT_fopenmp,
42-
options::OPT_fopenmp_version_EQ,
43-
options::OPT_fopenacc,
44-
options::OPT_finput_charset_EQ,
45-
options::OPT_fimplicit_none,
46-
options::OPT_fno_implicit_none,
47-
options::OPT_fbackslash,
48-
options::OPT_fno_backslash,
49-
options::OPT_flogical_abbreviations,
50-
options::OPT_fno_logical_abbreviations,
51-
options::OPT_fxor_operator,
52-
options::OPT_fno_xor_operator,
53-
options::OPT_falternative_parameter_statement,
54-
options::OPT_fdefault_real_8,
55-
options::OPT_fdefault_integer_8,
56-
options::OPT_fdefault_double_8,
57-
options::OPT_flarge_sizes,
58-
options::OPT_fno_automatic});
38+
Args.addAllArgs(
39+
CmdArgs, {options::OPT_ffixed_form, options::OPT_ffree_form,
40+
options::OPT_ffixed_line_length_EQ, options::OPT_fopenacc,
41+
options::OPT_finput_charset_EQ, options::OPT_fimplicit_none,
42+
options::OPT_fno_implicit_none, options::OPT_fbackslash,
43+
options::OPT_fno_backslash, options::OPT_flogical_abbreviations,
44+
options::OPT_fno_logical_abbreviations,
45+
options::OPT_fxor_operator, options::OPT_fno_xor_operator,
46+
options::OPT_falternative_parameter_statement,
47+
options::OPT_fdefault_real_8, options::OPT_fdefault_integer_8,
48+
options::OPT_fdefault_double_8, options::OPT_flarge_sizes,
49+
options::OPT_fno_automatic});
5950
}
6051

6152
void Flang::addPreprocessingOptions(const ArgList &Args,
@@ -763,6 +754,35 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
763754
// Add other compile options
764755
addOtherOptions(Args, CmdArgs);
765756

757+
// Forward flags for OpenMP. We don't do this if the current action is an
758+
// device offloading action other than OpenMP.
759+
if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
760+
options::OPT_fno_openmp, false) &&
761+
(JA.isDeviceOffloading(Action::OFK_None) ||
762+
JA.isDeviceOffloading(Action::OFK_OpenMP))) {
763+
switch (D.getOpenMPRuntime(Args)) {
764+
case Driver::OMPRT_OMP:
765+
case Driver::OMPRT_IOMP5:
766+
// Clang can generate useful OpenMP code for these two runtime libraries.
767+
CmdArgs.push_back("-fopenmp");
768+
Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ);
769+
770+
// FIXME: Clang supports a whole bunch more flags here.
771+
break;
772+
default:
773+
// By default, if Clang doesn't know how to generate useful OpenMP code
774+
// for a specific runtime library, we just don't pass the '-fopenmp' flag
775+
// down to the actual compilation.
776+
// FIXME: It would be better to have a mode which *only* omits IR
777+
// generation based on the OpenMP support so that we get consistent
778+
// semantic analysis, etc.
779+
const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ);
780+
D.Diag(diag::warn_drv_unsupported_openmp_library)
781+
<< A->getSpelling() << A->getValue();
782+
break;
783+
}
784+
}
785+
766786
// Offloading related options
767787
addOffloadOptions(C, Inputs, JA, Args, CmdArgs);
768788

flang/test/Driver/fopenmp.f90

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
! RUN: %flang -target x86_64-linux-gnu -fopenmp=libomp -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FC1-OPENMP
2+
! RUN: %flang -target x86_64-linux-gnu -fopenmp=libgomp -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FC1-NO-OPENMP
3+
! RUN: %flang -target x86_64-linux-gnu -fopenmp=libiomp5 -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FC1-OPENMP
4+
! RUN: %flang -target x86_64-apple-darwin -fopenmp=libomp -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FC1-OPENMP
5+
! RUN: %flang -target x86_64-apple-darwin -fopenmp=libgomp -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FC1-NO-OPENMP
6+
! RUN: %flang -target x86_64-apple-darwin -fopenmp=libiomp5 -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FC1-OPENMP
7+
! RUN: %flang -target x86_64-freebsd -fopenmp=libomp -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FC1-OPENMP
8+
! RUN: %flang -target x86_64-freebsd -fopenmp=libgomp -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FC1-NO-OPENMP
9+
! RUN: %flang -target x86_64-freebsd -fopenmp=libiomp5 -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FC1-OPENMP
10+
! RUN: %flang -target x86_64-windows-gnu -fopenmp=libomp -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FC1-OPENMP
11+
! RUN: %flang -target x86_64-windows-gnu -fopenmp=libgomp -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FC1-NO-OPENMP --check-prefix=CHECK-WARNING
12+
! RUN: %flang -target x86_64-windows-gnu -fopenmp=libiomp5 -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-FC1-OPENMP
13+
14+
! CHECK-FC1-OPENMP: "-fc1"
15+
! CHECK-FC1-OPENMP: "-fopenmp"
16+
!
17+
! CHECK-WARNING: warning: The library '-fopenmp=={{.*}}' is not supported, openmp is not be enabled
18+
! CHECK-FC1-NO-OPENMP: "-fc1"
19+
! CHECK-FC1-NO-OPENMP-NOT: "-fopenmp"
20+
!
21+
! RUN: %flang -target x86_64-linux-gnu -fopenmp=libomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-OMP
22+
! RUN: %flang -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
23+
! RUN: %flang -target x86_64-linux-gnu -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-IOMP5
24+
!
25+
! RUN: %flang -target x86_64-darwin -fopenmp=libomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-OMP
26+
! RUN: %flang -target x86_64-darwin -fopenmp=libgomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-GOMP --check-prefix=CHECK-LD-GOMP-NO-RT
27+
! RUN: %flang -target x86_64-darwin -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-IOMP5
28+
!
29+
! RUN: %flang -target x86_64-freebsd -fopenmp=libomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-OMP
30+
! RUN: %flang -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
31+
! RUN: %flang -target x86_64-freebsd -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-IOMP5
32+
!
33+
! RUN: %flang -target x86_64-windows-gnu -fopenmp=libomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-OMP
34+
! RUN: %flang -target x86_64-windows-gnu -fopenmp=libgomp %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-GOMP --check-prefix=CHECK-LD-GOMP-NO-RT
35+
! RUN: %flang -target x86_64-windows-gnu -fopenmp=libiomp5 %s -o %t -### 2>&1 | FileCheck %s --check-prefix=CHECK-LD-IOMP5MD
36+
!
37+
! CHECK-LD-OMP: "{{.*}}ld{{(.exe)?}}"
38+
! CHECK-LD-OMP: "-lomp"
39+
!
40+
! CHECK-LD-GOMP: "{{.*}}ld{{(.exe)?}}"
41+
! CHECK-LD-GOMP: "-lgomp"
42+
! CHECK-LD-GOMP-RT: "-lrt"
43+
! CHECK-LD-GOMP-NO-RT-NOT: "-lrt"
44+
!
45+
! CHECK-LD-IOMP5: "{{.*}}ld{{(.exe)?}}"
46+
! CHECK-LD-IOMP5: "-liomp5"
47+
!
48+
! CHECK-LD-IOMP5MD: "{{.*}}ld{{(.exe)?}}"
49+
! CHECK-LD-IOMP5MD: "-liomp5md"
50+
!
51+
! We'd like to check that the default is sane, but until we have the ability
52+
! to *always* semantically analyze OpenMP without always generating runtime
53+
! calls (in the event of an unsupported runtime), we don't have a good way to
54+
! test the CC1 invocation. Instead, just ensure we do eventually link *some*
55+
! OpenMP runtime.
56+
!
57+
! CHECK-LD-ANY: "{{.*}}ld{{(.exe)?}}"
58+
! CHECK-LD-ANY: "-l{{(omp|gomp|iomp5)}}"
59+
!
60+
! CHECK-LD-ANYMD: "{{.*}}ld{{(.exe)?}}"
61+
! CHECK-LD-ANYMD: "-l{{(omp|gomp|iomp5md)}}"

flang/test/Driver/omp-driver-offload.f90

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@
5757
! RUN: --target=aarch64-unknown-linux-gnu \
5858
! RUN: | FileCheck %s --check-prefix=OPENMP-OFFLOAD-ARGS
5959
! OPENMP-OFFLOAD-ARGS: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
60-
! OPENMP-OFFLOAD-ARGS-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-host-ir-file-path" "{{.*}}.bc" "-fopenmp-is-target-device" {{.*}}.f90"
60+
! OPENMP-OFFLOAD-ARGS-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa"
61+
! OPENMP-OFFLOAD-ARGS-SAME: "-fopenmp"
62+
! OPENMP-OFFLOAD-ARGS-SAME: "-fopenmp-host-ir-file-path" "{{.*}}.bc" "-fopenmp-is-target-device"
63+
! OPENMP-OFFLOAD-ARGS-SAME: {{.*}}.f90"
6164
! OPENMP-OFFLOAD-ARGS: "{{[^"]*}}clang-offload-packager{{.*}}" {{.*}} "--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
62-
! OPENMP-OFFLOAD-ARGS-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"
65+
! OPENMP-OFFLOAD-ARGS-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu"
66+
! OPENMP-OFFLOAD-ARGS-SAME: "-fopenmp"
67+
! OPENMP-OFFLOAD-ARGS-SAME: "-fembed-offload-object={{.*}}.out" {{.*}}.bc"
6368

6469
! Test -fopenmp with offload for RTL Flag Options
6570
! RUN: %flang -### %s -o %t 2>&1 \

0 commit comments

Comments
 (0)