[flang-commits] [clang] [flang] [Flang][Driver] Introduce -fopenmp-targets offloading option (PR #100152)
Sergio Afonso via flang-commits
flang-commits at lists.llvm.org
Tue Jul 30 08:38:13 PDT 2024
https://github.com/skatrak updated https://github.com/llvm/llvm-project/pull/100152
>From cf26a318d3b49eb6217f29405cee9fd2c20f8e8a Mon Sep 17 00:00:00 2001
From: Sergio Afonso <safonsof at amd.com>
Date: Tue, 23 Jul 2024 16:19:55 +0100
Subject: [PATCH 1/5] [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.
---
clang/include/clang/Driver/Options.td | 2 +-
clang/lib/Driver/ToolChains/Flang.cpp | 13 +++++++++++++
flang/test/Driver/omp-driver-offload.f90 | 17 +++++++++++++++++
3 files changed, 31 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 69269cf7537b0..4ef7c81fbd9e4 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3530,7 +3530,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]>,
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index c4f2375c64034..b0a3528dce05d 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -12,6 +12,7 @@
#include "clang/Basic/CodeGenOptions.h"
#include "clang/Driver/Options.h"
+#include "llvm/ADT/StringExtras.h"
#include "llvm/Frontend/Debug/Options.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
@@ -492,6 +493,18 @@ void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
if (Args.hasArg(options::OPT_nogpulib))
CmdArgs.push_back("-nogpulib");
}
+
+ // 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, ",")));
+ }
}
static void addFloatingPointOptions(const Driver &D, const ArgList &Args,
diff --git a/flang/test/Driver/omp-driver-offload.f90 b/flang/test/Driver/omp-driver-offload.f90
index 6fb4f4eeeeca1..70fb8c7298e77 100644
--- a/flang/test/Driver/omp-driver-offload.f90
+++ b/flang/test/Driver/omp-driver-offload.f90
@@ -227,3 +227,20 @@
! FORCE-USM-OFFLOAD-SAME: "-fopenmp" "-fopenmp-force-usm"
! FORCE-USM-OFFLOAD-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa"
! FORCE-USM-OFFLOAD-SAME: "-fopenmp" "-fopenmp-force-usm"
+
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN: | FileCheck %s --check-prefix=OFFLOAD-TARGETS
+
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN: | FileCheck %s --check-prefix=OFFLOAD-TARGETS
+
+! OFFLOAD-TARGETS: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-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" "aarch64-unknown-linux-gnu"
+! OFFLOAD-TARGETS-SAME: "-fopenmp-targets=amdgcn-amd-amdhsa"
>From a5d5c988044c3182fb51662f486e1b79820e29f8 Mon Sep 17 00:00:00 2001
From: Sergio Afonso <safonsof at amd.com>
Date: Wed, 24 Jul 2024 11:29:19 +0100
Subject: [PATCH 2/5] Attempt to fix buildbot issue
---
.../test/Driver/omp-driver-offload-amdgpu.f90 | 18 ++++++++++++++++++
flang/test/Driver/omp-driver-offload.f90 | 11 +++--------
2 files changed, 21 insertions(+), 8 deletions(-)
create mode 100644 flang/test/Driver/omp-driver-offload-amdgpu.f90
diff --git a/flang/test/Driver/omp-driver-offload-amdgpu.f90 b/flang/test/Driver/omp-driver-offload-amdgpu.f90
new file mode 100644
index 0000000000000..cf806d672f396
--- /dev/null
+++ b/flang/test/Driver/omp-driver-offload-amdgpu.f90
@@ -0,0 +1,18 @@
+! REQUIRES: amdgpu-registered-target
+
+! Test that AMDGPU-specific flang-new OpenMP offload related commands expand to
+! the appropriate commands for flang-new -fc1 as expected. Contrary to tests
+! located in omp-driver-offload.f90, driver tests here do require the amdgcn-
+! amd-amdhsa triple to be recognized.
+
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa \
+! RUN: --target=x86_64-unknown-linux-gnu \
+! 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"
diff --git a/flang/test/Driver/omp-driver-offload.f90 b/flang/test/Driver/omp-driver-offload.f90
index 70fb8c7298e77..34a3f4d880f5d 100644
--- a/flang/test/Driver/omp-driver-offload.f90
+++ b/flang/test/Driver/omp-driver-offload.f90
@@ -228,19 +228,14 @@
! FORCE-USM-OFFLOAD-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa"
! FORCE-USM-OFFLOAD-SAME: "-fopenmp" "-fopenmp-force-usm"
-! RUN: %flang -S -### %s -o %t 2>&1 \
-! RUN: -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa \
-! RUN: --target=aarch64-unknown-linux-gnu \
-! RUN: | FileCheck %s --check-prefix=OFFLOAD-TARGETS
-
! RUN: %flang -S -### %s -o %t 2>&1 \
! RUN: -fopenmp --offload-arch=gfx90a \
-! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN: --target=x86_64-unknown-linux-gnu \
! RUN: | FileCheck %s --check-prefix=OFFLOAD-TARGETS
-! OFFLOAD-TARGETS: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu"
+! 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" "aarch64-unknown-linux-gnu"
+! OFFLOAD-TARGETS: "{{[^"]*}}flang-new" "-fc1" "-triple" "x86_64-unknown-linux-gnu"
! OFFLOAD-TARGETS-SAME: "-fopenmp-targets=amdgcn-amd-amdhsa"
>From f2ec205140a214accd161919c40a966d5695c536 Mon Sep 17 00:00:00 2001
From: Sergio Afonso <safonsof at amd.com>
Date: Wed, 24 Jul 2024 15:51:48 +0100
Subject: [PATCH 3/5] Add missing test comment
---
flang/test/Driver/omp-driver-offload.f90 | 2 ++
1 file changed, 2 insertions(+)
diff --git a/flang/test/Driver/omp-driver-offload.f90 b/flang/test/Driver/omp-driver-offload.f90
index 34a3f4d880f5d..a4f578e08393e 100644
--- a/flang/test/Driver/omp-driver-offload.f90
+++ b/flang/test/Driver/omp-driver-offload.f90
@@ -228,6 +228,8 @@
! FORCE-USM-OFFLOAD-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa"
! FORCE-USM-OFFLOAD-SAME: "-fopenmp" "-fopenmp-force-usm"
+! Test that the -fopenmp-targets option is added to host compilation invocations
+! when --offload-arch is set.
! RUN: %flang -S -### %s -o %t 2>&1 \
! RUN: -fopenmp --offload-arch=gfx90a \
! RUN: --target=x86_64-unknown-linux-gnu \
>From c9ec56ec614ebc5e1d265e1e77e4beac8a0d91f5 Mon Sep 17 00:00:00 2001
From: Sergio Afonso <safonsof at amd.com>
Date: Thu, 25 Jul 2024 12:50:27 +0100
Subject: [PATCH 4/5] Fix buildbot issue
---
.../test/Driver/omp-driver-offload-amdgpu.f90 | 18 ------------------
flang/test/Driver/omp-driver-offload.f90 | 6 +++++-
2 files changed, 5 insertions(+), 19 deletions(-)
delete mode 100644 flang/test/Driver/omp-driver-offload-amdgpu.f90
diff --git a/flang/test/Driver/omp-driver-offload-amdgpu.f90 b/flang/test/Driver/omp-driver-offload-amdgpu.f90
deleted file mode 100644
index cf806d672f396..0000000000000
--- a/flang/test/Driver/omp-driver-offload-amdgpu.f90
+++ /dev/null
@@ -1,18 +0,0 @@
-! REQUIRES: amdgpu-registered-target
-
-! Test that AMDGPU-specific flang-new OpenMP offload related commands expand to
-! the appropriate commands for flang-new -fc1 as expected. Contrary to tests
-! located in omp-driver-offload.f90, driver tests here do require the amdgcn-
-! amd-amdhsa triple to be recognized.
-
-! RUN: %flang -S -### %s -o %t 2>&1 \
-! RUN: -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa \
-! RUN: --target=x86_64-unknown-linux-gnu \
-! 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"
diff --git a/flang/test/Driver/omp-driver-offload.f90 b/flang/test/Driver/omp-driver-offload.f90
index a4f578e08393e..787c3bec0f9c2 100644
--- a/flang/test/Driver/omp-driver-offload.f90
+++ b/flang/test/Driver/omp-driver-offload.f90
@@ -229,11 +229,15 @@
! FORCE-USM-OFFLOAD-SAME: "-fopenmp" "-fopenmp-force-usm"
! Test that the -fopenmp-targets option is added to host compilation invocations
-! when --offload-arch is set.
+! 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 \
! 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 \
+! 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"
>From 04cac5bc4acb63ca0efb2803aab61653c44a5ad2 Mon Sep 17 00:00:00 2001
From: Sergio Afonso <safonsof at amd.com>
Date: Tue, 30 Jul 2024 16:35:54 +0100
Subject: [PATCH 5/5] Refactor duplicated code and address review comments
---
clang/lib/Driver/ToolChains/Clang.cpp | 12 +-----------
clang/lib/Driver/ToolChains/CommonArgs.cpp | 19 +++++++++++++++++++
clang/lib/Driver/ToolChains/CommonArgs.h | 5 +++++
clang/lib/Driver/ToolChains/Flang.cpp | 12 +-----------
4 files changed, 26 insertions(+), 22 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 78936fd634f33..b5745d20ea316 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -7745,17 +7745,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,
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 019df16a909f4..10f76a118ac4f 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1262,6 +1262,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) {
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h b/clang/lib/Driver/ToolChains/CommonArgs.h
index 52818ecde924b..8695d3fe5b55b 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.h
+++ b/clang/lib/Driver/ToolChains/CommonArgs.h
@@ -116,6 +116,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);
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index b0a3528dce05d..414e9294e4c82 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -494,17 +494,7 @@ void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
CmdArgs.push_back("-nogpulib");
}
- // 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);
}
static void addFloatingPointOptions(const Driver &D, const ArgList &Args,
More information about the flang-commits
mailing list