Skip to content

Commit

Permalink
[Flang][Driver] Introduce -fopenmp-targets offloading option (llvm#10…
Browse files Browse the repository at this point in the history
…0152)

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.
  • Loading branch information
skatrak authored Aug 1, 2024
1 parent 229a165 commit e145123
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3532,7 +3532,7 @@ def fopenmp_use_tls : Flag<["-"], "fopenmp-use-tls">, Group<f_Group>,
def fnoopenmp_use_tls : Flag<["-"], "fnoopenmp-use-tls">, Group<f_Group>,
Flags<[NoArgumentUnused, HelpHidden]>, Visibility<[ClangOption, CC1Option]>;
def fopenmp_targets_EQ : CommaJoined<["-"], "fopenmp-targets=">,
Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option, FlangOption]>,
Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
HelpText<"Specify comma-separated list of triples OpenMP offloading targets to be supported">;
def fopenmp_relocatable_target : Flag<["-"], "fopenmp-relocatable-target">,
Group<f_Group>, Flags<[NoArgumentUnused, HelpHidden]>,
Expand Down
12 changes: 1 addition & 11 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7789,17 +7789,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
options::OPT_mno_amdgpu_ieee);
}

// For all the host OpenMP offloading compile jobs we need to pass the targets
// information using -fopenmp-targets= option.
if (JA.isHostOffloading(Action::OFK_OpenMP)) {
SmallString<128> Targets("-fopenmp-targets=");

SmallVector<std::string, 4> Triples;
auto TCRange = C.getOffloadToolChains<Action::OFK_OpenMP>();
std::transform(TCRange.first, TCRange.second, std::back_inserter(Triples),
[](auto TC) { return TC.second->getTripleString(); });
CmdArgs.push_back(Args.MakeArgString(Targets + llvm::join(Triples, ",")));
}
addOpenMPHostOffloadingArgs(C, JA, Args, CmdArgs);

bool VirtualFunctionElimination =
Args.hasFlag(options::OPT_fvirtual_function_elimination,
Expand Down
19 changes: 19 additions & 0 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,25 @@ bool tools::addOpenMPRuntime(const Compilation &C, ArgStringList &CmdArgs,
return true;
}

void tools::addOpenMPHostOffloadingArgs(const Compilation &C,
const JobAction &JA,
const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) {
if (!JA.isHostOffloading(Action::OFK_OpenMP))
return;

// For all the host OpenMP offloading compile jobs we need to pass the targets
// information using -fopenmp-targets= option.
constexpr llvm::StringLiteral Targets("-fopenmp-targets=");

SmallVector<std::string> Triples;
auto TCRange = C.getOffloadToolChains<Action::OFK_OpenMP>();
std::transform(TCRange.first, TCRange.second, std::back_inserter(Triples),
[](auto TC) { return TC.second->getTripleString(); });
CmdArgs.push_back(
Args.MakeArgString(Twine(Targets) + llvm::join(Triples, ",")));
}

/// Add Fortran runtime libs
void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) {
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Driver/ToolChains/CommonArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ bool addOpenMPRuntime(const Compilation &C, llvm::opt::ArgStringList &CmdArgs,
bool ForceStaticHostRuntime = false,
bool IsOffloadingHost = false, bool GompNeedsRT = false);

/// Adds offloading options for OpenMP host compilation to \p CmdArgs.
void addOpenMPHostOffloadingArgs(const Compilation &C, const JobAction &JA,
const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs);

/// Adds Fortran runtime libraries to \p CmdArgs.
void addFortranRuntimeLibs(const ToolChain &TC, const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs);
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
if (Args.hasArg(options::OPT_nogpulib))
CmdArgs.push_back("-nogpulib");
}

addOpenMPHostOffloadingArgs(C, JA, Args, CmdArgs);
}

static void addFloatingPointOptions(const Driver &D, const ArgList &Args,
Expand Down
18 changes: 18 additions & 0 deletions flang/test/Driver/omp-driver-offload.f90
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,21 @@
! RUN: | FileCheck --check-prefix=MLINK-BUILTIN-BITCODE %s
! MLINK-BUILTIN-BITCODE: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa"
! MLINK-BUILTIN-BITCODE-SAME: "-mlink-builtin-bitcode" {{.*Inputs.*rocm.*amdgcn.*bitcode.*}}oclc_isa_version_900.bc

! Test that the -fopenmp-targets option is added to host compilation invocations
! when --offload-arch or -fopenmp-targets are set.
! RUN: %flang -S -### %s -o %t 2>&1 \
! RUN: -fopenmp --offload-arch=gfx90a \
! RUN: --target=x86_64-unknown-linux-gnu -nogpulib \
! RUN: | FileCheck %s --check-prefix=OFFLOAD-TARGETS
! RUN: %flang -S -### %s -o %t 2>&1 \
! RUN: -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa --offload-arch=gfx90a \
! RUN: --target=x86_64-unknown-linux-gnu -nogpulib \
! RUN: | FileCheck %s --check-prefix=OFFLOAD-TARGETS

! OFFLOAD-TARGETS: "{{[^"]*}}flang-new" "-fc1" "-triple" "x86_64-unknown-linux-gnu"
! OFFLOAD-TARGETS-SAME: "-fopenmp-targets=amdgcn-amd-amdhsa"
! OFFLOAD-TARGETS-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa"
! OFFLOAD-TARGETS-NOT: -fopenmp-targets
! OFFLOAD-TARGETS: "{{[^"]*}}flang-new" "-fc1" "-triple" "x86_64-unknown-linux-gnu"
! OFFLOAD-TARGETS-SAME: "-fopenmp-targets=amdgcn-amd-amdhsa"

0 comments on commit e145123

Please sign in to comment.