xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/HIPUtility.cpp (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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   std::string BundlerInputArg = "-inputs=" NULL_FILE;
57 
58   // AMDGCN:
59   // For code object version 2 and 3, the offload kind in bundle ID is 'hip'
60   // for backward compatibility. For code object version 4 and greater, the
61   // offload kind in bundle ID is 'hipv4'.
62   std::string OffloadKind = "hip";
63   auto &TT = T.getToolChain().getTriple();
64   if (TT.isAMDGCN() && getAMDGPUCodeObjectVersion(C.getDriver(), Args) >= 4)
65     OffloadKind = OffloadKind + "v4";
66   for (const auto &II : Inputs) {
67     const auto *A = II.getAction();
68     auto ArchStr = llvm::StringRef(A->getOffloadingArch());
69     BundlerTargetArg +=
70         "," + OffloadKind + "-" + normalizeForBundler(TT, !ArchStr.empty());
71     if (!ArchStr.empty())
72       BundlerTargetArg += "-" + ArchStr.str();
73     BundlerInputArg = BundlerInputArg + "," + II.getFilename();
74   }
75   BundlerArgs.push_back(Args.MakeArgString(BundlerTargetArg));
76   BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg));
77 
78   std::string Output = std::string(OutputFileName);
79   auto *BundlerOutputArg =
80       Args.MakeArgString(std::string("-outputs=").append(Output));
81   BundlerArgs.push_back(BundlerOutputArg);
82 
83   const char *Bundler = Args.MakeArgString(
84       T.getToolChain().GetProgramPath("clang-offload-bundler"));
85   C.addCommand(std::make_unique<Command>(
86       JA, T, ResponseFileSupport::None(), Bundler, BundlerArgs, Inputs,
87       InputInfo(&JA, Args.MakeArgString(Output))));
88 }
89 
90 /// Add Generated HIP Object File which has device images embedded into the
91 /// host to the argument list for linking. Using MC directives, embed the
92 /// device code and also define symbols required by the code generation so that
93 /// the image can be retrieved at runtime.
94 void HIP::constructGenerateObjFileFromHIPFatBinary(
95     Compilation &C, const InputInfo &Output, const InputInfoList &Inputs,
96     const ArgList &Args, const JobAction &JA, const Tool &T) {
97   const ToolChain &TC = T.getToolChain();
98   std::string Name = std::string(llvm::sys::path::stem(Output.getFilename()));
99 
100   // Create Temp Object File Generator,
101   // Offload Bundled file and Bundled Object file.
102   // Keep them if save-temps is enabled.
103   const char *McinFile;
104   const char *BundleFile;
105   if (C.getDriver().isSaveTempsEnabled()) {
106     McinFile = C.getArgs().MakeArgString(Name + ".mcin");
107     BundleFile = C.getArgs().MakeArgString(Name + ".hipfb");
108   } else {
109     auto TmpNameMcin = C.getDriver().GetTemporaryPath(Name, "mcin");
110     McinFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameMcin));
111     auto TmpNameFb = C.getDriver().GetTemporaryPath(Name, "hipfb");
112     BundleFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameFb));
113   }
114   HIP::constructHIPFatbinCommand(C, JA, BundleFile, Inputs, Args, T);
115 
116   // Create a buffer to write the contents of the temp obj generator.
117   std::string ObjBuffer;
118   llvm::raw_string_ostream ObjStream(ObjBuffer);
119 
120   auto HostTriple =
121       C.getSingleOffloadToolChain<Action::OFK_Host>()->getTriple();
122 
123   // Add MC directives to embed target binaries. We ensure that each
124   // section and image is 16-byte aligned. This is not mandatory, but
125   // increases the likelihood of data to be aligned with a cache block
126   // in several main host machines.
127   ObjStream << "#       HIP Object Generator\n";
128   ObjStream << "# *** Automatically generated by Clang ***\n";
129   if (HostTriple.isWindowsMSVCEnvironment()) {
130     ObjStream << "  .section .hip_fatbin, \"dw\"\n";
131   } else {
132     ObjStream << "  .protected __hip_fatbin\n";
133     ObjStream << "  .type __hip_fatbin,@object\n";
134     ObjStream << "  .section .hip_fatbin,\"a\",@progbits\n";
135   }
136   ObjStream << "  .globl __hip_fatbin\n";
137   ObjStream << "  .p2align " << llvm::Log2(llvm::Align(HIPCodeObjectAlign))
138             << "\n";
139   ObjStream << "__hip_fatbin:\n";
140   ObjStream << "  .incbin ";
141   llvm::sys::printArg(ObjStream, BundleFile, /*Quote=*/true);
142   ObjStream << "\n";
143   ObjStream.flush();
144 
145   // Dump the contents of the temp object file gen if the user requested that.
146   // We support this option to enable testing of behavior with -###.
147   if (C.getArgs().hasArg(options::OPT_fhip_dump_offload_linker_script))
148     llvm::errs() << ObjBuffer;
149 
150   // Open script file and write the contents.
151   std::error_code EC;
152   llvm::raw_fd_ostream Objf(McinFile, EC, llvm::sys::fs::OF_None);
153 
154   if (EC) {
155     C.getDriver().Diag(clang::diag::err_unable_to_make_temp) << EC.message();
156     return;
157   }
158 
159   Objf << ObjBuffer;
160 
161   ArgStringList McArgs{"-triple", Args.MakeArgString(HostTriple.normalize()),
162                        "-o",      Output.getFilename(),
163                        McinFile,  "--filetype=obj"};
164   const char *Mc = Args.MakeArgString(TC.GetProgramPath("llvm-mc"));
165   C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(), Mc,
166                                          McArgs, Inputs, Output));
167 }
168