[clang] [flang] [Flang][Driver] Enable gpulibc/nogpulibc options for Flang, which allows linking of GPU LIBC for the fortran and OpenMP runtime (PR #77135)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 8 07:24:16 PST 2024


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

>From 71fee19666806c331555fdb9453d792063f8ffd8 Mon Sep 17 00:00:00 2001
From: Andrew Gozillon <Andrew.Gozillon at amd.com>
Date: Fri, 5 Jan 2024 14:16:16 -0600
Subject: [PATCH 1/2] [Flang][Driver] Enable gpulibc/nogpulibc options for
 Flang, which allows linking of GPU LIBC for the fortran and OpenMP runtime

This patch seeks to add the -gpulibc and -nogpulibc for Flang, which allows
the linking of the GPU libc library, this allows the use of memcpy and other useful library functions for GPU.

In particular, this allows the Fortran runtime (written in C++) to be compiled for offload and then linked against
the GPU LIBC library via this option to resolve memcpy and other C library functions that the fortran runtime
depends on for AMD GPU devices (and likely other GPU devices).

This is the current method I've tested and found to be able to utilise the Fortran runtime when compiled
for AMD GPU, albeit it requires compiling libc for GPU and then the Fortran runtime for GPU, so not
particularly straight forward or user friendly yet.

Activating this option will allow the subset of C functions to also be utilised for GPU in
other C/C++ based Fortran libraries if any are made.
---
 clang/include/clang/Driver/Options.td    | 4 ++--
 flang/test/Driver/driver-help-hidden.f90 | 1 +
 flang/test/Driver/driver-help.f90        | 2 ++
 3 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 6aff37f1336871..12f41a1ea03a8c 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5198,9 +5198,9 @@ def nogpulib : Flag<["-"], "nogpulib">, MarshallingInfoFlag<LangOpts<"NoGPULib">
   Visibility<[ClangOption, CC1Option]>,
   HelpText<"Do not link device library for CUDA/HIP device compilation">;
 def : Flag<["-"], "nocudalib">, Alias<nogpulib>;
-def gpulibc : Flag<["-"], "gpulibc">, Visibility<[ClangOption, CC1Option]>,
+def gpulibc : Flag<["-"], "gpulibc">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
   HelpText<"Link the LLVM C Library for GPUs">;
-def nogpulibc : Flag<["-"], "nogpulibc">, Visibility<[ClangOption, CC1Option]>;
+def nogpulibc : Flag<["-"], "nogpulibc">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>;
 def nodefaultlibs : Flag<["-"], "nodefaultlibs">;
 def nodriverkitlib : Flag<["-"], "nodriverkitlib">;
 def nofixprebinding : Flag<["-"], "nofixprebinding">;
diff --git a/flang/test/Driver/driver-help-hidden.f90 b/flang/test/Driver/driver-help-hidden.f90
index 9a11a7a571ffcc..70bb9f8eb512ce 100644
--- a/flang/test/Driver/driver-help-hidden.f90
+++ b/flang/test/Driver/driver-help-hidden.f90
@@ -108,6 +108,7 @@
 ! CHECK-NEXT: -fxor-operator          Enable .XOR. as a synonym of .NEQV.
 ! CHECK-NEXT: -gline-directives-only  Emit debug line info directives only
 ! CHECK-NEXT: -gline-tables-only      Emit debug line number tables only
+! CHECK-NEXT: -gpulibc                Link the LLVM C Library for GPUs
 ! CHECK-NEXT: -g                      Generate source-level debug information
 ! CHECK-NEXT: --help-hidden           Display help for hidden options
 ! CHECK-NEXT: -help                   Display available options
diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90
index e0e74dc56f331e..0d760616aace04 100644
--- a/flang/test/Driver/driver-help.f90
+++ b/flang/test/Driver/driver-help.f90
@@ -94,6 +94,7 @@
 ! HELP-NEXT: -fxor-operator          Enable .XOR. as a synonym of .NEQV.
 ! HELP-NEXT: -gline-directives-only  Emit debug line info directives only
 ! HELP-NEXT: -gline-tables-only      Emit debug line number tables only
+! HELP-NEXT: -gpulibc                Link the LLVM C Library for GPUs
 ! HELP-NEXT: -g                      Generate source-level debug information
 ! HELP-NEXT: --help-hidden           Display help for hidden options
 ! HELP-NEXT: -help                   Display available options
@@ -228,6 +229,7 @@
 ! HELP-FC1-NEXT: -fversion-loops-for-stride
 ! HELP-FC1-NEXT:                         Create unit-strided versions of loops
 ! HELP-FC1-NEXT: -fxor-operator          Enable .XOR. as a synonym of .NEQV.
+! HELP-FC1-NEXT: -gpulibc                Link the LLVM C Library for GPUs
 ! HELP-FC1-NEXT: -help                   Display available options
 ! HELP-FC1-NEXT: -init-only              Only execute frontend initialization
 ! HELP-FC1-NEXT: -I <dir>                Add directory to the end of the list of include search paths

>From 4461b8bd36c119600ee1129c24f846561237043a Mon Sep 17 00:00:00 2001
From: Andrew Gozillon <Andrew.Gozillon at amd.com>
Date: Mon, 8 Jan 2024 09:18:50 -0600
Subject: [PATCH 2/2] [Flang][Driver] Extend omp-driver-offload.f90 tests to
 test gpulibc/nogpulibc are working as we expect

---
 flang/test/Driver/omp-driver-offload.f90 | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/flang/test/Driver/omp-driver-offload.f90 b/flang/test/Driver/omp-driver-offload.f90
index ad50723b0e3a79..b45ed70195fb46 100644
--- a/flang/test/Driver/omp-driver-offload.f90
+++ b/flang/test/Driver/omp-driver-offload.f90
@@ -169,3 +169,16 @@
 ! RUN: -fopenmp-host-ir-file-path non-existant-file.bc \
 ! RUN: | FileCheck %s --check-prefix=HOST-IR-MISSING
 ! HOST-IR-MISSING: error: provided host compiler IR file 'non-existant-file.bc' is required to generate code for OpenMP target regions but cannot be found
+
+! Check that `-gpulibc` includes the LLVM C libraries for the GPU.
+! RUN:   %flang -### --target=x86_64-unknown-linux-gnu -fopenmp  \
+! RUN:      --offload-arch=gfx90a --offload-arch=sm_52 \
+! RUN:      -gpulibc %s 2>&1 \
+! RUN:   | FileCheck --check-prefix=LIBC-GPU %s
+! LIBC-GPU: "-lcgpu"{{.*}}"-lmgpu"
+
+! RUN:   %flang -### --target=x86_64-unknown-linux-gnu -fopenmp  \
+! RUN:      --offload-arch=gfx90a --offload-arch=sm_52 \
+! RUN:      -nogpulibc %s 2>&1 \
+! RUN:   | FileCheck --check-prefix=NO-LIBC-GPU %s
+! NO-LIBC-GPU-NOT: "-lcgpu"{{.*}}"-lmgpu"



More information about the cfe-commits mailing list