1//===-- clang-offload-bundler/ClangOffloadBundler.cpp ---------------------===//
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/// \file
10/// This file implements a stand-alone clang-offload-bundler tool using the
11/// OffloadBundler API.
12///
13//===----------------------------------------------------------------------===//
14
15#include "clang/Basic/Cuda.h"
16#include "clang/Basic/TargetID.h"
17#include "clang/Basic/Version.h"
18#include "clang/Driver/OffloadBundler.h"
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Object/Archive.h"
24#include "llvm/Object/ArchiveWriter.h"
25#include "llvm/Object/Binary.h"
26#include "llvm/Object/ObjectFile.h"
27#include "llvm/Support/Casting.h"
28#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/Errc.h"
31#include "llvm/Support/Error.h"
32#include "llvm/Support/ErrorOr.h"
33#include "llvm/Support/FileSystem.h"
34#include "llvm/Support/MemoryBuffer.h"
35#include "llvm/Support/Path.h"
36#include "llvm/Support/Program.h"
37#include "llvm/Support/Signals.h"
38#include "llvm/Support/StringSaver.h"
39#include "llvm/Support/WithColor.h"
40#include "llvm/Support/raw_ostream.h"
41#include "llvm/TargetParser/Host.h"
42#include "llvm/TargetParser/Triple.h"
43#include <algorithm>
44#include <cassert>
45#include <cstddef>
46#include <cstdint>
47#include <forward_list>
48#include <map>
49#include <memory>
50#include <set>
51#include <string>
52#include <system_error>
53#include <utility>
54
55using namespace llvm;
56using namespace llvm::object;
57using namespace clang;
58
59static void PrintVersion(raw_ostream &OS) {
60 OS << clang::getClangToolFullVersion(ToolName: "clang-offload-bundler") << '\n';
61}
62
63int main(int argc, const char **argv) {
64
65 // Mark all our options with this category, everything else (except for
66 // -version and -help) will be hidden.
67 cl::OptionCategory
68 ClangOffloadBundlerCategory("clang-offload-bundler options");
69 cl::list<std::string>
70 InputFileNames("input",
71 cl::desc("Input file."
72 " Can be specified multiple times "
73 "for multiple input files."),
74 cl::cat(ClangOffloadBundlerCategory));
75 cl::list<std::string>
76 InputFileNamesDeprecatedOpt("inputs", cl::CommaSeparated,
77 cl::desc("[<input file>,...] (deprecated)"),
78 cl::cat(ClangOffloadBundlerCategory));
79 cl::list<std::string>
80 OutputFileNames("output",
81 cl::desc("Output file."
82 " Can be specified multiple times "
83 "for multiple output files."),
84 cl::cat(ClangOffloadBundlerCategory));
85 cl::list<std::string>
86 OutputFileNamesDeprecatedOpt("outputs", cl::CommaSeparated,
87 cl::desc("[<output file>,...] (deprecated)"),
88 cl::cat(ClangOffloadBundlerCategory));
89 cl::list<std::string>
90 TargetNames("targets", cl::CommaSeparated,
91 cl::desc("[<offload kind>-<target triple>,...]"),
92 cl::cat(ClangOffloadBundlerCategory));
93 cl::opt<std::string> FilesType(
94 "type", cl::Required,
95 cl::desc("Type of the files to be bundled/unbundled.\n"
96 "Current supported types are:\n"
97 " i - cpp-output\n"
98 " ii - c++-cpp-output\n"
99 " cui - cuda-cpp-output\n"
100 " hipi - hip-cpp-output\n"
101 " d - dependency\n"
102 " ll - llvm\n"
103 " bc - llvm-bc\n"
104 " s - assembler\n"
105 " o - object\n"
106 " a - archive of objects\n"
107 " gch - precompiled-header\n"
108 " ast - clang AST file"),
109 cl::cat(ClangOffloadBundlerCategory));
110 cl::opt<bool>
111 Unbundle("unbundle",
112 cl::desc("Unbundle bundled file into several output files.\n"),
113 cl::init(Val: false), cl::cat(ClangOffloadBundlerCategory));
114 cl::opt<bool>
115 ListBundleIDs("list", cl::desc("List bundle IDs in the bundled file.\n"),
116 cl::init(Val: false), cl::cat(ClangOffloadBundlerCategory));
117 cl::opt<bool> PrintExternalCommands(
118 "###",
119 cl::desc("Print any external commands that are to be executed "
120 "instead of actually executing them - for testing purposes.\n"),
121 cl::init(Val: false), cl::cat(ClangOffloadBundlerCategory));
122 cl::opt<bool>
123 AllowMissingBundles("allow-missing-bundles",
124 cl::desc("Create empty files if bundles are missing "
125 "when unbundling.\n"),
126 cl::init(Val: false), cl::cat(ClangOffloadBundlerCategory));
127 cl::opt<unsigned>
128 BundleAlignment("bundle-align",
129 cl::desc("Alignment of bundle for binary files"),
130 cl::init(Val: 1), cl::cat(ClangOffloadBundlerCategory));
131 cl::opt<bool> CheckInputArchive(
132 "check-input-archive",
133 cl::desc("Check if input heterogeneous archive is "
134 "valid in terms of TargetID rules.\n"),
135 cl::init(Val: false), cl::cat(ClangOffloadBundlerCategory));
136 cl::opt<bool> HipOpenmpCompatible(
137 "hip-openmp-compatible",
138 cl::desc("Treat hip and hipv4 offload kinds as "
139 "compatible with openmp kind, and vice versa.\n"),
140 cl::init(Val: false), cl::cat(ClangOffloadBundlerCategory));
141 cl::opt<bool> Compress("compress",
142 cl::desc("Compress output file when bundling.\n"),
143 cl::init(Val: false), cl::cat(ClangOffloadBundlerCategory));
144 cl::opt<bool> Verbose("verbose", cl::desc("Print debug information.\n"),
145 cl::init(Val: false), cl::cat(ClangOffloadBundlerCategory));
146 cl::opt<int> CompressionLevel(
147 "compression-level", cl::desc("Specify the compression level (integer)"),
148 cl::value_desc("n"), cl::cat(ClangOffloadBundlerCategory));
149
150 // Process commandline options and report errors
151 sys::PrintStackTraceOnErrorSignal(Argv0: argv[0]);
152
153 cl::HideUnrelatedOptions(Category&: ClangOffloadBundlerCategory);
154 cl::SetVersionPrinter(PrintVersion);
155 cl::ParseCommandLineOptions(
156 argc, argv,
157 Overview: "A tool to bundle several input files of the specified type <type> \n"
158 "referring to the same source file but different targets into a single \n"
159 "one. The resulting file can also be unbundled into different files by \n"
160 "this tool if -unbundle is provided.\n");
161
162 /// Class to store bundler options in standard (non-cl::opt) data structures
163 // Avoid using cl::opt variables after these assignments when possible
164 OffloadBundlerConfig BundlerConfig;
165 BundlerConfig.AllowMissingBundles = AllowMissingBundles;
166 BundlerConfig.CheckInputArchive = CheckInputArchive;
167 BundlerConfig.PrintExternalCommands = PrintExternalCommands;
168 BundlerConfig.HipOpenmpCompatible = HipOpenmpCompatible;
169 BundlerConfig.BundleAlignment = BundleAlignment;
170 BundlerConfig.FilesType = FilesType;
171 BundlerConfig.ObjcopyPath = "";
172 // Do not override the default value Compress and Verbose in BundlerConfig.
173 if (Compress.getNumOccurrences() > 0)
174 BundlerConfig.Compress = Compress;
175 if (Verbose.getNumOccurrences() > 0)
176 BundlerConfig.Verbose = Verbose;
177 if (CompressionLevel.getNumOccurrences() > 0)
178 BundlerConfig.CompressionLevel = CompressionLevel;
179
180 BundlerConfig.TargetNames.assign(in_start: TargetNames.begin(), in_end: TargetNames.end());
181 BundlerConfig.InputFileNames.assign(in_start: InputFileNames.begin(),
182 in_end: InputFileNames.end());
183 BundlerConfig.OutputFileNames.assign(in_start: OutputFileNames.begin(),
184 in_end: OutputFileNames.end());
185
186 /// The index of the host input in the list of inputs.
187 BundlerConfig.HostInputIndex = ~0u;
188
189 /// Whether not having host target is allowed.
190 BundlerConfig.AllowNoHost = false;
191
192 auto reportError = [argv](Error E) {
193 logAllUnhandledErrors(E: std::move(E), OS&: WithColor::error(OS&: errs(), Prefix: argv[0]));
194 return 1;
195 };
196
197 auto doWork = [&](std::function<llvm::Error()> Work) {
198 if (llvm::Error Err = Work()) {
199 return reportError(std::move(Err));
200 }
201 return 0;
202 };
203
204 auto warningOS = [argv]() -> raw_ostream & {
205 return WithColor::warning(OS&: errs(), Prefix: StringRef(argv[0]));
206 };
207
208 /// Path to the current binary.
209 std::string BundlerExecutable = argv[0];
210
211 if (!llvm::sys::fs::exists(Path: BundlerExecutable))
212 BundlerExecutable =
213 sys::fs::getMainExecutable(argv0: argv[0], MainExecAddr: &BundlerExecutable);
214
215 // Find llvm-objcopy in order to create the bundle binary.
216 ErrorOr<std::string> Objcopy = sys::findProgramByName(
217 Name: "llvm-objcopy",
218 Paths: sys::path::parent_path(path: BundlerExecutable));
219 if (!Objcopy)
220 Objcopy = sys::findProgramByName(Name: "llvm-objcopy");
221 if (!Objcopy)
222 return reportError(createStringError(
223 EC: Objcopy.getError(), S: "unable to find 'llvm-objcopy' in path"));
224 else
225 BundlerConfig.ObjcopyPath = *Objcopy;
226
227 if (InputFileNames.getNumOccurrences() != 0 &&
228 InputFileNamesDeprecatedOpt.getNumOccurrences() != 0) {
229 return reportError(createStringError(
230 EC: errc::invalid_argument,
231 S: "-inputs and -input cannot be used together, use only -input instead"));
232 }
233
234 if (InputFileNamesDeprecatedOpt.size()) {
235 warningOS() << "-inputs is deprecated, use -input instead\n";
236 // temporary hack to support -inputs
237 std::vector<std::string> &s = InputFileNames;
238 s.insert(position: s.end(), first: InputFileNamesDeprecatedOpt.begin(),
239 last: InputFileNamesDeprecatedOpt.end());
240 }
241 BundlerConfig.InputFileNames.assign(in_start: InputFileNames.begin(),
242 in_end: InputFileNames.end());
243
244 if (OutputFileNames.getNumOccurrences() != 0 &&
245 OutputFileNamesDeprecatedOpt.getNumOccurrences() != 0) {
246 return reportError(createStringError(EC: errc::invalid_argument,
247 S: "-outputs and -output cannot be used "
248 "together, use only -output instead"));
249 }
250
251 if (OutputFileNamesDeprecatedOpt.size()) {
252 warningOS() << "-outputs is deprecated, use -output instead\n";
253 // temporary hack to support -outputs
254 std::vector<std::string> &s = OutputFileNames;
255 s.insert(position: s.end(), first: OutputFileNamesDeprecatedOpt.begin(),
256 last: OutputFileNamesDeprecatedOpt.end());
257 }
258 BundlerConfig.OutputFileNames.assign(in_start: OutputFileNames.begin(),
259 in_end: OutputFileNames.end());
260
261 if (ListBundleIDs) {
262 if (Unbundle) {
263 return reportError(
264 createStringError(EC: errc::invalid_argument,
265 S: "-unbundle and -list cannot be used together"));
266 }
267 if (InputFileNames.size() != 1) {
268 return reportError(createStringError(
269 EC: errc::invalid_argument, S: "only one input file supported for -list"));
270 }
271 if (OutputFileNames.size()) {
272 return reportError(createStringError(
273 EC: errc::invalid_argument, S: "-outputs option is invalid for -list"));
274 }
275 if (TargetNames.size()) {
276 return reportError(createStringError(
277 EC: errc::invalid_argument, S: "-targets option is invalid for -list"));
278 }
279
280 return doWork([&]() {
281 return OffloadBundler::ListBundleIDsInFile(InputFileName: InputFileNames.front(),
282 BundlerConfig);
283 });
284 }
285
286 if (BundlerConfig.CheckInputArchive) {
287 if (!Unbundle) {
288 return reportError(createStringError(
289 EC: errc::invalid_argument, S: "-check-input-archive cannot be used while "
290 "bundling"));
291 }
292 if (Unbundle && BundlerConfig.FilesType != "a") {
293 return reportError(createStringError(
294 EC: errc::invalid_argument, S: "-check-input-archive can only be used for "
295 "unbundling archives (-type=a)"));
296 }
297 }
298
299 if (OutputFileNames.size() == 0) {
300 return reportError(
301 createStringError(EC: errc::invalid_argument, S: "no output file specified!"));
302 }
303
304 if (TargetNames.getNumOccurrences() == 0) {
305 return reportError(createStringError(
306 EC: errc::invalid_argument,
307 S: "for the --targets option: must be specified at least once!"));
308 }
309
310 if (Unbundle) {
311 if (InputFileNames.size() != 1) {
312 return reportError(createStringError(
313 EC: errc::invalid_argument,
314 S: "only one input file supported in unbundling mode"));
315 }
316 if (OutputFileNames.size() != TargetNames.size()) {
317 return reportError(createStringError(
318 EC: errc::invalid_argument, S: "number of output files and targets should "
319 "match in unbundling mode"));
320 }
321 } else {
322 if (BundlerConfig.FilesType == "a") {
323 return reportError(createStringError(EC: errc::invalid_argument,
324 S: "Archive files are only supported "
325 "for unbundling"));
326 }
327 if (OutputFileNames.size() != 1) {
328 return reportError(
329 createStringError(EC: errc::invalid_argument,
330 S: "only one output file supported in bundling mode"));
331 }
332 if (InputFileNames.size() != TargetNames.size()) {
333 return reportError(createStringError(
334 EC: errc::invalid_argument,
335 S: "number of input files and targets should match in bundling mode"));
336 }
337 }
338
339 // Verify that the offload kinds and triples are known. We also check that we
340 // have exactly one host target.
341 unsigned Index = 0u;
342 unsigned HostTargetNum = 0u;
343 bool HIPOnly = true;
344 llvm::DenseSet<StringRef> ParsedTargets;
345 // Map {offload-kind}-{triple} to target IDs.
346 std::map<std::string, std::set<StringRef>> TargetIDs;
347 // Standardize target names to include env field
348 std::vector<std::string> StandardizedTargetNames;
349 for (StringRef Target : TargetNames) {
350 if (!ParsedTargets.insert(V: Target).second) {
351 return reportError(createStringError(
352 EC: errc::invalid_argument, S: "Duplicate targets are not allowed"));
353 }
354
355 if (!checkOffloadBundleID(Str: Target)) {
356 return reportError(createStringError(
357 EC: errc::invalid_argument,
358 S: "Targets need to follow the format '<offload kind>-<target triple>', "
359 "where '<target triple>' follows the format "
360 "'<kind>-<arch>-<vendor>-<os>-<env>[-<target id>[:target "
361 "features]]'."));
362 }
363
364 auto OffloadInfo = OffloadTargetInfo(Target, BundlerConfig);
365 bool KindIsValid = OffloadInfo.isOffloadKindValid();
366 bool TripleIsValid = OffloadInfo.isTripleValid();
367
368 StandardizedTargetNames.push_back(x: OffloadInfo.str());
369
370 if (!KindIsValid || !TripleIsValid) {
371 SmallVector<char, 128u> Buf;
372 raw_svector_ostream Msg(Buf);
373 Msg << "invalid target '" << Target << "'";
374 if (!KindIsValid)
375 Msg << ", unknown offloading kind '" << OffloadInfo.OffloadKind << "'";
376 if (!TripleIsValid)
377 Msg << ", unknown target triple '" << OffloadInfo.Triple.str() << "'";
378 return reportError(createStringError(EC: errc::invalid_argument, S: Msg.str()));
379 }
380
381 TargetIDs[OffloadInfo.OffloadKind.str() + "-" + OffloadInfo.Triple.str()]
382 .insert(x: OffloadInfo.TargetID);
383 if (KindIsValid && OffloadInfo.hasHostKind()) {
384 ++HostTargetNum;
385 // Save the index of the input that refers to the host.
386 BundlerConfig.HostInputIndex = Index;
387 }
388
389 if (OffloadInfo.OffloadKind != "hip" && OffloadInfo.OffloadKind != "hipv4")
390 HIPOnly = false;
391
392 ++Index;
393 }
394
395 BundlerConfig.TargetNames.assign(in_start: StandardizedTargetNames.begin(),
396 in_end: StandardizedTargetNames.end());
397
398 for (const auto &TargetID : TargetIDs) {
399 if (auto ConflictingTID =
400 clang::getConflictTargetIDCombination(TargetIDs: TargetID.second)) {
401 SmallVector<char, 128u> Buf;
402 raw_svector_ostream Msg(Buf);
403 Msg << "Cannot bundle inputs with conflicting targets: '"
404 << TargetID.first + "-" + ConflictingTID->first << "' and '"
405 << TargetID.first + "-" + ConflictingTID->second << "'";
406 return reportError(createStringError(EC: errc::invalid_argument, S: Msg.str()));
407 }
408 }
409
410 // HIP uses clang-offload-bundler to bundle device-only compilation results
411 // for multiple GPU archs, therefore allow no host target if all entries
412 // are for HIP.
413 BundlerConfig.AllowNoHost = HIPOnly;
414
415 // Host triple is not really needed for unbundling operation, so do not
416 // treat missing host triple as error if we do unbundling.
417 if ((Unbundle && HostTargetNum > 1) ||
418 (!Unbundle && HostTargetNum != 1 && !BundlerConfig.AllowNoHost)) {
419 return reportError(createStringError(
420 EC: errc::invalid_argument,
421 S: "expecting exactly one host target but got " + Twine(HostTargetNum)));
422 }
423
424 OffloadBundler Bundler(BundlerConfig);
425
426 return doWork([&]() {
427 if (Unbundle)
428 return (BundlerConfig.FilesType == "a") ? Bundler.UnbundleArchive()
429 : Bundler.UnbundleFiles();
430 return Bundler.BundleFiles();
431 });
432}
433