1 =================================
2 LLVM Testing Infrastructure Guide
3 =================================
12 TestSuiteMakefileGuide
17 This document is the reference manual for the LLVM testing
18 infrastructure. It documents the structure of the LLVM testing
19 infrastructure, the tools needed to use it, and how to add and run
25 In order to use the LLVM testing infrastructure, you will need all of the
26 software required to build LLVM, as well as `Python <http://python.org>`_ 3.6 or
29 LLVM Testing Infrastructure Organization
30 ========================================
32 The LLVM testing infrastructure contains three major categories of tests:
33 unit tests, regression tests and whole programs. The unit tests and regression
34 tests are contained inside the LLVM repository itself under ``llvm/unittests``
35 and ``llvm/test`` respectively and are expected to always pass -- they should be
36 run before every commit.
38 The whole programs tests are referred to as the "LLVM test suite" (or
39 "test-suite") and are in the ``test-suite``
40 `repository on GitHub <https://github.com/llvm/llvm-test-suite.git>`_.
41 For historical reasons, these tests are also referred to as the "nightly
42 tests" in places, which is less ambiguous than "test-suite" and remains
43 in use although we run them much more often than nightly.
48 Unit tests are written using `Google Test <https://github.com/google/googletest/blob/master/docs/primer.md>`_
49 and `Google Mock <https://github.com/google/googletest/blob/master/docs/gmock_for_dummies.md>`_
50 and are located in the ``llvm/unittests`` directory.
51 In general unit tests are reserved for targeting the support library and other
52 generic data structure, we prefer relying on regression tests for testing
53 transformations and analysis on the IR.
58 The regression tests are small pieces of code that test a specific
59 feature of LLVM or trigger a specific bug in LLVM. The language they are
60 written in depends on the part of LLVM being tested. These tests are driven by
61 the :doc:`Lit <CommandGuide/lit>` testing tool (which is part of LLVM), and
62 are located in the ``llvm/test`` directory.
64 Typically when a bug is found in LLVM, a regression test containing just
65 enough code to reproduce the problem should be written and placed
66 somewhere underneath this directory. For example, it can be a small
67 piece of LLVM IR distilled from an actual application or benchmark.
72 An analysis is a pass that infer properties on some part of the IR and not
73 transforming it. They are tested in general using the same infrastructure as the
74 regression tests, by creating a separate "Printer" pass to consume the analysis
75 result and print it on the standard output in a textual format suitable for
77 See `llvm/test/Analysis/BranchProbabilityInfo/loop.ll <https://github.com/llvm/llvm-project/blob/main/llvm/test/Analysis/BranchProbabilityInfo/loop.ll>`_
78 for an example of such test.
83 The test suite contains whole programs, which are pieces of code which
84 can be compiled and linked into a stand-alone program that can be
85 executed. These programs are generally written in high level languages
88 These programs are compiled using a user specified compiler and set of
89 flags, and then executed to capture the program output and timing
90 information. The output of these programs is compared to a reference
91 output to ensure that the program is being compiled correctly.
93 In addition to compiling and executing programs, whole program tests
94 serve as a way of benchmarking LLVM performance, both in terms of the
95 efficiency of the programs generated as well as the speed with which
96 LLVM compiles, optimizes, and generates code.
98 The test-suite is located in the ``test-suite``
99 `repository on GitHub <https://github.com/llvm/llvm-test-suite.git>`_.
101 See the :doc:`TestSuiteGuide` for details.
103 Debugging Information tests
104 ---------------------------
106 The test suite contains tests to check quality of debugging information.
107 The test are written in C based languages or in LLVM assembly language.
109 These tests are compiled and run under a debugger. The debugger output
110 is checked to validate of debugging information. See README.txt in the
111 test suite for more information. This test suite is located in the
112 ``cross-project-tests/debuginfo-tests`` directory.
117 The tests are located in two separate repositories. The unit and
118 regression tests are in the main "llvm"/ directory under the directories
119 ``llvm/unittests`` and ``llvm/test`` (so you get these tests for free with the
120 main LLVM tree). Use ``make check-all`` to run the unit and regression tests
123 The ``test-suite`` module contains more comprehensive tests including whole C
124 and C++ programs. See the :doc:`TestSuiteGuide` for details.
126 Unit and Regression tests
127 -------------------------
129 To run all of the LLVM unit tests use the check-llvm-unit target:
133 % make check-llvm-unit
135 To run all of the LLVM regression tests use the check-llvm target:
141 In order to get reasonable testing performance, build LLVM and subprojects
142 in release mode, i.e.
146 % cmake -DCMAKE_BUILD_TYPE="Release" -DLLVM_ENABLE_ASSERTIONS=On
148 If you have `Clang <https://clang.llvm.org/>`_ checked out and built, you
149 can run the LLVM and Clang tests simultaneously using:
155 To run the tests with Valgrind (Memcheck by default), use the ``LIT_ARGS`` make
156 variable to pass the required options to lit. For example, you can use:
160 % make check LIT_ARGS="-v --vg --vg-leak"
162 to enable testing with valgrind and with leak checking enabled.
164 To run individual tests or subsets of tests, you can use the ``llvm-lit``
165 script which is built as part of LLVM. For example, to run the
166 ``Integer/BitPacked.ll`` test by itself you can run:
170 % llvm-lit ~/llvm/test/Integer/BitPacked.ll
172 or to run all of the ARM CodeGen tests:
176 % llvm-lit ~/llvm/test/CodeGen/ARM
178 The regression tests will use the Python psutil module only if installed in a
179 **non-user** location. Under Linux, install with sudo or within a virtual
180 environment. Under Windows, install Python for all users and then run
181 ``pip install psutil`` in an elevated command prompt.
183 For more information on using the :program:`lit` tool, see ``llvm-lit --help``
184 or the :doc:`lit man page <CommandGuide/lit>`.
186 Debugging Information tests
187 ---------------------------
189 To run debugging information tests simply add the ``cross-project-tests``
190 project to your ``LLVM_ENABLE_PROJECTS`` define on the cmake
193 Regression test structure
194 =========================
196 The LLVM regression tests are driven by :program:`lit` and are located in the
197 ``llvm/test`` directory.
199 This directory contains a large array of small tests that exercise
200 various features of LLVM and to ensure that regressions do not occur.
201 The directory is broken into several sub-directories, each focused on a
202 particular area of LLVM.
204 Writing new regression tests
205 ----------------------------
207 The regression test structure is very simple, but does require some
208 information to be set. This information is gathered via ``cmake``
209 and is written to a file, ``test/lit.site.cfg`` in the build directory.
210 The ``llvm/test`` Makefile does this work for you.
212 In order for the regression tests to work, each directory of tests must
213 have a ``lit.local.cfg`` file. :program:`lit` looks for this file to determine
214 how to run the tests. This file is just Python code and thus is very
215 flexible, but we've standardized it for the LLVM regression tests. If
216 you're adding a directory of tests, just copy ``lit.local.cfg`` from
217 another directory to get running. The standard ``lit.local.cfg`` simply
218 specifies which files to look in for tests. Any directory that contains
219 only directories does not need the ``lit.local.cfg`` file. Read the :doc:`Lit
220 documentation <CommandGuide/lit>` for more information.
222 Each test file must contain lines starting with "RUN:" that tell :program:`lit`
223 how to run it. If there are no RUN lines, :program:`lit` will issue an error
224 while running a test.
226 RUN lines are specified in the comments of the test program using the
227 keyword ``RUN`` followed by a colon, and lastly the command (pipeline)
228 to execute. Together, these lines form the "script" that :program:`lit`
229 executes to run the test case. The syntax of the RUN lines is similar to a
230 shell's syntax for pipelines including I/O redirection and variable
231 substitution. However, even though these lines may *look* like a shell
232 script, they are not. RUN lines are interpreted by :program:`lit`.
233 Consequently, the syntax differs from shell in a few ways. You can specify
234 as many RUN lines as needed.
236 :program:`lit` performs substitution on each RUN line to replace LLVM tool names
237 with the full paths to the executable built for each tool (in
238 ``$(LLVM_OBJ_ROOT)/$(BuildMode)/bin)``. This ensures that :program:`lit` does
239 not invoke any stray LLVM tools in the user's path during testing.
241 Each RUN line is executed on its own, distinct from other lines unless
242 its last character is ``\``. This continuation character causes the RUN
243 line to be concatenated with the next one. In this way you can build up
244 long pipelines of commands without making huge line lengths. The lines
245 ending in ``\`` are concatenated until a RUN line that doesn't end in
246 ``\`` is found. This concatenated set of RUN lines then constitutes one
247 execution. :program:`lit` will substitute variables and arrange for the pipeline
248 to be executed. If any process in the pipeline fails, the entire line (and
249 test case) fails too.
251 Below is an example of legal RUN lines in a ``.ll`` file:
255 ; RUN: llvm-as < %s | llvm-dis > %t1
256 ; RUN: llvm-dis < %s.bc-13 > %t2
259 As with a Unix shell, the RUN lines permit pipelines and I/O
260 redirection to be used.
262 There are some quoting rules that you must pay attention to when writing
263 your RUN lines. In general nothing needs to be quoted. :program:`lit` won't
264 strip off any quote characters so they will get passed to the invoked program.
265 To avoid this use curly braces to tell :program:`lit` that it should treat
266 everything enclosed as one value.
268 In general, you should strive to keep your RUN lines as simple as possible,
269 using them only to run tools that generate textual output you can then examine.
270 The recommended way to examine output to figure out if the test passes is using
271 the :doc:`FileCheck tool <CommandGuide/FileCheck>`. *[The usage of grep in RUN
272 lines is deprecated - please do not send or commit patches that use it.]*
274 Put related tests into a single file rather than having a separate file per
275 test. Check if there are files already covering your feature and consider
276 adding your code there instead of creating a new file.
278 Generating assertions in regression tests
279 -----------------------------------------
281 Some regression test cases are very large and complex to write/update by hand.
282 In that case to reduce the human work we can use the scripts available in
283 llvm/utils/ to generate the assertions.
285 For example to generate assertions in an :program:`llc`-based test, run:
289 % llvm/utils/update_llc_test_checks.py --llc-binary build/bin/llc test.ll
291 And if you want to update assertions in an existing test case, pass `-u` option
292 which first check the ``NOTE:`` line exists and matches the script name.
294 These are the most common scripts and their purposes/applications in generating
299 update_analyze_test_checks.py
300 opt -passes='print<cost-model>'
302 update_cc_test_checks.py
303 C/C++, or clang/clang++ (IR checks)
305 update_llc_test_checks.py
306 llc (assembly checks)
308 update_mca_test_checks.py
311 update_mir_test_checks.py
314 update_test_checks.py
320 If your test requires extra files besides the file containing the ``RUN:`` lines
321 and the extra files are small, consider specifying them in the same file and
322 using ``split-file`` to extract them. For example,
326 ; RUN: split-file %s %t
327 ; RUN: llvm-link -S %t/a.ll %t/b.ll | FileCheck %s
336 The parts are separated by the regex ``^(.|//)--- <part>``.
338 If you want to test relative line numbers like ``[[#@LINE+1]]``, specify
339 ``--leading-lines`` to add leading empty lines to preserve line numbers.
341 If the extra files are large, the idiomatic place to put them is in a subdirectory ``Inputs``.
342 You can then refer to the extra files as ``%S/Inputs/foo.bar``.
344 For example, consider ``test/Linker/ident.ll``. The directory structure is
354 For convenience, these are the contents:
360 ; RUN: llvm-link %S/Inputs/ident.a.ll %S/Inputs/ident.b.ll -S | FileCheck %s
362 ; Verify that multiple input llvm.ident metadata are linked together.
364 ; CHECK-DAG: !llvm.ident = !{!0, !1, !2}
365 ; CHECK-DAG: "Compiler V1"
366 ; CHECK-DAG: "Compiler V2"
367 ; CHECK-DAG: "Compiler V3"
369 ;;;;; Inputs/ident.a.ll:
371 !llvm.ident = !{!0, !1}
372 !0 = metadata !{metadata !"Compiler V1"}
373 !1 = metadata !{metadata !"Compiler V2"}
375 ;;;;; Inputs/ident.b.ll:
378 !0 = metadata !{metadata !"Compiler V3"}
380 For symmetry reasons, ``ident.ll`` is just a dummy file that doesn't
381 actually participate in the test besides holding the ``RUN:`` lines.
385 Some existing tests use ``RUN: true`` in extra files instead of just
386 putting the extra files in an ``Inputs/`` directory. This pattern is
392 It is easy to write a fragile test that would fail spuriously if the tool being
393 tested outputs a full path to the input file. For example, :program:`opt` by
394 default outputs a ``ModuleID``:
396 .. code-block:: console
399 define i32 @main() nounwind {
403 $ opt -S /path/to/example.ll
404 ; ModuleID = '/path/to/example.ll'
406 define i32 @main() nounwind {
410 ``ModuleID`` can unexpectedly match against ``CHECK`` lines. For example:
414 ; RUN: opt -S %s | FileCheck
416 define i32 @main() nounwind {
421 This test will fail if placed into a ``download`` directory.
423 To make your tests robust, always use ``opt ... < %s`` in the RUN line.
424 :program:`opt` does not output a ``ModuleID`` when input comes from stdin.
426 Platform-Specific Tests
427 -----------------------
429 Whenever adding tests that require the knowledge of a specific platform,
430 either related to code generated, specific output or back-end features,
431 you must make sure to isolate the features, so that buildbots that
432 run on different architectures (and don't even compile all back-ends),
435 The first problem is to check for target-specific output, for example sizes
436 of structures, paths and architecture names, for example:
438 * Tests containing Windows paths will fail on Linux and vice-versa.
439 * Tests that check for ``x86_64`` somewhere in the text will fail anywhere else.
440 * Tests where the debug information calculates the size of types and structures.
442 Also, if the test rely on any behaviour that is coded in any back-end, it must
443 go in its own directory. So, for instance, code generator tests for ARM go
444 into ``test/CodeGen/ARM`` and so on. Those directories contain a special
445 ``lit`` configuration file that ensure all tests in that directory will
446 only run if a specific back-end is compiled and available.
448 For instance, on ``test/CodeGen/ARM``, the ``lit.local.cfg`` is:
450 .. code-block:: python
452 config.suffixes = ['.ll', '.c', '.cpp', '.test']
453 if not 'ARM' in config.root.targets:
454 config.unsupported = True
456 Other platform-specific tests are those that depend on a specific feature
457 of a specific sub-architecture, for example only to Intel chips that support ``AVX2``.
459 For instance, ``test/CodeGen/X86/psubus.ll`` tests three sub-architecture
464 ; RUN: llc -mcpu=core2 < %s | FileCheck %s -check-prefix=SSE2
465 ; RUN: llc -mcpu=corei7-avx < %s | FileCheck %s -check-prefix=AVX1
466 ; RUN: llc -mcpu=core-avx2 < %s | FileCheck %s -check-prefix=AVX2
468 And the checks are different:
473 ; SSE2: psubusw LCPI0_0(%rip), %xmm0
475 ; AVX1: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0
477 ; AVX2: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0
479 So, if you're testing for a behaviour that you know is platform-specific or
480 depends on special features of sub-architectures, you must add the specific
481 triple, test with the specific FileCheck and put it into the specific
482 directory that will filter out all other architectures.
485 Constraining test execution
486 ---------------------------
488 Some tests can be run only in specific configurations, such as
489 with debug builds or on particular platforms. Use ``REQUIRES``
490 and ``UNSUPPORTED`` to control when the test is enabled.
492 Some tests are expected to fail. For example, there may be a known bug
493 that the test detect. Use ``XFAIL`` to mark a test as an expected failure.
494 An ``XFAIL`` test will be successful if its execution fails, and
495 will be a failure if its execution succeeds.
499 ; This test will be only enabled in the build with asserts.
501 ; This test is disabled on Linux.
502 ; UNSUPPORTED: -linux-
503 ; This test is expected to fail on PowerPC.
506 ``REQUIRES`` and ``UNSUPPORTED`` and ``XFAIL`` all accept a comma-separated
507 list of boolean expressions. The values in each expression may be:
509 - Features added to ``config.available_features`` by configuration files such as ``lit.cfg``.
510 String comparison of features is case-sensitive. Furthermore, a boolean expression can
511 contain any Python regular expression enclosed in ``{{ }}``, in which case the boolean
512 expression is satisfied if any feature matches the regular expression. Regular
513 expressions can appear inside an identifier, so for example ``he{{l+}}o`` would match
514 ``helo``, ``hello``, ``helllo``, and so on.
515 - Substrings of the target triple (``UNSUPPORTED`` and ``XFAIL`` only).
517 | ``REQUIRES`` enables the test if all expressions are true.
518 | ``UNSUPPORTED`` disables the test if any expression is true.
519 | ``XFAIL`` expects the test to fail if any expression is true.
521 As a special case, ``XFAIL: *`` is expected to fail everywhere.
525 ; This test is disabled on Windows,
526 ; and is disabled on Linux, except for Android Linux.
527 ; UNSUPPORTED: windows, linux && !android
528 ; This test is expected to fail on both PowerPC and ARM.
529 ; XFAIL: powerpc || arm
535 Besides replacing LLVM tool names the following substitutions are performed in
539 Replaced by a single ``%``. This allows escaping other substitutions.
542 File path to the test case's source. This is suitable for passing on the
543 command line as the input to an LLVM tool.
545 Example: ``/home/user/llvm/test/MC/ELF/foo_test.s``
548 Directory path to the test case's source.
550 Example: ``/home/user/llvm/test/MC/ELF``
553 File path to a temporary file name that could be used for this test case.
554 The file name won't conflict with other test cases. You can append to it
555 if you need multiple temporaries. This is useful as the destination of
556 some redirected output.
558 Example: ``/home/user/llvm.build/test/MC/ELF/Output/foo_test.s.tmp``
561 Directory of ``%t``. Deprecated. Shouldn't be used, because it can be easily
562 misused and cause race conditions between tests.
564 Use ``rm -rf %t && mkdir %t`` instead if a temporary directory is necessary.
566 Example: ``/home/user/llvm.build/test/MC/ELF/Output``
570 Expands to the path separator, i.e. ``:`` (or ``;`` on Windows).
573 Expands to the root component of file system paths for the source directory,
574 i.e. ``/`` on Unix systems or ``C:\`` (or another drive) on Windows.
577 Expands to the root component of file system paths for the test's temporary
578 directory, i.e. ``/`` on Unix systems or ``C:\`` (or another drive) on
582 Expands to the file system separator, i.e. ``/`` or ``\`` on Windows.
584 ``%/s, %/S, %/t, %/T:``
586 Act like the corresponding substitution above but replace any ``\``
587 character with a ``/``. This is useful to normalize path separators.
589 Example: ``%s: C:\Desktop Files/foo_test.s.tmp``
591 Example: ``%/s: C:/Desktop Files/foo_test.s.tmp``
593 ``%:s, %:S, %:t, %:T:``
595 Act like the corresponding substitution above but remove colons at
596 the beginning of Windows paths. This is useful to allow concatenation
597 of absolute paths on Windows to produce a legal path.
599 Example: ``%s: C:\Desktop Files\foo_test.s.tmp``
601 Example: ``%:s: C\Desktop Files\foo_test.s.tmp``
605 Some error messages may be substituted to allow different spellings
606 based on the host platform.
608 The following error codes are currently supported:
609 ENOENT, EISDIR, EINVAL, EACCES.
611 Example: ``Linux %errc_ENOENT: No such file or directory``
613 Example: ``Windows %errc_ENOENT: no such file or directory``
615 ``%if feature %{<if branch>%} %else %{<else branch>%}``
617 Conditional substitution: if ``feature`` is available it expands to
618 ``<if branch>``, otherwise it expands to ``<else branch>``.
619 ``%else %{<else branch>%}`` is optional and treated like ``%else %{%}``
622 **LLVM-specific substitutions:**
625 The suffix for the host platforms shared library files. This includes the
626 period as the first character.
628 Example: ``.so`` (Linux), ``.dylib`` (macOS), ``.dll`` (Windows)
631 The suffix for the host platforms executable files. This includes the
632 period as the first character.
634 Example: ``.exe`` (Windows), empty on Linux.
636 ``%(line)``, ``%(line+<number>)``, ``%(line-<number>)``
637 The number of the line where this substitution is used, with an optional
638 integer offset. This can be used in tests with multiple RUN lines, which
639 reference test file's line numbers.
642 **Clang-specific substitutions:**
645 Invokes the Clang driver.
648 Invokes the Clang driver for C++.
651 Invokes the CL-compatible Clang driver.
654 Invokes the G++-compatible Clang driver.
657 Invokes the Clang frontend.
659 ``%itanium_abi_triple``, ``%ms_abi_triple``
660 These substitutions can be used to get the current target triple adjusted to
661 the desired ABI. For example, if the test suite is running with the
662 ``i686-pc-win32`` target, ``%itanium_abi_triple`` will expand to
663 ``i686-pc-mingw32``. This allows a test to run with a specific ABI without
664 constraining it to a specific triple.
666 **FileCheck-specific substitutions:**
668 ``%ProtectFileCheckOutput``
669 This should precede a ``FileCheck`` call if and only if the call's textual
670 output affects test results. It's usually easy to tell: just look for
671 redirection or piping of the ``FileCheck`` call's stdout or stderr.
673 To add more substitutions, look at ``test/lit.cfg`` or ``lit.local.cfg``.
679 The llvm lit configuration allows to customize some things with user options:
681 ``llc``, ``opt``, ...
682 Substitute the respective llvm tool name with a custom command line. This
683 allows to specify custom paths and default arguments for these tools.
686 % llvm-lit "-Dllc=llc -verify-machineinstrs"
689 Enable the execution of long running tests.
692 Load the specified lit configuration instead of the default one.
698 To make RUN line writing easier, there are several helper programs. These
699 helpers are in the PATH when running tests, so you can just call them using
700 their name. For example:
703 This program runs its arguments and then inverts the result code from it.
704 Zero result codes become 1. Non-zero result codes become 0.
706 To make the output more useful, :program:`lit` will scan
707 the lines of the test case for ones that contain a pattern that matches
708 ``PR[0-9]+``. This is the syntax for specifying a PR (Problem Report) number
709 that is related to the test case. The number after "PR" specifies the
710 LLVM Bugzilla number. When a PR number is specified, it will be used in
711 the pass/fail reporting. This is useful to quickly get some context when
714 Finally, any line that contains "END." will cause the special
715 interpretation of lines to terminate. This is generally done right after
716 the last RUN: line. This has two side effects:
718 (a) it prevents special interpretation of lines that are part of the test
719 program, not the instructions to the test case, and
721 (b) it speeds things up for really big test cases by avoiding
722 interpretation of the remainder of the file.