1 //===--- HIPAMD.cpp - HIP Tool and ToolChain Implementations ----*- 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 "HIPAMD.h"
10 #include "AMDGPU.h"
11 #include "CommonArgs.h"
12 #include "HIPUtility.h"
13 #include "SPIRV.h"
14 #include "clang/Basic/Cuda.h"
15 #include "clang/Basic/TargetID.h"
16 #include "clang/Driver/Compilation.h"
17 #include "clang/Driver/Driver.h"
18 #include "clang/Driver/DriverDiagnostic.h"
19 #include "clang/Driver/InputInfo.h"
20 #include "clang/Driver/Options.h"
21 #include "clang/Driver/SanitizerArgs.h"
22 #include "llvm/Support/Alignment.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/TargetParser/TargetParser.h"
26
27 using namespace clang::driver;
28 using namespace clang::driver::toolchains;
29 using namespace clang::driver::tools;
30 using namespace clang;
31 using namespace llvm::opt;
32
33 #if defined(_WIN32) || defined(_WIN64)
34 #define NULL_FILE "nul"
35 #else
36 #define NULL_FILE "/dev/null"
37 #endif
38
shouldSkipSanitizeOption(const ToolChain & TC,const llvm::opt::ArgList & DriverArgs,StringRef TargetID,const llvm::opt::Arg * A)39 static bool shouldSkipSanitizeOption(const ToolChain &TC,
40 const llvm::opt::ArgList &DriverArgs,
41 StringRef TargetID,
42 const llvm::opt::Arg *A) {
43 // For actions without targetID, do nothing.
44 if (TargetID.empty())
45 return false;
46 Option O = A->getOption();
47 if (!O.matches(options::OPT_fsanitize_EQ))
48 return false;
49
50 if (!DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
51 options::OPT_fno_gpu_sanitize, true))
52 return true;
53
54 auto &Diags = TC.getDriver().getDiags();
55
56 // For simplicity, we only allow -fsanitize=address
57 SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
58 if (K != SanitizerKind::Address)
59 return true;
60
61 llvm::StringMap<bool> FeatureMap;
62 auto OptionalGpuArch = parseTargetID(TC.getTriple(), TargetID, &FeatureMap);
63
64 assert(OptionalGpuArch && "Invalid Target ID");
65 (void)OptionalGpuArch;
66 auto Loc = FeatureMap.find("xnack");
67 if (Loc == FeatureMap.end() || !Loc->second) {
68 Diags.Report(
69 clang::diag::warn_drv_unsupported_option_for_offload_arch_req_feature)
70 << A->getAsString(DriverArgs) << TargetID << "xnack+";
71 return true;
72 }
73 return false;
74 }
75
constructLlvmLinkCommand(Compilation & C,const JobAction & JA,const InputInfoList & Inputs,const InputInfo & Output,const llvm::opt::ArgList & Args) const76 void AMDGCN::Linker::constructLlvmLinkCommand(Compilation &C,
77 const JobAction &JA,
78 const InputInfoList &Inputs,
79 const InputInfo &Output,
80 const llvm::opt::ArgList &Args) const {
81 // Construct llvm-link command.
82 // The output from llvm-link is a bitcode file.
83 ArgStringList LlvmLinkArgs;
84
85 assert(!Inputs.empty() && "Must have at least one input.");
86
87 LlvmLinkArgs.append({"-o", Output.getFilename()});
88 for (auto Input : Inputs)
89 LlvmLinkArgs.push_back(Input.getFilename());
90
91 // Look for archive of bundled bitcode in arguments, and add temporary files
92 // for the extracted archive of bitcode to inputs.
93 auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
94 AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LlvmLinkArgs, "amdgcn",
95 TargetID, /*IsBitCodeSDL=*/true);
96
97 const char *LlvmLink =
98 Args.MakeArgString(getToolChain().GetProgramPath("llvm-link"));
99 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
100 LlvmLink, LlvmLinkArgs, Inputs,
101 Output));
102 }
103
constructLldCommand(Compilation & C,const JobAction & JA,const InputInfoList & Inputs,const InputInfo & Output,const llvm::opt::ArgList & Args) const104 void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
105 const InputInfoList &Inputs,
106 const InputInfo &Output,
107 const llvm::opt::ArgList &Args) const {
108 // Construct lld command.
109 // The output from ld.lld is an HSA code object file.
110 ArgStringList LldArgs{"-flavor",
111 "gnu",
112 "-m",
113 "elf64_amdgpu",
114 "--no-undefined",
115 "-shared",
116 "-plugin-opt=-amdgpu-internalize-symbols"};
117 if (Args.hasArg(options::OPT_hipstdpar))
118 LldArgs.push_back("-plugin-opt=-amdgpu-enable-hipstdpar");
119
120 auto &TC = getToolChain();
121 auto &D = TC.getDriver();
122 assert(!Inputs.empty() && "Must have at least one input.");
123 bool IsThinLTO = D.getLTOMode(/*IsOffload=*/true) == LTOK_Thin;
124 addLTOOptions(TC, Args, LldArgs, Output, Inputs[0], IsThinLTO);
125
126 // Extract all the -m options
127 std::vector<llvm::StringRef> Features;
128 amdgpu::getAMDGPUTargetFeatures(D, TC.getTriple(), Args, Features);
129
130 // Add features to mattr such as cumode
131 std::string MAttrString = "-plugin-opt=-mattr=";
132 for (auto OneFeature : unifyTargetFeatures(Features)) {
133 MAttrString.append(Args.MakeArgString(OneFeature));
134 if (OneFeature != Features.back())
135 MAttrString.append(",");
136 }
137 if (!Features.empty())
138 LldArgs.push_back(Args.MakeArgString(MAttrString));
139
140 // ToDo: Remove this option after AMDGPU backend supports ISA-level linking.
141 // Since AMDGPU backend currently does not support ISA-level linking, all
142 // called functions need to be imported.
143 if (IsThinLTO)
144 LldArgs.push_back(Args.MakeArgString("-plugin-opt=-force-import-all"));
145
146 for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
147 LldArgs.push_back(
148 Args.MakeArgString(Twine("-plugin-opt=") + A->getValue(0)));
149 }
150
151 if (C.getDriver().isSaveTempsEnabled())
152 LldArgs.push_back("-save-temps");
153
154 addLinkerCompressDebugSectionsOption(TC, Args, LldArgs);
155
156 // Given that host and device linking happen in separate processes, the device
157 // linker doesn't always have the visibility as to which device symbols are
158 // needed by a program, especially for the device symbol dependencies that are
159 // introduced through the host symbol resolution.
160 // For example: host_A() (A.obj) --> host_B(B.obj) --> device_kernel_B()
161 // (B.obj) In this case, the device linker doesn't know that A.obj actually
162 // depends on the kernel functions in B.obj. When linking to static device
163 // library, the device linker may drop some of the device global symbols if
164 // they aren't referenced. As a workaround, we are adding to the
165 // --whole-archive flag such that all global symbols would be linked in.
166 LldArgs.push_back("--whole-archive");
167
168 for (auto *Arg : Args.filtered(options::OPT_Xoffload_linker)) {
169 StringRef ArgVal = Arg->getValue(1);
170 auto SplitArg = ArgVal.split("-mllvm=");
171 if (!SplitArg.second.empty()) {
172 LldArgs.push_back(
173 Args.MakeArgString(Twine("-plugin-opt=") + SplitArg.second));
174 } else {
175 LldArgs.push_back(Args.MakeArgString(ArgVal));
176 }
177 Arg->claim();
178 }
179
180 LldArgs.append({"-o", Output.getFilename()});
181 for (auto Input : Inputs)
182 LldArgs.push_back(Input.getFilename());
183
184 // Look for archive of bundled bitcode in arguments, and add temporary files
185 // for the extracted archive of bitcode to inputs.
186 auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
187 AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LldArgs, "amdgcn",
188 TargetID, /*IsBitCodeSDL=*/true);
189
190 LldArgs.push_back("--no-whole-archive");
191
192 const char *Lld = Args.MakeArgString(getToolChain().GetProgramPath("lld"));
193 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
194 Lld, LldArgs, Inputs, Output));
195 }
196
197 // For SPIR-V the inputs for the job are device AMDGCN SPIR-V flavoured bitcode
198 // and the output is either a compiled SPIR-V binary or bitcode (-emit-llvm). It
199 // calls llvm-link and then the llvm-spirv translator. Once the SPIR-V BE will
200 // be promoted from experimental, we will switch to using that. TODO: consider
201 // if we want to run any targeted optimisations over IR here, over generic
202 // SPIR-V.
constructLinkAndEmitSpirvCommand(Compilation & C,const JobAction & JA,const InputInfoList & Inputs,const InputInfo & Output,const llvm::opt::ArgList & Args) const203 void AMDGCN::Linker::constructLinkAndEmitSpirvCommand(
204 Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
205 const InputInfo &Output, const llvm::opt::ArgList &Args) const {
206 assert(!Inputs.empty() && "Must have at least one input.");
207
208 constructLlvmLinkCommand(C, JA, Inputs, Output, Args);
209
210 // Linked BC is now in Output
211
212 // Emit SPIR-V binary.
213 llvm::opt::ArgStringList TrArgs{
214 "--spirv-max-version=1.6",
215 "--spirv-ext=+all",
216 "--spirv-allow-extra-diexpressions",
217 "--spirv-allow-unknown-intrinsics",
218 "--spirv-lower-const-expr",
219 "--spirv-preserve-auxdata",
220 "--spirv-debug-info-version=nonsemantic-shader-200"};
221 SPIRV::constructTranslateCommand(C, *this, JA, Output, Output, TrArgs);
222 }
223
224 // For amdgcn the inputs of the linker job are device bitcode and output is
225 // either an object file or bitcode (-emit-llvm). It calls llvm-link, opt,
226 // llc, then lld steps.
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const227 void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
228 const InputInfo &Output,
229 const InputInfoList &Inputs,
230 const ArgList &Args,
231 const char *LinkingOutput) const {
232 if (Inputs.size() > 0 &&
233 Inputs[0].getType() == types::TY_Image &&
234 JA.getType() == types::TY_Object)
235 return HIP::constructGenerateObjFileFromHIPFatBinary(C, Output, Inputs,
236 Args, JA, *this);
237
238 if (JA.getType() == types::TY_HIP_FATBIN)
239 return HIP::constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs,
240 Args, *this);
241
242 if (JA.getType() == types::TY_LLVM_BC)
243 return constructLlvmLinkCommand(C, JA, Inputs, Output, Args);
244
245 if (getToolChain().getTriple().isSPIRV())
246 return constructLinkAndEmitSpirvCommand(C, JA, Inputs, Output, Args);
247
248 return constructLldCommand(C, JA, Inputs, Output, Args);
249 }
250
HIPAMDToolChain(const Driver & D,const llvm::Triple & Triple,const ToolChain & HostTC,const ArgList & Args)251 HIPAMDToolChain::HIPAMDToolChain(const Driver &D, const llvm::Triple &Triple,
252 const ToolChain &HostTC, const ArgList &Args)
253 : ROCMToolChain(D, Triple, Args), HostTC(HostTC) {
254 // Lookup binaries into the driver directory, this is used to
255 // discover the clang-offload-bundler executable.
256 getProgramPaths().push_back(getDriver().Dir);
257
258 // Diagnose unsupported sanitizer options only once.
259 if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize,
260 true))
261 return;
262 for (auto *A : Args.filtered(options::OPT_fsanitize_EQ)) {
263 SanitizerMask K = parseSanitizerValue(A->getValue(), /*AllowGroups=*/false);
264 if (K != SanitizerKind::Address)
265 D.getDiags().Report(clang::diag::warn_drv_unsupported_option_for_target)
266 << A->getAsString(Args) << getTriple().str();
267 }
268 }
269
addClangTargetOptions(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args,Action::OffloadKind DeviceOffloadingKind) const270 void HIPAMDToolChain::addClangTargetOptions(
271 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
272 Action::OffloadKind DeviceOffloadingKind) const {
273 HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
274
275 assert(DeviceOffloadingKind == Action::OFK_HIP &&
276 "Only HIP offloading kinds are supported for GPUs.");
277
278 CC1Args.push_back("-fcuda-is-device");
279
280 if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
281 false))
282 CC1Args.append({"-mllvm", "-amdgpu-internalize-symbols"});
283 if (DriverArgs.hasArgNoClaim(options::OPT_hipstdpar))
284 CC1Args.append({"-mllvm", "-amdgpu-enable-hipstdpar"});
285
286 StringRef MaxThreadsPerBlock =
287 DriverArgs.getLastArgValue(options::OPT_gpu_max_threads_per_block_EQ);
288 if (!MaxThreadsPerBlock.empty()) {
289 std::string ArgStr =
290 (Twine("--gpu-max-threads-per-block=") + MaxThreadsPerBlock).str();
291 CC1Args.push_back(DriverArgs.MakeArgStringRef(ArgStr));
292 }
293
294 CC1Args.push_back("-fcuda-allow-variadic-functions");
295
296 // Default to "hidden" visibility, as object level linking will not be
297 // supported for the foreseeable future.
298 if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
299 options::OPT_fvisibility_ms_compat)) {
300 CC1Args.append({"-fvisibility=hidden"});
301 CC1Args.push_back("-fapply-global-visibility-to-externs");
302 }
303
304 // For SPIR-V we embed the command-line into the generated binary, in order to
305 // retrieve it at JIT time and be able to do target specific compilation with
306 // options that match the user-supplied ones.
307 if (getTriple().isSPIRV() &&
308 !DriverArgs.hasArg(options::OPT_fembed_bitcode_marker))
309 CC1Args.push_back("-fembed-bitcode=marker");
310
311 for (auto BCFile : getDeviceLibs(DriverArgs)) {
312 CC1Args.push_back(BCFile.ShouldInternalize ? "-mlink-builtin-bitcode"
313 : "-mlink-bitcode-file");
314 CC1Args.push_back(DriverArgs.MakeArgString(BCFile.Path));
315 }
316 }
317
318 llvm::opt::DerivedArgList *
TranslateArgs(const llvm::opt::DerivedArgList & Args,StringRef BoundArch,Action::OffloadKind DeviceOffloadKind) const319 HIPAMDToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
320 StringRef BoundArch,
321 Action::OffloadKind DeviceOffloadKind) const {
322 DerivedArgList *DAL =
323 HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
324 if (!DAL)
325 DAL = new DerivedArgList(Args.getBaseArgs());
326
327 const OptTable &Opts = getDriver().getOpts();
328
329 for (Arg *A : Args) {
330 if (!shouldSkipSanitizeOption(*this, Args, BoundArch, A))
331 DAL->append(A);
332 }
333
334 if (!BoundArch.empty()) {
335 DAL->eraseArg(options::OPT_mcpu_EQ);
336 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mcpu_EQ), BoundArch);
337 checkTargetID(*DAL);
338 }
339
340 return DAL;
341 }
342
buildLinker() const343 Tool *HIPAMDToolChain::buildLinker() const {
344 assert(getTriple().getArch() == llvm::Triple::amdgcn ||
345 getTriple().getArch() == llvm::Triple::spirv64);
346 return new tools::AMDGCN::Linker(*this);
347 }
348
addClangWarningOptions(ArgStringList & CC1Args) const349 void HIPAMDToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
350 AMDGPUToolChain::addClangWarningOptions(CC1Args);
351 HostTC.addClangWarningOptions(CC1Args);
352 }
353
354 ToolChain::CXXStdlibType
GetCXXStdlibType(const ArgList & Args) const355 HIPAMDToolChain::GetCXXStdlibType(const ArgList &Args) const {
356 return HostTC.GetCXXStdlibType(Args);
357 }
358
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const359 void HIPAMDToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
360 ArgStringList &CC1Args) const {
361 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
362 }
363
AddClangCXXStdlibIncludeArgs(const ArgList & Args,ArgStringList & CC1Args) const364 void HIPAMDToolChain::AddClangCXXStdlibIncludeArgs(
365 const ArgList &Args, ArgStringList &CC1Args) const {
366 HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
367 }
368
AddIAMCUIncludeArgs(const ArgList & Args,ArgStringList & CC1Args) const369 void HIPAMDToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
370 ArgStringList &CC1Args) const {
371 HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
372 }
373
AddHIPIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const374 void HIPAMDToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
375 ArgStringList &CC1Args) const {
376 RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
377 }
378
getSupportedSanitizers() const379 SanitizerMask HIPAMDToolChain::getSupportedSanitizers() const {
380 // The HIPAMDToolChain only supports sanitizers in the sense that it allows
381 // sanitizer arguments on the command line if they are supported by the host
382 // toolchain. The HIPAMDToolChain will actually ignore any command line
383 // arguments for any of these "supported" sanitizers. That means that no
384 // sanitization of device code is actually supported at this time.
385 //
386 // This behavior is necessary because the host and device toolchains
387 // invocations often share the command line, so the device toolchain must
388 // tolerate flags meant only for the host toolchain.
389 return HostTC.getSupportedSanitizers();
390 }
391
computeMSVCVersion(const Driver * D,const ArgList & Args) const392 VersionTuple HIPAMDToolChain::computeMSVCVersion(const Driver *D,
393 const ArgList &Args) const {
394 return HostTC.computeMSVCVersion(D, Args);
395 }
396
397 llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
getDeviceLibs(const llvm::opt::ArgList & DriverArgs) const398 HIPAMDToolChain::getDeviceLibs(const llvm::opt::ArgList &DriverArgs) const {
399 llvm::SmallVector<BitCodeLibraryInfo, 12> BCLibs;
400 if (DriverArgs.hasArg(options::OPT_nogpulib) ||
401 (getTriple().getArch() == llvm::Triple::spirv64 &&
402 getTriple().getVendor() == llvm::Triple::AMD))
403 return {};
404 ArgStringList LibraryPaths;
405
406 // Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
407 for (StringRef Path : RocmInstallation->getRocmDeviceLibPathArg())
408 LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
409
410 addDirectoryList(DriverArgs, LibraryPaths, "", "HIP_DEVICE_LIB_PATH");
411
412 // Maintain compatability with --hip-device-lib.
413 auto BCLibArgs = DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ);
414 if (!BCLibArgs.empty()) {
415 llvm::for_each(BCLibArgs, [&](StringRef BCName) {
416 StringRef FullName;
417 for (StringRef LibraryPath : LibraryPaths) {
418 SmallString<128> Path(LibraryPath);
419 llvm::sys::path::append(Path, BCName);
420 FullName = Path;
421 if (llvm::sys::fs::exists(FullName)) {
422 BCLibs.push_back(FullName);
423 return;
424 }
425 }
426 getDriver().Diag(diag::err_drv_no_such_file) << BCName;
427 });
428 } else {
429 if (!RocmInstallation->hasDeviceLibrary()) {
430 getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
431 return {};
432 }
433 StringRef GpuArch = getGPUArch(DriverArgs);
434 assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
435
436 // If --hip-device-lib is not set, add the default bitcode libraries.
437 if (DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
438 options::OPT_fno_gpu_sanitize, true) &&
439 getSanitizerArgs(DriverArgs).needsAsanRt()) {
440 auto AsanRTL = RocmInstallation->getAsanRTLPath();
441 if (AsanRTL.empty()) {
442 unsigned DiagID = getDriver().getDiags().getCustomDiagID(
443 DiagnosticsEngine::Error,
444 "AMDGPU address sanitizer runtime library (asanrtl) is not found. "
445 "Please install ROCm device library which supports address "
446 "sanitizer");
447 getDriver().Diag(DiagID);
448 return {};
449 } else
450 BCLibs.emplace_back(AsanRTL, /*ShouldInternalize=*/false);
451 }
452
453 // Add the HIP specific bitcode library.
454 BCLibs.push_back(RocmInstallation->getHIPPath());
455
456 // Add common device libraries like ocml etc.
457 for (StringRef N : getCommonDeviceLibNames(DriverArgs, GpuArch.str()))
458 BCLibs.emplace_back(N);
459
460 // Add instrument lib.
461 auto InstLib =
462 DriverArgs.getLastArgValue(options::OPT_gpu_instrument_lib_EQ);
463 if (InstLib.empty())
464 return BCLibs;
465 if (llvm::sys::fs::exists(InstLib))
466 BCLibs.push_back(InstLib);
467 else
468 getDriver().Diag(diag::err_drv_no_such_file) << InstLib;
469 }
470
471 return BCLibs;
472 }
473
checkTargetID(const llvm::opt::ArgList & DriverArgs) const474 void HIPAMDToolChain::checkTargetID(
475 const llvm::opt::ArgList &DriverArgs) const {
476 auto PTID = getParsedTargetID(DriverArgs);
477 if (PTID.OptionalTargetID && !PTID.OptionalGPUArch) {
478 getDriver().Diag(clang::diag::err_drv_bad_target_id)
479 << *PTID.OptionalTargetID;
480 }
481 }
482