Skip to content

Commit 44ff273

Browse files
jhuber6yuxuanchen1997
authored andcommitted
[Clang] Fix C library wrappers for offloading (#99716)
Summary: This block of code wraps around the standard C library includes. However, the order C library includes are presented is actually important. If they are visible before the `libc++` headers then it will cause errors. This patch simply moves the logic to just before it is normally done. A more optimal solution would be to put this in the toolchain, however doing it correctly would require knowing the offloading kind and that would require rewriting the function signature in all 30 or so ToolChains.
1 parent 5671809 commit 44ff273

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,33 +1077,6 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
10771077
if (JA.isOffloading(Action::OFK_HIP))
10781078
getToolChain().AddHIPIncludeArgs(Args, CmdArgs);
10791079

1080-
// If we are compiling for a GPU target we want to override the system headers
1081-
// with ones created by the 'libc' project if present.
1082-
if (!Args.hasArg(options::OPT_nostdinc) &&
1083-
!Args.hasArg(options::OPT_nogpuinc) &&
1084-
!Args.hasArg(options::OPT_nobuiltininc)) {
1085-
// Without an offloading language we will include these headers directly.
1086-
// Offloading languages will instead only use the declarations stored in
1087-
// the resource directory at clang/lib/Headers/llvm_libc_wrappers.
1088-
if ((getToolChain().getTriple().isNVPTX() ||
1089-
getToolChain().getTriple().isAMDGCN()) &&
1090-
C.getActiveOffloadKinds() == Action::OFK_None) {
1091-
SmallString<128> P(llvm::sys::path::parent_path(D.Dir));
1092-
llvm::sys::path::append(P, "include");
1093-
llvm::sys::path::append(P, getToolChain().getTripleString());
1094-
CmdArgs.push_back("-internal-isystem");
1095-
CmdArgs.push_back(Args.MakeArgString(P));
1096-
} else if (C.getActiveOffloadKinds() == Action::OFK_OpenMP) {
1097-
// TODO: CUDA / HIP include their own headers for some common functions
1098-
// implemented here. We'll need to clean those up so they do not conflict.
1099-
SmallString<128> P(D.ResourceDir);
1100-
llvm::sys::path::append(P, "include");
1101-
llvm::sys::path::append(P, "llvm_libc_wrappers");
1102-
CmdArgs.push_back("-internal-isystem");
1103-
CmdArgs.push_back(Args.MakeArgString(P));
1104-
}
1105-
}
1106-
11071080
// If we are offloading to a target via OpenMP we need to include the
11081081
// openmp_wrappers folder which contains alternative system headers.
11091082
if (JA.isDeviceOffloading(Action::OFK_OpenMP) &&
@@ -1276,6 +1249,35 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
12761249
});
12771250
}
12781251

1252+
// If we are compiling for a GPU target we want to override the system headers
1253+
// with ones created by the 'libc' project if present.
1254+
// TODO: This should be moved to `AddClangSystemIncludeArgs` by passing the
1255+
// OffloadKind as an argument.
1256+
if (!Args.hasArg(options::OPT_nostdinc) &&
1257+
!Args.hasArg(options::OPT_nogpuinc) &&
1258+
!Args.hasArg(options::OPT_nobuiltininc)) {
1259+
// Without an offloading language we will include these headers directly.
1260+
// Offloading languages will instead only use the declarations stored in
1261+
// the resource directory at clang/lib/Headers/llvm_libc_wrappers.
1262+
if ((getToolChain().getTriple().isNVPTX() ||
1263+
getToolChain().getTriple().isAMDGCN()) &&
1264+
C.getActiveOffloadKinds() == Action::OFK_None) {
1265+
SmallString<128> P(llvm::sys::path::parent_path(D.Dir));
1266+
llvm::sys::path::append(P, "include");
1267+
llvm::sys::path::append(P, getToolChain().getTripleString());
1268+
CmdArgs.push_back("-internal-isystem");
1269+
CmdArgs.push_back(Args.MakeArgString(P));
1270+
} else if (C.getActiveOffloadKinds() == Action::OFK_OpenMP) {
1271+
// TODO: CUDA / HIP include their own headers for some common functions
1272+
// implemented here. We'll need to clean those up so they do not conflict.
1273+
SmallString<128> P(D.ResourceDir);
1274+
llvm::sys::path::append(P, "include");
1275+
llvm::sys::path::append(P, "llvm_libc_wrappers");
1276+
CmdArgs.push_back("-internal-isystem");
1277+
CmdArgs.push_back(Args.MakeArgString(P));
1278+
}
1279+
}
1280+
12791281
// Add system include arguments for all targets but IAMCU.
12801282
if (!IsIAMCU)
12811283
forAllAssociatedToolChains(C, JA, getToolChain(),

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --sysroot=./ \
55
// RUN: -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target=nvptx64-nvidia-cuda --offload-arch=sm_70 \
66
// RUN: -nogpulib %s 2>&1 | FileCheck %s --check-prefix=CHECK-HEADERS
7-
// CHECK-HEADERS: "-cc1"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}llvm_libc_wrappers"{{.*}}"-isysroot" "./"
8-
// CHECK-HEADERS: "-cc1"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}llvm_libc_wrappers"{{.*}}"-isysroot" "./"
7+
// CHECK-HEADERS: "-cc1"{{.*}}"-isysroot" "./"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}llvm_libc_wrappers"
8+
// CHECK-HEADERS: "-cc1"{{.*}}"-isysroot" "./"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}llvm_libc_wrappers"
99

1010
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a --sysroot=./ \
1111
// RUN: -nogpulib %s 2>&1 | FileCheck %s --check-prefix=CHECK-HEADERS-AMDGPU
1212
// RUN: %clang -### --target=nvptx64-nvidia-cuda -march=sm_89 --sysroot=./ \
1313
// RUN: -nogpulib %s 2>&1 | FileCheck %s --check-prefix=CHECK-HEADERS-NVPTX
14-
// CHECK-HEADERS-AMDGPU: "-cc1"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}amdgcn-amd-amdhsa"{{.*}}"-isysroot" "./"
15-
// CHECK-HEADERS-NVPTX: "-cc1"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}nvptx64-nvidia-cuda"{{.*}}"-isysroot" "./"
14+
// CHECK-HEADERS-AMDGPU: "-cc1"{{.*}}"-isysroot" "./"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}amdgcn-amd-amdhsa"
15+
// CHECK-HEADERS-NVPTX: "-cc1"{{.*}}"-isysroot" "./"{{.*}}"-internal-isystem" "{{.*}}include{{.*}}nvptx64-nvidia-cuda"
1616

1717
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx1030 -nogpulib \
1818
// RUN: -nogpuinc %s 2>&1 | FileCheck %s --check-prefix=CHECK-HEADERS-DISABLED

0 commit comments

Comments
 (0)