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