| 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/Driver/CommonArgs.h" |
| 11 |
#include "clang/Driver/Compilation.h" |
| 12 |
#include "clang/Options/Options.h" |
| 13 |
#include "llvm/ADT/StringExtras.h" |
| 14 |
#include "llvm/ADT/StringRef.h" |
| 15 |
#include "llvm/Object/Archive.h" |
| 16 |
#include "llvm/Object/ObjectFile.h" |
| 17 |
#include "llvm/Support/MD5.h" |
| 18 |
#include "llvm/Support/MemoryBuffer.h" |
| 19 |
#include "llvm/Support/Path.h" |
| 20 |
#include "llvm/Support/raw_ostream.h" |
| 21 |
#include "llvm/TargetParser/Triple.h" |
| 22 |
#include <deque> |
| 23 |
#include <set> |
| 24 |
|
| 25 |
using namespace clang; |
| 26 |
using namespace clang::driver; |
| 27 |
using namespace clang::driver::tools; |
| 28 |
using namespace llvm::opt; |
| 29 |
using llvm::dyn_cast; |
| 30 |
|
| 31 |
#if defined(_WIN32) || defined(_WIN64) |
| 32 |
#define NULL_FILE "nul" |
| 33 |
#else |
| 34 |
#define NULL_FILE "/dev/null" |
| 35 |
#endif |
| 36 |
|
| 37 |
namespace { |
| 38 |
const unsigned HIPCodeObjectAlign = 4096; |
| 39 |
} // namespace |
| 40 |
|
| 41 |
// Constructs a triple string for clang offload bundler. |
| 42 |
static std::string normalizeForBundler(const llvm::Triple &T, |
| 43 |
bool HasTargetID) { |
| 44 |
return HasTargetID ? (T.getArchName() + "-" + T.getVendorName() + "-" + |
| 45 |
T.getOSName() + "-" + T.getEnvironmentName()) |
| 46 |
.str() |
| 47 |
: T.normalize(llvm::Triple::CanonicalForm::FOUR_IDENT); |
| 48 |
} |
| 49 |
|
| 50 |
// Collect undefined __hip_fatbin* and __hip_gpubin_handle* symbols from all |
| 51 |
// input object or archive files. |
| 52 |
class HIPUndefinedFatBinSymbols { |
| 53 |
public: |
| 54 |
HIPUndefinedFatBinSymbols(const Compilation &C, |
| 55 |
const llvm::opt::ArgList &Args_) |
| 56 |
: C(C), Args(Args_), |
| 57 |
DiagID(C.getDriver().getDiags().getCustomDiagID( |
| 58 |
DiagnosticsEngine::Error, |
| 59 |
"Error collecting HIP undefined fatbin symbols: %0")), |
| 60 |
Quiet(C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)), |
| 61 |
Verbose(C.getArgs().hasArg(options::OPT_v)) { |
| 62 |
populateSymbols(); |
| 63 |
processStaticLibraries(); |
| 64 |
if (Verbose) { |
| 65 |
for (const auto &Name : FatBinSymbols) |
| 66 |
llvm::errs() << "Found undefined HIP fatbin symbol: " << Name << "\n"; |
| 67 |
for (const auto &Name : GPUBinHandleSymbols) |
| 68 |
llvm::errs() << "Found undefined HIP gpubin handle symbol: " << Name |
| 69 |
<< "\n"; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
const std::set<std::string> &getFatBinSymbols() const { |
| 74 |
return FatBinSymbols; |
| 75 |
} |
| 76 |
|
| 77 |
const std::set<std::string> &getGPUBinHandleSymbols() const { |
| 78 |
return GPUBinHandleSymbols; |
| 79 |
} |
| 80 |
|
| 81 |
// Collect symbols from static libraries specified by -l options. |
| 82 |
void processStaticLibraries() { |
| 83 |
llvm::SmallVector<llvm::StringRef, 16> LibNames; |
| 84 |
llvm::SmallVector<llvm::StringRef, 16> LibPaths; |
| 85 |
llvm::SmallVector<llvm::StringRef, 16> ExactLibNames; |
| 86 |
llvm::Triple Triple(C.getDriver().getTargetTriple()); |
| 87 |
bool IsMSVC = Triple.isWindowsMSVCEnvironment(); |
| 88 |
llvm::StringRef Ext = IsMSVC ? ".lib" : ".a"; |
| 89 |
|
| 90 |
for (const auto *Arg : Args.filtered(options::OPT_l)) { |
| 91 |
llvm::StringRef Value = Arg->getValue(); |
| 92 |
if (Value.starts_with(":")) |
| 93 |
ExactLibNames.push_back(Value.drop_front()); |
| 94 |
else |
| 95 |
LibNames.push_back(Value); |
| 96 |
} |
| 97 |
for (const auto *Arg : Args.filtered(options::OPT_L)) { |
| 98 |
auto Path = Arg->getValue(); |
| 99 |
LibPaths.push_back(Path); |
| 100 |
if (Verbose) |
| 101 |
llvm::errs() << "HIP fatbin symbol search uses library path: " << Path |
| 102 |
<< "\n"; |
| 103 |
} |
| 104 |
|
| 105 |
auto ProcessLib = [&](llvm::StringRef LibName, bool IsExact) { |
| 106 |
llvm::SmallString<256> FullLibName( |
| 107 |
IsExact ? Twine(LibName).str() |
| 108 |
: IsMSVC ? (Twine(LibName) + Ext).str() |
| 109 |
: (Twine("lib") + LibName + Ext).str()); |
| 110 |
|
| 111 |
bool Found = false; |
| 112 |
for (const auto Path : LibPaths) { |
| 113 |
llvm::SmallString<256> FullPath = Path; |
| 114 |
llvm::sys::path::append(FullPath, FullLibName); |
| 115 |
|
| 116 |
if (llvm::sys::fs::exists(FullPath)) { |
| 117 |
if (Verbose) |
| 118 |
llvm::errs() << "HIP fatbin symbol search found library: " |
| 119 |
<< FullPath << "\n"; |
| 120 |
auto BufferOrErr = llvm::MemoryBuffer::getFile(FullPath); |
| 121 |
if (!BufferOrErr) { |
| 122 |
errorHandler(llvm::errorCodeToError(BufferOrErr.getError())); |
| 123 |
continue; |
| 124 |
} |
| 125 |
processInput(BufferOrErr.get()->getMemBufferRef()); |
| 126 |
Found = true; |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
if (!Found && Verbose) |
| 131 |
llvm::errs() << "HIP fatbin symbol search could not find library: " |
| 132 |
<< FullLibName << "\n"; |
| 133 |
}; |
| 134 |
|
| 135 |
for (const auto LibName : ExactLibNames) |
| 136 |
ProcessLib(LibName, true); |
| 137 |
|
| 138 |
for (const auto LibName : LibNames) |
| 139 |
ProcessLib(LibName, false); |
| 140 |
} |
| 141 |
|
| 142 |
private: |
| 143 |
const Compilation &C; |
| 144 |
const llvm::opt::ArgList &Args; |
| 145 |
unsigned DiagID; |
| 146 |
bool Quiet; |
| 147 |
bool Verbose; |
| 148 |
std::set<std::string> FatBinSymbols; |
| 149 |
std::set<std::string> GPUBinHandleSymbols; |
| 150 |
std::set<std::string, std::less<>> DefinedFatBinSymbols; |
| 151 |
std::set<std::string, std::less<>> DefinedGPUBinHandleSymbols; |
| 152 |
const std::string FatBinPrefix = "__hip_fatbin"; |
| 153 |
const std::string GPUBinHandlePrefix = "__hip_gpubin_handle"; |
| 154 |
|
| 155 |
void populateSymbols() { |
| 156 |
std::deque<const Action *> WorkList; |
| 157 |
std::set<const Action *> Visited; |
| 158 |
|
| 159 |
for (const auto &Action : C.getActions()) |
| 160 |
WorkList.push_back(Action); |
| 161 |
|
| 162 |
while (!WorkList.empty()) { |
| 163 |
const Action *CurrentAction = WorkList.front(); |
| 164 |
WorkList.pop_front(); |
| 165 |
|
| 166 |
if (!CurrentAction || !Visited.insert(CurrentAction).second) |
| 167 |
continue; |
| 168 |
|
| 169 |
if (const auto *IA = dyn_cast<InputAction>(CurrentAction)) { |
| 170 |
std::string ID = IA->getId().str(); |
| 171 |
if (!ID.empty()) { |
| 172 |
ID = llvm::utohexstr(llvm::MD5Hash(ID), /*LowerCase=*/true); |
| 173 |
FatBinSymbols.insert((FatBinPrefix + Twine('_') + ID).str()); |
| 174 |
GPUBinHandleSymbols.insert( |
| 175 |
(GPUBinHandlePrefix + Twine('_') + ID).str()); |
| 176 |
continue; |
| 177 |
} |
| 178 |
if (IA->getInputArg().getNumValues() == 0) |
| 179 |
continue; |
| 180 |
const char *Filename = IA->getInputArg().getValue(); |
| 181 |
if (!Filename) |
| 182 |
continue; |
| 183 |
auto BufferOrErr = llvm::MemoryBuffer::getFile(Filename); |
| 184 |
// Input action could be options to linker, therefore, ignore it |
| 185 |
// if cannot read it. If it turns out to be a file that cannot be read, |
| 186 |
// the error will be caught by the linker. |
| 187 |
if (!BufferOrErr) |
| 188 |
continue; |
| 189 |
|
| 190 |
processInput(BufferOrErr.get()->getMemBufferRef()); |
| 191 |
} else |
| 192 |
llvm::append_range(WorkList, CurrentAction->getInputs()); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
void processInput(const llvm::MemoryBufferRef &Buffer) { |
| 197 |
// Try processing as object file first. |
| 198 |
auto ObjFileOrErr = llvm::object::ObjectFile::createObjectFile(Buffer); |
| 199 |
if (ObjFileOrErr) { |
| 200 |
processSymbols(**ObjFileOrErr); |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
// Then try processing as archive files. |
| 205 |
llvm::consumeError(ObjFileOrErr.takeError()); |
| 206 |
auto ArchiveOrErr = llvm::object::Archive::create(Buffer); |
| 207 |
if (ArchiveOrErr) { |
| 208 |
llvm::Error Err = llvm::Error::success(); |
| 209 |
llvm::object::Archive &Archive = *ArchiveOrErr.get(); |
| 210 |
for (auto &Child : Archive.children(Err)) { |
| 211 |
auto ChildBufOrErr = Child.getMemoryBufferRef(); |
| 212 |
if (ChildBufOrErr) |
| 213 |
processInput(*ChildBufOrErr); |
| 214 |
else |
| 215 |
errorHandler(ChildBufOrErr.takeError()); |
| 216 |
} |
| 217 |
|
| 218 |
if (Err) |
| 219 |
errorHandler(std::move(Err)); |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
// Ignore other files. |
| 224 |
llvm::consumeError(ArchiveOrErr.takeError()); |
| 225 |
} |
| 226 |
|
| 227 |
void processSymbols(const llvm::object::ObjectFile &Obj) { |
| 228 |
for (const auto &Symbol : Obj.symbols()) { |
| 229 |
auto FlagOrErr = Symbol.getFlags(); |
| 230 |
if (!FlagOrErr) { |
| 231 |
errorHandler(FlagOrErr.takeError()); |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
auto NameOrErr = Symbol.getName(); |
| 236 |
if (!NameOrErr) { |
| 237 |
errorHandler(NameOrErr.takeError()); |
| 238 |
continue; |
| 239 |
} |
| 240 |
llvm::StringRef Name = *NameOrErr; |
| 241 |
|
| 242 |
bool isUndefined = |
| 243 |
FlagOrErr.get() & llvm::object::SymbolRef::SF_Undefined; |
| 244 |
bool isHidden = FlagOrErr.get() & llvm::object::SymbolRef::SF_Hidden; |
| 245 |
bool isFatBinSymbol = Name.starts_with(FatBinPrefix); |
| 246 |
bool isGPUBinHandleSymbol = Name.starts_with(GPUBinHandlePrefix); |
| 247 |
|
| 248 |
// Add undefined symbols if they are not in the defined sets |
| 249 |
if (isUndefined) { |
| 250 |
if (isFatBinSymbol && |
| 251 |
DefinedFatBinSymbols.find(Name) == DefinedFatBinSymbols.end()) |
| 252 |
FatBinSymbols.insert(Name.str()); |
| 253 |
else if (isGPUBinHandleSymbol && |
| 254 |
DefinedGPUBinHandleSymbols.find(Name) == |
| 255 |
DefinedGPUBinHandleSymbols.end()) |
| 256 |
GPUBinHandleSymbols.insert(Name.str()); |
| 257 |
continue; |
| 258 |
} |
| 259 |
|
| 260 |
// Ignore hidden defined symbols |
| 261 |
if (isHidden) |
| 262 |
continue; |
| 263 |
|
| 264 |
// Handling for non-hidden defined symbols |
| 265 |
if (isFatBinSymbol) { |
| 266 |
DefinedFatBinSymbols.insert(Name.str()); |
| 267 |
FatBinSymbols.erase(Name.str()); |
| 268 |
} else if (isGPUBinHandleSymbol) { |
| 269 |
DefinedGPUBinHandleSymbols.insert(Name.str()); |
| 270 |
GPUBinHandleSymbols.erase(Name.str()); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
void errorHandler(llvm::Error Err) { |
| 276 |
if (Quiet) |
| 277 |
return; |
| 278 |
C.getDriver().Diag(DiagID) << llvm::toString(std::move(Err)); |
| 279 |
} |
| 280 |
}; |
| 281 |
|
| 282 |
// Construct a clang-offload-bundler command to bundle code objects for |
| 283 |
// different devices into a HIP fat binary. |
| 284 |
void HIP::constructHIPFatbinCommand(Compilation &C, const JobAction &JA, |
| 285 |
llvm::StringRef OutputFileName, |
| 286 |
const InputInfoList &Inputs, |
| 287 |
const llvm::opt::ArgList &Args, |
| 288 |
const Tool &T) { |
| 289 |
// Construct clang-offload-bundler command to bundle object files for |
| 290 |
// for different GPU archs. |
| 291 |
ArgStringList BundlerArgs; |
| 292 |
BundlerArgs.push_back(Args.MakeArgString("-type=o")); |
| 293 |
BundlerArgs.push_back( |
| 294 |
Args.MakeArgString("-bundle-align=" + Twine(HIPCodeObjectAlign))); |
| 295 |
|
| 296 |
// ToDo: Remove the dummy host binary entry which is required by |
| 297 |
// clang-offload-bundler. |
| 298 |
std::string BundlerTargetArg = "-targets=host-x86_64-unknown-linux-gnu"; |
| 299 |
// AMDGCN: |
| 300 |
// For code object version 2 and 3, the offload kind in bundle ID is 'hip' |
| 301 |
// for backward compatibility. For code object version 4 and greater, the |
| 302 |
// offload kind in bundle ID is 'hipv4'. |
| 303 |
std::string OffloadKind = "hip"; |
| 304 |
auto &TT = T.getToolChain().getTriple(); |
| 305 |
if (TT.isAMDGCN() && getAMDGPUCodeObjectVersion(C.getDriver(), Args) >= 4) |
| 306 |
OffloadKind = OffloadKind + "v4"; |
| 307 |
for (const auto &II : Inputs) { |
| 308 |
const auto *A = II.getAction(); |
| 309 |
auto ArchStr = llvm::StringRef(A->getOffloadingArch()); |
| 310 |
BundlerTargetArg += ',' + OffloadKind + '-'; |
| 311 |
if (ArchStr == "amdgcnspirv") |
| 312 |
BundlerTargetArg += |
| 313 |
normalizeForBundler(llvm::Triple("spirv64-amd-amdhsa"), true); |
| 314 |
else |
| 315 |
BundlerTargetArg += normalizeForBundler(TT, !ArchStr.empty()); |
| 316 |
if (!ArchStr.empty()) |
| 317 |
BundlerTargetArg += '-' + ArchStr.str(); |
| 318 |
} |
| 319 |
BundlerArgs.push_back(Args.MakeArgString(BundlerTargetArg)); |
| 320 |
|
| 321 |
// Use a NULL file as input for the dummy host binary entry |
| 322 |
std::string BundlerInputArg = "-input=" NULL_FILE; |
| 323 |
BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg)); |
| 324 |
for (const auto &II : Inputs) { |
| 325 |
BundlerInputArg = std::string("-input=") + II.getFilename(); |
| 326 |
BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg)); |
| 327 |
} |
| 328 |
|
| 329 |
std::string Output = std::string(OutputFileName); |
| 330 |
auto *BundlerOutputArg = |
| 331 |
Args.MakeArgString(std::string("-output=").append(Output)); |
| 332 |
BundlerArgs.push_back(BundlerOutputArg); |
| 333 |
|
| 334 |
addOffloadCompressArgs(Args, BundlerArgs); |
| 335 |
|
| 336 |
const char *Bundler = Args.MakeArgString( |
| 337 |
T.getToolChain().GetProgramPath("clang-offload-bundler")); |
| 338 |
C.addCommand(std::make_unique<Command>( |
| 339 |
JA, T, ResponseFileSupport::None(), Bundler, BundlerArgs, Inputs, |
| 340 |
InputInfo(&JA, Args.MakeArgString(Output)))); |
| 341 |
} |
| 342 |
|
| 343 |
/// Add Generated HIP Object File which has device images embedded into the |
| 344 |
/// host to the argument list for linking. Using MC directives, embed the |
| 345 |
/// device code and also define symbols required by the code generation so that |
| 346 |
/// the image can be retrieved at runtime. |
| 347 |
void HIP::constructGenerateObjFileFromHIPFatBinary( |
| 348 |
Compilation &C, const InputInfo &Output, const InputInfoList &Inputs, |
| 349 |
const ArgList &Args, const JobAction &JA, const Tool &T) { |
| 350 |
const Driver &D = C.getDriver(); |
| 351 |
std::string Name = std::string(llvm::sys::path::stem(Output.getFilename())); |
| 352 |
|
| 353 |
// Create Temp Object File Generator, |
| 354 |
// Offload Bundled file and Bundled Object file. |
| 355 |
// Keep them if save-temps is enabled. |
| 356 |
const char *ObjinFile; |
| 357 |
const char *BundleFile; |
| 358 |
if (D.isSaveTempsEnabled()) { |
| 359 |
ObjinFile = C.getArgs().MakeArgString(Name + ".mcin"); |
| 360 |
BundleFile = C.getArgs().MakeArgString(Name + ".hipfb"); |
| 361 |
} else { |
| 362 |
auto TmpNameMcin = D.GetTemporaryPath(Name, "mcin"); |
| 363 |
ObjinFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameMcin)); |
| 364 |
auto TmpNameFb = D.GetTemporaryPath(Name, "hipfb"); |
| 365 |
BundleFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameFb)); |
| 366 |
} |
| 367 |
HIP::constructHIPFatbinCommand(C, JA, BundleFile, Inputs, Args, T); |
| 368 |
|
| 369 |
// Create a buffer to write the contents of the temp obj generator. |
| 370 |
std::string ObjBuffer; |
| 371 |
llvm::raw_string_ostream ObjStream(ObjBuffer); |
| 372 |
|
| 373 |
auto HostTriple = |
| 374 |
C.getSingleOffloadToolChain<Action::OFK_Host>()->getTriple(); |
| 375 |
|
| 376 |
HIPUndefinedFatBinSymbols Symbols(C, Args); |
| 377 |
|
| 378 |
std::string PrimaryHipFatbinSymbol; |
| 379 |
std::string PrimaryGpuBinHandleSymbol; |
| 380 |
bool FoundPrimaryHipFatbinSymbol = false; |
| 381 |
bool FoundPrimaryGpuBinHandleSymbol = false; |
| 382 |
|
| 383 |
std::vector<std::string> AliasHipFatbinSymbols; |
| 384 |
std::vector<std::string> AliasGpuBinHandleSymbols; |
| 385 |
|
| 386 |
// Iterate through symbols to find the primary ones and collect others for |
| 387 |
// aliasing |
| 388 |
for (const auto &Symbol : Symbols.getFatBinSymbols()) { |
| 389 |
if (!FoundPrimaryHipFatbinSymbol) { |
| 390 |
PrimaryHipFatbinSymbol = Symbol; |
| 391 |
FoundPrimaryHipFatbinSymbol = true; |
| 392 |
} else |
| 393 |
AliasHipFatbinSymbols.push_back(Symbol); |
| 394 |
} |
| 395 |
|
| 396 |
for (const auto &Symbol : Symbols.getGPUBinHandleSymbols()) { |
| 397 |
if (!FoundPrimaryGpuBinHandleSymbol) { |
| 398 |
PrimaryGpuBinHandleSymbol = Symbol; |
| 399 |
FoundPrimaryGpuBinHandleSymbol = true; |
| 400 |
} else |
| 401 |
AliasGpuBinHandleSymbols.push_back(Symbol); |
| 402 |
} |
| 403 |
|
| 404 |
// Add MC directives to embed target binaries. We ensure that each |
| 405 |
// section and image is 16-byte aligned. This is not mandatory, but |
| 406 |
// increases the likelihood of data to be aligned with a cache block |
| 407 |
// in several main host machines. |
| 408 |
ObjStream << "# HIP Object Generator\n"; |
| 409 |
ObjStream << "# *** Automatically generated by Clang ***\n"; |
| 410 |
if (FoundPrimaryGpuBinHandleSymbol) { |
| 411 |
// Define the first gpubin handle symbol |
| 412 |
if (HostTriple.isWindowsMSVCEnvironment()) |
| 413 |
ObjStream << " .section .hip_gpubin_handle,\"dw\"\n"; |
| 414 |
else { |
| 415 |
ObjStream << " .protected " << PrimaryGpuBinHandleSymbol << "\n"; |
| 416 |
ObjStream << " .type " << PrimaryGpuBinHandleSymbol << ",@object\n"; |
| 417 |
ObjStream << " .section .hip_gpubin_handle,\"aw\"\n"; |
| 418 |
} |
| 419 |
ObjStream << " .globl " << PrimaryGpuBinHandleSymbol << "\n"; |
| 420 |
ObjStream << " .p2align 3\n"; // Align 8 |
| 421 |
ObjStream << PrimaryGpuBinHandleSymbol << ":\n"; |
| 422 |
ObjStream << " .zero 8\n"; // Size 8 |
| 423 |
|
| 424 |
// Generate alias directives for other gpubin handle symbols |
| 425 |
for (const auto &AliasSymbol : AliasGpuBinHandleSymbols) { |
| 426 |
ObjStream << " .globl " << AliasSymbol << "\n"; |
| 427 |
ObjStream << " .set " << AliasSymbol << "," << PrimaryGpuBinHandleSymbol |
| 428 |
<< "\n"; |
| 429 |
} |
| 430 |
} |
| 431 |
if (FoundPrimaryHipFatbinSymbol) { |
| 432 |
// Define the first fatbin symbol |
| 433 |
if (HostTriple.isWindowsMSVCEnvironment()) |
| 434 |
ObjStream << " .section .hip_fatbin,\"dw\"\n"; |
| 435 |
else { |
| 436 |
ObjStream << " .protected " << PrimaryHipFatbinSymbol << "\n"; |
| 437 |
ObjStream << " .type " << PrimaryHipFatbinSymbol << ",@object\n"; |
| 438 |
ObjStream << " .section .hip_fatbin,\"a\",@progbits\n"; |
| 439 |
} |
| 440 |
ObjStream << " .globl " << PrimaryHipFatbinSymbol << "\n"; |
| 441 |
ObjStream << " .p2align " << llvm::Log2(llvm::Align(HIPCodeObjectAlign)) |
| 442 |
<< "\n"; |
| 443 |
// Generate alias directives for other fatbin symbols |
| 444 |
for (const auto &AliasSymbol : AliasHipFatbinSymbols) { |
| 445 |
ObjStream << " .globl " << AliasSymbol << "\n"; |
| 446 |
ObjStream << " .set " << AliasSymbol << "," << PrimaryHipFatbinSymbol |
| 447 |
<< "\n"; |
| 448 |
} |
| 449 |
ObjStream << PrimaryHipFatbinSymbol << ":\n"; |
| 450 |
ObjStream << " .incbin "; |
| 451 |
llvm::sys::printArg(ObjStream, BundleFile, /*Quote=*/true); |
| 452 |
ObjStream << "\n"; |
| 453 |
} |
| 454 |
if (HostTriple.isOSLinux() && HostTriple.isOSBinFormatELF()) |
| 455 |
ObjStream << " .section .note.GNU-stack, \"\", @progbits\n"; |
| 456 |
|
| 457 |
// Dump the contents of the temp object file gen if the user requested that. |
| 458 |
// We support this option to enable testing of behavior with -###. |
| 459 |
if (C.getArgs().hasArg(options::OPT_fhip_dump_offload_linker_script)) |
| 460 |
llvm::errs() << ObjBuffer; |
| 461 |
|
| 462 |
// Open script file and write the contents. |
| 463 |
std::error_code EC; |
| 464 |
llvm::raw_fd_ostream Objf(ObjinFile, EC, llvm::sys::fs::OF_None); |
| 465 |
|
| 466 |
if (EC) { |
| 467 |
D.Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
| 468 |
return; |
| 469 |
} |
| 470 |
|
| 471 |
Objf << ObjBuffer; |
| 472 |
|
| 473 |
ArgStringList ClangArgs{"-target", Args.MakeArgString(HostTriple.normalize()), |
| 474 |
"-o", Output.getFilename(), |
| 475 |
"-x", "assembler", |
| 476 |
ObjinFile, "-c"}; |
| 477 |
C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(), |
| 478 |
D.getClangProgramPath(), ClangArgs, |
| 479 |
Inputs, Output, D.getPrependArg())); |
| 480 |
} |
| 481 |
|
| 482 |
// Convenience function for creating temporary file for both modes of |
| 483 |
// isSaveTempsEnabled(). |
| 484 |
const char *HIP::getTempFile(Compilation &C, StringRef Prefix, |
| 485 |
StringRef Extension) { |
| 486 |
if (C.getDriver().isSaveTempsEnabled()) { |
| 487 |
return C.getArgs().MakeArgString(Prefix + "." + Extension); |
| 488 |
} |
| 489 |
auto TmpFile = C.getDriver().GetTemporaryPath(Prefix, Extension); |
| 490 |
return C.addTempFile(C.getArgs().MakeArgString(TmpFile)); |
| 491 |
} |