clang 19.0.0git
AMDGPUOpenMP.cpp
Go to the documentation of this file.
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"
15#include "clang/Driver/Driver.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
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(getDriver().Dir);
40}
41
43 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
44 Action::OffloadKind DeviceOffloadingKind) const {
45 HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
46
47 assert(DeviceOffloadingKind == Action::OFK_OpenMP &&
48 "Only OpenMP offloading kinds are supported.");
49
50 CC1Args.push_back("-fcuda-is-device");
51
52 if (DriverArgs.hasArg(options::OPT_nogpulib))
53 return;
54
55 for (auto BCFile : getDeviceLibs(DriverArgs)) {
56 CC1Args.push_back(BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
57 : "-mlink-bitcode-file");
58 CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path));
59 }
60
61 // Link the bitcode library late if we're using device LTO.
62 if (getDriver().isUsingLTO(/* IsOffload */ true))
63 return;
64}
65
66llvm::opt::DerivedArgList *AMDGPUOpenMPToolChain::TranslateArgs(
67 const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
68 Action::OffloadKind DeviceOffloadKind) const {
69 DerivedArgList *DAL =
70 HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
71 if (!DAL)
72 DAL = new DerivedArgList(Args.getBaseArgs());
73
74 const OptTable &Opts = getDriver().getOpts();
75
76 if (DeviceOffloadKind == Action::OFK_OpenMP) {
77 for (Arg *A : Args)
78 if (!llvm::is_contained(*DAL, A))
79 DAL->append(A);
80
81 if (!DAL->hasArg(options::OPT_march_EQ)) {
82 StringRef Arch = BoundArch;
83 if (Arch.empty()) {
84 auto ArchsOrErr = getSystemGPUArchs(Args);
85 if (!ArchsOrErr) {
86 std::string ErrMsg =
87 llvm::formatv("{0}", llvm::fmt_consume(ArchsOrErr.takeError()));
88 getDriver().Diag(diag::err_drv_undetermined_gpu_arch)
89 << llvm::Triple::getArchTypeName(getArch()) << ErrMsg << "-march";
91 } else {
92 Arch = Args.MakeArgString(ArchsOrErr->front());
93 }
94 }
95 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), Arch);
96 }
97
98 return DAL;
99 }
100
101 for (Arg *A : Args) {
102 DAL->append(A);
103 }
104
105 if (!BoundArch.empty()) {
106 DAL->eraseArg(options::OPT_march_EQ);
107 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
108 BoundArch);
109 }
110
111 return DAL;
112}
113
115 ArgStringList &CC1Args) const {
118}
119
121AMDGPUOpenMPToolChain::GetCXXStdlibType(const ArgList &Args) const {
122 return HostTC.GetCXXStdlibType(Args);
123}
124
126 const ArgList &DriverArgs, ArgStringList &CC1Args) const {
127 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
128}
129
131 ArgStringList &CC1Args) const {
132 HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
133}
134
136 // The AMDGPUOpenMPToolChain only supports sanitizers in the sense that it
137 // allows sanitizer arguments on the command line if they are supported by the
138 // host toolchain. The AMDGPUOpenMPToolChain will actually ignore any command
139 // line arguments for any of these "supported" sanitizers. That means that no
140 // sanitization of device code is actually supported at this time.
141 //
142 // This behavior is necessary because the host and device toolchains
143 // invocations often share the command line, so the device toolchain must
144 // tolerate flags meant only for the host toolchain.
146}
147
148VersionTuple
150 const ArgList &Args) const {
151 return HostTC.computeMSVCVersion(D, Args);
152}
153
155AMDGPUOpenMPToolChain::getDeviceLibs(const llvm::opt::ArgList &Args) const {
156 if (Args.hasArg(options::OPT_nogpulib))
157 return {};
158
159 if (!RocmInstallation->hasDeviceLibrary()) {
160 getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
161 return {};
162 }
163
164 StringRef GpuArch = getProcessorFromTargetID(
165 getTriple(), Args.getLastArgValue(options::OPT_march_EQ));
166
168 for (auto BCLib : getCommonDeviceLibNames(Args, GpuArch.str(),
169 /*IsOpenMP=*/true))
170 BCLibs.emplace_back(BCLib);
171
172 return BCLibs;
173}
const Decl * D
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:144
const llvm::opt::OptTable & getOpts() const
Definition: Driver.h:399
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:92
virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const
Add warning options that need to be passed to cc1 for this target.
Definition: ToolChain.cpp:1081
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:269
const Driver & getDriver() const
Definition: ToolChain.h:253
path_list & getProgramPaths()
Definition: ToolChain.h:298
virtual llvm::opt::DerivedArgList * TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, Action::OffloadKind DeviceOffloadKind) const
TranslateArgs - Create a new derived argument list for any argument translations this ToolChain may w...
Definition: ToolChain.h:359
const llvm::Triple & getTriple() const
Definition: ToolChain.h:255
virtual VersionTuple computeMSVCVersion(const Driver *D, const llvm::opt::ArgList &Args) const
On Windows, returns the MSVC compatibility version.
Definition: ToolChain.cpp:1423
virtual void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Add arguments to use MCU GCC toolchain includes.
Definition: ToolChain.cpp:1406
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:1153
virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const
Add options that need to be passed to cc1 for this target.
Definition: ToolChain.cpp:1074
virtual void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Add the clang cc1 arguments for system include paths.
Definition: ToolChain.cpp:1069
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:1370
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override
Common warning options shared by AMDGPU HIP, OpenCL and OpenMP toolchains.
void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use MCU GCC toolchain includes.
llvm::opt::DerivedArgList * TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, Action::OffloadKind DeviceOffloadKind) const override
TranslateArgs - Create a new derived argument list for any argument translations this ToolChain may w...
llvm::SmallVector< BitCodeLibraryInfo, 12 > getDeviceLibs(const llvm::opt::ArgList &Args) const override
Get paths for device libraries.
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
VersionTuple computeMSVCVersion(const Driver *D, const llvm::opt::ArgList &Args) const override
On Windows, returns the MSVC compatibility version.
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
AMDGPUOpenMPToolChain(const Driver &D, const llvm::Triple &Triple, const ToolChain &HostTC, const llvm::opt::ArgList &Args)
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override
Common warning options shared by AMDGPU HIP, OpenCL and OpenMP toolchains.
Definition: AMDGPU.cpp:841
virtual Expected< SmallVector< std::string > > getSystemGPUArchs(const llvm::opt::ArgList &Args) const override
Uses amdgpu-arch tool to get arch of the system GPU.
Definition: AMDGPU.cpp:877
LazyDetector< RocmInstallationDetector > RocmInstallation
Definition: Gnu.h:290
llvm::SmallVector< std::string, 12 > getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs, const std::string &GPUArch, bool isOpenMP=false) const
Definition: AMDGPU.cpp:1005
The JSON file list parser is used to communicate input to InstallAPI.
llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T, llvm::StringRef OffloadArch)
Get processor name from target ID.
Definition: TargetID.cpp:54
const char * OffloadArchToString(OffloadArch A)
Definition: Cuda.cpp:157