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`` module in subversion. For
40 historical reasons, these tests are also referred to as the "nightly
41 tests" in places, which is less ambiguous than "test-suite" and remains
42 in use although we run them much more often than nightly.
47 Unit tests are written using `Google Test <https://github.com/google/googletest/blob/master/docs/primer.md>`_
48 and `Google Mock <https://github.com/google/googletest/blob/master/docs/gmock_for_dummies.md>`_
49 and are located in the ``llvm/unittests`` directory.
50 In general unit tests are reserved for targeting the support library and other
51 generic data structure, we prefer relying on regression tests for testing
52 transformations and analysis on the IR.
57 The regression tests are small pieces of code that test a specific
58 feature of LLVM or trigger a specific bug in LLVM. The language they are
59 written in depends on the part of LLVM being tested. These tests are driven by
60 the :doc:`Lit <CommandGuide/lit>` testing tool (which is part of LLVM), and
61 are located in the ``llvm/test`` directory.
63 Typically when a bug is found in LLVM, a regression test containing just
64 enough code to reproduce the problem should be written and placed
65 somewhere underneath this directory. For example, it can be a small
66 piece of LLVM IR distilled from an actual application or benchmark.
71 An analysis is a pass that infer properties on some part of the IR and not
72 transforming it. They are tested in general using the same infrastructure as the
73 regression tests, by creating a separate "Printer" pass to consume the analysis
74 result and print it on the standard output in a textual format suitable for
76 See `llvm/test/Analysis/BranchProbabilityInfo/loop.ll <https://github.com/llvm/llvm-project/blob/main/llvm/test/Analysis/BranchProbabilityInfo/loop.ll>`_
77 for an example of such test.
82 The test suite contains whole programs, which are pieces of code which
83 can be compiled and linked into a stand-alone program that can be
84 executed. These programs are generally written in high level languages
87 These programs are compiled using a user specified compiler and set of
88 flags, and then executed to capture the program output and timing
89 information. The output of these programs is compared to a reference
90 output to ensure that the program is being compiled correctly.
92 In addition to compiling and executing programs, whole program tests
93 serve as a way of benchmarking LLVM performance, both in terms of the
94 efficiency of the programs generated as well as the speed with which
95 LLVM compiles, optimizes, and generates code.
97 The test-suite is located in the ``test-suite`` Subversion module.
99 See the :doc:`TestSuiteGuide` for details.
101 Debugging Information tests
102 ---------------------------
104 The test suite contains tests to check quality of debugging information.
105 The test are written in C based languages or in LLVM assembly language.
107 These tests are compiled and run under a debugger. The debugger output
108 is checked to validate of debugging information. See README.txt in the
109 test suite for more information. This test suite is located in the
110 ``cross-project-tests/debuginfo-tests`` directory.
115 The tests are located in two separate Subversion modules. The unit and
116 regression tests are in the main "llvm" module under the directories
117 ``llvm/unittests`` and ``llvm/test`` (so you get these tests for free with the
118 main LLVM tree). Use ``make check-all`` to run the unit and regression tests
121 The ``test-suite`` module contains more comprehensive tests including whole C
122 and C++ programs. See the :doc:`TestSuiteGuide` for details.
124 Unit and Regression tests
125 -------------------------
127 To run all of the LLVM unit tests use the check-llvm-unit target:
131 % make check-llvm-unit
133 To run all of the LLVM regression tests use the check-llvm target:
139 In order to get reasonable testing performance, build LLVM and subprojects
140 in release mode, i.e.
144 % cmake -DCMAKE_BUILD_TYPE="Release" -DLLVM_ENABLE_ASSERTIONS=On
146 If you have `Clang <https://clang.llvm.org/>`_ checked out and built, you
147 can run the LLVM and Clang tests simultaneously using:
153 To run the tests with Valgrind (Memcheck by default), use the ``LIT_ARGS`` make
154 variable to pass the required options to lit. For example, you can use:
158 % make check LIT_ARGS="-v --vg --vg-leak"
160 to enable testing with valgrind and with leak checking enabled.
162 To run individual tests or subsets of tests, you can use the ``llvm-lit``
163 script which is built as part of LLVM. For example, to run the
164 ``Integer/BitPacked.ll`` test by itself you can run:
168 % llvm-lit ~/llvm/test/Integer/BitPacked.ll
170 or to run all of the ARM CodeGen tests:
174 % llvm-lit ~/llvm/test/CodeGen/ARM
176 The regression tests will use the Python psutil module only if installed in a
177 **non-user** location. Under Linux, install with sudo or within a virtual
178 environment. Under Windows, install Python for all users and then run
179 ``pip install psutil`` in an elevated command prompt.
181 For more information on using the :program:`lit` tool, see ``llvm-lit --help``
182 or the :doc:`lit man page <CommandGuide/lit>`.
184 Debugging Information tests
185 ---------------------------
187 To run debugging information tests simply add the ``debuginfo-tests``
188 project to your ``LLVM_ENABLE_PROJECTS`` define on the cmake
191 Regression test structure
192 =========================
194 The LLVM regression tests are driven by :program:`lit` and are located in the
195 ``llvm/test`` directory.
197 This directory contains a large array of small tests that exercise
198 various features of LLVM and to ensure that regressions do not occur.
199 The directory is broken into several sub-directories, each focused on a
200 particular area of LLVM.
202 Writing new regression tests
203 ----------------------------
205 The regression test structure is very simple, but does require some
206 information to be set. This information is gathered via ``cmake``
207 and is written to a file, ``test/lit.site.cfg`` in the build directory.
208 The ``llvm/test`` Makefile does this work for you.
210 In order for the regression tests to work, each directory of tests must
211 have a ``lit.local.cfg`` file. :program:`lit` looks for this file to determine
212 how to run the tests. This file is just Python code and thus is very
213 flexible, but we've standardized it for the LLVM regression tests. If
214 you're adding a directory of tests, just copy ``lit.local.cfg`` from
215 another directory to get running. The standard ``lit.local.cfg`` simply
216 specifies which files to look in for tests. Any directory that contains
217 only directories does not need the ``lit.local.cfg`` file. Read the :doc:`Lit
218 documentation <CommandGuide/lit>` for more information.
220 Each test file must contain lines starting with "RUN:" that tell :program:`lit`
221 how to run it. If there are no RUN lines, :program:`lit` will issue an error
222 while running a test.
224 RUN lines are specified in the comments of the test program using the
225 keyword ``RUN`` followed by a colon, and lastly the command (pipeline)
226 to execute. Together, these lines form the "script" that :program:`lit`
227 executes to run the test case. The syntax of the RUN lines is similar to a
228 shell's syntax for pipelines including I/O redirection and variable
229 substitution. However, even though these lines may *look* like a shell
230 script, they are not. RUN lines are interpreted by :program:`lit`.
231 Consequently, the syntax differs from shell in a few ways. You can specify
232 as many RUN lines as needed.
234 :program:`lit` performs substitution on each RUN line to replace LLVM tool names
235 with the full paths to the executable built for each tool (in
236 ``$(LLVM_OBJ_ROOT)/$(BuildMode)/bin)``. This ensures that :program:`lit` does
237 not invoke any stray LLVM tools in the user's path during testing.
239 Each RUN line is executed on its own, distinct from other lines unless
240 its last character is ``\``. This continuation character causes the RUN
241 line to be concatenated with the next one. In this way you can build up
242 long pipelines of commands without making huge line lengths. The lines
243 ending in ``\`` are concatenated until a RUN line that doesn't end in
244 ``\`` is found. This concatenated set of RUN lines then constitutes one
245 execution. :program:`lit` will substitute variables and arrange for the pipeline
246 to be executed. If any process in the pipeline fails, the entire line (and
247 test case) fails too.
249 Below is an example of legal RUN lines in a ``.ll`` file:
253 ; RUN: llvm-as < %s | llvm-dis > %t1
254 ; RUN: llvm-dis < %s.bc-13 > %t2
257 As with a Unix shell, the RUN lines permit pipelines and I/O
258 redirection to be used.
260 There are some quoting rules that you must pay attention to when writing
261 your RUN lines. In general nothing needs to be quoted. :program:`lit` won't
262 strip off any quote characters so they will get passed to the invoked program.
263 To avoid this use curly braces to tell :program:`lit` that it should treat
264 everything enclosed as one value.
266 In general, you should strive to keep your RUN lines as simple as possible,
267 using them only to run tools that generate textual output you can then examine.
268 The recommended way to examine output to figure out if the test passes is using
269 the :doc:`FileCheck tool <CommandGuide/FileCheck>`. *[The usage of grep in RUN
270 lines is deprecated - please do not send or commit patches that use it.]*
272 Put related tests into a single file rather than having a separate file per
273 test. Check if there are files already covering your feature and consider
274 adding your code there instead of creating a new file.
279 If your test requires extra files besides the file containing the ``RUN:`` lines
280 and the extra files are small, consider specifying them in the same file and
281 using ``split-file`` to extract them. For example,
285 ; RUN: split-file %s %t
286 ; RUN: llvm-link -S %t/a.ll %t/b.ll | FileCheck %s
295 The parts are separated by the regex ``^(.|//)--- <part>``. By default the
296 extracted content has leading empty lines to preserve line numbers. Specify
297 ``--no-leading-lines`` to drop leading lines.
299 If the extra files are large, the idiomatic place to put them is in a subdirectory ``Inputs``.
300 You can then refer to the extra files as ``%S/Inputs/foo.bar``.
302 For example, consider ``test/Linker/ident.ll``. The directory structure is
312 For convenience, these are the contents:
318 ; RUN: llvm-link %S/Inputs/ident.a.ll %S/Inputs/ident.b.ll -S | FileCheck %s
320 ; Verify that multiple input llvm.ident metadata are linked together.
322 ; CHECK-DAG: !llvm.ident = !{!0, !1, !2}
323 ; CHECK-DAG: "Compiler V1"
324 ; CHECK-DAG: "Compiler V2"
325 ; CHECK-DAG: "Compiler V3"
327 ;;;;; Inputs/ident.a.ll:
329 !llvm.ident = !{!0, !1}
330 !0 = metadata !{metadata !"Compiler V1"}
331 !1 = metadata !{metadata !"Compiler V2"}
333 ;;;;; Inputs/ident.b.ll:
336 !0 = metadata !{metadata !"Compiler V3"}
338 For symmetry reasons, ``ident.ll`` is just a dummy file that doesn't
339 actually participate in the test besides holding the ``RUN:`` lines.
343 Some existing tests use ``RUN: true`` in extra files instead of just
344 putting the extra files in an ``Inputs/`` directory. This pattern is
350 It is easy to write a fragile test that would fail spuriously if the tool being
351 tested outputs a full path to the input file. For example, :program:`opt` by
352 default outputs a ``ModuleID``:
354 .. code-block:: console
357 define i32 @main() nounwind {
361 $ opt -S /path/to/example.ll
362 ; ModuleID = '/path/to/example.ll'
364 define i32 @main() nounwind {
368 ``ModuleID`` can unexpectedly match against ``CHECK`` lines. For example:
372 ; RUN: opt -S %s | FileCheck
374 define i32 @main() nounwind {
379 This test will fail if placed into a ``download`` directory.
381 To make your tests robust, always use ``opt ... < %s`` in the RUN line.
382 :program:`opt` does not output a ``ModuleID`` when input comes from stdin.
384 Platform-Specific Tests
385 -----------------------
387 Whenever adding tests that require the knowledge of a specific platform,
388 either related to code generated, specific output or back-end features,
389 you must make sure to isolate the features, so that buildbots that
390 run on different architectures (and don't even compile all back-ends),
393 The first problem is to check for target-specific output, for example sizes
394 of structures, paths and architecture names, for example:
396 * Tests containing Windows paths will fail on Linux and vice-versa.
397 * Tests that check for ``x86_64`` somewhere in the text will fail anywhere else.
398 * Tests where the debug information calculates the size of types and structures.
400 Also, if the test rely on any behaviour that is coded in any back-end, it must
401 go in its own directory. So, for instance, code generator tests for ARM go
402 into ``test/CodeGen/ARM`` and so on. Those directories contain a special
403 ``lit`` configuration file that ensure all tests in that directory will
404 only run if a specific back-end is compiled and available.
406 For instance, on ``test/CodeGen/ARM``, the ``lit.local.cfg`` is:
408 .. code-block:: python
410 config.suffixes = ['.ll', '.c', '.cpp', '.test']
411 if not 'ARM' in config.root.targets:
412 config.unsupported = True
414 Other platform-specific tests are those that depend on a specific feature
415 of a specific sub-architecture, for example only to Intel chips that support ``AVX2``.
417 For instance, ``test/CodeGen/X86/psubus.ll`` tests three sub-architecture
422 ; RUN: llc -mcpu=core2 < %s | FileCheck %s -check-prefix=SSE2
423 ; RUN: llc -mcpu=corei7-avx < %s | FileCheck %s -check-prefix=AVX1
424 ; RUN: llc -mcpu=core-avx2 < %s | FileCheck %s -check-prefix=AVX2
426 And the checks are different:
431 ; SSE2: psubusw LCPI0_0(%rip), %xmm0
433 ; AVX1: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0
435 ; AVX2: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0
437 So, if you're testing for a behaviour that you know is platform-specific or
438 depends on special features of sub-architectures, you must add the specific
439 triple, test with the specific FileCheck and put it into the specific
440 directory that will filter out all other architectures.
443 Constraining test execution
444 ---------------------------
446 Some tests can be run only in specific configurations, such as
447 with debug builds or on particular platforms. Use ``REQUIRES``
448 and ``UNSUPPORTED`` to control when the test is enabled.
450 Some tests are expected to fail. For example, there may be a known bug
451 that the test detect. Use ``XFAIL`` to mark a test as an expected failure.
452 An ``XFAIL`` test will be successful if its execution fails, and
453 will be a failure if its execution succeeds.
457 ; This test will be only enabled in the build with asserts.
459 ; This test is disabled on Linux.
460 ; UNSUPPORTED: -linux-
461 ; This test is expected to fail on PowerPC.
464 ``REQUIRES`` and ``UNSUPPORTED`` and ``XFAIL`` all accept a comma-separated
465 list of boolean expressions. The values in each expression may be:
467 - Features added to ``config.available_features`` by configuration files such as ``lit.cfg``.
468 String comparison of features is case-sensitive. Furthermore, a boolean expression can
469 contain any Python regular expression enclosed in ``{{ }}``, in which case the boolean
470 expression is satisfied if any feature matches the regular expression. Regular
471 expressions can appear inside an identifier, so for example ``he{{l+}}o`` would match
472 ``helo``, ``hello``, ``helllo``, and so on.
473 - Substrings of the target triple (``UNSUPPORTED`` and ``XFAIL`` only).
475 | ``REQUIRES`` enables the test if all expressions are true.
476 | ``UNSUPPORTED`` disables the test if any expression is true.
477 | ``XFAIL`` expects the test to fail if any expression is true.
479 As a special case, ``XFAIL: *`` is expected to fail everywhere.
483 ; This test is disabled on Windows,
484 ; and is disabled on Linux, except for Android Linux.
485 ; UNSUPPORTED: windows, linux && !android
486 ; This test is expected to fail on both PowerPC and ARM.
487 ; XFAIL: powerpc || arm
493 Besides replacing LLVM tool names the following substitutions are performed in
497 Replaced by a single ``%``. This allows escaping other substitutions.
500 File path to the test case's source. This is suitable for passing on the
501 command line as the input to an LLVM tool.
503 Example: ``/home/user/llvm/test/MC/ELF/foo_test.s``
506 Directory path to the test case's source.
508 Example: ``/home/user/llvm/test/MC/ELF``
511 File path to a temporary file name that could be used for this test case.
512 The file name won't conflict with other test cases. You can append to it
513 if you need multiple temporaries. This is useful as the destination of
514 some redirected output.
516 Example: ``/home/user/llvm.build/test/MC/ELF/Output/foo_test.s.tmp``
519 Directory of ``%t``. Deprecated. Shouldn't be used, because it can be easily
520 misused and cause race conditions between tests.
522 Use ``rm -rf %t && mkdir %t`` instead if a temporary directory is necessary.
524 Example: ``/home/user/llvm.build/test/MC/ELF/Output``
528 Expands to the path separator, i.e. ``:`` (or ``;`` on Windows).
530 ``%/s, %/S, %/t, %/T:``
532 Act like the corresponding substitution above but replace any ``\``
533 character with a ``/``. This is useful to normalize path separators.
535 Example: ``%s: C:\Desktop Files/foo_test.s.tmp``
537 Example: ``%/s: C:/Desktop Files/foo_test.s.tmp``
539 ``%:s, %:S, %:t, %:T:``
541 Act like the corresponding substitution above but remove colons at
542 the beginning of Windows paths. This is useful to allow concatenation
543 of absolute paths on Windows to produce a legal path.
545 Example: ``%s: C:\Desktop Files\foo_test.s.tmp``
547 Example: ``%:s: C\Desktop Files\foo_test.s.tmp``
551 Some error messages may be substituted to allow different spellings
552 based on the host platform.
554 The following error codes are currently supported:
555 ENOENT, EISDIR, EINVAL, EACCES.
557 Example: ``Linux %errc_ENOENT: No such file or directory``
559 Example: ``Windows %errc_ENOENT: no such file or directory``
561 **LLVM-specific substitutions:**
564 The suffix for the host platforms shared library files. This includes the
565 period as the first character.
567 Example: ``.so`` (Linux), ``.dylib`` (macOS), ``.dll`` (Windows)
570 The suffix for the host platforms executable files. This includes the
571 period as the first character.
573 Example: ``.exe`` (Windows), empty on Linux.
575 ``%(line)``, ``%(line+<number>)``, ``%(line-<number>)``
576 The number of the line where this substitution is used, with an optional
577 integer offset. This can be used in tests with multiple RUN lines, which
578 reference test file's line numbers.
581 **Clang-specific substitutions:**
584 Invokes the Clang driver.
587 Invokes the Clang driver for C++.
590 Invokes the CL-compatible Clang driver.
593 Invokes the G++-compatible Clang driver.
596 Invokes the Clang frontend.
598 ``%itanium_abi_triple``, ``%ms_abi_triple``
599 These substitutions can be used to get the current target triple adjusted to
600 the desired ABI. For example, if the test suite is running with the
601 ``i686-pc-win32`` target, ``%itanium_abi_triple`` will expand to
602 ``i686-pc-mingw32``. This allows a test to run with a specific ABI without
603 constraining it to a specific triple.
605 **FileCheck-specific substitutions:**
607 ``%ProtectFileCheckOutput``
608 This should precede a ``FileCheck`` call if and only if the call's textual
609 output affects test results. It's usually easy to tell: just look for
610 redirection or piping of the ``FileCheck`` call's stdout or stderr.
612 To add more substitutions, look at ``test/lit.cfg`` or ``lit.local.cfg``.
618 The llvm lit configuration allows to customize some things with user options:
620 ``llc``, ``opt``, ...
621 Substitute the respective llvm tool name with a custom command line. This
622 allows to specify custom paths and default arguments for these tools.
625 % llvm-lit "-Dllc=llc -verify-machineinstrs"
628 Enable the execution of long running tests.
631 Load the specified lit configuration instead of the default one.
637 To make RUN line writing easier, there are several helper programs. These
638 helpers are in the PATH when running tests, so you can just call them using
639 their name. For example:
642 This program runs its arguments and then inverts the result code from it.
643 Zero result codes become 1. Non-zero result codes become 0.
645 To make the output more useful, :program:`lit` will scan
646 the lines of the test case for ones that contain a pattern that matches
647 ``PR[0-9]+``. This is the syntax for specifying a PR (Problem Report) number
648 that is related to the test case. The number after "PR" specifies the
649 LLVM Bugzilla number. When a PR number is specified, it will be used in
650 the pass/fail reporting. This is useful to quickly get some context when
653 Finally, any line that contains "END." will cause the special
654 interpretation of lines to terminate. This is generally done right after
655 the last RUN: line. This has two side effects:
657 (a) it prevents special interpretation of lines that are part of the test
658 program, not the instructions to the test case, and
660 (b) it speeds things up for really big test cases by avoiding
661 interpretation of the remainder of the file.