[flang-commits] [clang] [flang] [Flang][Driver][Offload] Support -Xoffload-linker argument in Flang (PR #109907)

via flang-commits flang-commits at lists.llvm.org
Thu Sep 26 10:41:55 PDT 2024


https://github.com/agozillon updated https://github.com/llvm/llvm-project/pull/109907

>From 22abf5c21692619f71bc87758f6b6433c245ed40 Mon Sep 17 00:00:00 2001
From: agozillon <Andrew.Gozillon at amd.com>
Date: Wed, 25 Sep 2024 00:18:26 -0500
Subject: [PATCH 1/8] [Flang][Driver][Offload] Support -Xoffload-linker
 argument in Flang

The -Xoffload-linker command allows forwarding of linker commands to the
clang-linker-wrapper used for linking offload libraries into the resulting offload
binaries amongst other tasks. This is a rather useful command to have to support
the offloading programming models flang-new currently aims to support
(OpenMP/OpenACC).

Currently this flag is utilised in the check-offload tests after a recent addition and is
used in conjunction with the Fortran OpenMP test suite there, which fails at the
moment due to flang-new not recognizing the command, this fixes the issue. The
alternative to this would of course be to setup the test config to avoid using this
flag with Fortran, but I believe adding support of the flag to flang-new has more
merit as having the same compatability/communication capabilities as Clang to
the clang-linker-wrapper is important as it's a critical component of the offload
pipeline, and the command will likely see more use in the near future.
---
 clang/include/clang/Driver/Options.td | 1 +
 flang/test/Driver/xoffload-linker.f90 | 7 +++++++
 2 files changed, 8 insertions(+)
 create mode 100644 flang/test/Driver/xoffload-linker.f90

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 3f4d1a328b4c27..e5fc3840cf2ade 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1055,6 +1055,7 @@ def Xlinker : Separate<["-"], "Xlinker">, Flags<[LinkerInput, RenderAsInput]>,
   HelpText<"Pass <arg> to the linker">, MetaVarName<"<arg>">,
   Group<Link_Group>;
 def Xoffload_linker : JoinedAndSeparate<["-"], "Xoffload-linker">,
+  Visibility<[ClangOption, CLOption, FlangOption, DXCOption]>,
   HelpText<"Pass <arg> to the offload linkers or the ones identified by -<triple>">,
   MetaVarName<"<triple> <arg>">, Group<Link_Group>;
 def Xpreprocessor : Separate<["-"], "Xpreprocessor">, Group<Preprocessor_Group>,
diff --git a/flang/test/Driver/xoffload-linker.f90 b/flang/test/Driver/xoffload-linker.f90
new file mode 100644
index 00000000000000..886e649c1e8760
--- /dev/null
+++ b/flang/test/Driver/xoffload-linker.f90
@@ -0,0 +1,7 @@
+! Test the -Xoffload-linker flag that forwards link commands to the clang-linker-wrapper used
+! to help link offloading device libraries
+
+! RUN:   %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a \
+! RUN:      -Xoffload-linker a %s 2>&1 | FileCheck %s --check-prefix=CHECK-XLINKER
+
+! CHECK-XLINKER: -device-linker=a{{.*}}--

>From 3a36160efe807231c047b3438e2ba3f9f286ba7a Mon Sep 17 00:00:00 2001
From: agozillon <Andrew.Gozillon at amd.com>
Date: Wed, 25 Sep 2024 11:51:05 -0500
Subject: [PATCH 2/8] [Flang][Driver][Offload] Add new test case and attempt to
 appease the CI gods

---
 flang/test/Driver/xoffload-linker.f90 | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/flang/test/Driver/xoffload-linker.f90 b/flang/test/Driver/xoffload-linker.f90
index 886e649c1e8760..aa7d3e78c66ced 100644
--- a/flang/test/Driver/xoffload-linker.f90
+++ b/flang/test/Driver/xoffload-linker.f90
@@ -4,4 +4,10 @@
 ! RUN:   %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a \
 ! RUN:      -Xoffload-linker a %s 2>&1 | FileCheck %s --check-prefix=CHECK-XLINKER
 
-! CHECK-XLINKER: -device-linker=a{{.*}}--
+! CHECK-XLINKER: {{.*}}-device-linker=a{{.*}}
+
+! RUN:   %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a \
+! RUN:      -Xoffload-linker a -Xoffload-linker-amdgcn-amd-amdhsa b \
+! RUN:      %s 2>&1 | FileCheck %s --check-prefix=CHECK-XLINKER-AMDGCN
+
+! CHECK-XLINKER-AMDGCN: {{.*}}-device-linker=a{{.*}}-device-linker=amdgcn-amd-amdhsa=b{{.*}}

>From 9b9ea26989d1733e2ed112b33842945fc16bfa0a Mon Sep 17 00:00:00 2001
From: agozillon <Andrew.Gozillon at amd.com>
Date: Wed, 25 Sep 2024 23:59:19 -0500
Subject: [PATCH 3/8] Attempt at fixing CI error #2

---
 flang/test/Driver/xoffload-linker.f90 | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/flang/test/Driver/xoffload-linker.f90 b/flang/test/Driver/xoffload-linker.f90
index aa7d3e78c66ced..563914ae5e904d 100644
--- a/flang/test/Driver/xoffload-linker.f90
+++ b/flang/test/Driver/xoffload-linker.f90
@@ -1,13 +1,12 @@
 ! Test the -Xoffload-linker flag that forwards link commands to the clang-linker-wrapper used
 ! to help link offloading device libraries
 
-! RUN:   %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a \
-! RUN:      -Xoffload-linker a %s 2>&1 | FileCheck %s --check-prefix=CHECK-XLINKER
+! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a %s 2>&1 | FileCheck %s --check-prefix=CHECK-XLINKER
 
-! CHECK-XLINKER: {{.*}}-device-linker=a{{.*}}
+! CHECK-XLINKER: {{.*}}--device-linker=a{{.*}}
 
-! RUN:   %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a \
-! RUN:      -Xoffload-linker a -Xoffload-linker-amdgcn-amd-amdhsa b \
-! RUN:      %s 2>&1 | FileCheck %s --check-prefix=CHECK-XLINKER-AMDGCN
+! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a -Xoffload-linker-amdgcn-amd-amdhsa b %s 2>&1 | FileCheck %s --check-prefix=CHECK-XLINKER-AMDGCN
 
-! CHECK-XLINKER-AMDGCN: {{.*}}-device-linker=a{{.*}}-device-linker=amdgcn-amd-amdhsa=b{{.*}}
+! CHECK-XLINKER-AMDGCN: {{.*}}"--device-linker=a"{{.*}}"--device-linker=amdgcn-amd-amdhsa=b"{{.*}}
+
+end program

>From e0c4dce20a3de6f51a3b5f609d4997dd40c3ec9e Mon Sep 17 00:00:00 2001
From: agozillon <Andrew.Gozillon at amd.com>
Date: Thu, 26 Sep 2024 00:20:59 -0500
Subject: [PATCH 4/8] remove first test, try with only one

---
 flang/test/Driver/xoffload-linker.f90 | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/flang/test/Driver/xoffload-linker.f90 b/flang/test/Driver/xoffload-linker.f90
index 563914ae5e904d..2c9ad07eec071f 100644
--- a/flang/test/Driver/xoffload-linker.f90
+++ b/flang/test/Driver/xoffload-linker.f90
@@ -1,11 +1,8 @@
-! Test the -Xoffload-linker flag that forwards link commands to the clang-linker-wrapper used
-! to help link offloading device libraries
+! RUN %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a %s 2>&1 | FileCheck %s --check-prefixes=CHECK-XLINKER
 
-! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a %s 2>&1 | FileCheck %s --check-prefix=CHECK-XLINKER
+! CHECK-XLINKER {{.*}}--device-linker=a{{.*}}
 
-! CHECK-XLINKER: {{.*}}--device-linker=a{{.*}}
-
-! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a -Xoffload-linker-amdgcn-amd-amdhsa b %s 2>&1 | FileCheck %s --check-prefix=CHECK-XLINKER-AMDGCN
+! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a -Xoffload-linker-amdgcn-amd-amdhsa b %s 2>&1 | FileCheck %s --check-prefixes=CHECK-XLINKER-AMDGCN
 
 ! CHECK-XLINKER-AMDGCN: {{.*}}"--device-linker=a"{{.*}}"--device-linker=amdgcn-amd-amdhsa=b"{{.*}}
 

>From c18e6d2a4a20f65a5c912f07ac056cd3e2c12873 Mon Sep 17 00:00:00 2001
From: agozillon <Andrew.Gozillon at amd.com>
Date: Thu, 26 Sep 2024 08:55:30 -0500
Subject: [PATCH 5/8] next alteration to try to make CI happy

---
 flang/test/Driver/xoffload-linker.f90 | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/flang/test/Driver/xoffload-linker.f90 b/flang/test/Driver/xoffload-linker.f90
index 2c9ad07eec071f..397d7fd7807fc3 100644
--- a/flang/test/Driver/xoffload-linker.f90
+++ b/flang/test/Driver/xoffload-linker.f90
@@ -1,9 +1,10 @@
-! RUN %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a %s 2>&1 | FileCheck %s --check-prefixes=CHECK-XLINKER
+! Test the -Xoffload-linker flag that forwards link commands to the clang-linker-wrapper used
+! to help link offloading device libraries
 
-! CHECK-XLINKER {{.*}}--device-linker=a{{.*}}
+! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a %s 2>&1 | FileCheck %s --check-prefixes=CHECK-XLINKER
 
-! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a -Xoffload-linker-amdgcn-amd-amdhsa b %s 2>&1 | FileCheck %s --check-prefixes=CHECK-XLINKER-AMDGCN
+! CHECK-XLINKER: -device-linker=a{{.*}}-
 
-! CHECK-XLINKER-AMDGCN: {{.*}}"--device-linker=a"{{.*}}"--device-linker=amdgcn-amd-amdhsa=b"{{.*}}
+! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a -Xoffload-linker-amdgcn-amd-amdhsa b %s 2>&1 | FileCheck %s --check-prefixes=CHECK-XLINKER-AMDGCN
 
-end program
+! CHECK-XLINKER-AMDGCN: -device-linker=a{{.*}}-device-linker=amdgcn-amd-amdhsa=b{{.*}}--

>From 84e13e67e2b96945bb4383c5c4a03de83b7c3d0e Mon Sep 17 00:00:00 2001
From: agozillon <Andrew.Gozillon at amd.com>
Date: Thu, 26 Sep 2024 09:49:00 -0500
Subject: [PATCH 6/8] Borrow a similar test to see if it passes...

---
 flang/test/Driver/xoffload-linker.f90 | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/flang/test/Driver/xoffload-linker.f90 b/flang/test/Driver/xoffload-linker.f90
index 397d7fd7807fc3..feafadcfcec452 100644
--- a/flang/test/Driver/xoffload-linker.f90
+++ b/flang/test/Driver/xoffload-linker.f90
@@ -1,10 +1,21 @@
-! Test the -Xoffload-linker flag that forwards link commands to the clang-linker-wrapper used
-! to help link offloading device libraries
+! RUN: %flang -### --target=ppc64le-linux-gnu -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
+! RUN: %flang -### --target=aarch64-apple-darwin -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
+! RUN: %flang -### --target=sparc-sun-solaris2.11 -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
+! RUN: %flang -### --target=x86_64-unknown-freebsd -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
+! RUN: %flang -### --target=x86_64-unknown-netbsd -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
+! RUN: %flang -### --target=x86_64-unknown-openbsd -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
+! RUN: %flang -### --target=x86_64-unknown-dragonfly -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
+! RUN: %flang -### --target=x86_64-unknown-haiku %s -Xlinker -rpath -Xlinker /not/a/real/path 2>&1 | FileCheck %s --check-prefixes=UNIX
+! RUN: %flang -### --target=x86_64-windows-gnu -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
+! RUN: %flang -### --target=aarch64-windows-msvc -Xlinker -rpath -Xlinker /not/a/real/path -o obscure.exe %s 2>&1 | FileCheck %s --check-prefixes=MSVC
 
-! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a %s 2>&1 | FileCheck %s --check-prefixes=CHECK-XLINKER
+! UNIX-LABEL: "{{.*}}ld{{(\.exe)?}}"
+! UNIX-SAME: "-rpath" "/not/a/real/path"
 
-! CHECK-XLINKER: -device-linker=a{{.*}}-
+! The name of this file contains the word "link" which results in a match on
+! the compiler line as well. Instead look for the final name of the executable
+! to be created since that will only appear in the linker line.
+! MSVC: -out:obscure.exe
+! MSVC-SAME: "-rpath" "/not/a/real/path"
 
-! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a -Xoffload-linker-amdgcn-amd-amdhsa b %s 2>&1 | FileCheck %s --check-prefixes=CHECK-XLINKER-AMDGCN
-
-! CHECK-XLINKER-AMDGCN: -device-linker=a{{.*}}-device-linker=amdgcn-amd-amdhsa=b{{.*}}--
+end program

>From bd7f5b2df0515c4d90a333befbc46be19747ba60 Mon Sep 17 00:00:00 2001
From: agozillon <Andrew.Gozillon at amd.com>
Date: Thu, 26 Sep 2024 10:28:59 -0500
Subject: [PATCH 7/8] Fix test back to normal

---
 flang/test/Driver/xoffload-linker.f90 | 25 +++++++------------------
 1 file changed, 7 insertions(+), 18 deletions(-)

diff --git a/flang/test/Driver/xoffload-linker.f90 b/flang/test/Driver/xoffload-linker.f90
index feafadcfcec452..50270d4f84a1fa 100644
--- a/flang/test/Driver/xoffload-linker.f90
+++ b/flang/test/Driver/xoffload-linker.f90
@@ -1,21 +1,10 @@
-! RUN: %flang -### --target=ppc64le-linux-gnu -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
-! RUN: %flang -### --target=aarch64-apple-darwin -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
-! RUN: %flang -### --target=sparc-sun-solaris2.11 -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
-! RUN: %flang -### --target=x86_64-unknown-freebsd -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
-! RUN: %flang -### --target=x86_64-unknown-netbsd -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
-! RUN: %flang -### --target=x86_64-unknown-openbsd -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
-! RUN: %flang -### --target=x86_64-unknown-dragonfly -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
-! RUN: %flang -### --target=x86_64-unknown-haiku %s -Xlinker -rpath -Xlinker /not/a/real/path 2>&1 | FileCheck %s --check-prefixes=UNIX
-! RUN: %flang -### --target=x86_64-windows-gnu -Xlinker -rpath -Xlinker /not/a/real/path %s 2>&1 | FileCheck %s --check-prefixes=UNIX
-! RUN: %flang -### --target=aarch64-windows-msvc -Xlinker -rpath -Xlinker /not/a/real/path -o obscure.exe %s 2>&1 | FileCheck %s --check-prefixes=MSVC
+! Test the -Xoffload-linker flag that forwards link commands to the clang-linker-wrapper used
+! to help link offloading device libraries
 
-! UNIX-LABEL: "{{.*}}ld{{(\.exe)?}}"
-! UNIX-SAME: "-rpath" "/not/a/real/path"
+! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a %s 2>&1 | FileCheck --check-prefixes=CHECK-XLINKER %s
 
-! The name of this file contains the word "link" which results in a match on
-! the compiler line as well. Instead look for the final name of the executable
-! to be created since that will only appear in the linker line.
-! MSVC: -out:obscure.exe
-! MSVC-SAME: "-rpath" "/not/a/real/path"
+! CHECK-XLINKER: -device-linker=a{{.*}}-
 
-end program
+! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a -Xoffload-linker-amdgcn-amd-amdhsa b %s 2>&1 | FileCheck --check-prefixes=CHECK-XLINKER-AMDGCN %s
+
+! CHECK-XLINKER-AMDGCN: -device-linker=a{{.*}}-device-linker=amdgcn-amd-amdhsa=b{{.*}}--

>From 383224a0652b262f44d6b3972339fedc0824c9a7 Mon Sep 17 00:00:00 2001
From: agozillon <Andrew.Gozillon at amd.com>
Date: Thu, 26 Sep 2024 12:40:32 -0500
Subject: [PATCH 8/8] Try alternate way to write the test

---
 flang/test/Driver/xoffload-linker.f90 | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/flang/test/Driver/xoffload-linker.f90 b/flang/test/Driver/xoffload-linker.f90
index 50270d4f84a1fa..c8988de1fdec8e 100644
--- a/flang/test/Driver/xoffload-linker.f90
+++ b/flang/test/Driver/xoffload-linker.f90
@@ -1,10 +1,10 @@
 ! Test the -Xoffload-linker flag that forwards link commands to the clang-linker-wrapper used
 ! to help link offloading device libraries
 
-! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a %s 2>&1 | FileCheck --check-prefixes=CHECK-XLINKER %s
+! RUN: %flang -### %s -o %t 2>&1 -fopenmp --offload-arch=gfx90a --target=aarch64-unknown-linux-gnu -nogpulib -Xoffload-linker a | FileCheck %s --check-prefix=CHECK-XLINKER
 
-! CHECK-XLINKER: -device-linker=a{{.*}}-
+! CHECK-XLINKER: "{{[^"]*}}clang-linker-wrapper{{.*}}"{{.*}}"--device-linker=a"{{.*}}
 
-! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a -Xoffload-linker a -Xoffload-linker-amdgcn-amd-amdhsa b %s 2>&1 | FileCheck --check-prefixes=CHECK-XLINKER-AMDGCN %s
+! RUN: %flang -### %s -o %t 2>&1 -fopenmp --offload-arch=gfx90a --target=aarch64-unknown-linux-gnu -nogpulib -Xoffload-linker a -Xoffload-linker-amdgcn-amd-amdhsa b | FileCheck %s --check-prefix=CHECK-XLINKER-AMDGCN
 
-! CHECK-XLINKER-AMDGCN: -device-linker=a{{.*}}-device-linker=amdgcn-amd-amdhsa=b{{.*}}--
+! CHECK-XLINKER-AMDGCN: "{{[^"]*}}clang-linker-wrapper{{.*}}"{{.*}}"--device-linker=a"{{.*}}"--device-linker=amdgcn-amd-amdhsa=b"{{.*}}



More information about the flang-commits mailing list