clang  16.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"
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 
26 using namespace clang::driver;
27 using namespace clang::driver::toolchains;
28 using namespace clang::driver::tools;
29 using namespace clang;
30 using namespace llvm::opt;
31 
32 namespace {
33 
34 static bool checkSystemForAMDGPU(const ArgList &Args, const AMDGPUToolChain &TC,
35  std::string &GPUArch) {
36  if (auto Err = TC.getSystemGPUArch(Args, GPUArch)) {
37  std::string ErrMsg =
38  llvm::formatv("{0}", llvm::fmt_consume(std::move(Err)));
39  TC.getDriver().Diag(diag::err_drv_undetermined_amdgpu_arch) << ErrMsg;
40  return false;
41  }
42 
43  return true;
44 }
45 } // namespace
46 
48  const llvm::Triple &Triple,
49  const ToolChain &HostTC,
50  const ArgList &Args)
51  : ROCMToolChain(D, Triple, Args), HostTC(HostTC) {
52  // Lookup binaries into the driver directory, this is used to
53  // discover the clang-offload-bundler executable.
54  getProgramPaths().push_back(getDriver().Dir);
55 }
56 
58  const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
59  Action::OffloadKind DeviceOffloadingKind) const {
60  HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
61 
62  std::string GPUArch = DriverArgs.getLastArgValue(options::OPT_march_EQ).str();
63  if (GPUArch.empty()) {
64  if (!checkSystemForAMDGPU(DriverArgs, *this, GPUArch))
65  return;
66  }
67 
68  assert(DeviceOffloadingKind == Action::OFK_OpenMP &&
69  "Only OpenMP offloading kinds are supported.");
70 
71  CC1Args.push_back("-target-cpu");
72  CC1Args.push_back(DriverArgs.MakeArgStringRef(GPUArch));
73  CC1Args.push_back("-fcuda-is-device");
74 
75  if (DriverArgs.hasArg(options::OPT_nogpulib))
76  return;
77 
78  for (auto BCFile : getDeviceLibs(DriverArgs)) {
79  CC1Args.push_back(BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
80  : "-mlink-bitcode-file");
81  CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path));
82  }
83 
84  // Link the bitcode library late if we're using device LTO.
85  if (getDriver().isUsingLTO(/* IsOffload */ true))
86  return;
87 
88  addOpenMPDeviceRTL(getDriver(), DriverArgs, CC1Args, GPUArch, getTriple());
89 }
90 
91 llvm::opt::DerivedArgList *AMDGPUOpenMPToolChain::TranslateArgs(
92  const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
93  Action::OffloadKind DeviceOffloadKind) const {
94  DerivedArgList *DAL =
95  HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
96  if (!DAL)
97  DAL = new DerivedArgList(Args.getBaseArgs());
98 
99  const OptTable &Opts = getDriver().getOpts();
100 
101  if (DeviceOffloadKind == Action::OFK_OpenMP) {
102  for (Arg *A : Args)
103  if (!llvm::is_contained(*DAL, A))
104  DAL->append(A);
105 
106  if (!DAL->hasArg(options::OPT_march_EQ)) {
107  std::string Arch = BoundArch.str();
108  if (BoundArch.empty())
109  checkSystemForAMDGPU(Args, *this, Arch);
110  DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), Arch);
111  }
112 
113  return DAL;
114  }
115 
116  for (Arg *A : Args) {
117  DAL->append(A);
118  }
119 
120  if (!BoundArch.empty()) {
121  DAL->eraseArg(options::OPT_march_EQ);
122  DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
123  BoundArch);
124  }
125 
126  return DAL;
127 }
128 
130  ArgStringList &CC1Args) const {
132 }
133 
135 AMDGPUOpenMPToolChain::GetCXXStdlibType(const ArgList &Args) const {
136  return HostTC.GetCXXStdlibType(Args);
137 }
138 
140  const ArgList &DriverArgs, ArgStringList &CC1Args) const {
141  HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
142 }
143 
145  ArgStringList &CC1Args) const {
146  HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
147 }
148 
150  // The AMDGPUOpenMPToolChain only supports sanitizers in the sense that it
151  // allows sanitizer arguments on the command line if they are supported by the
152  // host toolchain. The AMDGPUOpenMPToolChain will actually ignore any command
153  // line arguments for any of these "supported" sanitizers. That means that no
154  // sanitization of device code is actually supported at this time.
155  //
156  // This behavior is necessary because the host and device toolchains
157  // invocations often share the command line, so the device toolchain must
158  // tolerate flags meant only for the host toolchain.
160 }
161 
162 VersionTuple
164  const ArgList &Args) const {
165  return HostTC.computeMSVCVersion(D, Args);
166 }
167 
169 AMDGPUOpenMPToolChain::getDeviceLibs(const llvm::opt::ArgList &Args) const {
170  if (Args.hasArg(options::OPT_nogpulib))
171  return {};
172 
174  getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
175  return {};
176  }
177 
178  StringRef GpuArch = getProcessorFromTargetID(
179  getTriple(), Args.getLastArgValue(options::OPT_march_EQ));
180 
182  for (auto BCLib : getCommonDeviceLibNames(Args, GpuArch.str(),
183  /*IsOpenMP=*/true))
184  BCLibs.emplace_back(BCLib);
185 
186  return BCLibs;
187 }
clang::driver::toolchains
Definition: AIX.h:55
clang::driver::ToolChain::getProgramPaths
path_list & getProgramPaths()
Definition: ToolChain.h:277
Driver.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::driver::toolchains::AMDGPUOpenMPToolChain::AddIAMCUIncludeArgs
void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use MCU GCC toolchain includes.
Definition: AMDGPUOpenMP.cpp:144
clang::driver::toolchains::AMDGPUOpenMPToolChain::TranslateArgs
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...
Definition: AMDGPUOpenMP.cpp:91
clang::driver::ToolChain::computeMSVCVersion
virtual VersionTuple computeMSVCVersion(const Driver *D, const llvm::opt::ArgList &Args) const
On Windows, returns the MSVC compatibility version.
Definition: ToolChain.cpp:1142
AMDGPUOpenMP.h
llvm::SmallVector
Definition: LLVM.h:38
clang::driver::toolchains::AMDGPUOpenMPToolChain::GetCXXStdlibType
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition: AMDGPUOpenMP.cpp:135
clang::driver::tools
Definition: AIX.h:17
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:232
clang::driver::ToolChain::addClangWarningOptions
virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const
Add warning options that need to be passed to cc1 for this target.
Definition: ToolChain.cpp:821
clang::driver::Driver::getOpts
const llvm::opt::OptTable & getOpts() const
Definition: Driver.h:365
clang::driver::Action::OFK_OpenMP
@ OFK_OpenMP
Definition: Action.h:94
InputInfo.h
clang::driver::toolchains::AMDGPUOpenMPToolChain::getDeviceLibs
llvm::SmallVector< BitCodeLibraryInfo, 12 > getDeviceLibs(const llvm::opt::ArgList &Args) const override
Get paths for device libraries.
Definition: AMDGPUOpenMP.cpp:169
clang::driver::Driver::Diag
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:142
DiagnosticDriver.h
Options.h
clang::driver::toolchains::AMDGPUOpenMPToolChain::AMDGPUOpenMPToolChain
AMDGPUOpenMPToolChain(const Driver &D, const llvm::Triple &Triple, const ToolChain &HostTC, const llvm::opt::ArgList &Args)
Definition: AMDGPUOpenMP.cpp:47
llvm::opt
Definition: DiagnosticOptions.h:19
clang::driver::toolchains::AMDGPUOpenMPToolChain::addClangWarningOptions
void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override
Add warning options that need to be passed to cc1 for this target.
Definition: AMDGPUOpenMP.cpp:129
DriverDiagnostic.h
clang::driver::ToolChain::AddClangSystemIncludeArgs
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:809
clang::driver::ToolChain::CXXStdlibType
CXXStdlibType
Definition: ToolChain.h:95
clang::getProcessorFromTargetID
llvm::StringRef getProcessorFromTargetID(const llvm::Triple &T, llvm::StringRef OffloadArch)
Get processor name from target ID.
Definition: TargetID.cpp:53
Tool.h
clang::driver::ToolChain::getTriple
const llvm::Triple & getTriple() const
Definition: ToolChain.h:234
AMDGPU.h
clang::driver::toolchains::AMDGPUOpenMPToolChain::getSupportedSanitizers
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: AMDGPUOpenMP.cpp:149
clang::driver::RocmInstallationDetector::hasDeviceLibrary
bool hasDeviceLibrary() const
Check whether we detected a valid ROCm device library.
Definition: ROCm.h:181
clang::driver::tools::addOpenMPDeviceRTL
void addOpenMPDeviceRTL(const Driver &D, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, StringRef BitcodeSuffix, const llvm::Triple &Triple)
Definition: CommonArgs.cpp:2299
clang::driver::toolchains::Generic_GCC::RocmInstallation
RocmInstallationDetector RocmInstallation
Definition: Gnu.h:290
clang::driver::ToolChain::getSupportedSanitizers
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:1089
ROCm.h
clang::driver::toolchains::AMDGPUOpenMPToolChain::addClangTargetOptions
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.
Definition: AMDGPUOpenMP.cpp:57
Compilation.h
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
clang::driver::toolchains::ROCMToolChain::getCommonDeviceLibNames
llvm::SmallVector< std::string, 12 > getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs, const std::string &GPUArch, bool isOpenMP=false) const
Definition: AMDGPU.cpp:943
clang::driver::toolchains::AMDGPUOpenMPToolChain::AddClangSystemIncludeArgs
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: AMDGPUOpenMP.cpp:139
clang::driver::ToolChain::GetCXXStdlibType
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:893
clang::driver::ToolChain::AddIAMCUIncludeArgs
virtual void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Add arguments to use MCU GCC toolchain includes.
Definition: ToolChain.cpp:1125
CommonArgs.h
clang
Definition: CalledOnceCheck.h:17
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:75
clang::driver::ToolChain::TranslateArgs
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:321
clang::driver::toolchains::ROCMToolChain
Definition: AMDGPU.h:133
clang::driver::ToolChain::addClangTargetOptions
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:814
clang::driver::toolchains::AMDGPUOpenMPToolChain::computeMSVCVersion
VersionTuple computeMSVCVersion(const Driver *D, const llvm::opt::ArgList &Args) const override
On Windows, returns the MSVC compatibility version.
Definition: AMDGPUOpenMP.cpp:163
clang::driver::Action::OffloadKind
OffloadKind
Definition: Action.h:86
clang::driver
Definition: Action.h:31
clang::driver::toolchains::AMDGPUToolChain::getSystemGPUArch
llvm::Error getSystemGPUArch(const llvm::opt::ArgList &Args, std::string &GPUArch) const
Uses amdgpu_arch tool to get arch of the system GPU.
Definition: AMDGPU.cpp:817
clang::driver::toolchains::AMDGPUOpenMPToolChain::HostTC
const ToolChain & HostTC
Definition: AMDGPUOpenMP.h:60
clang::SanitizerMask
Definition: Sanitizers.h:30
clang::driver::toolchains::AMDGPUToolChain
Definition: AMDGPU.h:49