1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>How To Build Clang and LLVM with Profile-Guided Optimizations — LLVM 13 documentation</title>
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/llvm-theme.css" type="text/css" />
<script id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="How to build Windows Itanium applications." href="HowToBuildWindowsItaniumPrograms.html" />
<link rel="prev" title="How To Build On ARM" href="HowToBuildOnARM.html" />
<style type="text/css">
table.right { float: right; margin-left: 20px; }
table.right td { border: 1px solid #ccc; }
</style>
</head><body>
<div class="logo">
<a href="index.html">
<img src="_static/logo.png"
alt="LLVM Logo" width="250" height="88"/></a>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="HowToBuildWindowsItaniumPrograms.html" title="How to build Windows Itanium applications."
accesskey="N">next</a> |</li>
<li class="right" >
<a href="HowToBuildOnARM.html" title="How To Build On ARM"
accesskey="P">previous</a> |</li>
<li><a href="https://llvm.org/">LLVM Home</a> | </li>
<li><a href="index.html">Documentation</a>»</li>
<li class="nav-item nav-item-1"><a href="UserGuides.html" accesskey="U">User Guides</a> »</li>
<li class="nav-item nav-item-this"><a href="">How To Build Clang and LLVM with Profile-Guided Optimizations</a></li>
</ul>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3>Documentation</h3>
<ul class="want-points">
<li><a href="https://llvm.org/docs/GettingStartedTutorials.html">Getting Started/Tutorials</a></li>
<li><a href="https://llvm.org/docs/UserGuides.html">User Guides</a></li>
<li><a href="https://llvm.org/docs/Reference.html">Reference</a></li>
</ul>
<h3>Getting Involved</h3>
<ul class="want-points">
<li><a href="https://llvm.org/docs/Contributing.html">Contributing to LLVM</a></li>
<li><a href="https://llvm.org/docs/HowToSubmitABug.html">Submitting Bug Reports</a></li>
<li><a href="https://llvm.org/docs/GettingInvolved.html#mailing-lists">Mailing Lists</a></li>
<li><a href="https://llvm.org/docs/GettingInvolved.html#irc">IRC</a></li>
<li><a href="https://llvm.org/docs/GettingInvolved.html#meetups-and-social-events">Meetups and Social Events</a></li>
</ul>
<h3>Additional Links</h3>
<ul class="want-points">
<li><a href="https://llvm.org/docs/FAQ.html">FAQ</a></li>
<li><a href="https://llvm.org/docs/Lexicon.html">Glossary</a></li>
<li><a href="https://llvm.org/pubs">Publications</a></li>
<li><a href="https://github.com/llvm/llvm-project//">Github Repository</a></li>
</ul>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/HowToBuildWithPGO.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" />
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="how-to-build-clang-and-llvm-with-profile-guided-optimizations">
<h1>How To Build Clang and LLVM with Profile-Guided Optimizations<a class="headerlink" href="#how-to-build-clang-and-llvm-with-profile-guided-optimizations" title="Permalink to this headline">¶</a></h1>
<div class="section" id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>PGO (Profile-Guided Optimization) allows your compiler to better optimize code
for how it actually runs. Users report that applying this to Clang and LLVM can
decrease overall compile time by 20%.</p>
<p>This guide walks you through how to build Clang with PGO, though it also applies
to other subprojects, such as LLD.</p>
<p>If you want to build other software with PGO, see the <a class="reference external" href="https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization">end-user documentation
for PGO</a>.</p>
</div>
<div class="section" id="using-preconfigured-cmake-caches">
<h2>Using preconfigured CMake caches<a class="headerlink" href="#using-preconfigured-cmake-caches" title="Permalink to this headline">¶</a></h2>
<p>See <a class="reference external" href="https://llvm.org/docs/AdvancedBuilds.html#multi-stage-pgo">https://llvm.org/docs/AdvancedBuilds.html#multi-stage-pgo</a></p>
</div>
<div class="section" id="using-the-script">
<h2>Using the script<a class="headerlink" href="#using-the-script" title="Permalink to this headline">¶</a></h2>
<p>We have a script at <code class="docutils literal notranslate"><span class="pre">utils/collect_and_build_with_pgo.py</span></code>. This script is
tested on a few Linux flavors, and requires a checkout of LLVM, Clang, and
compiler-rt. Despite the name, it performs four clean builds of Clang, so it
can take a while to run to completion. Please see the script’s <code class="docutils literal notranslate"><span class="pre">--help</span></code> for
more information on how to run it, and the different options available to you.
If you want to get the most out of PGO for a particular use-case (e.g. compiling
a specific large piece of software), please do read the section below on
‘benchmark’ selection.</p>
<p>Please note that this script is only tested on a few Linux distros. Patches to
add support for other platforms, as always, are highly appreciated. :)</p>
<p>This script also supports a <code class="docutils literal notranslate"><span class="pre">--dry-run</span></code> option, which causes it to print
important commands instead of running them.</p>
</div>
<div class="section" id="selecting-benchmarks">
<h2>Selecting ‘benchmarks’<a class="headerlink" href="#selecting-benchmarks" title="Permalink to this headline">¶</a></h2>
<p>PGO does best when the profiles gathered represent how the user plans to use the
compiler. Notably, highly accurate profiles of llc building x86_64 code aren’t
incredibly helpful if you’re going to be targeting ARM.</p>
<p>By default, the script above does two things to get solid coverage. It:</p>
<ul class="simple">
<li><p>runs all of Clang and LLVM’s lit tests, and</p></li>
<li><p>uses the instrumented Clang to build Clang, LLVM, and all of the other
LLVM subprojects available to it.</p></li>
</ul>
<p>Together, these should give you:</p>
<ul class="simple">
<li><p>solid coverage of building C++,</p></li>
<li><p>good coverage of building C,</p></li>
<li><p>great coverage of running optimizations,</p></li>
<li><p>great coverage of the backend for your host’s architecture, and</p></li>
<li><p>some coverage of other architectures (if other arches are supported backends).</p></li>
</ul>
<p>Altogether, this should cover a diverse set of uses for Clang and LLVM. If you
have very specific needs (e.g. your compiler is meant to compile a large browser
for four different platforms, or similar), you may want to do something else.
This is configurable in the script itself.</p>
</div>
<div class="section" id="building-clang-with-pgo">
<h2>Building Clang with PGO<a class="headerlink" href="#building-clang-with-pgo" title="Permalink to this headline">¶</a></h2>
<p>If you prefer to not use the script or the cmake cache, this briefly goes over
how to build Clang/LLVM with PGO.</p>
<p>First, you should have at least LLVM, Clang, and compiler-rt checked out
locally.</p>
<p>Next, at a high level, you’re going to need to do the following:</p>
<ol class="arabic simple">
<li><p>Build a standard Release Clang and the relevant libclang_rt.profile library</p></li>
<li><p>Build Clang using the Clang you built above, but with instrumentation</p></li>
<li><p>Use the instrumented Clang to generate profiles, which consists of two steps:</p></li>
</ol>
<blockquote>
<div><ul class="simple">
<li><p>Running the instrumented Clang/LLVM/lld/etc. on tasks that represent how
users will use said tools.</p></li>
<li><p>Using a tool to convert the “raw” profiles generated above into a single,
final PGO profile.</p></li>
</ul>
</div></blockquote>
<ol class="arabic simple" start="4">
<li><p>Build a final release Clang (along with whatever other binaries you need)
using the profile collected from your benchmark</p></li>
</ol>
<p>In more detailed steps:</p>
<ol class="arabic simple">
<li><p>Configure a Clang build as you normally would. It’s highly recommended that
you use the Release configuration for this, since it will be used to build
another Clang. Because you need Clang and supporting libraries, you’ll want
to build the <code class="docutils literal notranslate"><span class="pre">all</span></code> target (e.g. <code class="docutils literal notranslate"><span class="pre">ninja</span> <span class="pre">all</span></code> or <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">-j4</span> <span class="pre">all</span></code>).</p></li>
<li><p>Configure a Clang build as above, but add the following CMake args:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">-DLLVM_BUILD_INSTRUMENTED=IR</span></code> – This causes us to build everything
with instrumentation.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-DLLVM_BUILD_RUNTIME=No</span></code> – A few projects have bad interactions when
built with profiling, and aren’t necessary to build. This flag turns them
off.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-DCMAKE_C_COMPILER=/path/to/stage1/clang</span></code> - Use the Clang we built in
step 1.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-DCMAKE_CXX_COMPILER=/path/to/stage1/clang++</span></code> - Same as above.</p></li>
</ul>
</li>
</ol>
<blockquote>
<div><p>In this build directory, you simply need to build the <code class="docutils literal notranslate"><span class="pre">clang</span></code> target (and
whatever supporting tooling your benchmark requires).</p>
</div></blockquote>
<ol class="arabic" start="3">
<li><p>As mentioned above, this has two steps: gathering profile data, and then
massaging it into a useful form:</p>
<ol class="loweralpha">
<li><p>Build your benchmark using the Clang generated in step 2. The ‘standard’
benchmark recommended is to run <code class="docutils literal notranslate"><span class="pre">check-clang</span></code> and <code class="docutils literal notranslate"><span class="pre">check-llvm</span></code> in your
instrumented Clang’s build directory, and to do a full build of Clang/LLVM
using your instrumented Clang. So, create yet another build directory,
with the following CMake arguments:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">-DCMAKE_C_COMPILER=/path/to/stage2/clang</span></code> - Use the Clang we built in
step 2.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-DCMAKE_CXX_COMPILER=/path/to/stage2/clang++</span></code> - Same as above.</p></li>
</ul>
<p>If your users are fans of debug info, you may want to consider using
<code class="docutils literal notranslate"><span class="pre">-DCMAKE_BUILD_TYPE=RelWithDebInfo</span></code> instead of
<code class="docutils literal notranslate"><span class="pre">-DCMAKE_BUILD_TYPE=Release</span></code>. This will grant better coverage of
debug info pieces of clang, but will take longer to complete and will
result in a much larger build directory.</p>
<p>It’s recommended to build the <code class="docutils literal notranslate"><span class="pre">all</span></code> target with your instrumented Clang,
since more coverage is often better.</p>
</li>
</ol>
</li>
</ol>
<blockquote>
<div><ol class="loweralpha simple" start="2">
<li><p>You should now have a few <code class="docutils literal notranslate"><span class="pre">*.profraw</span></code> files in
<code class="docutils literal notranslate"><span class="pre">path/to/stage2/profiles/</span></code>. You need to merge these using
<code class="docutils literal notranslate"><span class="pre">llvm-profdata</span></code> (even if you only have one! The profile merge transforms
profraw into actual profile data, as well). This can be done with
<code class="docutils literal notranslate"><span class="pre">/path/to/stage1/llvm-profdata</span> <span class="pre">merge</span>
<span class="pre">-output=/path/to/output/profdata.prof</span> <span class="pre">path/to/stage2/profiles/*.profraw</span></code>.</p></li>
</ol>
</div></blockquote>
<ol class="arabic" start="4">
<li><p>Now, build your final, PGO-optimized Clang. To do this, you’ll want to pass
the following additional arguments to CMake.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">-DLLVM_PROFDATA_FILE=/path/to/output/profdata.prof</span></code> - Use the PGO
profile from the previous step.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-DCMAKE_C_COMPILER=/path/to/stage1/clang</span></code> - Use the Clang we built in
step 1.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">-DCMAKE_CXX_COMPILER=/path/to/stage1/clang++</span></code> - Same as above.</p></li>
</ul>
<p>From here, you can build whatever targets you need.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>You may see warnings about a mismatched profile in the build output. These
are generally harmless. To silence them, you can add
<code class="docutils literal notranslate"><span class="pre">-DCMAKE_C_FLAGS='-Wno-backend-plugin'</span>
<span class="pre">-DCMAKE_CXX_FLAGS='-Wno-backend-plugin'</span></code> to your CMake invocation.</p>
</div>
</li>
</ol>
<p>Congrats! You now have a Clang built with profile-guided optimizations, and you
can delete all but the final build directory if you’d like.</p>
<p>If this worked well for you and you plan on doing it often, there’s a slight
optimization that can be made: LLVM and Clang have a tool called tblgen that’s
built and run during the build process. While it’s potentially nice to build
this for coverage as part of step 3, none of your other builds should benefit
from building it. You can pass the CMake options
<code class="docutils literal notranslate"><span class="pre">-DCLANG_TABLEGEN=/path/to/stage1/bin/clang-tblgen</span>
<span class="pre">-DLLVM_TABLEGEN=/path/to/stage1/bin/llvm-tblgen</span></code> to steps 2 and onward to avoid
these useless rebuilds.</p>
</div>
</div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="HowToBuildWindowsItaniumPrograms.html" title="How to build Windows Itanium applications."
>next</a> |</li>
<li class="right" >
<a href="HowToBuildOnARM.html" title="How To Build On ARM"
>previous</a> |</li>
<li><a href="https://llvm.org/">LLVM Home</a> | </li>
<li><a href="index.html">Documentation</a>»</li>
<li class="nav-item nav-item-1"><a href="UserGuides.html" >User Guides</a> »</li>
<li class="nav-item nav-item-this"><a href="">How To Build Clang and LLVM with Profile-Guided Optimizations</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2003-2021, LLVM Project.
Last updated on 2021-09-18.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.5.4.
</div>
</body>
</html>
|