Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Driver/ToolChains/HIPUtility.cpp
Line
Count
Source (jump to first uncovered line)
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 "CommonArgs.h"
11
#include "clang/Driver/Compilation.h"
12
#include "llvm/ADT/StringRef.h"
13
#include "llvm/Support/Path.h"
14
#include "llvm/TargetParser/Triple.h"
15
16
using namespace clang::driver;
17
using namespace clang::driver::tools;
18
using namespace llvm::opt;
19
20
#if defined(_WIN32) || defined(_WIN64)
21
#define NULL_FILE "nul"
22
#else
23
0
#define NULL_FILE "/dev/null"
24
#endif
25
26
namespace {
27
const unsigned HIPCodeObjectAlign = 4096;
28
} // namespace
29
30
// Constructs a triple string for clang offload bundler.
31
static std::string normalizeForBundler(const llvm::Triple &T,
32
0
                                       bool HasTargetID) {
33
0
  return HasTargetID ? (T.getArchName() + "-" + T.getVendorName() + "-" +
34
0
                        T.getOSName() + "-" + T.getEnvironmentName())
35
0
                           .str()
36
0
                     : T.normalize();
37
0
}
38
39
// Construct a clang-offload-bundler command to bundle code objects for
40
// different devices into a HIP fat binary.
41
void HIP::constructHIPFatbinCommand(Compilation &C, const JobAction &JA,
42
                                    llvm::StringRef OutputFileName,
43
                                    const InputInfoList &Inputs,
44
                                    const llvm::opt::ArgList &Args,
45
0
                                    const Tool &T) {
46
  // Construct clang-offload-bundler command to bundle object files for
47
  // for different GPU archs.
48
0
  ArgStringList BundlerArgs;
49
0
  BundlerArgs.push_back(Args.MakeArgString("-type=o"));
50
0
  BundlerArgs.push_back(
51
0
      Args.MakeArgString("-bundle-align=" + Twine(HIPCodeObjectAlign)));
52
53
  // ToDo: Remove the dummy host binary entry which is required by
54
  // clang-offload-bundler.
55
0
  std::string BundlerTargetArg = "-targets=host-x86_64-unknown-linux";
56
  // AMDGCN:
57
  // For code object version 2 and 3, the offload kind in bundle ID is 'hip'
58
  // for backward compatibility. For code object version 4 and greater, the
59
  // offload kind in bundle ID is 'hipv4'.
60
0
  std::string OffloadKind = "hip";
61
0
  auto &TT = T.getToolChain().getTriple();
62
0
  if (TT.isAMDGCN() && getAMDGPUCodeObjectVersion(C.getDriver(), Args) >= 4)
63
0
    OffloadKind = OffloadKind + "v4";
64
0
  for (const auto &II : Inputs) {
65
0
    const auto *A = II.getAction();
66
0
    auto ArchStr = llvm::StringRef(A->getOffloadingArch());
67
0
    BundlerTargetArg +=
68
0
        "," + OffloadKind + "-" + normalizeForBundler(TT, !ArchStr.empty());
69
0
    if (!ArchStr.empty())
70
0
      BundlerTargetArg += "-" + ArchStr.str();
71
0
  }
72
0
  BundlerArgs.push_back(Args.MakeArgString(BundlerTargetArg));
73
74
  // Use a NULL file as input for the dummy host binary entry
75
0
  std::string BundlerInputArg = "-input=" NULL_FILE;
76
0
  BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg));
77
0
  for (const auto &II : Inputs) {
78
0
    BundlerInputArg = std::string("-input=") + II.getFilename();
79
0
    BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg));
80
0
  }
81
82
0
  std::string Output = std::string(OutputFileName);
83
0
  auto *BundlerOutputArg =
84
0
      Args.MakeArgString(std::string("-output=").append(Output));
85
0
  BundlerArgs.push_back(BundlerOutputArg);
86
87
0
  if (Args.hasFlag(options::OPT_offload_compress,
88
0
                   options::OPT_no_offload_compress, false))
89
0
    BundlerArgs.push_back("-compress");
90
0
  if (Args.hasArg(options::OPT_v))
91
0
    BundlerArgs.push_back("-verbose");
92
93
0
  const char *Bundler = Args.MakeArgString(
94
0
      T.getToolChain().GetProgramPath("clang-offload-bundler"));
95
0
  C.addCommand(std::make_unique<Command>(
96
0
      JA, T, ResponseFileSupport::None(), Bundler, BundlerArgs, Inputs,
97
0
      InputInfo(&JA, Args.MakeArgString(Output))));
98
0
}
99
100
/// Add Generated HIP Object File which has device images embedded into the
101
/// host to the argument list for linking. Using MC directives, embed the
102
/// device code and also define symbols required by the code generation so that
103
/// the image can be retrieved at runtime.
104
void HIP::constructGenerateObjFileFromHIPFatBinary(
105
    Compilation &C, const InputInfo &Output, const InputInfoList &Inputs,
106
0
    const ArgList &Args, const JobAction &JA, const Tool &T) {
107
0
  const ToolChain &TC = T.getToolChain();
108
0
  std::string Name = std::string(llvm::sys::path::stem(Output.getFilename()));
109
110
  // Create Temp Object File Generator,
111
  // Offload Bundled file and Bundled Object file.
112
  // Keep them if save-temps is enabled.
113
0
  const char *McinFile;
114
0
  const char *BundleFile;
115
0
  if (C.getDriver().isSaveTempsEnabled()) {
116
0
    McinFile = C.getArgs().MakeArgString(Name + ".mcin");
117
0
    BundleFile = C.getArgs().MakeArgString(Name + ".hipfb");
118
0
  } else {
119
0
    auto TmpNameMcin = C.getDriver().GetTemporaryPath(Name, "mcin");
120
0
    McinFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameMcin));
121
0
    auto TmpNameFb = C.getDriver().GetTemporaryPath(Name, "hipfb");
122
0
    BundleFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameFb));
123
0
  }
124
0
  HIP::constructHIPFatbinCommand(C, JA, BundleFile, Inputs, Args, T);
125
126
  // Create a buffer to write the contents of the temp obj generator.
127
0
  std::string ObjBuffer;
128
0
  llvm::raw_string_ostream ObjStream(ObjBuffer);
129
130
0
  auto HostTriple =
131
0
      C.getSingleOffloadToolChain<Action::OFK_Host>()->getTriple();
132
133
  // Add MC directives to embed target binaries. We ensure that each
134
  // section and image is 16-byte aligned. This is not mandatory, but
135
  // increases the likelihood of data to be aligned with a cache block
136
  // in several main host machines.
137
0
  ObjStream << "#       HIP Object Generator\n";
138
0
  ObjStream << "# *** Automatically generated by Clang ***\n";
139
0
  if (HostTriple.isWindowsMSVCEnvironment()) {
140
0
    ObjStream << "  .section .hip_fatbin, \"dw\"\n";
141
0
  } else {
142
0
    ObjStream << "  .protected __hip_fatbin\n";
143
0
    ObjStream << "  .type __hip_fatbin,@object\n";
144
0
    ObjStream << "  .section .hip_fatbin,\"a\",@progbits\n";
145
0
  }
146
0
  ObjStream << "  .globl __hip_fatbin\n";
147
0
  ObjStream << "  .p2align " << llvm::Log2(llvm::Align(HIPCodeObjectAlign))
148
0
            << "\n";
149
0
  ObjStream << "__hip_fatbin:\n";
150
0
  ObjStream << "  .incbin ";
151
0
  llvm::sys::printArg(ObjStream, BundleFile, /*Quote=*/true);
152
0
  ObjStream << "\n";
153
0
  if (HostTriple.isOSLinux() && HostTriple.isOSBinFormatELF())
154
0
    ObjStream << "  .section .note.GNU-stack, \"\", @progbits\n";
155
0
  ObjStream.flush();
156
157
  // Dump the contents of the temp object file gen if the user requested that.
158
  // We support this option to enable testing of behavior with -###.
159
0
  if (C.getArgs().hasArg(options::OPT_fhip_dump_offload_linker_script))
160
0
    llvm::errs() << ObjBuffer;
161
162
  // Open script file and write the contents.
163
0
  std::error_code EC;
164
0
  llvm::raw_fd_ostream Objf(McinFile, EC, llvm::sys::fs::OF_None);
165
166
0
  if (EC) {
167
0
    C.getDriver().Diag(clang::diag::err_unable_to_make_temp) << EC.message();
168
0
    return;
169
0
  }
170
171
0
  Objf << ObjBuffer;
172
173
0
  ArgStringList McArgs{"-triple", Args.MakeArgString(HostTriple.normalize()),
174
0
                       "-o",      Output.getFilename(),
175
0
                       McinFile,  "--filetype=obj"};
176
0
  const char *Mc = Args.MakeArgString(TC.GetProgramPath("llvm-mc"));
177
0
  C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(), Mc,
178
0
                                         McArgs, Inputs, Output));
179
0
}