/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/HIPUtility.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 233 | #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 | 300 | bool HasTargetID) { |
33 | 300 | return HasTargetID ? (T.getArchName() + "-" + T.getVendorName() + "-" + |
34 | 300 | T.getOSName() + "-" + T.getEnvironmentName()) |
35 | 300 | .str() |
36 | 300 | : T.normalize()0 ; |
37 | 300 | } |
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 | 233 | const Tool &T) { |
46 | | // Construct clang-offload-bundler command to bundle object files for |
47 | | // for different GPU archs. |
48 | 233 | ArgStringList BundlerArgs; |
49 | 233 | BundlerArgs.push_back(Args.MakeArgString("-type=o")); |
50 | 233 | BundlerArgs.push_back( |
51 | 233 | Args.MakeArgString("-bundle-align=" + Twine(HIPCodeObjectAlign))); |
52 | | |
53 | | // ToDo: Remove the dummy host binary entry which is required by |
54 | | // clang-offload-bundler. |
55 | 233 | 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 | 233 | std::string OffloadKind = "hip"; |
61 | 233 | auto &TT = T.getToolChain().getTriple(); |
62 | 233 | if (TT.isAMDGCN() && getAMDGPUCodeObjectVersion(C.getDriver(), Args) >= 4224 ) |
63 | 219 | OffloadKind = OffloadKind + "v4"; |
64 | 300 | for (const auto &II : Inputs) { |
65 | 300 | const auto *A = II.getAction(); |
66 | 300 | auto ArchStr = llvm::StringRef(A->getOffloadingArch()); |
67 | 300 | BundlerTargetArg += |
68 | 300 | "," + OffloadKind + "-" + normalizeForBundler(TT, !ArchStr.empty()); |
69 | 300 | if (!ArchStr.empty()) |
70 | 300 | BundlerTargetArg += "-" + ArchStr.str(); |
71 | 300 | } |
72 | 233 | BundlerArgs.push_back(Args.MakeArgString(BundlerTargetArg)); |
73 | | |
74 | | // Use a NULL file as input for the dummy host binary entry |
75 | 233 | std::string BundlerInputArg = "-input=" NULL_FILE; |
76 | 233 | BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg)); |
77 | 300 | for (const auto &II : Inputs) { |
78 | 300 | BundlerInputArg = std::string("-input=") + II.getFilename(); |
79 | 300 | BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg)); |
80 | 300 | } |
81 | | |
82 | 233 | std::string Output = std::string(OutputFileName); |
83 | 233 | auto *BundlerOutputArg = |
84 | 233 | Args.MakeArgString(std::string("-output=").append(Output)); |
85 | 233 | BundlerArgs.push_back(BundlerOutputArg); |
86 | | |
87 | 233 | const char *Bundler = Args.MakeArgString( |
88 | 233 | T.getToolChain().GetProgramPath("clang-offload-bundler")); |
89 | 233 | C.addCommand(std::make_unique<Command>( |
90 | 233 | JA, T, ResponseFileSupport::None(), Bundler, BundlerArgs, Inputs, |
91 | 233 | InputInfo(&JA, Args.MakeArgString(Output)))); |
92 | 233 | } |
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 | 46 | const ArgList &Args, const JobAction &JA, const Tool &T) { |
101 | 46 | const ToolChain &TC = T.getToolChain(); |
102 | 46 | 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 | 46 | const char *McinFile; |
108 | 46 | const char *BundleFile; |
109 | 46 | if (C.getDriver().isSaveTempsEnabled()) { |
110 | 5 | McinFile = C.getArgs().MakeArgString(Name + ".mcin"); |
111 | 5 | BundleFile = C.getArgs().MakeArgString(Name + ".hipfb"); |
112 | 41 | } else { |
113 | 41 | auto TmpNameMcin = C.getDriver().GetTemporaryPath(Name, "mcin"); |
114 | 41 | McinFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameMcin)); |
115 | 41 | auto TmpNameFb = C.getDriver().GetTemporaryPath(Name, "hipfb"); |
116 | 41 | BundleFile = C.addTempFile(C.getArgs().MakeArgString(TmpNameFb)); |
117 | 41 | } |
118 | 46 | HIP::constructHIPFatbinCommand(C, JA, BundleFile, Inputs, Args, T); |
119 | | |
120 | | // Create a buffer to write the contents of the temp obj generator. |
121 | 46 | std::string ObjBuffer; |
122 | 46 | llvm::raw_string_ostream ObjStream(ObjBuffer); |
123 | | |
124 | 46 | auto HostTriple = |
125 | 46 | 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 | 46 | ObjStream << "# HIP Object Generator\n"; |
132 | 46 | ObjStream << "# *** Automatically generated by Clang ***\n"; |
133 | 46 | if (HostTriple.isWindowsMSVCEnvironment()) { |
134 | 4 | ObjStream << " .section .hip_fatbin, \"dw\"\n"; |
135 | 42 | } else { |
136 | 42 | ObjStream << " .protected __hip_fatbin\n"; |
137 | 42 | ObjStream << " .type __hip_fatbin,@object\n"; |
138 | 42 | ObjStream << " .section .hip_fatbin,\"a\",@progbits\n"; |
139 | 42 | } |
140 | 46 | ObjStream << " .globl __hip_fatbin\n"; |
141 | 46 | ObjStream << " .p2align " << llvm::Log2(llvm::Align(HIPCodeObjectAlign)) |
142 | 46 | << "\n"; |
143 | 46 | ObjStream << "__hip_fatbin:\n"; |
144 | 46 | ObjStream << " .incbin "; |
145 | 46 | llvm::sys::printArg(ObjStream, BundleFile, /*Quote=*/true); |
146 | 46 | ObjStream << "\n"; |
147 | 46 | 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 | 46 | if (C.getArgs().hasArg(options::OPT_fhip_dump_offload_linker_script)) |
152 | 2 | llvm::errs() << ObjBuffer; |
153 | | |
154 | | // Open script file and write the contents. |
155 | 46 | std::error_code EC; |
156 | 46 | llvm::raw_fd_ostream Objf(McinFile, EC, llvm::sys::fs::OF_None); |
157 | | |
158 | 46 | if (EC) { |
159 | 0 | C.getDriver().Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
160 | 0 | return; |
161 | 0 | } |
162 | | |
163 | 46 | Objf << ObjBuffer; |
164 | | |
165 | 46 | ArgStringList McArgs{"-triple", Args.MakeArgString(HostTriple.normalize()), |
166 | 46 | "-o", Output.getFilename(), |
167 | 46 | McinFile, "--filetype=obj"}; |
168 | 46 | const char *Mc = Args.MakeArgString(TC.GetProgramPath("llvm-mc")); |
169 | 46 | C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(), Mc, |
170 | 46 | McArgs, Inputs, Output)); |
171 | 46 | } |