Index: include/clang/Basic/DiagnosticDriverKinds.td =================================================================== --- include/clang/Basic/DiagnosticDriverKinds.td +++ include/clang/Basic/DiagnosticDriverKinds.td @@ -41,6 +41,7 @@ "install, pass a different GPU arch with --cuda-gpu-arch, or pass " "--no-cuda-version-check.">; def err_drv_cuda_host_arch : Error<"unsupported architecture '%0' for host compilation.">; +def err_drv_mix_cuda_hip : Error<"Mixed Cuda and HIP compilation is not supported.">; def err_drv_invalid_thread_model_for_target : Error< "invalid thread model '%0' in '%1' for this target">; def err_drv_invalid_linker_name : Error< Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -555,6 +555,8 @@ HelpText<"Do not include PTX for the follwing GPU architecture (e.g. sm_35) or 'all'. May be specified more than once.">; def cuda_gpu_arch_EQ : Joined<["--"], "cuda-gpu-arch=">, Flags<[DriverOption]>, HelpText<"CUDA GPU architecture (e.g. sm_35). May be specified more than once.">; +def hip_link : Flag<["--"], "hip-link">, + HelpText<"Link clang-offload-bundler bundles for HIP">; def no_cuda_gpu_arch_EQ : Joined<["--"], "no-cuda-gpu-arch=">, Flags<[DriverOption]>, HelpText<"Remove GPU architecture (e.g. sm_35) from the list of GPUs to compile for. " "'all' resets the list to its default value.">; Index: include/clang/Driver/Types.h =================================================================== --- include/clang/Driver/Types.h +++ include/clang/Driver/Types.h @@ -77,6 +77,9 @@ /// isCuda - Is this a CUDA input. bool isCuda(ID Id); + /// isHIP - Is this a HIP input. + bool isHIP(ID Id); + /// isObjC - Is this an "ObjC" input (Obj-C and Obj-C++ sources and headers). bool isObjC(ID Id); Index: lib/Driver/Driver.cpp =================================================================== --- lib/Driver/Driver.cpp +++ lib/Driver/Driver.cpp @@ -538,24 +538,46 @@ InputList &Inputs) { // - // CUDA + // CUDA/HIP // - // We need to generate a CUDA toolchain if any of the inputs has a CUDA type. - if (llvm::any_of(Inputs, [](std::pair &I) { + // We need to generate a CUDA toolchain if any of the inputs has a CUDA + // or HIP type. However, mixed CUDA/HIP compilation is not supported. + bool IsCuda = + llvm::any_of(Inputs, [](std::pair &I) { return types::isCuda(I.first); - })) { + }); + bool IsHIP = + llvm::any_of(Inputs, + [](std::pair &I) { + return types::isHIP(I.first); + }) || + C.getInputArgs().hasArg(options::OPT_hip_link); + if (IsCuda && IsHIP) { + Diag(clang::diag::err_drv_mix_cuda_hip); + return; + } + if (IsCuda || IsHIP) { const ToolChain *HostTC = C.getSingleOffloadToolChain(); const llvm::Triple &HostTriple = HostTC->getTriple(); - llvm::Triple CudaTriple(HostTriple.isArch64Bit() ? "nvptx64-nvidia-cuda" - : "nvptx-nvidia-cuda"); - // Use the CUDA and host triples as the key into the ToolChains map, because - // the device toolchain we create depends on both. + StringRef DeviceTripleStr; + auto OFK = IsHIP ? Action::OFK_HIP : Action::OFK_Cuda; + if (IsHIP) { + // HIP is only supported on amdgcn. + DeviceTripleStr = "amdgcn-amd-amdhsa"; + } else { + // CUDA is only supported on nvptx. + DeviceTripleStr = HostTriple.isArch64Bit() ? "nvptx64-nvidia-cuda" + : "nvptx-nvidia-cuda"; + } + llvm::Triple CudaTriple(DeviceTripleStr); + // Use the CUDA/HIP and host triples as the key into the ToolChains map, + // because the device toolchain we create depends on both. auto &CudaTC = ToolChains[CudaTriple.str() + "/" + HostTriple.str()]; if (!CudaTC) { CudaTC = llvm::make_unique( - *this, CudaTriple, *HostTC, C.getInputArgs(), Action::OFK_Cuda); + *this, CudaTriple, *HostTC, C.getInputArgs(), OFK); } - C.addOffloadDeviceToolChain(CudaTC.get(), Action::OFK_Cuda); + C.addOffloadDeviceToolChain(CudaTC.get(), OFK); } // Index: lib/Driver/Types.cpp =================================================================== --- lib/Driver/Types.cpp +++ lib/Driver/Types.cpp @@ -172,6 +172,15 @@ case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE: + return true; + } +} + +bool types::isHIP(ID Id) { + switch (Id) { + default: + return false; + case TY_HIP: case TY_PP_HIP: case TY_HIP_DEVICE: @@ -230,6 +239,7 @@ .Case("fpp", TY_Fortran) .Case("FPP", TY_Fortran) .Case("gch", TY_PCH) + .Case("hip", TY_HIP) .Case("hpp", TY_CXXHeader) .Case("iim", TY_PP_CXXModule) .Case("lib", TY_Object) Index: test/Driver/hip-inputs.hip =================================================================== --- /dev/null +++ test/Driver/hip-inputs.hip @@ -0,0 +1,23 @@ +// REQUIRES: clang-driver +// REQUIRES: x86-registered-target +// REQUIRES: amdgpu-registered-target + +// RUN: %clang -ccc-print-phases -target x86_64-linux-gnu \ +// RUN: -x hip --cuda-gpu-arch=gfx803 -c \ +// RUN: %S/Inputs/hip_multiple_inputs/a.cu \ +// RUN: %S/Inputs/hip_multiple_inputs/b.hip 2>&1 \ +// RUN: | FileCheck %s + +// RUN: not %clang -ccc-print-phases -target x86_64-linux-gnu \ +// RUN: --cuda-gpu-arch=gfx803 -c \ +// RUN: %S/Inputs/hip_multiple_inputs/a.cu \ +// RUN: %S/Inputs/hip_multiple_inputs/b.hip 2>&1 \ +// RUN: | FileCheck -check-prefix=MIX %s + +// RUN: not %clang -ccc-print-phases -target x86_64-linux-gnu \ +// RUN: --cuda-gpu-arch=gfx803 -c \ +// RUN: --hip-link %S/Inputs/hip_multiple_inputs/a.cu 2>&1 \ +// RUN: | FileCheck -check-prefix=MIX %s + +// CHECK-NOT: error: Mixed Cuda and HIP compilation is not supported. +// MIX: error: Mixed Cuda and HIP compilation is not supported.