[clang] [flang] [Flang][Driver] Align -fopenmp-target-fast with Clang (PR #247122)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 30 02:47:30 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-driver
@llvm/pr-subscribers-clang-driver
Author: Michael Klemm (mjklemm)
<details>
<summary>Changes</summary>
Make -fopenmp-target-fast in Flang behave like it does in Clang:
* It now also implies -fopenmp-assume-teams-oversubscription and -fopenmp-assume-threads-oversubscription. These flags are forwarded to both the host and the device compilation, so that both agree on SPMD-no-loop promotion of target regions.
* It is enabled by default with -Ofast.
* Like in Clang, it only takes effect when OpenMP is enabled with the libomp or libiomp5 runtime, and only for host or OpenMP device jobs.
As before, -fno-openmp-target-fast disables it and each implied flag can be disabled individually with its -fno- variant.
---
Full diff: https://github.com/llvm/llvm-project/pull/247122.diff
2 Files Affected:
- (modified) clang/lib/Driver/ToolChains/Flang.cpp (+21-9)
- (modified) flang/test/Driver/openmp-target-fast-flag.f90 (+46-11)
``````````diff
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 6a464323c6c225..ce52f41ebebfde 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -902,17 +902,36 @@ void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
}
}
+ // -fopenmp-target-fast (enabled by default with -Ofast) implies
+ // -fopenmp-assume-teams-oversubscription,
+ // -fopenmp-assume-threads-oversubscription, -fopenmp-assume-no-thread-state
+ // and -fopenmp-assume-no-nested-parallelism, and forces -O3 on the device
+ // unless an explicit optimization level was requested. Like Clang, only do
+ // this when generating OpenMP code for the host or an OpenMP device with a
+ // runtime library that Flang can generate OpenMP code for.
+ const Driver &D = getToolChain().getDriver();
+ Driver::OpenMPRuntimeKind OMPRuntime = D.getOpenMPRuntime(Args);
+ bool TargetFastUsed =
+ Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
+ options::OPT_fno_openmp, false) &&
+ (OMPRuntime == Driver::OMPRT_OMP || OMPRuntime == Driver::OMPRT_IOMP5) &&
+ (JA.isDeviceOffloading(Action::OFK_None) ||
+ JA.isDeviceOffloading(Action::OFK_OpenMP)) &&
+ Args.hasFlag(options::OPT_fopenmp_target_fast,
+ options::OPT_fno_openmp_target_fast,
+ isOptimizationLevelFast(Args));
+
// When in OpenMP offloading mode, forward assumptions information about
// thread and team counts in the target device. The host needs to know about
// this to prevent the SPMD to SPMD-no-loop promotion being done differently
// for host and device on the same target region.
if (Args.hasFlag(options::OPT_fopenmp_assume_teams_oversubscription,
options::OPT_fno_openmp_assume_teams_oversubscription,
- /*Default=*/false))
+ /*Default=*/TargetFastUsed))
CmdArgs.push_back("-fopenmp-assume-teams-oversubscription");
if (Args.hasFlag(options::OPT_fopenmp_assume_threads_oversubscription,
options::OPT_fno_openmp_assume_threads_oversubscription,
- /*Default=*/false))
+ /*Default=*/TargetFastUsed))
CmdArgs.push_back("-fopenmp-assume-threads-oversubscription");
// This forwards the option to switch off/on mapper generation for scenarios
@@ -930,13 +949,6 @@ void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
// generating code for a device, so that only the relevant code is emitted.
CmdArgs.push_back("-fopenmp-is-target-device");
- // -fopenmp-target-fast implies -fopenmp-assume-no-thread-state and
- // -fopenmp-assume-no-nested-parallelism, and forces -O3 unless an
- // explicit optimization level was requested.
- bool TargetFastUsed =
- Args.hasFlag(options::OPT_fopenmp_target_fast,
- options::OPT_fno_openmp_target_fast, false);
-
if (TargetFastUsed && !Args.hasArg(options::OPT_O_Group))
CmdArgs.push_back("-O3");
diff --git a/flang/test/Driver/openmp-target-fast-flag.f90 b/flang/test/Driver/openmp-target-fast-flag.f90
index 33083376b04da4..43aee5ac750fd9 100644
--- a/flang/test/Driver/openmp-target-fast-flag.f90
+++ b/flang/test/Driver/openmp-target-fast-flag.f90
@@ -1,26 +1,61 @@
! REQUIRES: amdgpu-registered-target
-! -fopenmp-target-fast is a meta-flag: on the OpenMP offload device compilation
-! it implies -fopenmp-assume-no-thread-state and
+! -fopenmp-target-fast is a meta-flag. It implies
+! -fopenmp-assume-teams-oversubscription and
+! -fopenmp-assume-threads-oversubscription on both the host and the OpenMP
+! offload device compilation, so that both agree on SPMD-no-loop promotion. On
+! the device compilation it also implies -fopenmp-assume-no-thread-state and
! -fopenmp-assume-no-nested-parallelism, and forces -O3 unless an explicit
-! optimization level is requested. It is only enabled when explicitly
-! requested; -Ofast does not enable it. -fno-openmp-target-fast disables it.
+! optimization level is requested. It is enabled by default with -Ofast when
+! OpenMP is enabled with the libomp or libiomp5 runtime.
+! -fno-openmp-target-fast disables it, and each implied flag can be disabled
+! individually.
! RUN: %flang -### -fopenmp --offload-arch=gfx90a -nogpulib -O0 %s 2>&1 \
-! RUN: | FileCheck -check-prefixes=DefaultTState,DefaultNoNestParallel %s
+! RUN: | FileCheck -check-prefixes=DefaultOVS,DefaultTState,DefaultNoNestParallel %s
! RUN: %flang -### -fopenmp --offload-arch=gfx90a -nogpulib -O0 -fopenmp-target-fast %s 2>&1 \
-! RUN: | FileCheck -check-prefixes=TState,NestParallel %s
+! RUN: | FileCheck -check-prefixes=OVS,TState,NestParallel %s
! RUN: %flang -### -fopenmp --offload-arch=gfx90a -nogpulib -fopenmp-target-fast %s 2>&1 \
-! RUN: | FileCheck -check-prefixes=O3,TState,NestParallel %s
+! RUN: | FileCheck -check-prefixes=OVS,O3,TState,NestParallel %s
! RUN: %flang -### -fopenmp --offload-arch=gfx90a -nogpulib -O3 -fno-openmp-target-fast %s 2>&1 \
-! RUN: | FileCheck -check-prefixes=DefaultTState,DefaultNoNestParallel %s
+! RUN: | FileCheck -check-prefixes=DefaultOVS,DefaultTState,DefaultNoNestParallel %s
-! -Ofast must NOT enable target-fast.
+! -Ofast enables target-fast.
! RUN: %flang -### -fopenmp --offload-arch=gfx90a -nogpulib -Ofast %s 2>&1 \
-! RUN: | FileCheck -check-prefixes=DefaultTState,DefaultNoNestParallel %s
+! RUN: | FileCheck -check-prefixes=OVS,TState,NestParallel %s
+
+! RUN: %flang -### -fopenmp --offload-arch=gfx90a -nogpulib -Ofast -fno-openmp-target-fast %s 2>&1 \
+! RUN: | FileCheck -check-prefixes=DefaultOVS,DefaultTState,DefaultNoNestParallel %s
+
+! Without -fopenmp, neither -Ofast nor -fopenmp-target-fast has any effect.
+! RUN: %flang -### -Ofast %s 2>&1 \
+! RUN: | FileCheck -check-prefixes=DefaultOVS,DefaultTState,DefaultNoNestParallel %s
+! RUN: %flang -### -fopenmp-target-fast %s 2>&1 \
+! RUN: | FileCheck -check-prefixes=DefaultOVS,DefaultTState,DefaultNoNestParallel %s
+
+! Neither has any effect with an OpenMP runtime library that Flang does not
+! generate OpenMP code for.
+! RUN: %flang -### -fopenmp=libgomp -Ofast %s 2>&1 \
+! RUN: | FileCheck -check-prefixes=DefaultOVS,DefaultTState,DefaultNoNestParallel %s
+! RUN: %flang -### -fopenmp=libgomp -fopenmp-target-fast %s 2>&1 \
+! RUN: | FileCheck -check-prefixes=DefaultOVS,DefaultTState,DefaultNoNestParallel %s
+
+! Implied flags can be disabled individually.
+! RUN: %flang -### -fopenmp --offload-arch=gfx90a -nogpulib -fopenmp-target-fast \
+! RUN: -fno-openmp-assume-teams-oversubscription %s 2>&1 \
+! RUN: | FileCheck -check-prefixes=NoTeamsOVS %s
+
+! OVS: "-fc1" "-triple" "{{[^"]*}}"{{.*}} "-fopenmp-assume-teams-oversubscription" "-fopenmp-assume-threads-oversubscription"
+! OVS: "-fc1"{{.*}} "-fopenmp-assume-teams-oversubscription" "-fopenmp-assume-threads-oversubscription" "-fopenmp-is-target-device"
+! OVS-NOT: "-fno-openmp-assume-{{(teams|threads)}}-oversubscription"
+! DefaultOVS-NOT: "-f{{(no-)?}}openmp-assume-{{(teams|threads)}}-oversubscription"
+
+! NoTeamsOVS-NOT: "-fopenmp-assume-teams-oversubscription"
+! NoTeamsOVS: "-fopenmp-assume-threads-oversubscription" "-fopenmp-is-target-device"
+! NoTeamsOVS-NOT: "-fopenmp-assume-teams-oversubscription"
! O3: "-O3"
@@ -32,4 +67,4 @@
! NestParallel-NOT: "-fno-openmp-assume-no-nested-parallelism"
! DefaultNoNestParallel-NOT: "-f{{(no-)?}}openmp-assume-no-nested-parallelism"
-end program
\ No newline at end of file
+end program
``````````
</details>
https://github.com/llvm/llvm-project/pull/247122
More information about the cfe-commits
mailing list