1//===--- HIPUtility.cpp - Common HIP Tool Chain Utilities -------*- 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 "HIPUtility.h"
10#include "clang/Basic/TargetID.h"
11#include "clang/Driver/CommonArgs.h"
12#include "clang/Driver/Compilation.h"
13#include "clang/Options/Options.h"
14#include "llvm/Support/Path.h"
15
16using namespace clang;
17using namespace clang::driver;
18using namespace clang::driver::tools;
19using namespace llvm::opt;
20
21#if defined(_WIN32) || defined(_WIN64)
22#define NULL_FILE "nul"
23#else
24#define NULL_FILE "/dev/null"
25#endif
26
27namespace {
28const unsigned HIPCodeObjectAlign = 4096;
29} // namespace
30
31// Construct a clang-offload-bundler command to bundle code objects for
32// different devices into a HIP fat binary.
33void HIP::constructHIPFatbinCommand(Compilation &C, const JobAction &JA,
34 llvm::StringRef OutputFileName,
35 const InputInfoList &Inputs,
36 const llvm::opt::ArgList &Args,
37 const Tool &T) {
38 // Construct clang-offload-bundler command to bundle object files for
39 // for different GPU archs.
40 ArgStringList BundlerArgs;
41 BundlerArgs.push_back(Elt: Args.MakeArgString(Str: "-type=o"));
42 BundlerArgs.push_back(
43 Elt: Args.MakeArgString(Str: "-bundle-align=" + Twine(HIPCodeObjectAlign)));
44
45 // ToDo: Remove the dummy host binary entry which is required by
46 // clang-offload-bundler.
47 std::string BundlerTargetArg = "-targets=host-x86_64-unknown-linux-gnu";
48 // AMDGCN:
49 // For code object version 2 and 3, the offload kind in bundle ID is 'hip'
50 // for backward compatibility. For code object version 4 and greater, the
51 // offload kind in bundle ID is 'hipv4'.
52 std::string OffloadKind = "hip";
53 if (T.getToolChain().getTriple().isAMDGCN() &&
54 getAMDGPUCodeObjectVersion(D: C.getDriver(), Args) >= 4)
55 OffloadKind = OffloadKind + "v4";
56 for (const auto &II : Inputs) {
57 const auto *A = II.getAction();
58 const llvm::Triple &InputTriple = A->getOffloadingToolChain()->getTriple();
59
60 BoundArch BA = A->getOffloadingArch();
61 BundlerTargetArg += ',' + OffloadKind + '-';
62 if (BA.ArchName == "amdgcnspirv")
63 BundlerTargetArg += "spirv64-amd-amdhsa-";
64 else
65 BundlerTargetArg += normalizeForBundler(T: InputTriple, BoundArch: BA.ArchName);
66 if (BA)
67 BundlerTargetArg += '-' + BA.ArchName.str();
68 }
69 BundlerArgs.push_back(Elt: Args.MakeArgString(Str: BundlerTargetArg));
70
71 // Use a NULL file as input for the dummy host binary entry
72 std::string BundlerInputArg = "-input=" NULL_FILE;
73 BundlerArgs.push_back(Elt: Args.MakeArgString(Str: BundlerInputArg));
74 for (const auto &II : Inputs) {
75 BundlerInputArg = std::string("-input=") + II.getFilename();
76 BundlerArgs.push_back(Elt: Args.MakeArgString(Str: BundlerInputArg));
77 }
78
79 std::string Output = std::string(OutputFileName);
80 auto *BundlerOutputArg =
81 Args.MakeArgString(Str: std::string("-output=").append(str: Output));
82 BundlerArgs.push_back(Elt: BundlerOutputArg);
83
84 addOffloadCompressArgs(TCArgs: Args, CmdArgs&: BundlerArgs);
85
86 const char *Bundler = Args.MakeArgString(
87 Str: T.getToolChain().GetProgramPath(Name: "clang-offload-bundler"));
88 C.addCommand(Cmd: std::make_unique<Command>(
89 args: JA, args: T, args: ResponseFileSupport::None(), args&: Bundler, args&: BundlerArgs, args: Inputs,
90 args: InputInfo(&JA, Args.MakeArgString(Str: Output))));
91}
92
93// Convenience function for creating temporary file for both modes of
94// isSaveTempsEnabled().
95const char *HIP::getTempFile(Compilation &C, StringRef Prefix,
96 StringRef Extension) {
97 if (C.getDriver().isSaveTempsEnabled()) {
98 return C.getArgs().MakeArgString(Str: Prefix + "." + Extension);
99 }
100 auto TmpFile = C.getDriver().GetTemporaryPath(Prefix, Suffix: Extension);
101 return C.addTempFile(Name: C.getArgs().MakeArgString(Str: TmpFile));
102}
103