11 This tutorial will guide you through the process of making a change to
12 LLVM, and contributing it back to the LLVM project.
15 The code changes presented here are only an example and not something you
16 should actually submit to the LLVM project. For your first real change to LLVM,
17 the code will be different but the rest of the guide will still apply.
19 We'll be making a change to Clang, but the steps for other parts of LLVM are the same.
20 Even though the change we'll be making is simple, we're going to cover
21 steps like building LLVM, running the tests, and code review. This is
22 good practice, and you'll be prepared for making larger changes.
26 - know how to use an editor,
28 - have basic C++ knowledge,
30 - know how to install software on your system,
32 - are comfortable with the command line,
34 - have basic knowledge of git.
37 The change we're making
38 -----------------------
40 Clang has a warning for infinite recursion:
44 $ echo "void foo() { foo(); }" > ~/test.cc
45 $ clang -c -Wall ~/test.cc
46 test.cc:1:12: warning: all paths through this function will call itself [-Winfinite-recursion]
48 This is clear enough, but not exactly catchy. Let's improve the wording
53 test.cc:1:12: warning: to understand recursion, you must first understand recursion [-Winfinite-recursion]
59 We're going to need some tools:
61 - git: to check out the LLVM source code,
63 - a C++ compiler: to compile LLVM source code. You'll want `a recent
64 version <host_cpp_toolchain>` of Clang, GCC, or Visual Studio.
66 - CMake: used to configure how LLVM should be built on your system,
68 - ninja: runs the C++ compiler to (re)build specific parts of LLVM,
70 - python: to run the LLVM tests.
72 As an example, on Ubuntu:
76 $ sudo apt-get install git clang cmake ninja-build python
86 The source code is stored `on
87 Github <https://github.com/llvm/llvm-project>`__ in one large repository
90 It may take a while to download!
94 $ git clone https://github.com/llvm/llvm-project.git
96 This will create a directory "llvm-project" with all of the source
97 code. (Checking out anonymously is OK - pushing commits uses a different
98 mechanism, as we'll see later.)
100 Configure your workspace
101 ------------------------
103 Before we can build the code, we must configure exactly how to build it
104 by running CMake. CMake combines information from three sources:
106 - explicit choices you make (is this a debug build?)
108 - settings detected from your system (where are libraries installed?)
110 - project structure (which files are part of 'clang'?)
112 First, create a directory to build in. Usually, this is ``llvm-project/build``.
116 $ mkdir llvm-project/build
117 $ cd llvm-project/build
123 $ cmake -G Ninja ../llvm -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=clang
125 If all goes well, you'll see a lot of "performing test" lines, and
132 Build files have been written to: /path/llvm-project/build
134 And you should see a ``build.ninja`` file in the current directory.
136 Let's break down that last command a little:
138 - **-G Ninja**: Tells CMake that we're going to use ninja to build, and to create
139 the ``build.ninja`` file.
141 - **../llvm**: this is the path to the source of the "main" LLVM
144 - The two **-D** flags set CMake variables, which override
145 CMake/project defaults:
147 - **CMAKE_BUILD_TYPE=Release**: build in optimized mode, which is
148 (surprisingly) the fastest option.
150 If you want to run under a debugger, you should use the default Debug
151 (which is totally unoptimized, and will lead to >10x slower test
152 runs) or RelWithDebInfo which is a halfway point.
154 Assertions are not enabled in ``Release`` builds by default.
155 You can enable them using ``LLVM_ENABLE_ASSERTIONS=ON``.
157 - **LLVM_ENABLE_PROJECTS=clang**: this lists the LLVM subprojects
158 you are interested in building, in addition to LLVM itself. Multiple
159 projects can be listed, separated by semicolons, such as ``clang;lldb``.
160 In this example, we'll be making a change to Clang, so we only add clang.
162 Finally, create a symlink (or copy) of ``llvm-project/build/compile-commands.json``
163 into ``llvm-project/``:
167 $ ln -s build/compile_commands.json ../
169 (This isn't strictly necessary for building and testing, but allows
170 tools like clang-tidy, clang-query, and clangd to work in your source
177 Finally, we can build the code! It's important to do this first, to
178 ensure we're in a good state before making changes. But what to build?
179 In ninja, you specify a **target**. If we just want to build the clang
180 binary, our target name is "clang" and we run:
186 The first time we build will be very slow - Clang + LLVM is a lot of
187 code. But incremental builds are fast: ninja will only rebuild the parts
188 that have changed. When it finally finishes you should have a working
189 clang binary. Try running:
193 $ bin/clang --version
195 There's also a target for building and running all the clang tests:
201 This is a common pattern in LLVM: check-llvm is all the checks for the core of
202 LLVM, other projects have targets like ``check-lldb``, ``check-flang`` and so on.
212 We need to find the file containing the error message.
216 $ git grep "all paths through this function" ..
217 ../clang/include/clang/Basic/DiagnosticSemaKinds.td: "all paths through this function will call itself">,
219 The string that appears in ``DiagnosticSemaKinds.td`` is the one that is
220 printed by Clang. ``*.td`` files define tables - in this case it's a list
221 of warnings and errors clang can emit and their messages. Let's update
222 the message in your favorite editor:
226 $ vi ../clang/include/clang/Basic/DiagnosticSemaKinds.td
228 Find the message (it should be under ``warn_infinite_recursive_function``).
229 Change the message to "in order to understand recursion, you must first understand recursion".
235 To verify our change, we can build clang and manually check that it
241 $ bin/clang -c -Wall ~/test.cc
242 test.cc:1:12: warning: in order to understand recursion, you must first understand recursion [-Winfinite-recursion]
244 We should also run the tests to make sure we didn't break something.
250 Notice that it is much faster to build this time, but the tests take
251 just as long to run. Ninja doesn't know which tests might be affected,
258 Clang :: SemaCXX/warn-infinite-recursion.cpp
260 Well, that makes senseā¦ and the test output suggests it's looking for
261 the old string "call itself" and finding our new message instead.
262 Note that more tests may fail in a similar way as new tests are added
265 Let's fix it by updating the expectation in the test.
269 $ vi ../clang/test/SemaCXX/warn-infinite-recursion.cpp
271 Everywhere we see ``// expected-warning{{call itself}}`` (or something similar
272 from the original warning text), let's replace it with
273 ``// expected-warning{{to understand recursion}}``.
275 Now we could run **all** the tests again, but this is a slow way to
276 iterate on a change! Instead, let's find a way to re-run just the
277 specific test. There are two main types of tests in LLVM:
279 - **lit tests** (e.g. ``SemaCXX/warn-infinite-recursion.cpp``).
281 These are fancy shell scripts that run command-line tools and verify the
282 output. They live in files like
283 ``clang/**test**/FixIt/dereference-addressof.c``. Re-run like this:
287 $ bin/llvm-lit -v ../clang/test/SemaCXX/warn-infinite-recursion.cpp
289 - **unit tests** (e.g. ``ToolingTests/ReplacementTest.CanDeleteAllText``)
291 These are C++ programs that call LLVM functions and verify the results.
292 They live in suites like ToolingTests. Re-run like this:
296 $ ninja ToolingTests && tools/clang/unittests/Tooling/ToolingTests --gtest_filter=ReplacementTest.CanDeleteAllText
302 We'll save the change to a local git branch. This lets us work on other
303 things while the change is being reviewed. Changes should have a
304 title and description, to explain to reviewers and future readers of the code why
307 For now, we'll only add a title.
311 $ git checkout -b myfirstpatch
312 $ git commit -am "[clang][Diagnostic] Clarify -Winfinite-recursion message"
314 Now we're ready to send this change out into the world!
316 The ``[clang]`` and ``[Diagnostic]`` prefixes are what we call tags. This loose convention
317 tells readers of the git log what areas a change is modifying. If you don't know
318 the tags for the modules you've changed, you can look at the commit history
319 for those areas of the repository.
323 $ git log --oneline ../clang/
325 Or using GitHub, for example https://github.com/llvm/llvm-project/commits/main/clang.
327 Tagging is imprecise, so don't worry if you are not sure what to put. Reviewers
328 will suggest some if they think they are needed.
333 Uploading a change for review
334 -----------------------------
336 LLVM code reviews happen through pull-request on GitHub, see the
337 :ref:`GitHub <github-reviews>` documentation for how to open
338 a pull-request on GitHub.
343 Changes can be reviewed by anyone in the LLVM community. For larger and more
344 complicated changes, it's important that the
345 reviewer has experience with the area of LLVM and knows the design goals
346 well. The author of a change will often assign a specific reviewer. ``git blame``
347 and ``git log`` can be useful to find previous authors who can review.
349 Our GitHub bot will also tag and notify various "teams" around LLVM. The
350 team members contribute and review code for those specific areas regularly,
351 so one of them will review your change if you don't pick anyone specific.
356 When you open a pull-request, some automation will add a comment and
357 notify different members of the sub-projects depending on the parts you have
360 Within a few days, someone should start the review. They may add
361 themselves as a reviewer, or simply start leaving comments. You'll get
362 another email any time the review is updated. For more detail see the
363 :ref:`Code Review Poilicy <code_review_policy>`.
368 The reviewer can leave comments on the change, and you can reply. Some
369 comments are attached to specific lines, and appear interleaved with the
370 code. You can reply to these. Perhaps to clarify what was asked or to tell the
371 reviewer that you have done what was asked.
376 If you make changes in response to a reviewer's comments, simply update
377 your branch with more commits and push to your GitHub fork of ``llvm-project``.
378 It is best if you answer comments from the reviewer directly instead of expecting
379 them to read through all the changes again.
381 For example you might comment "I have done this." or "I was able to this part
382 but have a question about...".
387 In order to make LLVM a long-term sustainable effort, code needs to be
388 maintainable and well tested. Code reviews help to achieve that goal.
389 Especially for new contributors, that often means many rounds of reviews
390 and push-back on design decisions that do not fit well within the
391 overall architecture of the project.
393 For your first patches, this means:
395 - be kind, and expect reviewers to be kind in return - LLVM has a
396 :ref:`Code of Conduct <LLVM Community Code of Conduct>`
397 that everyone should be following;
399 - be patient - understanding how a new feature fits into the
400 architecture of the project is often a time consuming effort, and
401 people have to juggle this with other responsibilities in their
402 lives; **ping the review once a week** when there is no response;
404 - if you can't agree, generally the best way is to do what the reviewer
405 asks; we optimize for readability of the code, which the reviewer is
406 in a better position to judge; if this feels like it's not the right
407 option, you can ask them in a comment or add another reviewer to get a second
411 Accepting a pull request
412 ------------------------
414 When the reviewer is happy with the change, they will **Approve** the
415 pull request. They may leave some more minor comments that you should
416 address before it is merged, but at this point the review is complete.
417 It's time to get it merged!
426 As this is your first change, you won't have access to merge it
427 yourself yet. The reviewer **does not know this**, so you need to tell
428 them! Leave a comment on the review like:
430 Thanks @<username of reviewer>. I don't have commit access, can you merge this
433 The pull-request will be closed and you will be notified by GitHub.
435 Getting commit access
436 ---------------------
438 Once you've contributed a handful of patches to LLVM, start to think
439 about getting commit access yourself. It's probably a good idea if:
441 - you've landed 3-5 patches of larger scope than "fix a typo"
443 - you'd be willing to review changes that are closely related to yours
445 - you'd like to keep contributing to LLVM.
448 The process is described in the :ref:`developer policy document <obtaining_commit_access>`.
453 Actually, this would be a great time to read the rest of the :ref:`developer
454 policy <developer_policy>` too.
457 .. _MyFirstTypoFix Issues After Landing Your PR:
459 Issues After Landing Your PR
460 ============================
462 Once your change is submitted it will be picked up by automated build
463 bots that will build and test your patch in a variety of configurations.
465 The "console" view at http://lab.llvm.org/buildbot/#/console displays results
466 for specific commits. If you want to follow how your change is affecting the
467 build bots, this should be the first place to look.
469 The columns are build configurations and the rows are individual commits. Along
470 the rows are colored bubbles. The color of the bubble represents the build's
471 status. Green is passing, red has failed and yellow is a build in progress.
473 A red build may have already been failing before your change was committed. This
474 means you didn't break the build but you should check that you did not make it
475 any worse by adding new problems.
478 Only recent changes are shown in the console view. If your change is not
479 there, rely on PR comments and build bot emails to notify you of any problems.
481 If there is a problem in a build that includes your changes, you may receive
482 a report by email or as a comment on your PR. Please check whether the problem
483 has been caused by your changes specifically. As builds contain changes from
484 many authors and sometimes fail due to unrelated infrastructure problems.
486 To see the details of a build, click the bubble in the console view, or the link
487 provided in the problem report. You will be able to view and download logs for
488 each stage of that build.
490 If you need help understanding the problem, or have any other questions, you can
491 ask them as a comment on your PR, or on `Discord <https://discord.com/invite/xS7Z362>`__.
493 If you do not receive any reports of problems, no action is required from you.
494 Your changes are working as expected, well done!
499 If your change has caused a problem, it should be reverted as soon as possible.
500 This is a normal part of :ref:`LLVM development <revert_policy>`,
501 that every committer (no matter how experienced) goes through.
503 If you are in any doubt whether your change can be fixed quickly, revert it.
504 Then you have plenty of time to investigate and produce a solid fix.
506 Someone else may revert your change for you, or you can create a revert pull request using
507 the `GitHub interface <https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/reverting-a-pull-request>`__.
508 Add your original reviewers to this new pull request if possible.
513 Now you should have an understanding of the life cycle of a contribution to the
516 If some details are still unclear, do not worry. The LLVM Project's process does
517 differ from what you may be used to elsewhere on GitHub. Within the project
518 the expectations of different sub-projects may vary too.
520 So whatever you are contributing to, know that we are not expecting perfection.
521 Please ask questions whenever you are unsure, and expect that if you have missed
522 something, someone will politely point it out and help you address it.