Skip to content

Commit 44936c8

Browse files
authored
[CUDA][HIP] add options --[no-]offload-inc (#140106)
Currently there is only option -nogpuinc for disabling the default CUDA/HIP wrapper headers. However, there are situations where -nogpuinc needs to be overriden for enabling CUDA/HIP wrapper headers. This patch adds --[no-]offload-inc for that purpose. When both exist, the last wins. -nogpuinc and -nocudainc are now alias to --no-offload-inc.
1 parent b0366ee commit 44936c8

File tree

6 files changed

+32
-14
lines changed

6 files changed

+32
-14
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,8 @@ multiclass BoolWOption<string flag_base, KeyPathAndMacro kpm,
577577
// Works like BoolOption except without marshalling
578578
multiclass BoolOptionWithoutMarshalling<string prefix = "", string spelling_base,
579579
FlagDef flag1_base, FlagDef flag2_base,
580-
BothFlags suffix = BothFlags<[]>> {
580+
BothFlags suffix = BothFlags<[]>,
581+
list<string> flag_prefix = ["-"]> {
581582
defvar flag1 = FlagDefExpanded<ApplySuffix<flag1_base, suffix>.Result, prefix,
582583
NAME, spelling_base>;
583584

@@ -598,12 +599,12 @@ multiclass BoolOptionWithoutMarshalling<string prefix = "", string spelling_base
598599

599600
defvar implied = !if(flag1.CanBeImplied, flag1, flag2);
600601

601-
def flag1.RecordName : Flag<["-"], flag1.Spelling>, Flags<flag1.OptionFlags>,
602+
def flag1.RecordName : Flag<flag_prefix, flag1.Spelling>, Flags<flag1.OptionFlags>,
602603
Visibility<flag1.OptionVisibility>,
603604
HelpText<flag1.Help>,
604605
ImpliedByAnyOf<implied.ImpliedBy, implied.ValueAsCode>
605606
{}
606-
def flag2.RecordName : Flag<["-"], flag2.Spelling>, Flags<flag2.OptionFlags>,
607+
def flag2.RecordName : Flag<flag_prefix, flag2.Spelling>, Flags<flag2.OptionFlags>,
607608
Visibility<flag2.OptionVisibility>,
608609
HelpText<flag2.Help>,
609610
ImpliedByAnyOf<implied.ImpliedBy, implied.ValueAsCode>
@@ -5756,12 +5757,17 @@ def nobuiltininc : Flag<["-"], "nobuiltininc">,
57565757
Group<IncludePath_Group>,
57575758
HelpText<"Disable builtin #include directories only">,
57585759
MarshallingInfoNegativeFlag<HeaderSearchOpts<"UseBuiltinIncludes">>;
5759-
def nogpuinc : Flag<["-"], "nogpuinc">, Group<IncludePath_Group>,
5760-
HelpText<"Do not add include paths for CUDA/HIP and"
5761-
" do not include the default CUDA/HIP wrapper headers">;
5760+
defm offload_inc: BoolOptionWithoutMarshalling<"", "offload-inc",
5761+
PosFlag<SetTrue, [], [ClangOption], "Add include paths for CUDA/HIP and"
5762+
" include the default CUDA/HIP wrapper headers (default)">,
5763+
NegFlag<SetFalse, [], [ClangOption], "Do not add include paths for CUDA/HIP"
5764+
" and include the default CUDA/HIP wrapper headers">,
5765+
BothFlags<[]>, ["--"]>, Group<IncludePath_Group>;
5766+
def : Flag<["-"], "nogpuinc">, Alias<no_offload_inc>;
5767+
57625768
def nohipwrapperinc : Flag<["-"], "nohipwrapperinc">, Group<IncludePath_Group>,
57635769
HelpText<"Do not include the default HIP wrapper headers and include paths">;
5764-
def : Flag<["-"], "nocudainc">, Alias<nogpuinc>;
5770+
def : Flag<["-"], "nocudainc">, Alias<no_offload_inc>;
57655771
def no_offloadlib
57665772
: Flag<["--"], "no-offloadlib">,
57675773
MarshallingInfoFlag<LangOpts<"NoGPULib">>,

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ void RocmInstallationDetector::AddHIPIncludeArgs(const ArgList &DriverArgs,
525525
"hipstdpar_lib.hpp"});
526526
};
527527

528-
if (DriverArgs.hasArg(options::OPT_nogpuinc)) {
528+
if (!DriverArgs.hasFlag(options::OPT_offload_inc, options::OPT_no_offload_inc,
529+
true)) {
529530
if (HasHipStdPar)
530531
HandleHipStdPar();
531532

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,8 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
937937
// openmp_wrappers folder which contains alternative system headers.
938938
if (JA.isDeviceOffloading(Action::OFK_OpenMP) &&
939939
!Args.hasArg(options::OPT_nostdinc) &&
940-
!Args.hasArg(options::OPT_nogpuinc) &&
940+
Args.hasFlag(options::OPT_offload_inc, options::OPT_no_offload_inc,
941+
true) &&
941942
getToolChain().getTriple().isGPU()) {
942943
if (!Args.hasArg(options::OPT_nobuiltininc)) {
943944
// Add openmp_wrappers/* to our system include path. This lets us wrap
@@ -1120,7 +1121,8 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
11201121
// TODO: This should be moved to `AddClangSystemIncludeArgs` by passing the
11211122
// OffloadKind as an argument.
11221123
if (!Args.hasArg(options::OPT_nostdinc) &&
1123-
!Args.hasArg(options::OPT_nogpuinc) &&
1124+
Args.hasFlag(options::OPT_offload_inc, options::OPT_no_offload_inc,
1125+
true) &&
11241126
!Args.hasArg(options::OPT_nobuiltininc)) {
11251127
// Without an offloading language we will include these headers directly.
11261128
// Offloading languages will instead only use the declarations stored in

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ void CudaInstallationDetector::AddCudaIncludeArgs(
302302
CC1Args.push_back(DriverArgs.MakeArgString(P));
303303
}
304304

305-
if (DriverArgs.hasArg(options::OPT_nogpuinc))
305+
if (!DriverArgs.hasFlag(options::OPT_offload_inc, options::OPT_no_offload_inc,
306+
true))
306307
return;
307308

308309
if (!isValid()) {
@@ -928,7 +929,8 @@ llvm::DenormalMode CudaToolChain::getDefaultDenormalModeForType(
928929
void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
929930
ArgStringList &CC1Args) const {
930931
// Check our CUDA version if we're going to include the CUDA headers.
931-
if (!DriverArgs.hasArg(options::OPT_nogpuinc) &&
932+
if (DriverArgs.hasFlag(options::OPT_offload_inc, options::OPT_no_offload_inc,
933+
true) &&
932934
!DriverArgs.hasArg(options::OPT_no_cuda_version_check)) {
933935
StringRef Arch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
934936
assert(!Arch.empty() && "Must have an explicit GPU arch.");
@@ -1001,7 +1003,9 @@ void CudaToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
10011003
ArgStringList &CC1Args) const {
10021004
HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
10031005

1004-
if (!DriverArgs.hasArg(options::OPT_nogpuinc) && CudaInstallation.isValid())
1006+
if (DriverArgs.hasFlag(options::OPT_offload_inc, options::OPT_no_offload_inc,
1007+
true) &&
1008+
CudaInstallation.isValid())
10051009
CC1Args.append(
10061010
{"-internal-isystem",
10071011
DriverArgs.MakeArgString(CudaInstallation.getIncludePath())});

clang/lib/Driver/ToolChains/HIPSPV.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ void HIPSPVToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
187187

188188
void HIPSPVToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
189189
ArgStringList &CC1Args) const {
190-
if (DriverArgs.hasArg(options::OPT_nogpuinc))
190+
if (!DriverArgs.hasFlag(options::OPT_offload_inc, options::OPT_no_offload_inc,
191+
true))
191192
return;
192193

193194
StringRef hipPath = DriverArgs.getLastArgValue(options::OPT_hip_path_EQ);

clang/test/Driver/hip-include-path.hip

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
// RUN: -std=c++11 --rocm-path=%S/Inputs/rocm -nogpuinc -nogpulib %s 2>&1 \
1313
// RUN: | FileCheck -check-prefixes=COMMON,CLANG,NOHIP %s
1414

15+
// RUN: %clang -c -### --target=x86_64-unknown-linux-gnu --cuda-gpu-arch=gfx900 \
16+
// RUN: -std=c++11 --rocm-path=%S/Inputs/rocm --no-offload-inc -nogpulib --offload-inc %s 2>&1 \
17+
// RUN: | FileCheck -check-prefixes=COMMON,CLANG,HIP %s
18+
1519
// COMMON-LABEL: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
1620
// CLANG-SAME: "-internal-isystem" "{{[^"]*}}/lib{{[^"]*}}/clang/{{[^"]*}}/include/cuda_wrappers"
1721
// NOCLANG-NOT: "{{[^"]*}}/lib{{[^"]*}}/clang/{{[^"]*}}/include/cuda_wrappers"

0 commit comments

Comments
 (0)