diff --git a/clang/docs/ClangCommandLineReference.rst b/clang/docs/ClangCommandLineReference.rst --- a/clang/docs/ClangCommandLineReference.rst +++ b/clang/docs/ClangCommandLineReference.rst @@ -1143,9 +1143,9 @@ Add directory to SYSTEM include search path, absolute paths are relative to -isysroot -.. option:: --libomptarget-nvptx-path= +.. option:: --libomptarget-nvptx-bc-path= -Path to libomptarget-nvptx libraries +Path to libomptarget-nvptx bitcode library .. option:: --ptxas-path= diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -263,12 +263,12 @@ "The target '%0' is not a supported OpenMP host target.">; def err_drv_expecting_fopenmp_with_fopenmp_targets : Error< "The option -fopenmp-targets must be used in conjunction with a -fopenmp option compatible with offloading, please use -fopenmp=libomp or -fopenmp=libiomp5.">; +def err_drv_omp_offload_target_missingbcruntime : Error< + "No library '%0' found in the default clang lib directory or in LIBRARY_PATH. Please use --libomptarget-nvptx-bc-path to specify nvptx bitcode library.">; +def err_drv_omp_offload_target_bcruntime_not_found : Error<"Bitcode library '%0' does not exist.">; def warn_drv_omp_offload_target_duplicate : Warning< "The OpenMP offloading target '%0' is similar to target '%1' already specified - will be ignored.">, InGroup; -def warn_drv_omp_offload_target_missingbcruntime : Warning< - "No library '%0' found in the default clang lib directory or in LIBRARY_PATH. Expect degraded performance due to no inlining of runtime functions on target devices.">, - InGroup; def err_drv_unsupported_embed_bitcode : Error<"%0 is not supported with -fembed-bitcode">; def err_drv_bitcode_unsupported_on_toolchain : Error< diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -992,8 +992,8 @@ def gpu_instrument_lib_EQ : Joined<["--"], "gpu-instrument-lib=">, HelpText<"Instrument device library for HIP, which is a LLVM bitcode containing " "__cyg_profile_func_enter and __cyg_profile_func_exit">; -def libomptarget_nvptx_path_EQ : Joined<["--"], "libomptarget-nvptx-path=">, Group, - HelpText<"Path to libomptarget-nvptx libraries">; +def libomptarget_nvptx_bc_path_EQ : Joined<["--"], "libomptarget-nvptx-bc-path=">, Group, + HelpText<"Path to libomptarget-nvptx bitcode library">; def dD : Flag<["-"], "dD">, Group, Flags<[CC1Option]>, HelpText<"Print macro definitions in -E mode in addition to normal output">; def dI : Flag<["-"], "dI">, Group, Flags<[CC1Option]>, diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp --- a/clang/lib/Driver/ToolChains/Cuda.cpp +++ b/clang/lib/Driver/ToolChains/Cuda.cpp @@ -600,11 +600,6 @@ CmdArgs.push_back("-arch"); CmdArgs.push_back(Args.MakeArgString(GPUArch)); - // Assume that the directory specified with --libomptarget_nvptx_path - // contains the static library libomptarget-nvptx.a. - if (const Arg *A = Args.getLastArg(options::OPT_libomptarget_nvptx_path_EQ)) - CmdArgs.push_back(Args.MakeArgString(Twine("-L") + A->getValue())); - // Add paths specified in LIBRARY_PATH environment variable as -L options. addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH"); @@ -614,9 +609,6 @@ llvm::sys::path::append(DefaultLibPath, "lib" CLANG_LIBDIR_SUFFIX); CmdArgs.push_back(Args.MakeArgString(Twine("-L") + DefaultLibPath)); - // Add linking against library implementing OpenMP calls on NVPTX target. - CmdArgs.push_back("-lomptarget-nvptx"); - for (const auto &II : Inputs) { if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR || @@ -761,9 +753,6 @@ if (DeviceOffloadingKind == Action::OFK_OpenMP) { SmallVector LibraryPaths; - if (const Arg *A = DriverArgs.getLastArg(options::OPT_libomptarget_nvptx_path_EQ)) - LibraryPaths.push_back(A->getValue()); - // Add user defined library paths from LIBRARY_PATH. llvm::Optional LibPath = llvm::sys::Process::GetEnv("LIBRARY_PATH"); @@ -781,22 +770,37 @@ llvm::sys::path::append(DefaultLibPath, Twine("lib") + CLANG_LIBDIR_SUFFIX); LibraryPaths.emplace_back(DefaultLibPath.c_str()); - std::string LibOmpTargetName = - "libomptarget-nvptx-" + GpuArch.str() + ".bc"; - bool FoundBCLibrary = false; - for (StringRef LibraryPath : LibraryPaths) { - SmallString<128> LibOmpTargetFile(LibraryPath); - llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName); - if (llvm::sys::fs::exists(LibOmpTargetFile)) { + // First check whether user specifies bc library + if (const Arg *A = + DriverArgs.getLastArg(options::OPT_libomptarget_nvptx_bc_path_EQ)) { + std::string LibOmpTargetName(A->getValue()); + if (llvm::sys::fs::exists(LibOmpTargetName)) { CC1Args.push_back("-mlink-builtin-bitcode"); - CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile)); - FoundBCLibrary = true; - break; + CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetName)); + } else { + getDriver().Diag(diag::err_drv_omp_offload_target_bcruntime_not_found) + << LibOmpTargetName; + } + } else { + bool FoundBCLibrary = false; + + std::string LibOmpTargetName = + "libomptarget-nvptx-" + GpuArch.str() + ".bc"; + + for (StringRef LibraryPath : LibraryPaths) { + SmallString<128> LibOmpTargetFile(LibraryPath); + llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName); + if (llvm::sys::fs::exists(LibOmpTargetFile)) { + CC1Args.push_back("-mlink-builtin-bitcode"); + CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile)); + FoundBCLibrary = true; + break; + } } + if (!FoundBCLibrary) + getDriver().Diag(diag::err_drv_omp_offload_target_missingbcruntime) + << LibOmpTargetName; } - if (!FoundBCLibrary) - getDriver().Diag(diag::warn_drv_omp_offload_target_missingbcruntime) - << LibOmpTargetName; } } diff --git a/clang/test/Driver/Inputs/libomptarget/libomptarget-nvptx-test.bc b/clang/test/Driver/Inputs/libomptarget/libomptarget-nvptx-test.bc new file mode 100644 diff --git a/clang/test/Driver/openmp-offload-gpu.c b/clang/test/Driver/openmp-offload-gpu.c --- a/clang/test/Driver/openmp-offload-gpu.c +++ b/clang/test/Driver/openmp-offload-gpu.c @@ -34,22 +34,6 @@ /// ########################################################################### -/// Check that -lomptarget-nvptx is passed to nvlink. -// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp \ -// RUN: -fopenmp-targets=nvptx64-nvidia-cuda %s 2>&1 \ -// RUN: | FileCheck -check-prefix=CHK-NVLINK %s -/// Check that the value of --libomptarget-nvptx-path is forwarded to nvlink. -// RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp \ -// RUN: --libomptarget-nvptx-path=/path/to/libomptarget/ \ -// RUN: -fopenmp-targets=nvptx64-nvidia-cuda %s 2>&1 \ -// RUN: | FileCheck -check-prefixes=CHK-NVLINK,CHK-LIBOMPTARGET-NVPTX-PATH %s - -// CHK-NVLINK: nvlink -// CHK-LIBOMPTARGET-NVPTX-PATH-SAME: "-L/path/to/libomptarget/" -// CHK-NVLINK-SAME: "-lomptarget-nvptx" - -/// ########################################################################### - /// Check cubin file generation and usage by nvlink // RUN: %clang -### -no-canonical-prefixes -target powerpc64le-unknown-linux-gnu -fopenmp=libomp \ // RUN: -fopenmp-targets=nvptx64-nvidia-cuda -save-temps %s 2>&1 \ @@ -173,13 +157,14 @@ // RUN: -Xopenmp-target -march=sm_20 --cuda-path=%S/Inputs/CUDA_80/usr/local/cuda \ // RUN: -fopenmp-relocatable-target -save-temps -no-canonical-prefixes %s 2>&1 \ // RUN: | FileCheck -check-prefix=CHK-BCLIB %s -/// The user can override default detection using --libomptarget-nvptx-path=. -// RUN: %clang -### -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda --libomptarget-nvptx-path=%S/Inputs/libomptarget \ +/// The user can override default detection using --libomptarget-nvptx-bc-path=. +// RUN: %clang -### -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda \ +// RUN: --libomptarget-nvptx-bc-path=%S/Inputs/libomptarget/libomptarget-nvptx-test.bc \ // RUN: -Xopenmp-target -march=sm_20 --cuda-path=%S/Inputs/CUDA_80/usr/local/cuda \ // RUN: -fopenmp-relocatable-target -save-temps -no-canonical-prefixes %s 2>&1 \ // RUN: | FileCheck -check-prefix=CHK-BCLIB %s -// CHK-BCLIB: clang{{.*}}-triple{{.*}}nvptx64-nvidia-cuda{{.*}}-mlink-builtin-bitcode{{.*}}libomptarget-nvptx-sm_20.bc +// CHK-BCLIB: clang{{.*}}-triple{{.*}}nvptx64-nvidia-cuda{{.*}}-mlink-builtin-bitcode{{.*}}libomptarget-nvptx-test.bc // CHK-BCLIB-NOT: {{error:|warning:}} /// ########################################################################### @@ -191,7 +176,18 @@ // RUN: -fopenmp-relocatable-target -save-temps -no-canonical-prefixes %s 2>&1 \ // RUN: | FileCheck -check-prefix=CHK-BCLIB-WARN %s -// CHK-BCLIB-WARN: No library 'libomptarget-nvptx-sm_20.bc' found in the default clang lib directory or in LIBRARY_PATH. Expect degraded performance due to no inlining of runtime functions on target devices. +// CHK-BCLIB-WARN: No library 'libomptarget-nvptx-sm_20.bc' found in the default clang lib directory or in LIBRARY_PATH. Please use --libomptarget-nvptx-bc-path to specify nvptx bitcode library. + +/// ########################################################################### + +/// Check that the error is thrown when the libomptarget bitcode library does not exist. +// RUN: %clang -### -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda \ +// RUN: -Xopenmp-target -march=sm_20 --cuda-path=%S/Inputs/CUDA_80/usr/local/cuda \ +// RUN: --libomptarget-nvptx-bc-path=not-exist.bc \ +// RUN: -fopenmp-relocatable-target -save-temps -no-canonical-prefixes %s 2>&1 \ +// RUN: | FileCheck -check-prefix=CHK-BCLIB-ERROR %s + +// CHK-BCLIB-ERROR: Bitcode library 'not-exist.bc' does not exist. /// Check that debug info is emitted in dwarf-2 // RUN: %clang -### -no-canonical-prefixes -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_60 %s -g -O1 --no-cuda-noopt-device-debug 2>&1 \