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/ADT/Triple.h"
14 #include "llvm/Support/Path.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   const char *Bundler = Args.MakeArgString(
88       T.getToolChain().GetProgramPath("clang-offload-bundler"));
89   C.addCommand(std::make_unique<Command>(
90       JA, T, ResponseFileSupport::None(), Bundler, BundlerArgs, Inputs,
91       InputInfo(&JA, Args.MakeArgString(Output))));
92 }
93 
94 /// Add Generated HIP Object File which has device images embedded into the
95 /// host to the argument list for linking. Using MC directives, embed the
96 /// device code and also define symbols required by the code generation so that
97 /// the image can be retrieved at runtime.
98 void HIP::constructGenerateObjFileFromHIPFatBinary(
99     Compilation &C, const InputInfo &Output, const InputInfoList &Inputs,
100     const ArgList &Args, const JobAction &JA, const Tool &T) {
101   const ToolChain &TC = T.getToolChain();
102   std::string Name = std::string(llvm::sys::path::stem(Output.getFilename()));
103 
104   // Create Temp Object File Generator,
105   // Offload Bundled file and Bundled Object file.
106   // Keep them if save-temps is enabled.
107   const char *McinFile;
108   const char *BundleFile;
109   if (C.getDriver().isSaveTempsEnabled()) {
110     McinFile = C.getArgs().MakeArgString(Name + ".mcin");
111     BundleFile = C.getArgs().MakeArgString(Name + ".hipfb");
112   } else {
113     auto TmpNameMcin = C.getDriver().GetTemporaryPath(Name, "mcin");
114     McinFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameMcin));
115     auto TmpNameFb = C.getDriver().GetTemporaryPath(Name, "hipfb");
116     BundleFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameFb));
117   }
118   HIP::constructHIPFatbinCommand(C, JA, BundleFile, Inputs, Args, T);
119 
120   // Create a buffer to write the contents of the temp obj generator.
121   std::string ObjBuffer;
122   llvm::raw_string_ostream ObjStream(ObjBuffer);
123 
124   auto HostTriple =
125       C.getSingleOffloadToolChain<Action::OFK_Host>()->getTriple();
126 
127   // Add MC directives to embed target binaries. We ensure that each
128   // section and image is 16-byte aligned. This is not mandatory, but
129   // increases the likelihood of data to be aligned with a cache block
130   // in several main host machines.
131   ObjStream << "#       HIP Object Generator\n";
132   ObjStream << "# *** Automatically generated by Clang ***\n";
133   if (HostTriple.isWindowsMSVCEnvironment()) {
134     ObjStream << "  .section .hip_fatbin, \"dw\"\n";
135   } else {
136     ObjStream << "  .protected __hip_fatbin\n";
137     ObjStream << "  .type __hip_fatbin,@object\n";
138     ObjStream << "  .section .hip_fatbin,\"a\",@progbits\n";
139   }
140   ObjStream << "  .globl __hip_fatbin\n";
141   ObjStream << "  .p2align " << llvm::Log2(llvm::Align(HIPCodeObjectAlign))
142             << "\n";
143   ObjStream << "__hip_fatbin:\n";
144   ObjStream << "  .incbin ";
145   llvm::sys::printArg(ObjStream, BundleFile, /*Quote=*/true);
146   ObjStream << "\n";
147   ObjStream.flush();
148 
149   // Dump the contents of the temp object file gen if the user requested that.
150   // We support this option to enable testing of behavior with -###.
151   if (C.getArgs().hasArg(options::OPT_fhip_dump_offload_linker_script))
152     llvm::errs() << ObjBuffer;
153 
154   // Open script file and write the contents.
155   std::error_code EC;
156   llvm::raw_fd_ostream Objf(McinFile, EC, llvm::sys::fs::OF_None);
157 
158   if (EC) {
159     C.getDriver().Diag(clang::diag::err_unable_to_make_temp) << EC.message();
160     return;
161   }
162 
163   Objf << ObjBuffer;
164 
165   ArgStringList McArgs{"-triple", Args.MakeArgString(HostTriple.normalize()),
166                        "-o",      Output.getFilename(),
167                        McinFile,  "--filetype=obj"};
168   const char *Mc = Args.MakeArgString(TC.GetProgramPath("llvm-mc"));
169   C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(), Mc,
170                                          McArgs, Inputs, Output));
171 }
172