Skip to content

Commit 3cf1909

Browse files
authored
[clang-offload-bundler] Add support for -check-input-archive (llvm#73709)
In this patch, we add support for checking a heterogeneous archive. We also significantly improve the clang-offload-bundler documentation.
1 parent 246b8ea commit 3cf1909

File tree

5 files changed

+466
-109
lines changed

5 files changed

+466
-109
lines changed

clang/docs/ClangOffloadBundler.rst

Lines changed: 254 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,58 +30,139 @@ includes an ``init`` function that will use the runtime corresponding to the
3030
offload kind (see :ref:`clang-offload-kind-table`) to load the offload code
3131
objects appropriate to the devices present when the host program is executed.
3232

33+
:program:`clang-offload-bundler` is located in
34+
`clang/tools/clang-offload-bundler`.
35+
36+
.. code-block:: console
37+
38+
$ clang-offload-bundler -help
39+
OVERVIEW: A tool to bundle several input files of the specified type <type>
40+
referring to the same source file but different targets into a single
41+
one. The resulting file can also be unbundled into different files by
42+
this tool if -unbundle is provided.
43+
44+
USAGE: clang-offload-bundler [options]
45+
46+
OPTIONS:
47+
48+
Generic Options:
49+
50+
--help - Display available options (--help-hidden for more)
51+
--help-list - Display list of available options (--help-list-hidden for more)
52+
--version - Display the version of this program
53+
54+
clang-offload-bundler options:
55+
56+
--### - Print any external commands that are to be executed instead of actually executing them - for testing purposes.
57+
--allow-missing-bundles - Create empty files if bundles are missing when unbundling.
58+
--bundle-align=<uint> - Alignment of bundle for binary files
59+
--check-input-archive - Check if input heterogeneous archive is valid in terms of TargetID rules.
60+
--inputs=<string> - [<input file>,...]
61+
--list - List bundle IDs in the bundled file.
62+
--outputs=<string> - [<output file>,...]
63+
--targets=<string> - [<offload kind>-<target triple>,...]
64+
--type=<string> - Type of the files to be bundled/unbundled.
65+
Current supported types are:
66+
i - cpp-output
67+
ii - c++-cpp-output
68+
cui - cuda/hip-output
69+
d - dependency
70+
ll - llvm
71+
bc - llvm-bc
72+
s - assembler
73+
o - object
74+
a - archive of bundled files
75+
gch - precompiled-header
76+
ast - clang AST file
77+
--unbundle - Unbundle bundled file into several output files.
78+
79+
Usage
80+
=====
81+
82+
This tool can be used as follows for bundling:
83+
84+
::
85+
86+
clang-offload-bundler -targets=triple1,triple2 -type=ii -inputs=a.triple1.ii,a.triple2.ii -outputs=a.ii
87+
88+
or, it can be used as follows for unbundling:
89+
90+
::
91+
92+
clang-offload-bundler -targets=triple1,triple2 -type=ii -outputs=a.triple1.ii,a.triple2.ii -inputs=a.ii -unbundle
93+
94+
3395
Supported File Formats
3496
======================
35-
Several text and binary file formats are supported for bundling/unbundling. See
36-
:ref:`supported-file-formats-table` for a list of currently supported formats.
97+
98+
Multiple text and binary file formats are supported for bundling/unbundling. See
99+
:ref:`supported-file-formats-table` for a list of currently supported input
100+
formats. Use the ``File Type`` column to determine the value to pass to the
101+
``--type`` option based on the type of input files while bundling/unbundling.
37102

38103
.. table:: Supported File Formats
39104
:name: supported-file-formats-table
40105

41-
+--------------------+----------------+-------------+
42-
| File Format | File Extension | Text/Binary |
43-
+====================+================+=============+
44-
| CPP output | i | Text |
45-
+--------------------+----------------+-------------+
46-
| C++ CPP output | ii | Text |
47-
+--------------------+----------------+-------------+
48-
| CUDA/HIP output | cui | Text |
49-
+--------------------+----------------+-------------+
50-
| Dependency | d | Text |
51-
+--------------------+----------------+-------------+
52-
| LLVM | ll | Text |
53-
+--------------------+----------------+-------------+
54-
| LLVM Bitcode | bc | Binary |
55-
+--------------------+----------------+-------------+
56-
| Assembler | s | Text |
57-
+--------------------+----------------+-------------+
58-
| Object | o | Binary |
59-
+--------------------+----------------+-------------+
60-
| Archive of objects | a | Binary |
61-
+--------------------+----------------+-------------+
62-
| Precompiled header | gch | Binary |
63-
+--------------------+----------------+-------------+
64-
| Clang AST file | ast | Binary |
65-
+--------------------+----------------+-------------+
106+
+--------------------------+----------------+-------------+
107+
| File Format | File Type | Text/Binary |
108+
+==========================+================+=============+
109+
| CPP output | i | Text |
110+
+--------------------------+----------------+-------------+
111+
| C++ CPP output | ii | Text |
112+
+--------------------------+----------------+-------------+
113+
| CUDA/HIP output | cui | Text |
114+
+--------------------------+----------------+-------------+
115+
| Dependency | d | Text |
116+
+--------------------------+----------------+-------------+
117+
| LLVM | ll | Text |
118+
+--------------------------+----------------+-------------+
119+
| LLVM Bitcode | bc | Binary |
120+
+--------------------------+----------------+-------------+
121+
| Assembler | s | Text |
122+
+--------------------------+----------------+-------------+
123+
| Object | o | Binary |
124+
+--------------------------+----------------+-------------+
125+
| Archive of bundled files | a | Binary |
126+
+--------------------------+----------------+-------------+
127+
| Precompiled header | gch | Binary |
128+
+--------------------------+----------------+-------------+
129+
| Clang AST file | ast | Binary |
130+
+--------------------------+----------------+-------------+
66131

67132
.. _clang-bundled-code-object-layout-text:
68133

69134
Bundled Text File Layout
70135
========================
71136

72-
The format of the bundled files is currently very simple: text formats are
73-
concatenated with comments that have a magic string and bundle entry ID in
74-
between.
137+
The text file formats are concatenated with comments that have a magic string
138+
and bundle entry ID in between. The BNF syntax to represent a code object
139+
bundle file is:
75140

76141
::
77142

78-
"Comment OFFLOAD_BUNDLER_MAGIC_STR__START__ 1st Bundle Entry ID"
79-
Bundle 1
80-
"Comment OFFLOAD_BUNDLER_MAGIC_STR__END__ 1st Bundle Entry ID"
81-
...
82-
"Comment OFFLOAD_BUNDLER_MAGIC_STR__START__ Nth Bundle Entry ID"
83-
Bundle N
84-
"Comment OFFLOAD_BUNDLER_MAGIC_STR__END__ 1st Bundle Entry ID"
143+
<file> ::== <bundle> | <bundle> <file>
144+
<bundle> ::== <comment> <start> <bundle_id> <eol> <bundle> <eol>
145+
<comment> end <bundle_id> <eol>
146+
<start> ::== OFFLOAD_BUNDLER_MAGIC_STR__START__
147+
<end> ::== OFFLOAD_BUNDLER_MAGIC_STR__END__
148+
149+
**comment**
150+
The symbol used for starting single-line comment in the file type of
151+
constituting bundles. E.g. it is ";" for ll ``File Type`` and "#" for "s"
152+
``File Type``.
153+
154+
**bundle_id**
155+
The :ref:`clang-bundle-entry-id` for the enclosing bundle.
156+
157+
**eol**
158+
The end of line character.
159+
160+
**bundle**
161+
The code object stored in one of the supported text file formats.
162+
163+
**OFFLOAD_BUNDLER_MAGIC_STR__**
164+
Magic string that marks the existence of offloading data i.e.
165+
"__CLANG_OFFLOAD_BUNDLE__".
85166

86167
.. _clang-bundled-code-object-layout:
87168

@@ -126,8 +207,8 @@ The layout of a bundled code object is defined by the following table:
126207
Bundle Entry ID
127208
===============
128209

129-
Each entry in a bundled code object (see
130-
:ref:`clang-bundled-code-object-layout`) has a bundle entry ID that indicates
210+
Each entry in a bundled code object (see :ref:`clang-bundled-code-object-layout-text`
211+
and :ref:`clang-bundled-code-object-layout`) has a bundle entry ID that indicates
131212
the kind of the entry's code object and the runtime that manages it.
132213

133214
Bundle entry ID syntax is defined by the following BNF syntax:
@@ -193,11 +274,30 @@ Where:
193274
The canonical target ID of the code object. Present only if the target
194275
supports a target ID. See :ref:`clang-target-id`.
195276

196-
Each entry of a bundled code object must have a different bundle entry ID. There
197-
can be multiple entries for the same processor provided they differ in target
198-
feature settings. If there is an entry with a target feature specified as *Any*,
199-
then all entries must specify that target feature as *Any* for the same
200-
processor. There may be additional target specific restrictions.
277+
.. _code-object-composition:
278+
279+
Bundled Code Object Composition
280+
-------------------------------
281+
282+
* Each entry of a bundled code object must have a different bundle entry ID.
283+
* There can be multiple entries for the same processor provided they differ
284+
in target feature settings.
285+
* If there is an entry with a target feature specified as *Any*, then all
286+
entries must specify that target feature as *Any* for the same processor.
287+
288+
There may be additional target specific restrictions.
289+
290+
.. _compatibility-bundle-entry-id:
291+
292+
Compatibility Rules for Bundle Entry ID
293+
---------------------------------------
294+
295+
A code object, specified using its Bundle Entry ID, can be loaded and
296+
executed on a target processor, if:
297+
298+
* Their offload kinds are the same.
299+
* Their target triples are compatible.
300+
* Their Target IDs are compatible as defined in :ref:`compatibility-target-id`.
201301

202302
.. _clang-target-id:
203303

@@ -247,6 +347,17 @@ Where:
247347
object compiled with a target ID specifying a target feature off
248348
can only be loaded on a processor configured with the target feature off.
249349

350+
.. _compatibility-target-id:
351+
352+
Compatibility Rules for Target ID
353+
---------------------------------
354+
355+
A code object compiled for a Target ID is considered compatible for a
356+
target, if:
357+
358+
* Their processor is same.
359+
* Their feature set is compatible as defined above.
360+
250361
There are two forms of target ID:
251362

252363
*Non-Canonical Form*
@@ -279,14 +390,14 @@ Most other targets do not support target IDs.
279390
Archive Unbundling
280391
==================
281392

282-
Unbundling of heterogeneous device archive is done to create device specific
283-
archives. Heterogeneous Device Archive is in a format compatible with GNU ar
284-
utility and contains a collection of bundled device binaries where each bundle
285-
file will contain device binaries for a host and one or more targets. The
286-
output device specific archive is in a format compatible with GNU ar utility
287-
and contains a collection of device binaries for a specific target.
393+
Unbundling of a heterogeneous device archive (HDA) is done to create device specific
394+
archives. HDA is in a format compatible with GNU ``ar`` utility and contains a
395+
collection of bundled device binaries where each bundle file will contain
396+
device binaries for a host and one or more targets. The output device-specific
397+
archive is in a format compatible with GNU ``ar`` utility and contains a
398+
collection of device binaries for a specific target.
288399

289-
.. code::
400+
::
290401

291402
Heterogeneous Device Archive, HDA = {F1.X, F2.X, ..., FN.Y}
292403
where, Fi = Bundle{Host-DeviceBinary, T1-DeviceBinary, T2-DeviceBinary, ...,
@@ -299,16 +410,101 @@ and contains a collection of device binaries for a specific target.
299410
where, Fi-Tj-DeviceBinary.X represents device binary of i-th bundled device
300411
binary file for target Tj.
301412

302-
clang-offload-bundler extracts compatible device binaries for a given target
413+
The clang-offload-bundler extracts compatible device binaries for a given target
303414
from the bundled device binaries in a heterogeneous device archive and creates
304-
a target specific device archive without bundling.
415+
a target-specific device archive without bundling.
416+
417+
The clang-offload-bundler determines whether a device binary is compatible
418+
with a target by comparing bundle IDs. Two bundle IDs are considered
419+
compatible if:
420+
421+
* Their offload kinds are the same
422+
* Their target triples are the same
423+
* Their Target IDs are the same
424+
425+
Creating a Heterogeneous Device Archive
426+
---------------------------------------
427+
428+
1. Compile source file(s) to generate object file(s)
429+
430+
::
431+
432+
clang -O2 -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa,amdgcn-amd-amdhsa,\
433+
nvptx64-nvidia-cuda, nvptx64-nvidia-cuda \
434+
-Xopenmp-target=amdgcn-amd-amdhsa -march=gfx906:sramecc-:xnack+ \
435+
-Xopenmp-target=amdgcn-amd-amdhsa -march=gfx906:sramecc+:xnack+ \
436+
-Xopenmp-target=nvptx64-nvidia-cuda -march=sm_70 \
437+
-Xopenmp-target=nvptx64-nvidia-cuda -march=sm_80 \
438+
-c func_1.c -o func_1.o
439+
440+
clang -O2 -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa,amdgcn-amd-amdhsa,
441+
nvptx64-nvidia-cuda, nvptx64-nvidia-cuda \
442+
-Xopenmp-target=amdgcn-amd-amdhsa -march=gfx906:sramecc-:xnack+ \
443+
-Xopenmp-target=amdgcn-amd-amdhsa -march=gfx906:sramecc+:xnack+ \
444+
-Xopenmp-target=nvptx64-nvidia-cuda -march=sm_70 \
445+
-Xopenmp-target=nvptx64-nvidia-cuda -march=sm_80 \
446+
-c func_2.c -o func_2.o
447+
448+
2. Create a heterogeneous device archive by combining all the object file(s)
449+
450+
::
451+
452+
llvm-ar cr libFatArchive.a func_1.o func_2.o
453+
454+
Extracting a Device Specific Archive
455+
------------------------------------
456+
457+
UnbundleArchive takes a heterogeneous device archive file (".a") as input
458+
containing bundled device binary files, and a list of offload targets (not
459+
host), and extracts the device binaries into a new archive file for each
460+
offload target. Each resulting archive file contains all device binaries
461+
compatible with that particular offload target. Compatibility between a
462+
device binary in HDA and a target is based on the compatibility between their
463+
bundle entry IDs as defined in :ref:`compatibility-bundle-entry-id`.
464+
465+
Following cases may arise during compatibility testing:
466+
467+
* A binary is compatible with one or more targets: Insert the binary into the
468+
device-specific archive of each compatible target.
469+
* A binary is not compatible with any target: Skip the binary.
470+
* One or more binaries are compatible with a target: Insert all binaries into
471+
the device-specific archive of the target. The insertion need not be ordered.
472+
* No binary is compatible with a target: If ``allow-missing-bundles`` option is
473+
present then create an empty archive for the target. Otherwise, produce an
474+
error without creating an archive.
475+
476+
The created archive file does not contain an index of the symbols and device
477+
binary files are named as <<Parent Bundle Name>-<DeviceBinary's TargetID>>,
478+
with ':' replaced with '_'.
479+
480+
Usage
481+
-----
482+
483+
::
484+
485+
clang-offload-bundler --unbundle --inputs=libFatArchive.a -type=a \
486+
-targets=openmp-amdgcn-amdhsa-gfx906:sramecc+:xnack+, \
487+
openmp-amdgcn-amdhsa-gfx908:sramecc-:xnack+ \
488+
-outputs=devicelib-gfx906.a,deviceLib-gfx908.a
489+
490+
.. _additional-options-archive-unbundling:
491+
492+
Additional Options while Archive Unbundling
493+
-------------------------------------------
494+
495+
**-allow-missing-bundles**
496+
Create an empty archive file if no compatible device binary is found.
497+
498+
**-check-input-archive**
499+
Check if input heterogeneous device archive follows rules for composition
500+
as defined in :ref:`code-object-composition` before creating device-specific
501+
archive(s).
305502

306-
clang-offload-bundler determines whether a device binary is compatible with a
307-
target by comparing bundle ID's. Two bundle ID's are considered compatible if:
308503

309-
* Their offload kind are the same
310-
* Their target triple are the same
311-
* Their GPUArch are the same
504+
**-debug-only=CodeObjectCompatibility**
505+
Verbose printing of matched/unmatched comparisons between bundle entry id of
506+
a device binary from HDA and bundle entry ID of a given target processor
507+
(see :ref:`compatibility-bundle-entry-id`).
312508

313509
Compression and Decompression
314510
=============================

clang/include/clang/Driver/OffloadBundler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class OffloadBundler {
6666
llvm::Error UnbundleArchive();
6767
};
6868

69-
/// Obtain the offload kind, real machine triple, and an optional GPUArch
69+
/// Obtain the offload kind, real machine triple, and an optional TargetID
7070
/// out of the target information specified by the user.
7171
/// Bundle Entry ID (or, Offload Target String) has following components:
7272
/// * Offload Kind - Host, OpenMP, or HIP

0 commit comments

Comments
 (0)