Skip to content

Commit 9966008

Browse files
authored
[Clang] Append target search paths for direct offloading compilation (#82699)
Summary: Recent changes to the `libc` project caused the headers to be installed to `include/<triple>` for the GPU and the libraries to be in `lib/<triple>`. This means we should automatically append these search paths so they can be found by default. This allows the following to work targeting AMDGPU. ```shell $ clang foo.c -flto -mcpu=native --target=amdgcn-amd-amdhsa -lc <install>/lib/amdgcn-amd-amdhsa/crt1.o $ amdhsa-loader a.out ```
1 parent b43dd08 commit 9966008

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
625625

626626
addLinkerCompressDebugSectionsOption(getToolChain(), Args, CmdArgs);
627627
Args.AddAllArgs(CmdArgs, options::OPT_L);
628+
getToolChain().AddFilePathLibArgs(Args, CmdArgs);
628629
AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
629630
if (C.getDriver().isUsingLTO())
630631
addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0],

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,8 +1111,8 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
11111111
C.getActiveOffloadKinds() == Action::OFK_None) {
11121112
SmallString<128> P(llvm::sys::path::parent_path(D.InstalledDir));
11131113
llvm::sys::path::append(P, "include");
1114-
llvm::sys::path::append(P, "gpu-none-llvm");
1115-
CmdArgs.push_back("-c-isystem");
1114+
llvm::sys::path::append(P, getToolChain().getTripleString());
1115+
CmdArgs.push_back("-internal-isystem");
11161116
CmdArgs.push_back(Args.MakeArgString(P));
11171117
} else if (C.getActiveOffloadKinds() == Action::OFK_OpenMP) {
11181118
// TODO: CUDA / HIP include their own headers for some common functions

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,10 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
609609
// Add paths specified in LIBRARY_PATH environment variable as -L options.
610610
addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH");
611611

612+
// Add standard library search paths passed on the command line.
613+
Args.AddAllArgs(CmdArgs, options::OPT_L);
614+
getToolChain().AddFilePathLibArgs(Args, CmdArgs);
615+
612616
// Add paths for the default clang library path.
613617
SmallString<256> DefaultLibPath =
614618
llvm::sys::path::parent_path(TC.getDriver().Dir);

clang/test/Driver/gpu-libc-headers.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@
1010
// CHECK-HEADERS: "-cc1"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}llvm_libc_wrappers"{{.*}}"-isysroot" "./"
1111
// CHECK-HEADERS: "-cc1"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}llvm_libc_wrappers"{{.*}}"-isysroot" "./"
1212

13+
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a --sysroot=./ \
14+
// RUN: -nogpulib %s 2>&1 | FileCheck %s --check-prefix=CHECK-HEADERS-AMDGPU
15+
// RUN: %clang -### --target=nvptx64-nvidia-cuda -march=sm_89 --sysroot=./ \
16+
// RUN: -nogpulib %s 2>&1 | FileCheck %s --check-prefix=CHECK-HEADERS-NVPTX
17+
// CHECK-HEADERS-AMDGPU: "-cc1"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}amdgcn-amd-amdhsa"{{.*}}"-isysroot" "./"
18+
// CHECK-HEADERS-NVPTX: "-cc1"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}nvptx64-nvidia-cuda"{{.*}}"-isysroot" "./"
19+
1320
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx1030 -nogpulib \
1421
// RUN: -nogpuinc %s 2>&1 | FileCheck %s --check-prefix=CHECK-HEADERS-DISABLED
1522
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx1030 -nogpulib \
1623
// RUN: -nostdinc %s 2>&1 | FileCheck %s --check-prefix=CHECK-HEADERS-DISABLED
1724
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx1030 -nogpulib \
1825
// RUN: -nobuiltininc %s 2>&1 | FileCheck %s --check-prefix=CHECK-HEADERS-DISABLED
19-
// CHECK-HEADERS-DISABLED-NOT: "-cc1"{{.*}}"-c-isystem" "{{.*}}include{{.*}}gpu-none-llvm"
26+
// CHECK-HEADERS-DISABLED-NOT: "-cc1"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}gpu-none-llvm"

0 commit comments

Comments
 (0)