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 #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                                        bool HasTargetID) {
33   return HasTargetID ? (T.getArchName() + "-" + T.getVendorName() + "-" +
34                         T.getOSName() + "-" + T.getEnvironmentName())
35                            .str()
36                      : T.normalize();
37 }
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                                     const Tool &T) {
46   // Construct clang-offload-bundler command to bundle object files for
47   // for different GPU archs.
48   ArgStringList BundlerArgs;
49   BundlerArgs.push_back(Args.MakeArgString("-type=o"));
50   BundlerArgs.push_back(
51       Args.MakeArgString("-bundle-align=" + Twine(HIPCodeObjectAlign)));
52 
53   // ToDo: Remove the dummy host binary entry which is required by
54   // clang-offload-bundler.
55   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   std::string OffloadKind = "hip";
61   auto &TT = T.getToolChain().getTriple();
62   if (TT.isAMDGCN() && getAMDGPUCodeObjectVersion(C.getDriver(), Args) >= 4)
63     OffloadKind = OffloadKind + "v4";
64   for (const auto &II : Inputs) {
65     const auto *A = II.getAction();
66     auto ArchStr = llvm::StringRef(A->getOffloadingArch());
67     BundlerTargetArg +=
68         "," + OffloadKind + "-" + normalizeForBundler(TT, !ArchStr.empty());
69     if (!ArchStr.empty())
70       BundlerTargetArg += "-" + ArchStr.str();
71   }
72   BundlerArgs.push_back(Args.MakeArgString(BundlerTargetArg));
73 
74   // Use a NULL file as input for the dummy host binary entry
75   std::string BundlerInputArg = "-input=" NULL_FILE;
76   BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg));
77   for (const auto &II : Inputs) {
78     BundlerInputArg = std::string("-input=") + II.getFilename();
79     BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg));
80   }
81 
82   std::string Output = std::string(OutputFileName);
83   auto *BundlerOutputArg =
84       Args.MakeArgString(std::string("-output=").append(Output));
85   BundlerArgs.push_back(BundlerOutputArg);
86 
87   if (Args.hasFlag(options::OPT_offload_compress,
88                    options::OPT_no_offload_compress, false))
89     BundlerArgs.push_back("-compress");
90   if (Args.hasArg(options::OPT_v))
91     BundlerArgs.push_back("-verbose");
92 
93   const char *Bundler = Args.MakeArgString(
94       T.getToolChain().GetProgramPath("clang-offload-bundler"));
95   C.addCommand(std::make_unique<Command>(
96       JA, T, ResponseFileSupport::None(), Bundler, BundlerArgs, Inputs,
97       InputInfo(&JA, Args.MakeArgString(Output))));
98 }
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     const ArgList &Args, const JobAction &JA, const Tool &T) {
107   const ToolChain &TC = T.getToolChain();
108   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   const char *McinFile;
114   const char *BundleFile;
115   if (C.getDriver().isSaveTempsEnabled()) {
116     McinFile = C.getArgs().MakeArgString(Name + ".mcin");
117     BundleFile = C.getArgs().MakeArgString(Name + ".hipfb");
118   } else {
119     auto TmpNameMcin = C.getDriver().GetTemporaryPath(Name, "mcin");
120     McinFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameMcin));
121     auto TmpNameFb = C.getDriver().GetTemporaryPath(Name, "hipfb");
122     BundleFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameFb));
123   }
124   HIP::constructHIPFatbinCommand(C, JA, BundleFile, Inputs, Args, T);
125 
126   // Create a buffer to write the contents of the temp obj generator.
127   std::string ObjBuffer;
128   llvm::raw_string_ostream ObjStream(ObjBuffer);
129 
130   auto HostTriple =
131       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   ObjStream << "#       HIP Object Generator\n";
138   ObjStream << "# *** Automatically generated by Clang ***\n";
139   if (HostTriple.isWindowsMSVCEnvironment()) {
140     ObjStream << "  .section .hip_fatbin, \"dw\"\n";
141   } else {
142     ObjStream << "  .protected __hip_fatbin\n";
143     ObjStream << "  .type __hip_fatbin,@object\n";
144     ObjStream << "  .section .hip_fatbin,\"a\",@progbits\n";
145   }
146   ObjStream << "  .globl __hip_fatbin\n";
147   ObjStream << "  .p2align " << llvm::Log2(llvm::Align(HIPCodeObjectAlign))
148             << "\n";
149   ObjStream << "__hip_fatbin:\n";
150   ObjStream << "  .incbin ";
151   llvm::sys::printArg(ObjStream, BundleFile, /*Quote=*/true);
152   ObjStream << "\n";
153   if (HostTriple.isOSLinux() && HostTriple.isOSBinFormatELF())
154     ObjStream << "  .section .note.GNU-stack, \"\", @progbits\n";
155   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   if (C.getArgs().hasArg(options::OPT_fhip_dump_offload_linker_script))
160     llvm::errs() << ObjBuffer;
161 
162   // Open script file and write the contents.
163   std::error_code EC;
164   llvm::raw_fd_ostream Objf(McinFile, EC, llvm::sys::fs::OF_None);
165 
166   if (EC) {
167     C.getDriver().Diag(clang::diag::err_unable_to_make_temp) << EC.message();
168     return;
169   }
170 
171   Objf << ObjBuffer;
172 
173   ArgStringList McArgs{"-triple", Args.MakeArgString(HostTriple.normalize()),
174                        "-o",      Output.getFilename(),
175                        McinFile,  "--filetype=obj"};
176   const char *Mc = Args.MakeArgString(TC.GetProgramPath("llvm-mc"));
177   C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(), Mc,
178                                          McArgs, Inputs, Output));
179 }
180