[flang-commits] [clang] [flang] [Flang][Driver] Introduce -fopenmp-targets offloading option (PR #100152)

Sergio Afonso via flang-commits flang-commits at lists.llvm.org
Wed Jul 24 07:52:02 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/3] [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/3] 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/3] 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 \



More information about the flang-commits mailing list