| 1 | //===- AMDGPUOpenMP.cpp - AMDGPUOpenMP ToolChain Implementation -*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "AMDGPUOpenMP.h" |
| 10 | #include "AMDGPU.h" |
| 11 | #include "clang/Driver/Compilation.h" |
| 12 | #include "clang/Driver/Driver.h" |
| 13 | #include "clang/Driver/Tool.h" |
| 14 | #include "clang/Options/Options.h" |
| 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | |
| 17 | using namespace clang::driver; |
| 18 | using namespace clang::driver::toolchains; |
| 19 | using namespace clang::driver::tools; |
| 20 | using namespace clang; |
| 21 | using namespace llvm::opt; |
| 22 | |
| 23 | AMDGPUOpenMPToolChain::AMDGPUOpenMPToolChain(const Driver &D, |
| 24 | const llvm::Triple &Triple, |
| 25 | const ToolChain &HostTC, |
| 26 | const ArgList &Args) |
| 27 | : ROCMToolChain(D, Triple, Args), HostTC(HostTC) { |
| 28 | // Lookup binaries into the driver directory, this is used to |
| 29 | // discover the 'amdgpu-arch' executable. |
| 30 | getProgramPaths().push_back(Elt: getDriver().Dir); |
| 31 | } |
| 32 | |
| 33 | void AMDGPUOpenMPToolChain::addClangTargetOptions( |
| 34 | const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, |
| 35 | BoundArch BA, Action::OffloadKind DeviceOffloadingKind) const { |
| 36 | assert(DeviceOffloadingKind == Action::OFK_OpenMP && |
| 37 | "Only OpenMP offloading kinds are supported." ); |
| 38 | |
| 39 | if (!DriverArgs.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib, |
| 40 | Default: true)) |
| 41 | return; |
| 42 | |
| 43 | for (auto BCFile : getDeviceLibs(Args: DriverArgs, BA, DeviceOffloadKind: DeviceOffloadingKind)) { |
| 44 | CC1Args.push_back(Elt: BCFile.ShouldInternalize ? "-mlink-builtin-bitcode" |
| 45 | : "-mlink-bitcode-file" ); |
| 46 | CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: BCFile.Path)); |
| 47 | } |
| 48 | |
| 49 | // Link the bitcode library late if we're using device LTO. |
| 50 | if (isUsingLTO(Args: DriverArgs, Kind: DeviceOffloadingKind)) |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | ToolChain::CXXStdlibType |
| 55 | AMDGPUOpenMPToolChain::GetCXXStdlibType(const ArgList &Args) const { |
| 56 | return HostTC.GetCXXStdlibType(Args); |
| 57 | } |
| 58 | |
| 59 | void AMDGPUOpenMPToolChain::AddClangCXXStdlibIncludeArgs( |
| 60 | const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1Args) const { |
| 61 | HostTC.AddClangCXXStdlibIncludeArgs(DriverArgs: Args, CC1Args); |
| 62 | } |
| 63 | |
| 64 | void AMDGPUOpenMPToolChain::AddClangSystemIncludeArgs( |
| 65 | const ArgList &DriverArgs, ArgStringList &CC1Args) const { |
| 66 | HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args); |
| 67 | } |
| 68 | |
| 69 | void AMDGPUOpenMPToolChain::AddIAMCUIncludeArgs(const ArgList &Args, |
| 70 | ArgStringList &CC1Args) const { |
| 71 | HostTC.AddIAMCUIncludeArgs(DriverArgs: Args, CC1Args); |
| 72 | } |
| 73 | |
| 74 | VersionTuple |
| 75 | AMDGPUOpenMPToolChain::computeMSVCVersion(const Driver *D, |
| 76 | const ArgList &Args) const { |
| 77 | return HostTC.computeMSVCVersion(D, Args); |
| 78 | } |
| 79 | |
| 80 | llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> |
| 81 | AMDGPUOpenMPToolChain::getDeviceLibs( |
| 82 | const llvm::opt::ArgList &Args, BoundArch BA, |
| 83 | const Action::OffloadKind DeviceOffloadingKind) const { |
| 84 | if (!Args.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib, Default: true)) |
| 85 | return {}; |
| 86 | |
| 87 | StringRef GpuArch = getProcessorFromTargetID(T: getTriple(), OffloadArch: BA.ArchName); |
| 88 | SmallVector<BitCodeLibraryInfo, 12> BCLibs; |
| 89 | for (auto BCLib : getCommonDeviceLibNames(DriverArgs: Args, TargetID: BA.ArchName, GPUArch: GpuArch, |
| 90 | DeviceOffloadingKind)) |
| 91 | BCLibs.emplace_back(Args&: BCLib); |
| 92 | |
| 93 | return BCLibs; |
| 94 | } |
| 95 | |