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 "CommonArgs.h"
12#include "ToolChains/ROCm.h"
13#include "clang/Basic/DiagnosticDriver.h"
14#include "clang/Driver/Compilation.h"
15#include "clang/Driver/Driver.h"
16#include "clang/Driver/DriverDiagnostic.h"
17#include "clang/Driver/InputInfo.h"
18#include "clang/Driver/Options.h"
19#include "clang/Driver/Tool.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/Support/FileSystem.h"
22#include "llvm/Support/FormatAdapters.h"
23#include "llvm/Support/FormatVariadic.h"
24#include "llvm/Support/Path.h"
25
26using namespace clang::driver;
27using namespace clang::driver::toolchains;
28using namespace clang::driver::tools;
29using namespace clang;
30using namespace llvm::opt;
31
32AMDGPUOpenMPToolChain::AMDGPUOpenMPToolChain(const Driver &D,
33 const llvm::Triple &Triple,
34 const ToolChain &HostTC,
35 const ArgList &Args)
36 : ROCMToolChain(D, Triple, Args), HostTC(HostTC) {
37 // Lookup binaries into the driver directory, this is used to
38 // discover the 'amdgpu-arch' executable.
39 getProgramPaths().push_back(Elt: getDriver().Dir);
40}
41
42void AMDGPUOpenMPToolChain::addClangTargetOptions(
43 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
44 Action::OffloadKind DeviceOffloadingKind) const {
45 HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadKind: DeviceOffloadingKind);
46
47 assert(DeviceOffloadingKind == Action::OFK_OpenMP &&
48 "Only OpenMP offloading kinds are supported.");
49
50 if (DriverArgs.hasArg(Ids: options::OPT_nogpulib))
51 return;
52
53 for (auto BCFile : getDeviceLibs(Args: DriverArgs)) {
54 CC1Args.push_back(Elt: BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
55 : "-mlink-bitcode-file");
56 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: BCFile.Path));
57 }
58
59 // Link the bitcode library late if we're using device LTO.
60 if (getDriver().isUsingLTO(/* IsOffload */ true))
61 return;
62}
63
64llvm::opt::DerivedArgList *AMDGPUOpenMPToolChain::TranslateArgs(
65 const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
66 Action::OffloadKind DeviceOffloadKind) const {
67 DerivedArgList *DAL =
68 HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
69 if (!DAL)
70 DAL = new DerivedArgList(Args.getBaseArgs());
71
72 const OptTable &Opts = getDriver().getOpts();
73
74 if (DeviceOffloadKind == Action::OFK_OpenMP) {
75 for (Arg *A : Args)
76 if (!llvm::is_contained(Range&: *DAL, Element: A))
77 DAL->append(A);
78
79 if (!DAL->hasArg(Ids: options::OPT_march_EQ)) {
80 StringRef Arch = BoundArch;
81 if (Arch.empty()) {
82 auto ArchsOrErr = getSystemGPUArchs(Args);
83 if (!ArchsOrErr) {
84 std::string ErrMsg =
85 llvm::formatv(Fmt: "{0}", Vals: llvm::fmt_consume(Item: ArchsOrErr.takeError()));
86 getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch)
87 << llvm::Triple::getArchTypeName(Kind: getArch()) << ErrMsg << "-march";
88 Arch = OffloadArchToString(A: OffloadArch::HIPDefault);
89 } else {
90 Arch = Args.MakeArgString(Str: ArchsOrErr->front());
91 }
92 }
93 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_march_EQ), Value: Arch);
94 }
95
96 return DAL;
97 }
98
99 for (Arg *A : Args) {
100 DAL->append(A);
101 }
102
103 if (!BoundArch.empty()) {
104 DAL->eraseArg(Id: options::OPT_march_EQ);
105 DAL->AddJoinedArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_march_EQ),
106 Value: BoundArch);
107 }
108
109 return DAL;
110}
111
112void AMDGPUOpenMPToolChain::addClangWarningOptions(
113 ArgStringList &CC1Args) const {
114 AMDGPUToolChain::addClangWarningOptions(CC1Args);
115 HostTC.addClangWarningOptions(CC1Args);
116}
117
118ToolChain::CXXStdlibType
119AMDGPUOpenMPToolChain::GetCXXStdlibType(const ArgList &Args) const {
120 return HostTC.GetCXXStdlibType(Args);
121}
122
123void AMDGPUOpenMPToolChain::AddClangSystemIncludeArgs(
124 const ArgList &DriverArgs, ArgStringList &CC1Args) const {
125 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
126}
127
128void AMDGPUOpenMPToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
129 ArgStringList &CC1Args) const {
130 HostTC.AddIAMCUIncludeArgs(DriverArgs: Args, CC1Args);
131}
132
133SanitizerMask AMDGPUOpenMPToolChain::getSupportedSanitizers() const {
134 // The AMDGPUOpenMPToolChain only supports sanitizers in the sense that it
135 // allows sanitizer arguments on the command line if they are supported by the
136 // host toolchain. The AMDGPUOpenMPToolChain will actually ignore any command
137 // line arguments for any of these "supported" sanitizers. That means that no
138 // sanitization of device code is actually supported at this time.
139 //
140 // This behavior is necessary because the host and device toolchains
141 // invocations often share the command line, so the device toolchain must
142 // tolerate flags meant only for the host toolchain.
143 return HostTC.getSupportedSanitizers();
144}
145
146VersionTuple
147AMDGPUOpenMPToolChain::computeMSVCVersion(const Driver *D,
148 const ArgList &Args) const {
149 return HostTC.computeMSVCVersion(D, Args);
150}
151
152llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
153AMDGPUOpenMPToolChain::getDeviceLibs(const llvm::opt::ArgList &Args) const {
154 if (Args.hasArg(Ids: options::OPT_nogpulib))
155 return {};
156
157 if (!RocmInstallation->hasDeviceLibrary()) {
158 getDriver().Diag(DiagID: diag::err_drv_no_rocm_device_lib) << 0;
159 return {};
160 }
161
162 StringRef GpuArch = getProcessorFromTargetID(
163 T: getTriple(), OffloadArch: Args.getLastArgValue(Id: options::OPT_march_EQ));
164
165 SmallVector<BitCodeLibraryInfo, 12> BCLibs;
166 for (auto BCLib : getCommonDeviceLibNames(DriverArgs: Args, GPUArch: GpuArch.str(),
167 /*IsOpenMP=*/isOpenMP: true))
168 BCLibs.emplace_back(Args&: BCLib);
169
170 return BCLibs;
171}
172