Debian Codegraph @

clang/docs/ClangLinkerWrapper.rst

raw
1 ====================
2 Clang Linker Wrapper
3 ====================
4
5 .. contents::
6 :local:
7
8 .. _clang-linker-wrapper:
9
10 Introduction
11 ============
12
13 This tool works as a wrapper of the normal host linking job. This tool is used
14 to create linked device images for offloading and the necessary runtime calls to
15 register them. It works by first scanning the linker's input for embedded device
16 offloading data stored at the ``.llvm.offloading`` section. This section
17 contains binary data created by the :doc:`ClangOffloadPackager`. The extracted
18 device files will then be linked. The linked modules will then be wrapped into a
19 new object file containing the code necessary to register it with the offloading
20 runtime.
21
22 Usage
23 =====
24
25 This tool can be used with the following options. Any arguments not intended
26 only for the linker wrapper will be forwarded to the wrapped linker job.
27
28 .. code-block:: console
29
30 USAGE: clang-linker-wrapper [options] -- <options to passed to the linker>
31
32 OPTIONS:
33 --bitcode-library=<kind>-<triple>-<arch>=<path>
34 Extra bitcode library to link
35 --cuda-path=<dir> Set the system CUDA path
36 --device-debug Use debugging
37 --device-linker=<value> or <triple>=<value>
38 Arguments to pass to the device linker invocation
39 --dry-run Print program arguments without running
40 --embed-bitcode Embed linked bitcode in the module
41 --help-hidden Display all available options
42 --help Display available options (--help-hidden for more)
43 --host-triple=<triple> Triple to use for the host compilation
44 --linker-path=<path> The linker executable to invoke
45 -L <dir> Add <dir> to the library search path
46 -l <libname> Search for library <libname>
47 --opt-level=<O0, O1, O2, or O3>
48 Optimization level for LTO
49 --override-image=<kind=file>
50 Uses the provided file as if it were the output of the device link step
51 -o <path> Path to file to write output
52 --pass-remarks-analysis=<value>
53 Pass remarks for LTO
54 --pass-remarks-missed=<value>
55 Pass remarks for LTO
56 --pass-remarks=<value> Pass remarks for LTO
57 --print-wrapped-module Print the wrapped module's IR for testing
58 --ptxas-arg=<value> Argument to pass to the 'ptxas' invocation
59 --relocatable Link device code to create a relocatable offloading application
60 --save-temps Save intermediate results
61 --sysroot<value> Set the system root
62 --verbose Verbose output from tools
63 --v Display the version number and exit
64 -- The separator for the wrapped linker arguments
65
66 Relocatable Linking
67 ===================
68
69 The ``clang-linker-wrapper`` handles linking embedded device code and then
70 registering it with the appropriate runtime. Normally, this is only done when
71 the executable is created so other files containing device code can be linked
72 together. This can be somewhat problematic for users who wish to ship static
73 libraries that contain offloading code to users without a compatible offloading
74 toolchain.
75
76 When using a relocatable link with ``-r``, the ``clang-linker-wrapper`` will
77 perform the device linking and registration eagerly. This will remove the
78 embedded device code and register it correctly with the runtime. Semantically,
79 this is similar to creating a shared library object. If standard relocatable
80 linking is desired, simply do not run the binaries through the
81 ``clang-linker-wrapper``. This will simply append the embedded device code so
82 that it can be linked later.
83
84 Matching
85 ========
86
87 The linker wrapper will link extracted device code that is compatible with each
88 other. Generally, this requires that the target triple and architecture match.
89 An exception is made when the architecture is listed as ``generic``, which will
90 cause it be linked with any other device code with the same target triple.
91
92 Debugging
93 =========
94
95 The linker wrapper performs a lot of steps internally, such as input matching,
96 symbol resolution, and image registration. This makes it difficult to debug in
97 some scenarios. The behavior of the linker-wrapper is controlled mostly through
98 metadata, described in `clang documentation
99 <https://clang.llvm.org/docs/OffloadingDesign.html>`_. Intermediate output can
100 be obtained from the linker-wrapper using the ``--save-temps`` flag. These files
101 can then be modified.
102
103 .. code-block:: sh
104
105 $> clang openmp.c -fopenmp --offload-arch=gfx90a -c
106 $> clang openmp.o -fopenmp --offload-arch=gfx90a -Wl,--save-temps
107 $> ; Modify temp files.
108 $> llvm-objcopy --update-section=.llvm.offloading=out.bc openmp.o
109
110 Doing this will allow you to override one of the input files by replacing its
111 embedded offloading metadata with a user-modified version. However, this will be
112 more difficult when there are multiple input files. For a very large hammer, the
113 ``--override-image=<kind>=<file>`` flag can be used.
114
115 In the following example, we use the ``--save-temps`` to obtain the LLVM-IR just
116 before running the backend. We then modify it to test altered behavior, and then
117 compile it to a binary. This can then be passed to the linker-wrapper which will
118 then ignore all embedded metadata and use the provided image as if it were the
119 result of the device linking phase.
120
121 .. code-block:: sh
122
123 $> clang openmp.c -fopenmp --offload-arch=gfx90a -Wl,--save-temps
124 $> ; Modify temp files.
125 $> clang --target=amdgcn-amd-amdhsa -mcpu=gfx90a -nogpulib out.bc -o a.out
126 $> clang openmp.c -fopenmp --offload-arch=gfx90a -Wl,--override-image=openmp=a.out
127
128 Example
129 =======
130
131 This tool links object files with offloading images embedded within it using the
132 ``-fembed-offload-object`` flag in Clang. Given an input file containing the
133 magic section we can pass it to this tool to extract the data contained at that
134 section and run a device linking job on it.
135
136 .. code-block:: console
137
138 clang-linker-wrapper --host-triple=x86_64 --linker-path=/usr/bin/ld -- <Args>