From: Yaxun Liu Date: Fri, 11 May 2018 19:21:39 +0000 (+0000) Subject: [HIP] Set proper triple and offload kind for the toolchain X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6e79e3ea0e0d46dce51b57af77514060bb77c499;p=clang [HIP] Set proper triple and offload kind for the toolchain Also introduce --hip-link option to indicate HIP for linking. Differential Revision: https://reviews.llvm.org/D46475 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@332123 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticDriverKinds.td b/include/clang/Basic/DiagnosticDriverKinds.td index 1b94b2dc9c..003450d951 100644 --- a/include/clang/Basic/DiagnosticDriverKinds.td +++ b/include/clang/Basic/DiagnosticDriverKinds.td @@ -41,6 +41,7 @@ def err_drv_cuda_version_unsupported : Error< "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< diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index 8d03da31a3..0919f97665 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -557,6 +557,8 @@ def no_cuda_include_ptx_EQ : Joined<["--"], "no-cuda-include-ptx=">, Flags<[Driv 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.">; diff --git a/include/clang/Driver/Types.h b/include/clang/Driver/Types.h index 22a26ae46a..5bc6668a0d 100644 --- a/include/clang/Driver/Types.h +++ b/include/clang/Driver/Types.h @@ -77,6 +77,9 @@ namespace types { /// 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); diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index 1f4dbeb240..8ca40bc923 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -538,24 +538,46 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C, 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); } // diff --git a/lib/Driver/Types.cpp b/lib/Driver/Types.cpp index bcb35b1e0f..45bb699cfb 100644 --- a/lib/Driver/Types.cpp +++ b/lib/Driver/Types.cpp @@ -172,6 +172,15 @@ bool types::isCuda(ID Id) { 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 @@ types::ID types::lookupTypeForExtension(llvm::StringRef Ext) { .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) diff --git a/test/Driver/Inputs/hip_multiple_inputs/a.cu b/test/Driver/Inputs/hip_multiple_inputs/a.cu new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/Driver/Inputs/hip_multiple_inputs/b.hip b/test/Driver/Inputs/hip_multiple_inputs/b.hip new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/Driver/hip-inputs.hip b/test/Driver/hip-inputs.hip new file mode 100644 index 0000000000..2febe2039e --- /dev/null +++ b/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.