blob: 38dfb0e19231ccd8a02feea177dea2ad5fdde768 [file] [log] [blame]
Yaxun Liuf6144222018-05-30 00:53:50 +00001//===--- HIP.cpp - HIP Tool and ToolChain Implementations -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "HIP.h"
11#include "CommonArgs.h"
12#include "InputInfo.h"
13#include "clang/Basic/Cuda.h"
14#include "clang/Driver/Compilation.h"
15#include "clang/Driver/Driver.h"
16#include "clang/Driver/DriverDiagnostic.h"
17#include "clang/Driver/Options.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
20
21using namespace clang::driver;
22using namespace clang::driver::toolchains;
23using namespace clang::driver::tools;
24using namespace clang;
25using namespace llvm::opt;
26
27namespace {
28
29static void addBCLib(Compilation &C, const ArgList &Args,
30 ArgStringList &CmdArgs, ArgStringList LibraryPaths,
31 StringRef BCName) {
32 StringRef FullName;
33 for (std::string LibraryPath : LibraryPaths) {
34 SmallString<128> Path(LibraryPath);
35 llvm::sys::path::append(Path, BCName);
36 FullName = Path;
37 if (llvm::sys::fs::exists(FullName)) {
38 CmdArgs.push_back(Args.MakeArgString(FullName));
39 return;
40 }
41 }
42 C.getDriver().Diag(diag::err_drv_no_such_file) << BCName;
43}
44
45} // namespace
46
47const char *AMDGCN::Linker::constructLLVMLinkCommand(
48 Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
49 const ArgList &Args, StringRef SubArchName,
50 StringRef OutputFilePrefix) const {
51 ArgStringList CmdArgs;
52 // Add the input bc's created by compile step.
53 for (const auto &II : Inputs)
54 CmdArgs.push_back(II.getFilename());
55
56 ArgStringList LibraryPaths;
57
58 // Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
59 for (auto Path : Args.getAllArgValues(options::OPT_hip_device_lib_path_EQ))
60 LibraryPaths.push_back(Args.MakeArgString(Path));
61
62 addDirectoryList(Args, LibraryPaths, "-L", "HIP_DEVICE_LIB_PATH");
63
64 llvm::SmallVector<std::string, 10> BCLibs;
65
66 // Add bitcode library in --hip-device-lib.
67 for (auto Lib : Args.getAllArgValues(options::OPT_hip_device_lib_EQ)) {
68 BCLibs.push_back(Args.MakeArgString(Lib));
69 }
70
71 // If --hip-device-lib is not set, add the default bitcode libraries.
72 if (BCLibs.empty()) {
73 // Get the bc lib file name for ISA version. For example,
74 // gfx803 => oclc_isa_version_803.amdgcn.bc.
75 std::string ISAVerBC =
76 "oclc_isa_version_" + SubArchName.drop_front(3).str() + ".amdgcn.bc";
77
Aaron Enye Shi5c200be2018-06-26 17:40:36 +000078 BCLibs.append({"opencl.amdgcn.bc",
Yaxun Liuf6144222018-05-30 00:53:50 +000079 "ockl.amdgcn.bc", "irif.amdgcn.bc", "ocml.amdgcn.bc",
80 "oclc_finite_only_off.amdgcn.bc",
81 "oclc_daz_opt_off.amdgcn.bc",
82 "oclc_correctly_rounded_sqrt_on.amdgcn.bc",
Aaron Enye Shi5c200be2018-06-26 17:40:36 +000083 "oclc_unsafe_math_off.amdgcn.bc", ISAVerBC});
Yaxun Liuf6144222018-05-30 00:53:50 +000084 }
85 for (auto Lib : BCLibs)
86 addBCLib(C, Args, CmdArgs, LibraryPaths, Lib);
87
88 // Add an intermediate output file.
89 CmdArgs.push_back("-o");
90 std::string TmpName =
91 C.getDriver().GetTemporaryPath(OutputFilePrefix.str() + "-linked", "bc");
92 const char *OutputFileName =
93 C.addTempFile(C.getArgs().MakeArgString(TmpName));
94 CmdArgs.push_back(OutputFileName);
95 SmallString<128> ExecPath(C.getDriver().Dir);
96 llvm::sys::path::append(ExecPath, "llvm-link");
97 const char *Exec = Args.MakeArgString(ExecPath);
98 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
99 return OutputFileName;
100}
101
102const char *AMDGCN::Linker::constructOptCommand(
103 Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
104 const llvm::opt::ArgList &Args, llvm::StringRef SubArchName,
105 llvm::StringRef OutputFilePrefix, const char *InputFileName) const {
106 // Construct opt command.
107 ArgStringList OptArgs;
108 // The input to opt is the output from llvm-link.
109 OptArgs.push_back(InputFileName);
110 // Pass optimization arg to opt.
111 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
112 StringRef OOpt = "3";
113 if (A->getOption().matches(options::OPT_O4) ||
114 A->getOption().matches(options::OPT_Ofast))
115 OOpt = "3";
116 else if (A->getOption().matches(options::OPT_O0))
117 OOpt = "0";
118 else if (A->getOption().matches(options::OPT_O)) {
119 // -Os, -Oz, and -O(anything else) map to -O2
120 OOpt = llvm::StringSwitch<const char *>(A->getValue())
121 .Case("1", "1")
122 .Case("2", "2")
123 .Case("3", "3")
124 .Case("s", "2")
125 .Case("z", "2")
126 .Default("2");
127 }
128 OptArgs.push_back(Args.MakeArgString("-O" + OOpt));
129 }
130 OptArgs.push_back("-mtriple=amdgcn-amd-amdhsa");
131 OptArgs.push_back(Args.MakeArgString("-mcpu=" + SubArchName));
132 OptArgs.push_back("-o");
133 std::string TmpFileName = C.getDriver().GetTemporaryPath(
134 OutputFilePrefix.str() + "-optimized", "bc");
135 const char *OutputFileName =
136 C.addTempFile(C.getArgs().MakeArgString(TmpFileName));
137 OptArgs.push_back(OutputFileName);
138 SmallString<128> OptPath(C.getDriver().Dir);
139 llvm::sys::path::append(OptPath, "opt");
140 const char *OptExec = Args.MakeArgString(OptPath);
141 C.addCommand(llvm::make_unique<Command>(JA, *this, OptExec, OptArgs, Inputs));
142 return OutputFileName;
143}
144
145const char *AMDGCN::Linker::constructLlcCommand(
146 Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
147 const llvm::opt::ArgList &Args, llvm::StringRef SubArchName,
148 llvm::StringRef OutputFilePrefix, const char *InputFileName) const {
149 // Construct llc command.
150 ArgStringList LlcArgs{InputFileName, "-mtriple=amdgcn-amd-amdhsa",
151 "-filetype=obj",
152 Args.MakeArgString("-mcpu=" + SubArchName), "-o"};
153 std::string LlcOutputFileName =
154 C.getDriver().GetTemporaryPath(OutputFilePrefix, "o");
155 const char *LlcOutputFile =
156 C.addTempFile(C.getArgs().MakeArgString(LlcOutputFileName));
157 LlcArgs.push_back(LlcOutputFile);
158 SmallString<128> LlcPath(C.getDriver().Dir);
159 llvm::sys::path::append(LlcPath, "llc");
160 const char *Llc = Args.MakeArgString(LlcPath);
161 C.addCommand(llvm::make_unique<Command>(JA, *this, Llc, LlcArgs, Inputs));
162 return LlcOutputFile;
163}
164
165void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
166 const InputInfoList &Inputs,
167 const InputInfo &Output,
168 const llvm::opt::ArgList &Args,
169 const char *InputFileName) const {
170 // Construct lld command.
171 // The output from ld.lld is an HSA code object file.
172 ArgStringList LldArgs{"-flavor", "gnu", "--no-undefined",
173 "-shared", "-o", Output.getFilename(),
174 InputFileName};
175 SmallString<128> LldPath(C.getDriver().Dir);
176 llvm::sys::path::append(LldPath, "lld");
177 const char *Lld = Args.MakeArgString(LldPath);
178 C.addCommand(llvm::make_unique<Command>(JA, *this, Lld, LldArgs, Inputs));
179}
180
181// For amdgcn the inputs of the linker job are device bitcode and output is
182// object file. It calls llvm-link, opt, llc, then lld steps.
183void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
184 const InputInfo &Output,
185 const InputInfoList &Inputs,
186 const ArgList &Args,
187 const char *LinkingOutput) const {
188
Sam McCall43fdd222018-05-30 08:03:43 +0000189 assert(getToolChain().getTriple().getArch() == llvm::Triple::amdgcn &&
Yaxun Liuf6144222018-05-30 00:53:50 +0000190 "Unsupported target");
191
192 std::string SubArchName = JA.getOffloadingArch();
193 assert(StringRef(SubArchName).startswith("gfx") && "Unsupported sub arch");
194
195 // Prefix for temporary file name.
196 std::string Prefix =
197 llvm::sys::path::stem(Inputs[0].getFilename()).str() + "-" + SubArchName;
198
199 // Each command outputs different files.
200 const char *LLVMLinkCommand =
201 constructLLVMLinkCommand(C, JA, Inputs, Args, SubArchName, Prefix);
202 const char *OptCommand = constructOptCommand(C, JA, Inputs, Args, SubArchName,
203 Prefix, LLVMLinkCommand);
204 const char *LlcCommand =
205 constructLlcCommand(C, JA, Inputs, Args, SubArchName, Prefix, OptCommand);
206 constructLldCommand(C, JA, Inputs, Output, Args, LlcCommand);
207}
208
209HIPToolChain::HIPToolChain(const Driver &D, const llvm::Triple &Triple,
210 const ToolChain &HostTC, const ArgList &Args)
211 : ToolChain(D, Triple, Args), HostTC(HostTC) {
212 // Lookup binaries into the driver directory, this is used to
213 // discover the clang-offload-bundler executable.
214 getProgramPaths().push_back(getDriver().Dir);
215}
216
217void HIPToolChain::addClangTargetOptions(
218 const llvm::opt::ArgList &DriverArgs,
219 llvm::opt::ArgStringList &CC1Args,
220 Action::OffloadKind DeviceOffloadingKind) const {
221 HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
222
223 StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
224 assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
Sam McCall43fdd222018-05-30 08:03:43 +0000225 (void) GpuArch;
Yaxun Liuf6144222018-05-30 00:53:50 +0000226 assert(DeviceOffloadingKind == Action::OFK_HIP &&
227 "Only HIP offloading kinds are supported for GPUs.");
228
229 CC1Args.push_back("-fcuda-is-device");
230
231 if (DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
232 options::OPT_fno_cuda_flush_denormals_to_zero, false))
233 CC1Args.push_back("-fcuda-flush-denormals-to-zero");
234
235 if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
236 options::OPT_fno_cuda_approx_transcendentals, false))
237 CC1Args.push_back("-fcuda-approx-transcendentals");
238
239 if (DriverArgs.hasFlag(options::OPT_fcuda_rdc, options::OPT_fno_cuda_rdc,
240 false))
241 CC1Args.push_back("-fcuda-rdc");
242}
243
244llvm::opt::DerivedArgList *
245HIPToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
246 StringRef BoundArch,
247 Action::OffloadKind DeviceOffloadKind) const {
248 DerivedArgList *DAL =
249 HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
250 if (!DAL)
251 DAL = new DerivedArgList(Args.getBaseArgs());
252
253 const OptTable &Opts = getDriver().getOpts();
254
255 for (Arg *A : Args) {
256 if (A->getOption().matches(options::OPT_Xarch__)) {
Aaron Enye Shi4928d512018-06-26 17:12:29 +0000257 // Skip this argument unless the architecture matches BoundArch.
Yaxun Liuf6144222018-05-30 00:53:50 +0000258 if (BoundArch.empty() || A->getValue(0) != BoundArch)
259 continue;
260
261 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
262 unsigned Prev = Index;
263 std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
264
265 // If the argument parsing failed or more than one argument was
266 // consumed, the -Xarch_ argument's parameter tried to consume
267 // extra arguments. Emit an error and ignore.
268 //
269 // We also want to disallow any options which would alter the
270 // driver behavior; that isn't going to work in our model. We
271 // use isDriverOption() as an approximation, although things
272 // like -O4 are going to slip through.
273 if (!XarchArg || Index > Prev + 1) {
274 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
275 << A->getAsString(Args);
276 continue;
277 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
278 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
279 << A->getAsString(Args);
280 continue;
281 }
282 XarchArg->setBaseArg(A);
283 A = XarchArg.release();
284 DAL->AddSynthesizedArg(A);
285 }
286 DAL->append(A);
287 }
288
289 if (!BoundArch.empty()) {
290 DAL->eraseArg(options::OPT_march_EQ);
291 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), BoundArch);
292 }
293
294 return DAL;
295}
296
297Tool *HIPToolChain::buildLinker() const {
298 assert(getTriple().getArch() == llvm::Triple::amdgcn);
299 return new tools::AMDGCN::Linker(*this);
300}
301
302void HIPToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
303 HostTC.addClangWarningOptions(CC1Args);
304}
305
306ToolChain::CXXStdlibType
307HIPToolChain::GetCXXStdlibType(const ArgList &Args) const {
308 return HostTC.GetCXXStdlibType(Args);
309}
310
311void HIPToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
312 ArgStringList &CC1Args) const {
313 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
314}
315
316void HIPToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
317 ArgStringList &CC1Args) const {
318 HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
319}
320
321void HIPToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
322 ArgStringList &CC1Args) const {
323 HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
324}
325
326SanitizerMask HIPToolChain::getSupportedSanitizers() const {
327 // The HIPToolChain only supports sanitizers in the sense that it allows
328 // sanitizer arguments on the command line if they are supported by the host
329 // toolchain. The HIPToolChain will actually ignore any command line
330 // arguments for any of these "supported" sanitizers. That means that no
331 // sanitization of device code is actually supported at this time.
332 //
333 // This behavior is necessary because the host and device toolchains
334 // invocations often share the command line, so the device toolchain must
335 // tolerate flags meant only for the host toolchain.
336 return HostTC.getSupportedSanitizers();
337}
338
339VersionTuple HIPToolChain::computeMSVCVersion(const Driver *D,
340 const ArgList &Args) const {
341 return HostTC.computeMSVCVersion(D, Args);
342}