HIPUtility.cpp 6.9 KB

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