1 //===--- AMDGPU.cpp - AMDGPU 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 "AMDGPU.h"
10 #include "CommonArgs.h"
11 #include "InputInfo.h"
12 #include "clang/Basic/TargetID.h"
13 #include "clang/Driver/Compilation.h"
14 #include "clang/Driver/DriverDiagnostic.h"
15 #include "llvm/Option/ArgList.h"
16 #include "llvm/Support/Path.h"
17 #include "llvm/Support/VirtualFileSystem.h"
18
19 using namespace clang::driver;
20 using namespace clang::driver::tools;
21 using namespace clang::driver::toolchains;
22 using namespace clang;
23 using namespace llvm::opt;
24
scanLibDevicePath(llvm::StringRef Path)25 void RocmInstallationDetector::scanLibDevicePath(llvm::StringRef Path) {
26 assert(!Path.empty());
27
28 const StringRef Suffix(".bc");
29 const StringRef Suffix2(".amdgcn.bc");
30
31 std::error_code EC;
32 for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(Path, EC), LE;
33 !EC && LI != LE; LI = LI.increment(EC)) {
34 StringRef FilePath = LI->path();
35 StringRef FileName = llvm::sys::path::filename(FilePath);
36 if (!FileName.endswith(Suffix))
37 continue;
38
39 StringRef BaseName;
40 if (FileName.endswith(Suffix2))
41 BaseName = FileName.drop_back(Suffix2.size());
42 else if (FileName.endswith(Suffix))
43 BaseName = FileName.drop_back(Suffix.size());
44
45 if (BaseName == "ocml") {
46 OCML = FilePath;
47 } else if (BaseName == "ockl") {
48 OCKL = FilePath;
49 } else if (BaseName == "opencl") {
50 OpenCL = FilePath;
51 } else if (BaseName == "hip") {
52 HIP = FilePath;
53 } else if (BaseName == "oclc_finite_only_off") {
54 FiniteOnly.Off = FilePath;
55 } else if (BaseName == "oclc_finite_only_on") {
56 FiniteOnly.On = FilePath;
57 } else if (BaseName == "oclc_daz_opt_on") {
58 DenormalsAreZero.On = FilePath;
59 } else if (BaseName == "oclc_daz_opt_off") {
60 DenormalsAreZero.Off = FilePath;
61 } else if (BaseName == "oclc_correctly_rounded_sqrt_on") {
62 CorrectlyRoundedSqrt.On = FilePath;
63 } else if (BaseName == "oclc_correctly_rounded_sqrt_off") {
64 CorrectlyRoundedSqrt.Off = FilePath;
65 } else if (BaseName == "oclc_unsafe_math_on") {
66 UnsafeMath.On = FilePath;
67 } else if (BaseName == "oclc_unsafe_math_off") {
68 UnsafeMath.Off = FilePath;
69 } else if (BaseName == "oclc_wavefrontsize64_on") {
70 WavefrontSize64.On = FilePath;
71 } else if (BaseName == "oclc_wavefrontsize64_off") {
72 WavefrontSize64.Off = FilePath;
73 } else {
74 // Process all bitcode filenames that look like
75 // ocl_isa_version_XXX.amdgcn.bc
76 const StringRef DeviceLibPrefix = "oclc_isa_version_";
77 if (!BaseName.startswith(DeviceLibPrefix))
78 continue;
79
80 StringRef IsaVersionNumber =
81 BaseName.drop_front(DeviceLibPrefix.size());
82
83 llvm::Twine GfxName = Twine("gfx") + IsaVersionNumber;
84 SmallString<8> Tmp;
85 LibDeviceMap.insert(
86 std::make_pair(GfxName.toStringRef(Tmp), FilePath.str()));
87 }
88 }
89 }
90
91 // Parse and extract version numbers from `.hipVersion`. Return `true` if
92 // the parsing fails.
parseHIPVersionFile(llvm::StringRef V)93 bool RocmInstallationDetector::parseHIPVersionFile(llvm::StringRef V) {
94 SmallVector<StringRef, 4> VersionParts;
95 V.split(VersionParts, '\n');
96 unsigned Major = ~0U;
97 unsigned Minor = ~0U;
98 for (auto Part : VersionParts) {
99 auto Splits = Part.rtrim().split('=');
100 if (Splits.first == "HIP_VERSION_MAJOR") {
101 if (Splits.second.getAsInteger(0, Major))
102 return true;
103 } else if (Splits.first == "HIP_VERSION_MINOR") {
104 if (Splits.second.getAsInteger(0, Minor))
105 return true;
106 } else if (Splits.first == "HIP_VERSION_PATCH")
107 VersionPatch = Splits.second.str();
108 }
109 if (Major == ~0U || Minor == ~0U)
110 return true;
111 VersionMajorMinor = llvm::VersionTuple(Major, Minor);
112 DetectedVersion =
113 (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str();
114 return false;
115 }
116
117 // For candidate specified by --rocm-path we do not do strict check.
118 SmallVector<RocmInstallationDetector::Candidate, 4>
getInstallationPathCandidates()119 RocmInstallationDetector::getInstallationPathCandidates() {
120 SmallVector<Candidate, 4> Candidates;
121 if (!RocmPathArg.empty()) {
122 Candidates.emplace_back(RocmPathArg.str());
123 return Candidates;
124 }
125
126 // Try to find relative to the compiler binary.
127 const char *InstallDir = D.getInstalledDir();
128
129 // Check both a normal Unix prefix position of the clang binary, as well as
130 // the Windows-esque layout the ROCm packages use with the host architecture
131 // subdirectory of bin.
132
133 // Strip off directory (usually bin)
134 StringRef ParentDir = llvm::sys::path::parent_path(InstallDir);
135 StringRef ParentName = llvm::sys::path::filename(ParentDir);
136
137 // Some builds use bin/{host arch}, so go up again.
138 if (ParentName == "bin") {
139 ParentDir = llvm::sys::path::parent_path(ParentDir);
140 ParentName = llvm::sys::path::filename(ParentDir);
141 }
142
143 // Some versions of the rocm llvm package install to /opt/rocm/llvm/bin
144 if (ParentName == "llvm")
145 ParentDir = llvm::sys::path::parent_path(ParentDir);
146
147 Candidates.emplace_back(ParentDir.str(), /*StrictChecking=*/true);
148
149 // Device library may be installed in clang resource directory.
150 Candidates.emplace_back(D.ResourceDir, /*StrictChecking=*/true);
151
152 Candidates.emplace_back(D.SysRoot + "/opt/rocm", /*StrictChecking=*/true);
153 return Candidates;
154 }
155
RocmInstallationDetector(const Driver & D,const llvm::Triple & HostTriple,const llvm::opt::ArgList & Args,bool DetectHIPRuntime,bool DetectDeviceLib)156 RocmInstallationDetector::RocmInstallationDetector(
157 const Driver &D, const llvm::Triple &HostTriple,
158 const llvm::opt::ArgList &Args, bool DetectHIPRuntime, bool DetectDeviceLib)
159 : D(D) {
160 RocmPathArg = Args.getLastArgValue(clang::driver::options::OPT_rocm_path_EQ);
161 RocmDeviceLibPathArg =
162 Args.getAllArgValues(clang::driver::options::OPT_rocm_device_lib_path_EQ);
163 if (auto *A = Args.getLastArg(clang::driver::options::OPT_hip_version_EQ)) {
164 HIPVersionArg = A->getValue();
165 unsigned Major = 0;
166 unsigned Minor = 0;
167 SmallVector<StringRef, 3> Parts;
168 HIPVersionArg.split(Parts, '.');
169 if (Parts.size())
170 Parts[0].getAsInteger(0, Major);
171 if (Parts.size() > 1)
172 Parts[1].getAsInteger(0, Minor);
173 if (Parts.size() > 2)
174 VersionPatch = Parts[2].str();
175 if (VersionPatch.empty())
176 VersionPatch = "0";
177 if (Major == 0 || Minor == 0)
178 D.Diag(diag::err_drv_invalid_value)
179 << A->getAsString(Args) << HIPVersionArg;
180
181 VersionMajorMinor = llvm::VersionTuple(Major, Minor);
182 DetectedVersion =
183 (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str();
184 } else {
185 VersionPatch = DefaultVersionPatch;
186 VersionMajorMinor =
187 llvm::VersionTuple(DefaultVersionMajor, DefaultVersionMinor);
188 DetectedVersion = (Twine(DefaultVersionMajor) + "." +
189 Twine(DefaultVersionMinor) + "." + VersionPatch)
190 .str();
191 }
192
193 if (DetectHIPRuntime)
194 detectHIPRuntime();
195 if (DetectDeviceLib)
196 detectDeviceLibrary();
197 }
198
detectDeviceLibrary()199 void RocmInstallationDetector::detectDeviceLibrary() {
200 assert(LibDevicePath.empty());
201
202 if (!RocmDeviceLibPathArg.empty())
203 LibDevicePath = RocmDeviceLibPathArg[RocmDeviceLibPathArg.size() - 1];
204 else if (const char *LibPathEnv = ::getenv("HIP_DEVICE_LIB_PATH"))
205 LibDevicePath = LibPathEnv;
206
207 auto &FS = D.getVFS();
208 if (!LibDevicePath.empty()) {
209 // Maintain compatability with HIP flag/envvar pointing directly at the
210 // bitcode library directory. This points directly at the library path instead
211 // of the rocm root installation.
212 if (!FS.exists(LibDevicePath))
213 return;
214
215 scanLibDevicePath(LibDevicePath);
216 HasDeviceLibrary = allGenericLibsValid() && !LibDeviceMap.empty();
217 return;
218 }
219
220 // The install path situation in old versions of ROCm is a real mess, and
221 // use a different install layout. Multiple copies of the device libraries
222 // exist for each frontend project, and differ depending on which build
223 // system produced the packages. Standalone OpenCL builds also have a
224 // different directory structure from the ROCm OpenCL package.
225 auto Candidates = getInstallationPathCandidates();
226 for (const auto &Candidate : Candidates) {
227 auto CandidatePath = Candidate.Path;
228
229 // Check device library exists at the given path.
230 auto CheckDeviceLib = [&](StringRef Path) {
231 bool CheckLibDevice = (!NoBuiltinLibs || Candidate.StrictChecking);
232 if (CheckLibDevice && !FS.exists(Path))
233 return false;
234
235 scanLibDevicePath(Path);
236
237 if (!NoBuiltinLibs) {
238 // Check that the required non-target libraries are all available.
239 if (!allGenericLibsValid())
240 return false;
241
242 // Check that we have found at least one libdevice that we can link in
243 // if -nobuiltinlib hasn't been specified.
244 if (LibDeviceMap.empty())
245 return false;
246 }
247 return true;
248 };
249
250 // The possible structures are:
251 // - ${ROCM_ROOT}/amdgcn/bitcode/*
252 // - ${ROCM_ROOT}/lib/*
253 // - ${ROCM_ROOT}/lib/bitcode/*
254 // so try to detect these layouts.
255 static constexpr std::array<const char *, 2> SubDirsList[] = {
256 {"amdgcn", "bitcode"},
257 {"lib", ""},
258 {"lib", "bitcode"},
259 };
260
261 // Make a path by appending sub-directories to InstallPath.
262 auto MakePath = [&](const llvm::ArrayRef<const char *> &SubDirs) {
263 auto Path = CandidatePath;
264 for (auto SubDir : SubDirs)
265 llvm::sys::path::append(Path, SubDir);
266 return Path;
267 };
268
269 for (auto SubDirs : SubDirsList) {
270 LibDevicePath = MakePath(SubDirs);
271 HasDeviceLibrary = CheckDeviceLib(LibDevicePath);
272 if (HasDeviceLibrary)
273 return;
274 }
275 }
276 }
277
detectHIPRuntime()278 void RocmInstallationDetector::detectHIPRuntime() {
279 auto Candidates = getInstallationPathCandidates();
280 auto &FS = D.getVFS();
281
282 for (const auto &Candidate : Candidates) {
283 InstallPath = Candidate.Path;
284 if (InstallPath.empty() || !FS.exists(InstallPath))
285 continue;
286
287 BinPath = InstallPath;
288 llvm::sys::path::append(BinPath, "bin");
289 IncludePath = InstallPath;
290 llvm::sys::path::append(IncludePath, "include");
291 LibPath = InstallPath;
292 llvm::sys::path::append(LibPath, "lib");
293
294 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> VersionFile =
295 FS.getBufferForFile(BinPath + "/.hipVersion");
296 if (!VersionFile && Candidate.StrictChecking)
297 continue;
298
299 if (HIPVersionArg.empty() && VersionFile)
300 if (parseHIPVersionFile((*VersionFile)->getBuffer()))
301 continue;
302
303 HasHIPRuntime = true;
304 return;
305 }
306 HasHIPRuntime = false;
307 }
308
print(raw_ostream & OS) const309 void RocmInstallationDetector::print(raw_ostream &OS) const {
310 if (hasHIPRuntime())
311 OS << "Found HIP installation: " << InstallPath << ", version "
312 << DetectedVersion << '\n';
313 }
314
AddHIPIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const315 void RocmInstallationDetector::AddHIPIncludeArgs(const ArgList &DriverArgs,
316 ArgStringList &CC1Args) const {
317 bool UsesRuntimeWrapper = VersionMajorMinor > llvm::VersionTuple(3, 5);
318
319 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
320 // HIP header includes standard library wrapper headers under clang
321 // cuda_wrappers directory. Since these wrapper headers include_next
322 // standard C++ headers, whereas libc++ headers include_next other clang
323 // headers. The include paths have to follow this order:
324 // - wrapper include path
325 // - standard C++ include path
326 // - other clang include path
327 // Since standard C++ and other clang include paths are added in other
328 // places after this function, here we only need to make sure wrapper
329 // include path is added.
330 //
331 // ROCm 3.5 does not fully support the wrapper headers. Therefore it needs
332 // a workaround.
333 SmallString<128> P(D.ResourceDir);
334 if (UsesRuntimeWrapper)
335 llvm::sys::path::append(P, "include", "cuda_wrappers");
336 CC1Args.push_back("-internal-isystem");
337 CC1Args.push_back(DriverArgs.MakeArgString(P));
338 }
339
340 if (DriverArgs.hasArg(options::OPT_nogpuinc))
341 return;
342
343 if (!hasHIPRuntime()) {
344 D.Diag(diag::err_drv_no_hip_runtime);
345 return;
346 }
347
348 CC1Args.push_back("-internal-isystem");
349 CC1Args.push_back(DriverArgs.MakeArgString(getIncludePath()));
350 if (UsesRuntimeWrapper)
351 CC1Args.append({"-include", "__clang_hip_runtime_wrapper.h"});
352 }
353
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const354 void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
355 const InputInfo &Output,
356 const InputInfoList &Inputs,
357 const ArgList &Args,
358 const char *LinkingOutput) const {
359
360 std::string Linker = getToolChain().GetProgramPath(getShortName());
361 ArgStringList CmdArgs;
362 addLinkerCompressDebugSectionsOption(getToolChain(), Args, CmdArgs);
363 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
364 CmdArgs.push_back("-shared");
365 CmdArgs.push_back("-o");
366 CmdArgs.push_back(Output.getFilename());
367 C.addCommand(std::make_unique<Command>(
368 JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Linker),
369 CmdArgs, Inputs, Output));
370 }
371
getAMDGPUTargetFeatures(const Driver & D,const llvm::Triple & Triple,const llvm::opt::ArgList & Args,std::vector<StringRef> & Features)372 void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
373 const llvm::Triple &Triple,
374 const llvm::opt::ArgList &Args,
375 std::vector<StringRef> &Features) {
376 // Add target ID features to -target-feature options. No diagnostics should
377 // be emitted here since invalid target ID is diagnosed at other places.
378 StringRef TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
379 if (!TargetID.empty()) {
380 llvm::StringMap<bool> FeatureMap;
381 auto OptionalGpuArch = parseTargetID(Triple, TargetID, &FeatureMap);
382 if (OptionalGpuArch) {
383 StringRef GpuArch = OptionalGpuArch.getValue();
384 // Iterate through all possible target ID features for the given GPU.
385 // If it is mapped to true, add +feature.
386 // If it is mapped to false, add -feature.
387 // If it is not in the map (default), do not add it
388 for (auto &&Feature : getAllPossibleTargetIDFeatures(Triple, GpuArch)) {
389 auto Pos = FeatureMap.find(Feature);
390 if (Pos == FeatureMap.end())
391 continue;
392 Features.push_back(Args.MakeArgStringRef(
393 (Twine(Pos->second ? "+" : "-") + Feature).str()));
394 }
395 }
396 }
397
398 if (Args.hasFlag(options::OPT_mwavefrontsize64,
399 options::OPT_mno_wavefrontsize64, false))
400 Features.push_back("+wavefrontsize64");
401
402 handleTargetFeaturesGroup(
403 Args, Features, options::OPT_m_amdgpu_Features_Group);
404 }
405
406 /// AMDGPU Toolchain
AMDGPUToolChain(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)407 AMDGPUToolChain::AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
408 const ArgList &Args)
409 : Generic_ELF(D, Triple, Args),
410 OptionsDefault(
411 {{options::OPT_O, "3"}, {options::OPT_cl_std_EQ, "CL1.2"}}) {
412 // Check code object version options. Emit warnings for legacy options
413 // and errors for the last invalid code object version options.
414 // It is done here to avoid repeated warning or error messages for
415 // each tool invocation.
416 (void)getOrCheckAMDGPUCodeObjectVersion(D, Args, /*Diagnose=*/true);
417 }
418
buildLinker() const419 Tool *AMDGPUToolChain::buildLinker() const {
420 return new tools::amdgpu::Linker(*this);
421 }
422
423 DerivedArgList *
TranslateArgs(const DerivedArgList & Args,StringRef BoundArch,Action::OffloadKind DeviceOffloadKind) const424 AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
425 Action::OffloadKind DeviceOffloadKind) const {
426
427 DerivedArgList *DAL =
428 Generic_ELF::TranslateArgs(Args, BoundArch, DeviceOffloadKind);
429
430 const OptTable &Opts = getDriver().getOpts();
431
432 if (!DAL)
433 DAL = new DerivedArgList(Args.getBaseArgs());
434
435 for (Arg *A : Args) {
436 if (!shouldSkipArgument(A))
437 DAL->append(A);
438 }
439
440 checkTargetID(*DAL);
441
442 if (!Args.getLastArgValue(options::OPT_x).equals("cl"))
443 return DAL;
444
445 // Phase 1 (.cl -> .bc)
446 if (Args.hasArg(options::OPT_c) && Args.hasArg(options::OPT_emit_llvm)) {
447 DAL->AddFlagArg(nullptr, Opts.getOption(getTriple().isArch64Bit()
448 ? options::OPT_m64
449 : options::OPT_m32));
450
451 // Have to check OPT_O4, OPT_O0 & OPT_Ofast separately
452 // as they defined that way in Options.td
453 if (!Args.hasArg(options::OPT_O, options::OPT_O0, options::OPT_O4,
454 options::OPT_Ofast))
455 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O),
456 getOptionDefault(options::OPT_O));
457 }
458
459 return DAL;
460 }
461
getDefaultDenormsAreZeroForTarget(llvm::AMDGPU::GPUKind Kind)462 bool AMDGPUToolChain::getDefaultDenormsAreZeroForTarget(
463 llvm::AMDGPU::GPUKind Kind) {
464
465 // Assume nothing without a specific target.
466 if (Kind == llvm::AMDGPU::GK_NONE)
467 return false;
468
469 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind);
470
471 // Default to enabling f32 denormals by default on subtargets where fma is
472 // fast with denormals
473 const bool BothDenormAndFMAFast =
474 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) &&
475 (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32);
476 return !BothDenormAndFMAFast;
477 }
478
getDefaultDenormalModeForType(const llvm::opt::ArgList & DriverArgs,const JobAction & JA,const llvm::fltSemantics * FPType) const479 llvm::DenormalMode AMDGPUToolChain::getDefaultDenormalModeForType(
480 const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
481 const llvm::fltSemantics *FPType) const {
482 // Denormals should always be enabled for f16 and f64.
483 if (!FPType || FPType != &llvm::APFloat::IEEEsingle())
484 return llvm::DenormalMode::getIEEE();
485
486 if (JA.getOffloadingDeviceKind() == Action::OFK_HIP ||
487 JA.getOffloadingDeviceKind() == Action::OFK_Cuda) {
488 auto Arch = getProcessorFromTargetID(getTriple(), JA.getOffloadingArch());
489 auto Kind = llvm::AMDGPU::parseArchAMDGCN(Arch);
490 if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
491 DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
492 options::OPT_fno_cuda_flush_denormals_to_zero,
493 getDefaultDenormsAreZeroForTarget(Kind)))
494 return llvm::DenormalMode::getPreserveSign();
495
496 return llvm::DenormalMode::getIEEE();
497 }
498
499 const StringRef GpuArch = getGPUArch(DriverArgs);
500 auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
501
502 // TODO: There are way too many flags that change this. Do we need to check
503 // them all?
504 bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) ||
505 getDefaultDenormsAreZeroForTarget(Kind);
506
507 // Outputs are flushed to zero (FTZ), preserving sign. Denormal inputs are
508 // also implicit treated as zero (DAZ).
509 return DAZ ? llvm::DenormalMode::getPreserveSign() :
510 llvm::DenormalMode::getIEEE();
511 }
512
isWave64(const llvm::opt::ArgList & DriverArgs,llvm::AMDGPU::GPUKind Kind)513 bool AMDGPUToolChain::isWave64(const llvm::opt::ArgList &DriverArgs,
514 llvm::AMDGPU::GPUKind Kind) {
515 const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind);
516 bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32);
517
518 return !HasWave32 || DriverArgs.hasFlag(
519 options::OPT_mwavefrontsize64, options::OPT_mno_wavefrontsize64, false);
520 }
521
522
523 /// ROCM Toolchain
ROCMToolChain(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)524 ROCMToolChain::ROCMToolChain(const Driver &D, const llvm::Triple &Triple,
525 const ArgList &Args)
526 : AMDGPUToolChain(D, Triple, Args) {
527 RocmInstallation.detectDeviceLibrary();
528 }
529
addClangTargetOptions(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args,Action::OffloadKind DeviceOffloadingKind) const530 void AMDGPUToolChain::addClangTargetOptions(
531 const llvm::opt::ArgList &DriverArgs,
532 llvm::opt::ArgStringList &CC1Args,
533 Action::OffloadKind DeviceOffloadingKind) const {
534 // Default to "hidden" visibility, as object level linking will not be
535 // supported for the foreseeable future.
536 if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
537 options::OPT_fvisibility_ms_compat)) {
538 CC1Args.push_back("-fvisibility");
539 CC1Args.push_back("hidden");
540 CC1Args.push_back("-fapply-global-visibility-to-externs");
541 }
542 }
543
544 StringRef
getGPUArch(const llvm::opt::ArgList & DriverArgs) const545 AMDGPUToolChain::getGPUArch(const llvm::opt::ArgList &DriverArgs) const {
546 return getProcessorFromTargetID(
547 getTriple(), DriverArgs.getLastArgValue(options::OPT_mcpu_EQ));
548 }
549
checkTargetID(const llvm::opt::ArgList & DriverArgs) const550 void AMDGPUToolChain::checkTargetID(
551 const llvm::opt::ArgList &DriverArgs) const {
552 StringRef TargetID = DriverArgs.getLastArgValue(options::OPT_mcpu_EQ);
553 if (TargetID.empty())
554 return;
555
556 llvm::StringMap<bool> FeatureMap;
557 auto OptionalGpuArch = parseTargetID(getTriple(), TargetID, &FeatureMap);
558 if (!OptionalGpuArch) {
559 getDriver().Diag(clang::diag::err_drv_bad_target_id) << TargetID;
560 }
561 }
562
addClangTargetOptions(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args,Action::OffloadKind DeviceOffloadingKind) const563 void ROCMToolChain::addClangTargetOptions(
564 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
565 Action::OffloadKind DeviceOffloadingKind) const {
566 AMDGPUToolChain::addClangTargetOptions(DriverArgs, CC1Args,
567 DeviceOffloadingKind);
568
569 // For the OpenCL case where there is no offload target, accept -nostdlib to
570 // disable bitcode linking.
571 if (DeviceOffloadingKind == Action::OFK_None &&
572 DriverArgs.hasArg(options::OPT_nostdlib))
573 return;
574
575 if (DriverArgs.hasArg(options::OPT_nogpulib))
576 return;
577
578 if (!RocmInstallation.hasDeviceLibrary()) {
579 getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 0;
580 return;
581 }
582
583 // Get the device name and canonicalize it
584 const StringRef GpuArch = getGPUArch(DriverArgs);
585 auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
586 const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);
587 std::string LibDeviceFile = RocmInstallation.getLibDeviceFile(CanonArch);
588 if (LibDeviceFile.empty()) {
589 getDriver().Diag(diag::err_drv_no_rocm_device_lib) << 1 << GpuArch;
590 return;
591 }
592
593 bool Wave64 = isWave64(DriverArgs, Kind);
594
595 // TODO: There are way too many flags that change this. Do we need to check
596 // them all?
597 bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) ||
598 getDefaultDenormsAreZeroForTarget(Kind);
599 bool FiniteOnly = DriverArgs.hasArg(options::OPT_cl_finite_math_only);
600
601 bool UnsafeMathOpt =
602 DriverArgs.hasArg(options::OPT_cl_unsafe_math_optimizations);
603 bool FastRelaxedMath = DriverArgs.hasArg(options::OPT_cl_fast_relaxed_math);
604 bool CorrectSqrt =
605 DriverArgs.hasArg(options::OPT_cl_fp32_correctly_rounded_divide_sqrt);
606
607 // Add the OpenCL specific bitcode library.
608 CC1Args.push_back("-mlink-builtin-bitcode");
609 CC1Args.push_back(DriverArgs.MakeArgString(RocmInstallation.getOpenCLPath()));
610
611 // Add the generic set of libraries.
612 RocmInstallation.addCommonBitcodeLibCC1Args(
613 DriverArgs, CC1Args, LibDeviceFile, Wave64, DAZ, FiniteOnly,
614 UnsafeMathOpt, FastRelaxedMath, CorrectSqrt);
615 }
616
addCommonBitcodeLibCC1Args(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args,StringRef LibDeviceFile,bool Wave64,bool DAZ,bool FiniteOnly,bool UnsafeMathOpt,bool FastRelaxedMath,bool CorrectSqrt) const617 void RocmInstallationDetector::addCommonBitcodeLibCC1Args(
618 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
619 StringRef LibDeviceFile, bool Wave64, bool DAZ, bool FiniteOnly,
620 bool UnsafeMathOpt, bool FastRelaxedMath, bool CorrectSqrt) const {
621 static const char LinkBitcodeFlag[] = "-mlink-builtin-bitcode";
622
623 CC1Args.push_back(LinkBitcodeFlag);
624 CC1Args.push_back(DriverArgs.MakeArgString(getOCMLPath()));
625
626 CC1Args.push_back(LinkBitcodeFlag);
627 CC1Args.push_back(DriverArgs.MakeArgString(getOCKLPath()));
628
629 CC1Args.push_back(LinkBitcodeFlag);
630 CC1Args.push_back(DriverArgs.MakeArgString(getDenormalsAreZeroPath(DAZ)));
631
632 CC1Args.push_back(LinkBitcodeFlag);
633 CC1Args.push_back(DriverArgs.MakeArgString(
634 getUnsafeMathPath(UnsafeMathOpt || FastRelaxedMath)));
635
636 CC1Args.push_back(LinkBitcodeFlag);
637 CC1Args.push_back(DriverArgs.MakeArgString(
638 getFiniteOnlyPath(FiniteOnly || FastRelaxedMath)));
639
640 CC1Args.push_back(LinkBitcodeFlag);
641 CC1Args.push_back(
642 DriverArgs.MakeArgString(getCorrectlyRoundedSqrtPath(CorrectSqrt)));
643
644 CC1Args.push_back(LinkBitcodeFlag);
645 CC1Args.push_back(DriverArgs.MakeArgString(getWavefrontSize64Path(Wave64)));
646
647 CC1Args.push_back(LinkBitcodeFlag);
648 CC1Args.push_back(DriverArgs.MakeArgString(LibDeviceFile));
649 }
650
shouldSkipArgument(const llvm::opt::Arg * A) const651 bool AMDGPUToolChain::shouldSkipArgument(const llvm::opt::Arg *A) const {
652 Option O = A->getOption();
653 if (O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie))
654 return true;
655 return false;
656 }
657