Skip to content

Commit d9710d7

Browse files
authored
[Flang][Driver] Enable gpulibc/nogpulibc options for Flang, which allows linking of GPU LIBC for the fortran and OpenMP runtime (#77135)
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 when linking against GPU libc.
1 parent 98c6aa7 commit d9710d7

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5200,9 +5200,9 @@ def nogpulib : Flag<["-"], "nogpulib">, MarshallingInfoFlag<LangOpts<"NoGPULib">
52005200
Visibility<[ClangOption, CC1Option]>,
52015201
HelpText<"Do not link device library for CUDA/HIP device compilation">;
52025202
def : Flag<["-"], "nocudalib">, Alias<nogpulib>;
5203-
def gpulibc : Flag<["-"], "gpulibc">, Visibility<[ClangOption, CC1Option]>,
5203+
def gpulibc : Flag<["-"], "gpulibc">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
52045204
HelpText<"Link the LLVM C Library for GPUs">;
5205-
def nogpulibc : Flag<["-"], "nogpulibc">, Visibility<[ClangOption, CC1Option]>;
5205+
def nogpulibc : Flag<["-"], "nogpulibc">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>;
52065206
def nodefaultlibs : Flag<["-"], "nodefaultlibs">;
52075207
def nodriverkitlib : Flag<["-"], "nodriverkitlib">;
52085208
def nofixprebinding : Flag<["-"], "nofixprebinding">;

flang/test/Driver/driver-help-hidden.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
! CHECK-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
109109
! CHECK-NEXT: -gline-directives-only Emit debug line info directives only
110110
! CHECK-NEXT: -gline-tables-only Emit debug line number tables only
111+
! CHECK-NEXT: -gpulibc Link the LLVM C Library for GPUs
111112
! CHECK-NEXT: -g Generate source-level debug information
112113
! CHECK-NEXT: --help-hidden Display help for hidden options
113114
! CHECK-NEXT: -help Display available options

flang/test/Driver/driver-help.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
! HELP-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
9595
! HELP-NEXT: -gline-directives-only Emit debug line info directives only
9696
! HELP-NEXT: -gline-tables-only Emit debug line number tables only
97+
! HELP-NEXT: -gpulibc Link the LLVM C Library for GPUs
9798
! HELP-NEXT: -g Generate source-level debug information
9899
! HELP-NEXT: --help-hidden Display help for hidden options
99100
! HELP-NEXT: -help Display available options
@@ -228,6 +229,7 @@
228229
! HELP-FC1-NEXT: -fversion-loops-for-stride
229230
! HELP-FC1-NEXT: Create unit-strided versions of loops
230231
! HELP-FC1-NEXT: -fxor-operator Enable .XOR. as a synonym of .NEQV.
232+
! HELP-FC1-NEXT: -gpulibc Link the LLVM C Library for GPUs
231233
! HELP-FC1-NEXT: -help Display available options
232234
! HELP-FC1-NEXT: -init-only Only execute frontend initialization
233235
! HELP-FC1-NEXT: -I <dir> Add directory to the end of the list of include search paths

flang/test/Driver/omp-driver-offload.f90

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,16 @@
169169
! RUN: -fopenmp-host-ir-file-path non-existant-file.bc \
170170
! RUN: | FileCheck %s --check-prefix=HOST-IR-MISSING
171171
! 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
172+
173+
! Check that `-gpulibc` includes the LLVM C libraries for the GPU.
174+
! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp \
175+
! RUN: --offload-arch=gfx90a --offload-arch=sm_52 \
176+
! RUN: -gpulibc %s 2>&1 \
177+
! RUN: | FileCheck --check-prefix=LIBC-GPU %s
178+
! LIBC-GPU: "-lcgpu"{{.*}}"-lmgpu"
179+
180+
! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fopenmp \
181+
! RUN: --offload-arch=gfx90a --offload-arch=sm_52 \
182+
! RUN: -nogpulibc %s 2>&1 \
183+
! RUN: | FileCheck --check-prefix=NO-LIBC-GPU %s
184+
! NO-LIBC-GPU-NOT: "-lcgpu"{{.*}}"-lmgpu"

0 commit comments

Comments
 (0)