Skip to content

Commit cf26a31

Browse files
committed
[Flang][Driver] Introduce -fopenmp-targets offloading option
This patch modifies the flang driver to introduce the `-fopenmp-targets` option to the frontend compiler invocations corresponding to the OpenMP host device on offloading-enabled compilations. This option holds the list of offloading triples associated to the compilation and is used by clang to determine whether offloading calls should be generated for the host.
1 parent 58f3c5e commit cf26a31

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3530,7 +3530,7 @@ def fopenmp_use_tls : Flag<["-"], "fopenmp-use-tls">, Group<f_Group>,
35303530
def fnoopenmp_use_tls : Flag<["-"], "fnoopenmp-use-tls">, Group<f_Group>,
35313531
Flags<[NoArgumentUnused, HelpHidden]>, Visibility<[ClangOption, CC1Option]>;
35323532
def fopenmp_targets_EQ : CommaJoined<["-"], "fopenmp-targets=">,
3533-
Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option, FlangOption]>,
3533+
Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
35343534
HelpText<"Specify comma-separated list of triples OpenMP offloading targets to be supported">;
35353535
def fopenmp_relocatable_target : Flag<["-"], "fopenmp-relocatable-target">,
35363536
Group<f_Group>, Flags<[NoArgumentUnused, HelpHidden]>,

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "clang/Basic/CodeGenOptions.h"
1414
#include "clang/Driver/Options.h"
15+
#include "llvm/ADT/StringExtras.h"
1516
#include "llvm/Frontend/Debug/Options.h"
1617
#include "llvm/Support/FileSystem.h"
1718
#include "llvm/Support/Path.h"
@@ -492,6 +493,18 @@ void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
492493
if (Args.hasArg(options::OPT_nogpulib))
493494
CmdArgs.push_back("-nogpulib");
494495
}
496+
497+
// For all the host OpenMP offloading compile jobs we need to pass the targets
498+
// information using -fopenmp-targets= option.
499+
if (JA.isHostOffloading(Action::OFK_OpenMP)) {
500+
SmallString<128> Targets("-fopenmp-targets=");
501+
502+
SmallVector<std::string, 4> Triples;
503+
auto TCRange = C.getOffloadToolChains<Action::OFK_OpenMP>();
504+
std::transform(TCRange.first, TCRange.second, std::back_inserter(Triples),
505+
[](auto TC) { return TC.second->getTripleString(); });
506+
CmdArgs.push_back(Args.MakeArgString(Targets + llvm::join(Triples, ",")));
507+
}
495508
}
496509

497510
static void addFloatingPointOptions(const Driver &D, const ArgList &Args,

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,20 @@
227227
! FORCE-USM-OFFLOAD-SAME: "-fopenmp" "-fopenmp-force-usm"
228228
! FORCE-USM-OFFLOAD-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa"
229229
! FORCE-USM-OFFLOAD-SAME: "-fopenmp" "-fopenmp-force-usm"
230+
231+
! RUN: %flang -S -### %s -o %t 2>&1 \
232+
! RUN: -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa \
233+
! RUN: --target=aarch64-unknown-linux-gnu \
234+
! RUN: | FileCheck %s --check-prefix=OFFLOAD-TARGETS
235+
236+
! RUN: %flang -S -### %s -o %t 2>&1 \
237+
! RUN: -fopenmp --offload-arch=gfx90a \
238+
! RUN: --target=aarch64-unknown-linux-gnu \
239+
! RUN: | FileCheck %s --check-prefix=OFFLOAD-TARGETS
240+
241+
! OFFLOAD-TARGETS: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu"
242+
! OFFLOAD-TARGETS-SAME: "-fopenmp-targets=amdgcn-amd-amdhsa"
243+
! OFFLOAD-TARGETS-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa"
244+
! OFFLOAD-TARGETS-NOT: -fopenmp-targets
245+
! OFFLOAD-TARGETS: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu"
246+
! OFFLOAD-TARGETS-SAME: "-fopenmp-targets=amdgcn-amd-amdhsa"

0 commit comments

Comments
 (0)