Skip to content

Commit e145123

Browse files
authored
[Flang][Driver] Introduce -fopenmp-targets offloading option (llvm#100152)
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 229a165 commit e145123

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

clang/include/clang/Driver/Options.td

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

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7789,17 +7789,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
77897789
options::OPT_mno_amdgpu_ieee);
77907790
}
77917791

7792-
// For all the host OpenMP offloading compile jobs we need to pass the targets
7793-
// information using -fopenmp-targets= option.
7794-
if (JA.isHostOffloading(Action::OFK_OpenMP)) {
7795-
SmallString<128> Targets("-fopenmp-targets=");
7796-
7797-
SmallVector<std::string, 4> Triples;
7798-
auto TCRange = C.getOffloadToolChains<Action::OFK_OpenMP>();
7799-
std::transform(TCRange.first, TCRange.second, std::back_inserter(Triples),
7800-
[](auto TC) { return TC.second->getTripleString(); });
7801-
CmdArgs.push_back(Args.MakeArgString(Targets + llvm::join(Triples, ",")));
7802-
}
7792+
addOpenMPHostOffloadingArgs(C, JA, Args, CmdArgs);
78037793

78047794
bool VirtualFunctionElimination =
78057795
Args.hasFlag(options::OPT_fvirtual_function_elimination,

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,25 @@ bool tools::addOpenMPRuntime(const Compilation &C, ArgStringList &CmdArgs,
12441244
return true;
12451245
}
12461246

1247+
void tools::addOpenMPHostOffloadingArgs(const Compilation &C,
1248+
const JobAction &JA,
1249+
const llvm::opt::ArgList &Args,
1250+
llvm::opt::ArgStringList &CmdArgs) {
1251+
if (!JA.isHostOffloading(Action::OFK_OpenMP))
1252+
return;
1253+
1254+
// For all the host OpenMP offloading compile jobs we need to pass the targets
1255+
// information using -fopenmp-targets= option.
1256+
constexpr llvm::StringLiteral Targets("-fopenmp-targets=");
1257+
1258+
SmallVector<std::string> Triples;
1259+
auto TCRange = C.getOffloadToolChains<Action::OFK_OpenMP>();
1260+
std::transform(TCRange.first, TCRange.second, std::back_inserter(Triples),
1261+
[](auto TC) { return TC.second->getTripleString(); });
1262+
CmdArgs.push_back(
1263+
Args.MakeArgString(Twine(Targets) + llvm::join(Triples, ",")));
1264+
}
1265+
12471266
/// Add Fortran runtime libs
12481267
void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
12491268
llvm::opt::ArgStringList &CmdArgs) {

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ bool addOpenMPRuntime(const Compilation &C, llvm::opt::ArgStringList &CmdArgs,
119119
bool ForceStaticHostRuntime = false,
120120
bool IsOffloadingHost = false, bool GompNeedsRT = false);
121121

122+
/// Adds offloading options for OpenMP host compilation to \p CmdArgs.
123+
void addOpenMPHostOffloadingArgs(const Compilation &C, const JobAction &JA,
124+
const llvm::opt::ArgList &Args,
125+
llvm::opt::ArgStringList &CmdArgs);
126+
122127
/// Adds Fortran runtime libraries to \p CmdArgs.
123128
void addFortranRuntimeLibs(const ToolChain &TC, const llvm::opt::ArgList &Args,
124129
llvm::opt::ArgStringList &CmdArgs);

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
495495
if (Args.hasArg(options::OPT_nogpulib))
496496
CmdArgs.push_back("-nogpulib");
497497
}
498+
499+
addOpenMPHostOffloadingArgs(C, JA, Args, CmdArgs);
498500
}
499501

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

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,21 @@
207207
! RUN: | FileCheck --check-prefix=MLINK-BUILTIN-BITCODE %s
208208
! MLINK-BUILTIN-BITCODE: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa"
209209
! MLINK-BUILTIN-BITCODE-SAME: "-mlink-builtin-bitcode" {{.*Inputs.*rocm.*amdgcn.*bitcode.*}}oclc_isa_version_900.bc
210+
211+
! Test that the -fopenmp-targets option is added to host compilation invocations
212+
! when --offload-arch or -fopenmp-targets are set.
213+
! RUN: %flang -S -### %s -o %t 2>&1 \
214+
! RUN: -fopenmp --offload-arch=gfx90a \
215+
! RUN: --target=x86_64-unknown-linux-gnu -nogpulib \
216+
! RUN: | FileCheck %s --check-prefix=OFFLOAD-TARGETS
217+
! RUN: %flang -S -### %s -o %t 2>&1 \
218+
! RUN: -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa --offload-arch=gfx90a \
219+
! RUN: --target=x86_64-unknown-linux-gnu -nogpulib \
220+
! RUN: | FileCheck %s --check-prefix=OFFLOAD-TARGETS
221+
222+
! OFFLOAD-TARGETS: "{{[^"]*}}flang-new" "-fc1" "-triple" "x86_64-unknown-linux-gnu"
223+
! OFFLOAD-TARGETS-SAME: "-fopenmp-targets=amdgcn-amd-amdhsa"
224+
! OFFLOAD-TARGETS-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa"
225+
! OFFLOAD-TARGETS-NOT: -fopenmp-targets
226+
! OFFLOAD-TARGETS: "{{[^"]*}}flang-new" "-fc1" "-triple" "x86_64-unknown-linux-gnu"
227+
! OFFLOAD-TARGETS-SAME: "-fopenmp-targets=amdgcn-amd-amdhsa"

0 commit comments

Comments
 (0)