blob: 1e881502ee874f27c9fe31c99864570da709db07 [file] [log] [blame]
Yaxun Liuf6144222018-05-30 00:53:50 +00001//===--- HIP.cpp - HIP Tool and ToolChain Implementations -------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Yaxun Liuf6144222018-05-30 00:53:50 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "HIP.h"
10#include "CommonArgs.h"
11#include "InputInfo.h"
12#include "clang/Basic/Cuda.h"
13#include "clang/Driver/Compilation.h"
14#include "clang/Driver/Driver.h"
15#include "clang/Driver/DriverDiagnostic.h"
16#include "clang/Driver/Options.h"
17#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/Path.h"
19
20using namespace clang::driver;
21using namespace clang::driver::toolchains;
22using namespace clang::driver::tools;
23using namespace clang;
24using namespace llvm::opt;
25
Yaxun Liu4fa83fc2019-01-10 20:09:52 +000026#if _WIN32 || _WIN64
27#define NULL_FILE "nul"
28#else
29#define NULL_FILE "/dev/null"
30#endif
31
Yaxun Liuf6144222018-05-30 00:53:50 +000032namespace {
33
Yaxun Liu7bd8c372019-04-12 16:23:31 +000034static void addBCLib(const Driver &D, const ArgList &Args,
Yaxun Liuf6144222018-05-30 00:53:50 +000035 ArgStringList &CmdArgs, ArgStringList LibraryPaths,
36 StringRef BCName) {
37 StringRef FullName;
38 for (std::string LibraryPath : LibraryPaths) {
39 SmallString<128> Path(LibraryPath);
40 llvm::sys::path::append(Path, BCName);
41 FullName = Path;
42 if (llvm::sys::fs::exists(FullName)) {
Yaxun Liu7bd8c372019-04-12 16:23:31 +000043 CmdArgs.push_back("-mlink-builtin-bitcode");
Yaxun Liuf6144222018-05-30 00:53:50 +000044 CmdArgs.push_back(Args.MakeArgString(FullName));
45 return;
46 }
47 }
Yaxun Liu7bd8c372019-04-12 16:23:31 +000048 D.Diag(diag::err_drv_no_such_file) << BCName;
Yaxun Liuf6144222018-05-30 00:53:50 +000049}
50
51} // namespace
52
53const char *AMDGCN::Linker::constructLLVMLinkCommand(
54 Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
55 const ArgList &Args, StringRef SubArchName,
56 StringRef OutputFilePrefix) const {
57 ArgStringList CmdArgs;
58 // Add the input bc's created by compile step.
59 for (const auto &II : Inputs)
60 CmdArgs.push_back(II.getFilename());
61
Yaxun Liuf6144222018-05-30 00:53:50 +000062 // Add an intermediate output file.
63 CmdArgs.push_back("-o");
64 std::string TmpName =
65 C.getDriver().GetTemporaryPath(OutputFilePrefix.str() + "-linked", "bc");
66 const char *OutputFileName =
67 C.addTempFile(C.getArgs().MakeArgString(TmpName));
68 CmdArgs.push_back(OutputFileName);
69 SmallString<128> ExecPath(C.getDriver().Dir);
70 llvm::sys::path::append(ExecPath, "llvm-link");
71 const char *Exec = Args.MakeArgString(ExecPath);
72 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
73 return OutputFileName;
74}
75
76const char *AMDGCN::Linker::constructOptCommand(
77 Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
78 const llvm::opt::ArgList &Args, llvm::StringRef SubArchName,
79 llvm::StringRef OutputFilePrefix, const char *InputFileName) const {
80 // Construct opt command.
81 ArgStringList OptArgs;
82 // The input to opt is the output from llvm-link.
83 OptArgs.push_back(InputFileName);
84 // Pass optimization arg to opt.
85 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
86 StringRef OOpt = "3";
87 if (A->getOption().matches(options::OPT_O4) ||
88 A->getOption().matches(options::OPT_Ofast))
89 OOpt = "3";
90 else if (A->getOption().matches(options::OPT_O0))
91 OOpt = "0";
92 else if (A->getOption().matches(options::OPT_O)) {
93 // -Os, -Oz, and -O(anything else) map to -O2
94 OOpt = llvm::StringSwitch<const char *>(A->getValue())
95 .Case("1", "1")
96 .Case("2", "2")
97 .Case("3", "3")
98 .Case("s", "2")
99 .Case("z", "2")
100 .Default("2");
101 }
102 OptArgs.push_back(Args.MakeArgString("-O" + OOpt));
103 }
104 OptArgs.push_back("-mtriple=amdgcn-amd-amdhsa");
105 OptArgs.push_back(Args.MakeArgString("-mcpu=" + SubArchName));
Aaron Enye Shi04fddc92019-03-15 17:31:51 +0000106
107 for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
108 OptArgs.push_back(A->getValue(0));
109 }
110
Yaxun Liuf6144222018-05-30 00:53:50 +0000111 OptArgs.push_back("-o");
112 std::string TmpFileName = C.getDriver().GetTemporaryPath(
113 OutputFilePrefix.str() + "-optimized", "bc");
114 const char *OutputFileName =
115 C.addTempFile(C.getArgs().MakeArgString(TmpFileName));
116 OptArgs.push_back(OutputFileName);
117 SmallString<128> OptPath(C.getDriver().Dir);
118 llvm::sys::path::append(OptPath, "opt");
119 const char *OptExec = Args.MakeArgString(OptPath);
120 C.addCommand(llvm::make_unique<Command>(JA, *this, OptExec, OptArgs, Inputs));
121 return OutputFileName;
122}
123
124const char *AMDGCN::Linker::constructLlcCommand(
125 Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
126 const llvm::opt::ArgList &Args, llvm::StringRef SubArchName,
127 llvm::StringRef OutputFilePrefix, const char *InputFileName) const {
128 // Construct llc command.
129 ArgStringList LlcArgs{InputFileName, "-mtriple=amdgcn-amd-amdhsa",
Yaxun Liufd2c5c02019-02-28 17:08:26 +0000130 "-filetype=obj", "-mattr=-code-object-v3",
Aaron Enye Shi7084b562019-02-13 16:12:16 +0000131 Args.MakeArgString("-mcpu=" + SubArchName)};
132
133 // Extract all the -m options
134 std::vector<llvm::StringRef> Features;
135 handleTargetFeaturesGroup(
136 Args, Features, options::OPT_m_amdgpu_Features_Group);
137
Yaxun Liufd2c5c02019-02-28 17:08:26 +0000138 // Add features to mattr such as xnack
Aaron Enye Shi7084b562019-02-13 16:12:16 +0000139 std::string MAttrString = "-mattr=";
140 for(auto OneFeature : Features) {
141 MAttrString.append(Args.MakeArgString(OneFeature));
142 if (OneFeature != Features.back())
143 MAttrString.append(",");
144 }
145 if(!Features.empty())
146 LlcArgs.push_back(Args.MakeArgString(MAttrString));
147
Aaron Enye Shi04fddc92019-03-15 17:31:51 +0000148 for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
149 LlcArgs.push_back(A->getValue(0));
150 }
151
Aaron Enye Shi7084b562019-02-13 16:12:16 +0000152 // Add output filename
153 LlcArgs.push_back("-o");
Yaxun Liuf6144222018-05-30 00:53:50 +0000154 std::string LlcOutputFileName =
155 C.getDriver().GetTemporaryPath(OutputFilePrefix, "o");
156 const char *LlcOutputFile =
157 C.addTempFile(C.getArgs().MakeArgString(LlcOutputFileName));
158 LlcArgs.push_back(LlcOutputFile);
159 SmallString<128> LlcPath(C.getDriver().Dir);
160 llvm::sys::path::append(LlcPath, "llc");
161 const char *Llc = Args.MakeArgString(LlcPath);
162 C.addCommand(llvm::make_unique<Command>(JA, *this, Llc, LlcArgs, Inputs));
163 return LlcOutputFile;
164}
165
166void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
167 const InputInfoList &Inputs,
168 const InputInfo &Output,
169 const llvm::opt::ArgList &Args,
170 const char *InputFileName) const {
171 // Construct lld command.
172 // The output from ld.lld is an HSA code object file.
173 ArgStringList LldArgs{"-flavor", "gnu", "--no-undefined",
174 "-shared", "-o", Output.getFilename(),
175 InputFileName};
176 SmallString<128> LldPath(C.getDriver().Dir);
177 llvm::sys::path::append(LldPath, "lld");
178 const char *Lld = Args.MakeArgString(LldPath);
179 C.addCommand(llvm::make_unique<Command>(JA, *this, Lld, LldArgs, Inputs));
180}
181
Yaxun Liu97670892018-10-02 17:48:54 +0000182// Construct a clang-offload-bundler command to bundle code objects for
183// different GPU's into a HIP fat binary.
184void AMDGCN::constructHIPFatbinCommand(Compilation &C, const JobAction &JA,
185 StringRef OutputFileName, const InputInfoList &Inputs,
186 const llvm::opt::ArgList &Args, const Tool& T) {
187 // Construct clang-offload-bundler command to bundle object files for
188 // for different GPU archs.
189 ArgStringList BundlerArgs;
190 BundlerArgs.push_back(Args.MakeArgString("-type=o"));
191
192 // ToDo: Remove the dummy host binary entry which is required by
193 // clang-offload-bundler.
194 std::string BundlerTargetArg = "-targets=host-x86_64-unknown-linux";
Yaxun Liu4fa83fc2019-01-10 20:09:52 +0000195 std::string BundlerInputArg = "-inputs=" NULL_FILE;
Yaxun Liu97670892018-10-02 17:48:54 +0000196
197 for (const auto &II : Inputs) {
198 const auto* A = II.getAction();
199 BundlerTargetArg = BundlerTargetArg + ",hip-amdgcn-amd-amdhsa-" +
200 StringRef(A->getOffloadingArch()).str();
201 BundlerInputArg = BundlerInputArg + "," + II.getFilename();
202 }
203 BundlerArgs.push_back(Args.MakeArgString(BundlerTargetArg));
204 BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg));
205
206 auto BundlerOutputArg =
207 Args.MakeArgString(std::string("-outputs=").append(OutputFileName));
208 BundlerArgs.push_back(BundlerOutputArg);
209
210 SmallString<128> BundlerPath(C.getDriver().Dir);
211 llvm::sys::path::append(BundlerPath, "clang-offload-bundler");
212 const char *Bundler = Args.MakeArgString(BundlerPath);
213 C.addCommand(llvm::make_unique<Command>(JA, T, Bundler, BundlerArgs, Inputs));
214}
215
Yaxun Liuf6144222018-05-30 00:53:50 +0000216// For amdgcn the inputs of the linker job are device bitcode and output is
217// object file. It calls llvm-link, opt, llc, then lld steps.
218void AMDGCN::Linker::ConstructJob(Compilation &C, const JobAction &JA,
219 const InputInfo &Output,
220 const InputInfoList &Inputs,
221 const ArgList &Args,
222 const char *LinkingOutput) const {
223
Yaxun Liu97670892018-10-02 17:48:54 +0000224 if (JA.getType() == types::TY_HIP_FATBIN)
225 return constructHIPFatbinCommand(C, JA, Output.getFilename(), Inputs, Args, *this);
226
Sam McCall43fdd222018-05-30 08:03:43 +0000227 assert(getToolChain().getTriple().getArch() == llvm::Triple::amdgcn &&
Yaxun Liuf6144222018-05-30 00:53:50 +0000228 "Unsupported target");
229
230 std::string SubArchName = JA.getOffloadingArch();
231 assert(StringRef(SubArchName).startswith("gfx") && "Unsupported sub arch");
232
233 // Prefix for temporary file name.
234 std::string Prefix =
235 llvm::sys::path::stem(Inputs[0].getFilename()).str() + "-" + SubArchName;
236
237 // Each command outputs different files.
238 const char *LLVMLinkCommand =
239 constructLLVMLinkCommand(C, JA, Inputs, Args, SubArchName, Prefix);
240 const char *OptCommand = constructOptCommand(C, JA, Inputs, Args, SubArchName,
241 Prefix, LLVMLinkCommand);
242 const char *LlcCommand =
243 constructLlcCommand(C, JA, Inputs, Args, SubArchName, Prefix, OptCommand);
244 constructLldCommand(C, JA, Inputs, Output, Args, LlcCommand);
245}
246
247HIPToolChain::HIPToolChain(const Driver &D, const llvm::Triple &Triple,
248 const ToolChain &HostTC, const ArgList &Args)
249 : ToolChain(D, Triple, Args), HostTC(HostTC) {
250 // Lookup binaries into the driver directory, this is used to
251 // discover the clang-offload-bundler executable.
252 getProgramPaths().push_back(getDriver().Dir);
253}
254
255void HIPToolChain::addClangTargetOptions(
256 const llvm::opt::ArgList &DriverArgs,
257 llvm::opt::ArgStringList &CC1Args,
258 Action::OffloadKind DeviceOffloadingKind) const {
259 HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind);
260
261 StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_march_EQ);
262 assert(!GpuArch.empty() && "Must have an explicit GPU arch.");
Sam McCall43fdd222018-05-30 08:03:43 +0000263 (void) GpuArch;
Yaxun Liuf6144222018-05-30 00:53:50 +0000264 assert(DeviceOffloadingKind == Action::OFK_HIP &&
265 "Only HIP offloading kinds are supported for GPUs.");
266
Yaxun Liu6c3a74e2018-07-24 01:40:44 +0000267 CC1Args.push_back("-target-cpu");
268 CC1Args.push_back(DriverArgs.MakeArgStringRef(GpuArch));
Yaxun Liuf6144222018-05-30 00:53:50 +0000269 CC1Args.push_back("-fcuda-is-device");
270
271 if (DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
272 options::OPT_fno_cuda_flush_denormals_to_zero, false))
273 CC1Args.push_back("-fcuda-flush-denormals-to-zero");
274
275 if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
276 options::OPT_fno_cuda_approx_transcendentals, false))
277 CC1Args.push_back("-fcuda-approx-transcendentals");
278
Yaxun Liu97670892018-10-02 17:48:54 +0000279 if (DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
Yaxun Liuf6144222018-05-30 00:53:50 +0000280 false))
Yaxun Liu97670892018-10-02 17:48:54 +0000281 CC1Args.push_back("-fgpu-rdc");
Yaxun Liu5e98c2b2018-08-30 15:10:20 +0000282
283 // Default to "hidden" visibility, as object level linking will not be
284 // supported for the foreseeable future.
285 if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
Scott Linderbef26632019-01-28 17:12:19 +0000286 options::OPT_fvisibility_ms_compat)) {
Yaxun Liu5e98c2b2018-08-30 15:10:20 +0000287 CC1Args.append({"-fvisibility", "hidden"});
Scott Linderbef26632019-01-28 17:12:19 +0000288 CC1Args.push_back("-fapply-global-visibility-to-externs");
289 }
Yaxun Liu7bd8c372019-04-12 16:23:31 +0000290 ArgStringList LibraryPaths;
291
292 // Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
293 for (auto Path :
294 DriverArgs.getAllArgValues(options::OPT_hip_device_lib_path_EQ))
295 LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
296
297 addDirectoryList(DriverArgs, LibraryPaths, "-L", "HIP_DEVICE_LIB_PATH");
298
299 llvm::SmallVector<std::string, 10> BCLibs;
300
301 // Add bitcode library in --hip-device-lib.
302 for (auto Lib : DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ)) {
303 BCLibs.push_back(DriverArgs.MakeArgString(Lib));
304 }
305
306 // If --hip-device-lib is not set, add the default bitcode libraries.
307 if (BCLibs.empty()) {
308 // Get the bc lib file name for ISA version. For example,
309 // gfx803 => oclc_isa_version_803.amdgcn.bc.
310 std::string ISAVerBC =
311 "oclc_isa_version_" + GpuArch.drop_front(3).str() + ".amdgcn.bc";
312
313 llvm::StringRef FlushDenormalControlBC;
314 if (DriverArgs.hasArg(options::OPT_fcuda_flush_denormals_to_zero))
315 FlushDenormalControlBC = "oclc_daz_opt_on.amdgcn.bc";
316 else
317 FlushDenormalControlBC = "oclc_daz_opt_off.amdgcn.bc";
318
319 BCLibs.append({"hip.amdgcn.bc", "opencl.amdgcn.bc", "ocml.amdgcn.bc",
320 "ockl.amdgcn.bc", "oclc_finite_only_off.amdgcn.bc",
321 FlushDenormalControlBC,
322 "oclc_correctly_rounded_sqrt_on.amdgcn.bc",
323 "oclc_unsafe_math_off.amdgcn.bc", ISAVerBC});
324 }
325 for (auto Lib : BCLibs)
326 addBCLib(getDriver(), DriverArgs, CC1Args, LibraryPaths, Lib);
327
Yaxun Liuf6144222018-05-30 00:53:50 +0000328}
329
330llvm::opt::DerivedArgList *
331HIPToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
332 StringRef BoundArch,
333 Action::OffloadKind DeviceOffloadKind) const {
334 DerivedArgList *DAL =
335 HostTC.TranslateArgs(Args, BoundArch, DeviceOffloadKind);
336 if (!DAL)
337 DAL = new DerivedArgList(Args.getBaseArgs());
338
339 const OptTable &Opts = getDriver().getOpts();
340
341 for (Arg *A : Args) {
342 if (A->getOption().matches(options::OPT_Xarch__)) {
Aaron Enye Shi4928d512018-06-26 17:12:29 +0000343 // Skip this argument unless the architecture matches BoundArch.
Yaxun Liuf6144222018-05-30 00:53:50 +0000344 if (BoundArch.empty() || A->getValue(0) != BoundArch)
345 continue;
346
347 unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
348 unsigned Prev = Index;
349 std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
350
351 // If the argument parsing failed or more than one argument was
352 // consumed, the -Xarch_ argument's parameter tried to consume
353 // extra arguments. Emit an error and ignore.
354 //
355 // We also want to disallow any options which would alter the
356 // driver behavior; that isn't going to work in our model. We
357 // use isDriverOption() as an approximation, although things
358 // like -O4 are going to slip through.
359 if (!XarchArg || Index > Prev + 1) {
360 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
361 << A->getAsString(Args);
362 continue;
363 } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
364 getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
365 << A->getAsString(Args);
366 continue;
367 }
368 XarchArg->setBaseArg(A);
369 A = XarchArg.release();
370 DAL->AddSynthesizedArg(A);
371 }
372 DAL->append(A);
373 }
374
375 if (!BoundArch.empty()) {
376 DAL->eraseArg(options::OPT_march_EQ);
377 DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ), BoundArch);
378 }
379
380 return DAL;
381}
382
383Tool *HIPToolChain::buildLinker() const {
384 assert(getTriple().getArch() == llvm::Triple::amdgcn);
385 return new tools::AMDGCN::Linker(*this);
386}
387
388void HIPToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {
389 HostTC.addClangWarningOptions(CC1Args);
390}
391
392ToolChain::CXXStdlibType
393HIPToolChain::GetCXXStdlibType(const ArgList &Args) const {
394 return HostTC.GetCXXStdlibType(Args);
395}
396
397void HIPToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
398 ArgStringList &CC1Args) const {
399 HostTC.AddClangSystemIncludeArgs(DriverArgs, CC1Args);
400}
401
402void HIPToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &Args,
403 ArgStringList &CC1Args) const {
404 HostTC.AddClangCXXStdlibIncludeArgs(Args, CC1Args);
405}
406
407void HIPToolChain::AddIAMCUIncludeArgs(const ArgList &Args,
408 ArgStringList &CC1Args) const {
409 HostTC.AddIAMCUIncludeArgs(Args, CC1Args);
410}
411
412SanitizerMask HIPToolChain::getSupportedSanitizers() const {
413 // The HIPToolChain only supports sanitizers in the sense that it allows
414 // sanitizer arguments on the command line if they are supported by the host
415 // toolchain. The HIPToolChain will actually ignore any command line
416 // arguments for any of these "supported" sanitizers. That means that no
417 // sanitization of device code is actually supported at this time.
418 //
419 // This behavior is necessary because the host and device toolchains
420 // invocations often share the command line, so the device toolchain must
421 // tolerate flags meant only for the host toolchain.
422 return HostTC.getSupportedSanitizers();
423}
424
425VersionTuple HIPToolChain::computeMSVCVersion(const Driver *D,
426 const ArgList &Args) const {
427 return HostTC.computeMSVCVersion(D, Args);
428}