1 | //===--- AMDGPU.h - 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 | #ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H |
10 | #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H |
11 | |
12 | #include "Gnu.h" |
13 | #include "ROCm.h" |
14 | #include "clang/Basic/TargetID.h" |
15 | #include "clang/Driver/Options.h" |
16 | #include "clang/Driver/Tool.h" |
17 | #include "clang/Driver/ToolChain.h" |
18 | #include "llvm/ADT/SmallString.h" |
19 | #include "llvm/TargetParser/TargetParser.h" |
20 | |
21 | #include <map> |
22 | |
23 | namespace clang { |
24 | namespace driver { |
25 | |
26 | namespace tools { |
27 | namespace amdgpu { |
28 | |
29 | class LLVM_LIBRARY_VISIBILITY Linker final : public Tool { |
30 | public: |
31 | Linker(const ToolChain &TC) : Tool("amdgpu::Linker" , "ld.lld" , TC) {} |
32 | bool isLinkJob() const override { return true; } |
33 | bool hasIntegratedCPP() const override { return false; } |
34 | void ConstructJob(Compilation &C, const JobAction &JA, |
35 | const InputInfo &Output, const InputInfoList &Inputs, |
36 | const llvm::opt::ArgList &TCArgs, |
37 | const char *LinkingOutput) const override; |
38 | }; |
39 | |
40 | void getAMDGPUTargetFeatures(const Driver &D, const llvm::Triple &Triple, |
41 | const llvm::opt::ArgList &Args, |
42 | std::vector<StringRef> &Features); |
43 | |
44 | void addFullLTOPartitionOption(const Driver &D, const llvm::opt::ArgList &Args, |
45 | llvm::opt::ArgStringList &CmdArgs); |
46 | } // end namespace amdgpu |
47 | } // end namespace tools |
48 | |
49 | namespace toolchains { |
50 | |
51 | class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF { |
52 | protected: |
53 | const std::map<options::ID, const StringRef> OptionsDefault; |
54 | |
55 | Tool *buildLinker() const override; |
56 | StringRef getOptionDefault(options::ID OptID) const { |
57 | auto opt = OptionsDefault.find(x: OptID); |
58 | assert(opt != OptionsDefault.end() && "No Default for Option" ); |
59 | return opt->second; |
60 | } |
61 | |
62 | public: |
63 | AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple, |
64 | const llvm::opt::ArgList &Args); |
65 | unsigned GetDefaultDwarfVersion() const override { return 5; } |
66 | |
67 | bool IsMathErrnoDefault() const override { return false; } |
68 | bool isCrossCompiling() const override { return true; } |
69 | bool isPICDefault() const override { return true; } |
70 | bool isPIEDefault(const llvm::opt::ArgList &Args) const override { |
71 | return false; |
72 | } |
73 | bool isPICDefaultForced() const override { return true; } |
74 | bool SupportsProfiling() const override { return false; } |
75 | |
76 | llvm::opt::DerivedArgList * |
77 | TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, |
78 | Action::OffloadKind DeviceOffloadKind) const override; |
79 | |
80 | void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, |
81 | llvm::opt::ArgStringList &CC1Args, |
82 | Action::OffloadKind DeviceOffloadKind) const override; |
83 | |
84 | /// Return whether denormals should be flushed, and treated as 0 by default |
85 | /// for the subtarget. |
86 | static bool getDefaultDenormsAreZeroForTarget(llvm::AMDGPU::GPUKind GPUKind); |
87 | |
88 | llvm::DenormalMode getDefaultDenormalModeForType( |
89 | const llvm::opt::ArgList &DriverArgs, const JobAction &JA, |
90 | const llvm::fltSemantics *FPType = nullptr) const override; |
91 | |
92 | static bool isWave64(const llvm::opt::ArgList &DriverArgs, |
93 | llvm::AMDGPU::GPUKind Kind); |
94 | /// Needed for using lto. |
95 | bool HasNativeLLVMSupport() const override { |
96 | return true; |
97 | } |
98 | |
99 | /// Needed for translating LTO options. |
100 | const char *getDefaultLinker() const override { return "ld.lld" ; } |
101 | |
102 | /// Should skip sanitize options. |
103 | bool shouldSkipSanitizeOption(const ToolChain &TC, |
104 | const llvm::opt::ArgList &DriverArgs, |
105 | StringRef TargetID, |
106 | const llvm::opt::Arg *A) const; |
107 | |
108 | /// Uses amdgpu-arch tool to get arch of the system GPU. Will return error |
109 | /// if unable to find one. |
110 | virtual Expected<SmallVector<std::string>> |
111 | getSystemGPUArchs(const llvm::opt::ArgList &Args) const override; |
112 | |
113 | protected: |
114 | /// Check and diagnose invalid target ID specified by -mcpu. |
115 | virtual void checkTargetID(const llvm::opt::ArgList &DriverArgs) const; |
116 | |
117 | /// The struct type returned by getParsedTargetID. |
118 | struct ParsedTargetIDType { |
119 | std::optional<std::string> OptionalTargetID; |
120 | std::optional<std::string> OptionalGPUArch; |
121 | std::optional<llvm::StringMap<bool>> OptionalFeatures; |
122 | }; |
123 | |
124 | /// Get target ID, GPU arch, and target ID features if the target ID is |
125 | /// specified and valid. |
126 | ParsedTargetIDType |
127 | getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const; |
128 | |
129 | /// Get GPU arch from -mcpu without checking. |
130 | StringRef getGPUArch(const llvm::opt::ArgList &DriverArgs) const; |
131 | |
132 | /// Common warning options shared by AMDGPU HIP, OpenCL and OpenMP toolchains. |
133 | /// Language specific warning options should go to derived classes. |
134 | void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override; |
135 | }; |
136 | |
137 | class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain { |
138 | public: |
139 | ROCMToolChain(const Driver &D, const llvm::Triple &Triple, |
140 | const llvm::opt::ArgList &Args); |
141 | void |
142 | addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, |
143 | llvm::opt::ArgStringList &CC1Args, |
144 | Action::OffloadKind DeviceOffloadKind) const override; |
145 | |
146 | // Returns a list of device library names shared by different languages |
147 | llvm::SmallVector<BitCodeLibraryInfo, 12> |
148 | getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs, |
149 | const std::string &GPUArch, |
150 | bool isOpenMP = false) const; |
151 | |
152 | SanitizerMask getSupportedSanitizers() const override { |
153 | return SanitizerKind::Address; |
154 | } |
155 | |
156 | void diagnoseUnsupportedSanitizers(const llvm::opt::ArgList &Args) const { |
157 | if (!Args.hasFlag(options::OPT_fgpu_sanitize, options::OPT_fno_gpu_sanitize, |
158 | true)) |
159 | return; |
160 | auto &Diags = getDriver().getDiags(); |
161 | for (auto *A : Args.filtered(options::OPT_fsanitize_EQ)) { |
162 | SanitizerMask K = |
163 | parseSanitizerValue(A->getValue(), /*Allow Groups*/ false); |
164 | if (K != SanitizerKind::Address) |
165 | Diags.Report(clang::diag::warn_drv_unsupported_option_for_target) |
166 | << A->getAsString(Args) << getTriple().str(); |
167 | } |
168 | } |
169 | }; |
170 | |
171 | } // end namespace toolchains |
172 | } // end namespace driver |
173 | } // end namespace clang |
174 | |
175 | #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H |
176 | |