Skip to content

Commit e880e8a

Browse files
authored
Let clang-cl support CUDA/HIP (#68921)
clang-cl is a driver mode that accepts options of MSVC cl.exe as a drop-in replacement for cl.exe. Currently clang-cl accepts mixed clang style options and cl style options. To let clang-cl accept a clang-style option, just need to add visibility CLOption to that option. Currently nvcc can pass cl style options to cl.exe, which allows nvcc to compile C++ and CUDA programs with mixed nvcc and cl style options. On the other hand, clang cannot use mixed clang and cl style options to compile CUDA/HIP programs. This patch add visibility CLOption to options needed to compile CUDA/HIP programs. This allows clang-cl to compile CUDA/HIP programs with mixed clang and cl style options.
1 parent 3e49ce6 commit e880e8a

File tree

3 files changed

+61
-18
lines changed

3 files changed

+61
-18
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ def pedantic_Group : OptionGroup<"<pedantic group>">, Group<f_Group>,
148148
DocFlatten;
149149

150150
def offload_Group : OptionGroup<"<offload group>">, Group<f_Group>,
151-
DocName<"Common Offloading options">;
151+
DocName<"Common Offloading options">,
152+
Visibility<[ClangOption, CLOption]>;
152153

153154
def opencl_Group : OptionGroup<"<opencl group>">, Group<f_Group>,
154155
DocName<"OpenCL options">;
@@ -157,13 +158,16 @@ def sycl_Group : OptionGroup<"<SYCL group>">, Group<f_Group>,
157158
DocName<"SYCL options">;
158159

159160
def cuda_Group : OptionGroup<"<CUDA group>">, Group<f_Group>,
160-
DocName<"CUDA options">;
161+
DocName<"CUDA options">,
162+
Visibility<[ClangOption, CLOption]>;
161163

162164
def hip_Group : OptionGroup<"<HIP group>">, Group<f_Group>,
163-
DocName<"HIP options">;
165+
DocName<"HIP options">,
166+
Visibility<[ClangOption, CLOption]>;
164167

165168
def m_Group : OptionGroup<"<m group>">, Group<CompileOnly_Group>,
166-
DocName<"Target-dependent compilation options">;
169+
DocName<"Target-dependent compilation options">,
170+
Visibility<[ClangOption, CLOption]>;
167171

168172
// Feature groups - these take command line options that correspond directly to
169173
// target specific features and can be translated directly from command line
@@ -5167,14 +5171,16 @@ def prebind__all__twolevel__modules : Flag<["-"], "prebind_all_twolevel_modules"
51675171
def prebind : Flag<["-"], "prebind">;
51685172
def preload : Flag<["-"], "preload">;
51695173
def print_file_name_EQ : Joined<["-", "--"], "print-file-name=">,
5170-
HelpText<"Print the full library path of <file>">, MetaVarName<"<file>">;
5174+
HelpText<"Print the full library path of <file>">, MetaVarName<"<file>">,
5175+
Visibility<[ClangOption, CLOption]>;
51715176
def print_ivar_layout : Flag<["-"], "print-ivar-layout">,
51725177
Visibility<[ClangOption, CC1Option]>,
51735178
HelpText<"Enable Objective-C Ivar layout bitmap print trace">,
51745179
MarshallingInfoFlag<LangOpts<"ObjCGCBitmapPrint">>;
51755180
def print_libgcc_file_name : Flag<["-", "--"], "print-libgcc-file-name">,
51765181
HelpText<"Print the library path for the currently used compiler runtime "
5177-
"library (\"libgcc.a\" or \"libclang_rt.builtins.*.a\")">;
5182+
"library (\"libgcc.a\" or \"libclang_rt.builtins.*.a\")">,
5183+
Visibility<[ClangOption, CLOption]>;
51785184
def print_multi_directory : Flag<["-", "--"], "print-multi-directory">;
51795185
def print_multi_lib : Flag<["-", "--"], "print-multi-lib">;
51805186
def print_multi_flags : Flag<["-", "--"], "print-multi-flags-experimental">,
@@ -5183,27 +5189,34 @@ def print_multi_os_directory : Flag<["-", "--"], "print-multi-os-directory">,
51835189
Flags<[Unsupported]>;
51845190
def print_target_triple : Flag<["-", "--"], "print-target-triple">,
51855191
HelpText<"Print the normalized target triple">,
5186-
Visibility<[ClangOption, FlangOption]>;
5192+
Visibility<[ClangOption, FlangOption, CLOption]>;
51875193
def print_effective_triple : Flag<["-", "--"], "print-effective-triple">,
51885194
HelpText<"Print the effective target triple">,
5189-
Visibility<[ClangOption, FlangOption]>;
5195+
Visibility<[ClangOption, FlangOption, CLOption]>;
51905196
// GCC --disable-multiarch, GCC --enable-multiarch (upstream and Debian
51915197
// specific) have different behaviors. We choose not to support the option.
51925198
def : Flag<["-", "--"], "print-multiarch">, Flags<[Unsupported]>;
51935199
def print_prog_name_EQ : Joined<["-", "--"], "print-prog-name=">,
5194-
HelpText<"Print the full program path of <name>">, MetaVarName<"<name>">;
5200+
HelpText<"Print the full program path of <name>">, MetaVarName<"<name>">,
5201+
Visibility<[ClangOption, CLOption]>;
51955202
def print_resource_dir : Flag<["-", "--"], "print-resource-dir">,
5196-
HelpText<"Print the resource directory pathname">;
5203+
HelpText<"Print the resource directory pathname">,
5204+
Visibility<[ClangOption, CLOption]>;
51975205
def print_search_dirs : Flag<["-", "--"], "print-search-dirs">,
5198-
HelpText<"Print the paths used for finding libraries and programs">;
5206+
HelpText<"Print the paths used for finding libraries and programs">,
5207+
Visibility<[ClangOption, CLOption]>;
51995208
def print_targets : Flag<["-", "--"], "print-targets">,
5200-
HelpText<"Print the registered targets">;
5209+
HelpText<"Print the registered targets">,
5210+
Visibility<[ClangOption, CLOption]>;
52015211
def print_rocm_search_dirs : Flag<["-", "--"], "print-rocm-search-dirs">,
5202-
HelpText<"Print the paths used for finding ROCm installation">;
5212+
HelpText<"Print the paths used for finding ROCm installation">,
5213+
Visibility<[ClangOption, CLOption]>;
52035214
def print_runtime_dir : Flag<["-", "--"], "print-runtime-dir">,
5204-
HelpText<"Print the directory pathname containing clangs runtime libraries">;
5215+
HelpText<"Print the directory pathname containing clangs runtime libraries">,
5216+
Visibility<[ClangOption, CLOption]>;
52055217
def print_diagnostic_options : Flag<["-", "--"], "print-diagnostic-options">,
5206-
HelpText<"Print all of Clang's warning options">;
5218+
HelpText<"Print all of Clang's warning options">,
5219+
Visibility<[ClangOption, CLOption]>;
52075220
def private__bundle : Flag<["-"], "private_bundle">;
52085221
def pthreads : Flag<["-"], "pthreads">;
52095222
defm pthread : BoolOption<"", "pthread",
@@ -5230,7 +5243,7 @@ def resource_dir_EQ : Joined<["-"], "resource-dir=">, Flags<[NoXarchOption]>,
52305243
Visibility<[ClangOption, CLOption, DXCOption]>,
52315244
Alias<resource_dir>;
52325245
def rpath : Separate<["-"], "rpath">, Flags<[LinkerInput]>, Group<Link_Group>;
5233-
def rtlib_EQ : Joined<["-", "--"], "rtlib=">,
5246+
def rtlib_EQ : Joined<["-", "--"], "rtlib=">, Visibility<[ClangOption, CLOption]>,
52345247
HelpText<"Compiler runtime library to use">;
52355248
def frtlib_add_rpath: Flag<["-"], "frtlib-add-rpath">, Flags<[NoArgumentUnused]>,
52365249
HelpText<"Add -rpath with architecture-specific resource directory to the linker flags. "
@@ -5396,7 +5409,7 @@ def w : Flag<["-"], "w">, HelpText<"Suppress all warnings">,
53965409
MarshallingInfoFlag<DiagnosticOpts<"IgnoreWarnings">>;
53975410
def x : JoinedOrSeparate<["-"], "x">,
53985411
Flags<[NoXarchOption]>,
5399-
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
5412+
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option, CLOption]>,
54005413
HelpText<"Treat subsequent input files as having type <language>">,
54015414
MetaVarName<"<language>">;
54025415
def y : Joined<["-"], "y">;

clang/lib/Driver/Driver.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2589,8 +2589,11 @@ void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
25892589
Diag(clang::diag::note_drv_t_option_is_global);
25902590
}
25912591

2592+
// CUDA/HIP and their preprocessor expansions can be accepted by CL mode.
25922593
// Warn -x after last input file has no effect
2593-
if (!IsCLMode()) {
2594+
auto LastXArg = Args.getLastArgValue(options::OPT_x);
2595+
const llvm::StringSet<> ValidXArgs = {"cuda", "hip", "cui", "hipi"};
2596+
if (!IsCLMode() || ValidXArgs.find(LastXArg) != ValidXArgs.end()) {
25942597
Arg *LastXArg = Args.getLastArgNoClaim(options::OPT_x);
25952598
Arg *LastInputArg = Args.getLastArgNoClaim(options::OPT_INPUT);
25962599
if (LastXArg && LastInputArg &&

clang/test/Driver/cl-offload.cu

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_cl -### --offload-arch=sm_35 -fgpu-rdc \
2+
// RUN: --cuda-path=%S/Inputs/CUDA/usr/local/cuda \
3+
// RUN: /Wall -x cuda %s 2>&1 \
4+
// RUN: | FileCheck %s -check-prefix=CUDA
5+
6+
// RUN: %clang_cl -### --offload-arch=gfx1010 -fgpu-rdc --hip-link \
7+
// RUN: --rocm-path=%S/Inputs/rocm /Wall -x hip %s 2>&1 \
8+
// RUN: | FileCheck %s -check-prefix=HIP
9+
10+
// CUDA: "-cc1" "-triple" "nvptx64-nvidia-cuda" "-aux-triple" "x86_64-pc-windows-msvc"
11+
// CUDA-SAME: "-Weverything"
12+
// CUDA: ptxas
13+
// CUDA: "-cc1" "-triple" "x86_64-pc-windows-msvc{{.*}}" "-aux-triple" "nvptx64-nvidia-cuda"
14+
// CUDA-SAME: "-Weverything"
15+
// CUDA: link
16+
17+
// HIP: "-cc1" "-triple" "x86_64-pc-windows-msvc{{.*}}" "-aux-triple" "amdgcn-amd-amdhsa"
18+
// HIP-SAME: "-Weverything"
19+
// HIP: "-cc1" "-triple" "amdgcn-amd-amdhsa" "-aux-triple" "x86_64-pc-windows-msvc"
20+
// HIP-SAME: "-Weverything"
21+
// HIP: {{lld.* "-flavor" "gnu" "-m" "elf64_amdgpu"}}
22+
// HIP: {{link.* "amdhip64.lib"}}
23+
24+
// CMake uses this option when finding packages for HIP, so
25+
// make sure it does not cause error.
26+
27+
// RUN: %clang_cl --print-libgcc-file-name

0 commit comments

Comments
 (0)