1 //===-- clang-offload-bundler/ClangOffloadBundler.cpp ---------------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements a stand-alone clang-offload-bundler tool using the
11 /// OffloadBundler API.
13 //===----------------------------------------------------------------------===//
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"
47 #include <forward_list>
52 #include <system_error>
56 using namespace llvm::object
;
57 using namespace clang
;
59 static void PrintVersion(raw_ostream
&OS
) {
60 OS
<< clang::getClangToolFullVersion("clang-offload-bundler") << '\n';
63 int main(int argc
, const char **argv
) {
65 cl::opt
<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden
);
67 // Mark all our options with this category, everything else (except for
68 // -version and -help) will be hidden.
70 ClangOffloadBundlerCategory("clang-offload-bundler options");
72 InputFileNames("input",
73 cl::desc("Input file."
74 " Can be specified multiple times "
75 "for multiple input files."),
76 cl::cat(ClangOffloadBundlerCategory
));
78 InputFileNamesDeprecatedOpt("inputs", cl::CommaSeparated
,
79 cl::desc("[<input file>,...] (deprecated)"),
80 cl::cat(ClangOffloadBundlerCategory
));
82 OutputFileNames("output",
83 cl::desc("Output file."
84 " Can be specified multiple times "
85 "for multiple output files."),
86 cl::cat(ClangOffloadBundlerCategory
));
88 OutputFileNamesDeprecatedOpt("outputs", cl::CommaSeparated
,
89 cl::desc("[<output file>,...] (deprecated)"),
90 cl::cat(ClangOffloadBundlerCategory
));
92 TargetNames("targets", cl::CommaSeparated
,
93 cl::desc("[<offload kind>-<target triple>,...]"),
94 cl::cat(ClangOffloadBundlerCategory
));
95 cl::opt
<std::string
> FilesType(
97 cl::desc("Type of the files to be bundled/unbundled.\n"
98 "Current supported types are:\n"
100 " ii - c++-cpp-output\n"
101 " cui - cuda-cpp-output\n"
102 " hipi - hip-cpp-output\n"
108 " a - archive of objects\n"
109 " gch - precompiled-header\n"
110 " ast - clang AST file"),
111 cl::cat(ClangOffloadBundlerCategory
));
114 cl::desc("Unbundle bundled file into several output files.\n"),
115 cl::init(false), cl::cat(ClangOffloadBundlerCategory
));
117 ListBundleIDs("list", cl::desc("List bundle IDs in the bundled file.\n"),
118 cl::init(false), cl::cat(ClangOffloadBundlerCategory
));
119 cl::opt
<bool> PrintExternalCommands(
121 cl::desc("Print any external commands that are to be executed "
122 "instead of actually executing them - for testing purposes.\n"),
123 cl::init(false), cl::cat(ClangOffloadBundlerCategory
));
125 AllowMissingBundles("allow-missing-bundles",
126 cl::desc("Create empty files if bundles are missing "
127 "when unbundling.\n"),
128 cl::init(false), cl::cat(ClangOffloadBundlerCategory
));
130 BundleAlignment("bundle-align",
131 cl::desc("Alignment of bundle for binary files"),
132 cl::init(1), cl::cat(ClangOffloadBundlerCategory
));
133 cl::opt
<bool> HipOpenmpCompatible(
134 "hip-openmp-compatible",
135 cl::desc("Treat hip and hipv4 offload kinds as "
136 "compatible with openmp kind, and vice versa.\n"),
137 cl::init(false), cl::cat(ClangOffloadBundlerCategory
));
138 cl::opt
<bool> Compress("compress",
139 cl::desc("Compress output file when bundling.\n"),
140 cl::init(false), cl::cat(ClangOffloadBundlerCategory
));
141 cl::opt
<bool> Verbose("verbose", cl::desc("Print debug information.\n"),
142 cl::init(false), cl::cat(ClangOffloadBundlerCategory
));
144 // Process commandline options and report errors
145 sys::PrintStackTraceOnErrorSignal(argv
[0]);
147 cl::HideUnrelatedOptions(ClangOffloadBundlerCategory
);
148 cl::SetVersionPrinter(PrintVersion
);
149 cl::ParseCommandLineOptions(
151 "A tool to bundle several input files of the specified type <type> \n"
152 "referring to the same source file but different targets into a single \n"
153 "one. The resulting file can also be unbundled into different files by \n"
154 "this tool if -unbundle is provided.\n");
157 cl::PrintHelpMessage();
161 /// Class to store bundler options in standard (non-cl::opt) data structures
162 // Avoid using cl::opt variables after these assignments when possible
163 OffloadBundlerConfig BundlerConfig
;
164 BundlerConfig
.AllowMissingBundles
= AllowMissingBundles
;
165 BundlerConfig
.PrintExternalCommands
= PrintExternalCommands
;
166 BundlerConfig
.HipOpenmpCompatible
= HipOpenmpCompatible
;
167 BundlerConfig
.BundleAlignment
= BundleAlignment
;
168 BundlerConfig
.FilesType
= FilesType
;
169 BundlerConfig
.ObjcopyPath
= "";
170 // Do not override the default value Compress and Verbose in BundlerConfig.
171 if (Compress
.getNumOccurrences() > 0)
172 BundlerConfig
.Compress
= Compress
;
173 if (Verbose
.getNumOccurrences() > 0)
174 BundlerConfig
.Verbose
= Verbose
;
176 BundlerConfig
.TargetNames
= TargetNames
;
177 BundlerConfig
.InputFileNames
= InputFileNames
;
178 BundlerConfig
.OutputFileNames
= OutputFileNames
;
180 /// The index of the host input in the list of inputs.
181 BundlerConfig
.HostInputIndex
= ~0u;
183 /// Whether not having host target is allowed.
184 BundlerConfig
.AllowNoHost
= false;
186 auto reportError
= [argv
](Error E
) {
187 logAllUnhandledErrors(std::move(E
), WithColor::error(errs(), argv
[0]));
191 auto doWork
= [&](std::function
<llvm::Error()> Work
) {
192 if (llvm::Error Err
= Work()) {
193 reportError(std::move(Err
));
197 auto warningOS
= [argv
]() -> raw_ostream
& {
198 return WithColor::warning(errs(), StringRef(argv
[0]));
201 /// Path to the current binary.
202 std::string BundlerExecutable
= argv
[0];
204 if (!llvm::sys::fs::exists(BundlerExecutable
))
206 sys::fs::getMainExecutable(argv
[0], &BundlerExecutable
);
208 // Find llvm-objcopy in order to create the bundle binary.
209 ErrorOr
<std::string
> Objcopy
= sys::findProgramByName(
211 sys::path::parent_path(BundlerExecutable
));
213 Objcopy
= sys::findProgramByName("llvm-objcopy");
215 reportError(createStringError(Objcopy
.getError(),
216 "unable to find 'llvm-objcopy' in path"));
218 BundlerConfig
.ObjcopyPath
= *Objcopy
;
220 if (InputFileNames
.getNumOccurrences() != 0 &&
221 InputFileNamesDeprecatedOpt
.getNumOccurrences() != 0) {
222 reportError(createStringError(
223 errc::invalid_argument
,
224 "-inputs and -input cannot be used together, use only -input instead"));
227 if (InputFileNamesDeprecatedOpt
.size()) {
228 warningOS() << "-inputs is deprecated, use -input instead\n";
229 // temporary hack to support -inputs
230 std::vector
<std::string
> &s
= InputFileNames
;
231 s
.insert(s
.end(), InputFileNamesDeprecatedOpt
.begin(),
232 InputFileNamesDeprecatedOpt
.end());
234 BundlerConfig
.InputFileNames
= InputFileNames
;
236 if (OutputFileNames
.getNumOccurrences() != 0 &&
237 OutputFileNamesDeprecatedOpt
.getNumOccurrences() != 0) {
238 reportError(createStringError(errc::invalid_argument
,
239 "-outputs and -output cannot be used "
240 "together, use only -output instead"));
243 if (OutputFileNamesDeprecatedOpt
.size()) {
244 warningOS() << "-outputs is deprecated, use -output instead\n";
245 // temporary hack to support -outputs
246 std::vector
<std::string
> &s
= OutputFileNames
;
247 s
.insert(s
.end(), OutputFileNamesDeprecatedOpt
.begin(),
248 OutputFileNamesDeprecatedOpt
.end());
250 BundlerConfig
.OutputFileNames
= OutputFileNames
;
255 createStringError(errc::invalid_argument
,
256 "-unbundle and -list cannot be used together"));
258 if (InputFileNames
.size() != 1) {
259 reportError(createStringError(errc::invalid_argument
,
260 "only one input file supported for -list"));
262 if (OutputFileNames
.size()) {
263 reportError(createStringError(errc::invalid_argument
,
264 "-outputs option is invalid for -list"));
266 if (TargetNames
.size()) {
267 reportError(createStringError(errc::invalid_argument
,
268 "-targets option is invalid for -list"));
271 doWork([&]() { return OffloadBundler::ListBundleIDsInFile(
272 InputFileNames
.front(),
277 if (OutputFileNames
.size() == 0) {
279 createStringError(errc::invalid_argument
, "no output file specified!"));
282 if (TargetNames
.getNumOccurrences() == 0) {
283 reportError(createStringError(
284 errc::invalid_argument
,
285 "for the --targets option: must be specified at least once!"));
289 if (InputFileNames
.size() != 1) {
290 reportError(createStringError(
291 errc::invalid_argument
,
292 "only one input file supported in unbundling mode"));
294 if (OutputFileNames
.size() != TargetNames
.size()) {
295 reportError(createStringError(errc::invalid_argument
,
296 "number of output files and targets should "
297 "match in unbundling mode"));
300 if (BundlerConfig
.FilesType
== "a") {
301 reportError(createStringError(errc::invalid_argument
,
302 "Archive files are only supported "
305 if (OutputFileNames
.size() != 1) {
306 reportError(createStringError(
307 errc::invalid_argument
,
308 "only one output file supported in bundling mode"));
310 if (InputFileNames
.size() != TargetNames
.size()) {
311 reportError(createStringError(
312 errc::invalid_argument
,
313 "number of input files and targets should match in bundling mode"));
317 // Verify that the offload kinds and triples are known. We also check that we
318 // have exactly one host target.
320 unsigned HostTargetNum
= 0u;
322 llvm::DenseSet
<StringRef
> ParsedTargets
;
323 // Map {offload-kind}-{triple} to target IDs.
324 std::map
<std::string
, std::set
<StringRef
>> TargetIDs
;
325 // Standardize target names to include env field
326 std::vector
<std::string
> StandardizedTargetNames
;
327 for (StringRef Target
: TargetNames
) {
328 if (ParsedTargets
.contains(Target
)) {
329 reportError(createStringError(errc::invalid_argument
,
330 "Duplicate targets are not allowed"));
332 ParsedTargets
.insert(Target
);
334 auto OffloadInfo
= OffloadTargetInfo(Target
, BundlerConfig
);
335 bool KindIsValid
= OffloadInfo
.isOffloadKindValid();
336 bool TripleIsValid
= OffloadInfo
.isTripleValid();
338 StandardizedTargetNames
.push_back(OffloadInfo
.str());
340 if (!KindIsValid
|| !TripleIsValid
) {
341 SmallVector
<char, 128u> Buf
;
342 raw_svector_ostream
Msg(Buf
);
343 Msg
<< "invalid target '" << Target
<< "'";
345 Msg
<< ", unknown offloading kind '" << OffloadInfo
.OffloadKind
<< "'";
347 Msg
<< ", unknown target triple '" << OffloadInfo
.Triple
.str() << "'";
348 reportError(createStringError(errc::invalid_argument
, Msg
.str()));
351 TargetIDs
[OffloadInfo
.OffloadKind
.str() + "-" + OffloadInfo
.Triple
.str()]
352 .insert(OffloadInfo
.TargetID
);
353 if (KindIsValid
&& OffloadInfo
.hasHostKind()) {
355 // Save the index of the input that refers to the host.
356 BundlerConfig
.HostInputIndex
= Index
;
359 if (OffloadInfo
.OffloadKind
!= "hip" && OffloadInfo
.OffloadKind
!= "hipv4")
365 BundlerConfig
.TargetNames
= StandardizedTargetNames
;
367 for (const auto &TargetID
: TargetIDs
) {
368 if (auto ConflictingTID
=
369 clang::getConflictTargetIDCombination(TargetID
.second
)) {
370 SmallVector
<char, 128u> Buf
;
371 raw_svector_ostream
Msg(Buf
);
372 Msg
<< "Cannot bundle inputs with conflicting targets: '"
373 << TargetID
.first
+ "-" + ConflictingTID
->first
<< "' and '"
374 << TargetID
.first
+ "-" + ConflictingTID
->second
<< "'";
375 reportError(createStringError(errc::invalid_argument
, Msg
.str()));
379 // HIP uses clang-offload-bundler to bundle device-only compilation results
380 // for multiple GPU archs, therefore allow no host target if all entries
382 BundlerConfig
.AllowNoHost
= HIPOnly
;
384 // Host triple is not really needed for unbundling operation, so do not
385 // treat missing host triple as error if we do unbundling.
386 if ((Unbundle
&& HostTargetNum
> 1) ||
387 (!Unbundle
&& HostTargetNum
!= 1 && !BundlerConfig
.AllowNoHost
)) {
388 reportError(createStringError(errc::invalid_argument
,
389 "expecting exactly one host target but got " +
390 Twine(HostTargetNum
)));
393 OffloadBundler
Bundler(BundlerConfig
);
397 if (BundlerConfig
.FilesType
== "a")
398 return Bundler
.UnbundleArchive();
400 return Bundler
.UnbundleFiles();
402 return Bundler
.BundleFiles();