diff --git a/clang/include/clang/Driver/CommonArgs.h b/clang/include/clang/Driver/CommonArgs.h index 1464ce4e1b31b..ea4b73082623a 100644 --- a/clang/include/clang/Driver/CommonArgs.h +++ b/clang/include/clang/Driver/CommonArgs.h @@ -218,7 +218,8 @@ void addOpenMPDeviceRTL(const Driver &D, const llvm::opt::ArgList &DriverArgs, const ToolChain &HostTC); void addOpenCLBuiltinsLib(const Driver &D, const llvm::opt::ArgList &DriverArgs, - llvm::opt::ArgStringList &CC1Args); + llvm::opt::ArgStringList &CC1Args, + const StringRef InferredLibclcLibName = ""); void addOutlineAtomicsArgs(const Driver &D, const ToolChain &TC, const llvm::opt::ArgList &Args, diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 6aab43c9ed57f..1dc688d25b9b3 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -1435,6 +1435,8 @@ def openacc_macro_override_EQ // End Clang specific/exclusive options for OpenACC. +def libclc_lib : Joined<["--"], "libclc-lib">, Group, + HelpText<"Link with the libclc OpenCL bitcode library">; def libclc_lib_EQ : Joined<["--"], "libclc-lib=">, Group, HelpText<"Namespec of libclc OpenCL bitcode library to link">; def libomptarget_amdgpu_bc_path_EQ : Joined<["--"], "libomptarget-amdgpu-bc-path=">, Group, diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp index 5e463b9c98687..f1bcfd47831a3 100644 --- a/clang/lib/Driver/ToolChains/AMDGPU.cpp +++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp @@ -858,8 +858,19 @@ void AMDGPUToolChain::addClangTargetOptions( CC1Args.push_back("-fapply-global-visibility-to-externs"); } - if (DeviceOffloadingKind == Action::OFK_None) - addOpenCLBuiltinsLib(getDriver(), DriverArgs, CC1Args); + if (DeviceOffloadingKind == Action::OFK_None) { + auto GpuArch = getGPUArch(DriverArgs); + auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch); + const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind); + SmallString<128> InferredLibclcLibName(CanonArch); + if (getTriple().getOS() != llvm::Triple::Mesa3D) + InferredLibclcLibName += "-amdgcn--"; + else + InferredLibclcLibName += "-amdgcn-mesa-mesa3d"; + InferredLibclcLibName += ".bc"; + addOpenCLBuiltinsLib(getDriver(), DriverArgs, CC1Args, + InferredLibclcLibName); + } } void AMDGPUToolChain::addClangWarningOptions(ArgStringList &CC1Args) const { diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index eb88f7bd4b469..fb6a1c7eb0f9d 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -2999,27 +2999,39 @@ void tools::addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C, void tools::addOpenCLBuiltinsLib(const Driver &D, const llvm::opt::ArgList &DriverArgs, - llvm::opt::ArgStringList &CC1Args) { + llvm::opt::ArgStringList &CC1Args, + const StringRef InferredLibclcLibName) { // Check whether user specifies a libclc bytecode library - const Arg *A = DriverArgs.getLastArg(options::OPT_libclc_lib_EQ); + const Arg *A = DriverArgs.getLastArg(options::OPT_libclc_lib, + options::OPT_libclc_lib_EQ); if (!A) return; + bool IsEQOpt = A->getOption().matches(options::OPT_libclc_lib_EQ); + // Find device libraries in /lib/clang//lib/libclc/ SmallString<128> LibclcPath(D.ResourceDir); llvm::sys::path::append(LibclcPath, "lib", "libclc"); // If the namespec is of the form :filename, search for that file. - StringRef LibclcNamespec(A->getValue()); - bool FilenameSearch = LibclcNamespec.consume_front(":"); - SmallString<128> LibclcTargetFile(LibclcNamespec); + bool FilenameSearch = false; + SmallString<128> LibclcTargetFile; + + if (IsEQOpt) { + StringRef LibclcNamespec(A->getValue()); + FilenameSearch = LibclcNamespec.consume_front(":"); + LibclcTargetFile = LibclcNamespec; + } else { + assert(!InferredLibclcLibName.empty() && "No inferred libclc name"); + LibclcTargetFile = InferredLibclcLibName; + } if (FilenameSearch && llvm::sys::fs::exists(LibclcTargetFile)) { CC1Args.push_back("-mlink-builtin-bitcode"); CC1Args.push_back(DriverArgs.MakeArgString(LibclcTargetFile)); } else { // Search the library paths for the file - if (!FilenameSearch) + if (!FilenameSearch && IsEQOpt) LibclcTargetFile += ".bc"; llvm::sys::path::append(LibclcPath, LibclcTargetFile); diff --git a/clang/test/Driver/Inputs/resource_dir/lib/libclc/gfx908-amdgcn--.bc b/clang/test/Driver/Inputs/resource_dir/lib/libclc/gfx908-amdgcn--.bc new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/clang/test/Driver/Inputs/resource_dir/lib/libclc/gfx908-amdgcn-mesa-mesa3d.bc b/clang/test/Driver/Inputs/resource_dir/lib/libclc/gfx908-amdgcn-mesa-mesa3d.bc new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/clang/test/Driver/opencl-libclc.cl b/clang/test/Driver/opencl-libclc.cl index 185690768c75b..37093d7da3411 100644 --- a/clang/test/Driver/opencl-libclc.cl +++ b/clang/test/Driver/opencl-libclc.cl @@ -1,9 +1,17 @@ // RUN: %clang -### -target amdgcn-amd-amdhsa --no-offloadlib --libclc-lib=:%S/Inputs/libclc/libclc.bc %s 2>&1 | FileCheck %s // RUN: %clang -### -target amdgcn-amd-amdhsa --no-offloadlib --libclc-lib=:%S/Inputs/libclc/subdir/libclc.bc %s 2>&1 | FileCheck %s --check-prefix CHECK-SUBDIR +// RUN: %clang -### -target amdgcn-amd-amdhsa -mcpu=gfx908 --no-offloadlib -resource-dir=%S/Inputs/resource_dir --libclc-lib %s 2>&1 \ +// RUN: | FileCheck %s --check-prefix CHECK-INFER +// RUN: %clang -### -target amdgcn-mesa-mesa3d -mcpu=gfx908 --no-offloadlib -resource-dir=%S/Inputs/resource_dir --libclc-lib %s 2>&1 \ +// RUN: | FileCheck %s --check-prefix CHECK-INFER-MESA3D + // RUN: not %clang -### -target amdgcn-amd-amdhsa --no-offloadlib --libclc-lib=:%S/Inputs/libclc/subdir/not-here.bc %s 2>&1 | FileCheck %s --check-prefix CHECK-ERROR // CHECK: -mlink-builtin-bitcode{{.*}}Inputs{{/|\\\\}}libclc{{/|\\\\}}libclc.bc // CHECK-SUBDIR: -mlink-builtin-bitcode{{.*}}Inputs{{/|\\\\}}libclc{{/|\\\\}}subdir{{/|\\\\}}libclc.bc +// CHECK-INFER: -mlink-builtin-bitcode{{.*}}Inputs{{/|\\\\}}resource_dir{{/|\\\\}}lib{{/|\\\\}}libclc{{/|\\\\}}gfx908-amdgcn--.bc +// CHECK-INFER-MESA3D: -mlink-builtin-bitcode{{.*}}Inputs{{/|\\\\}}resource_dir{{/|\\\\}}lib{{/|\\\\}}libclc{{/|\\\\}}gfx908-amdgcn-mesa-mesa3d.bc + // CHECK-ERROR: no libclc library{{.*}}not-here.bc' found in the clang resource directory