[NFC] Turn collectTransitivePredecessors into a static function
[llvm-core.git] / docs / TestingGuide.rst
blobe3db60dd9e788de898bf97c9d2f96e82e961db2d
1 =================================
2 LLVM Testing Infrastructure Guide
3 =================================
5 .. contents::
6    :local:
8 .. toctree::
9    :hidden:
11    TestSuiteGuide
12    TestSuiteMakefileGuide
14 Overview
15 ========
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
20 tests.
22 Requirements
23 ============
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>`_ 2.7 or
27 later.
29 LLVM Testing Infrastructure Organization
30 ========================================
32 The LLVM testing infrastructure contains two major categories of tests:
33 regression tests and whole programs. The regression tests are contained
34 inside the LLVM repository itself under ``llvm/test`` and are expected
35 to always pass -- they should be run before every commit.
37 The whole programs tests are referred to as the "LLVM test suite" (or
38 "test-suite") and are in the ``test-suite`` module in subversion. For
39 historical reasons, these tests are also referred to as the "nightly
40 tests" in places, which is less ambiguous than "test-suite" and remains
41 in use although we run them much more often than nightly.
43 Regression tests
44 ----------------
46 The regression tests are small pieces of code that test a specific
47 feature of LLVM or trigger a specific bug in LLVM. The language they are
48 written in depends on the part of LLVM being tested. These tests are driven by
49 the :doc:`Lit <CommandGuide/lit>` testing tool (which is part of LLVM), and
50 are located in the ``llvm/test`` directory.
52 Typically when a bug is found in LLVM, a regression test containing just
53 enough code to reproduce the problem should be written and placed
54 somewhere underneath this directory. For example, it can be a small
55 piece of LLVM IR distilled from an actual application or benchmark.
57 ``test-suite``
58 --------------
60 The test suite contains whole programs, which are pieces of code which
61 can be compiled and linked into a stand-alone program that can be
62 executed. These programs are generally written in high level languages
63 such as C or C++.
65 These programs are compiled using a user specified compiler and set of
66 flags, and then executed to capture the program output and timing
67 information. The output of these programs is compared to a reference
68 output to ensure that the program is being compiled correctly.
70 In addition to compiling and executing programs, whole program tests
71 serve as a way of benchmarking LLVM performance, both in terms of the
72 efficiency of the programs generated as well as the speed with which
73 LLVM compiles, optimizes, and generates code.
75 The test-suite is located in the ``test-suite`` Subversion module.
77 See the :doc:`TestSuiteGuide` for details.
79 Debugging Information tests
80 ---------------------------
82 The test suite contains tests to check quality of debugging information.
83 The test are written in C based languages or in LLVM assembly language.
85 These tests are compiled and run under a debugger. The debugger output
86 is checked to validate of debugging information. See README.txt in the
87 test suite for more information . This test suite is located in the
88 ``debuginfo-tests`` Subversion module.
90 Quick start
91 ===========
93 The tests are located in two separate Subversion modules. The
94 regressions tests are in the main "llvm" module under the directory
95 ``llvm/test`` (so you get these tests for free with the main LLVM tree).
96 Use ``make check-all`` to run the regression tests after building LLVM.
98 The ``test-suite`` module contains more comprehensive tests including whole C
99 and C++ programs. See the :doc:`TestSuiteGuide` for details.
101 Regression tests
102 ----------------
104 To run all of the LLVM regression tests use the check-llvm target:
106 .. code-block:: bash
108     % make check-llvm
110 If you have `Clang <http://clang.llvm.org/>`_ checked out and built, you
111 can run the LLVM and Clang tests simultaneously using:
113 .. code-block:: bash
115     % make check-all
117 To run the tests with Valgrind (Memcheck by default), use the ``LIT_ARGS`` make
118 variable to pass the required options to lit. For example, you can use:
120 .. code-block:: bash
122     % make check LIT_ARGS="-v --vg --vg-leak"
124 to enable testing with valgrind and with leak checking enabled.
126 To run individual tests or subsets of tests, you can use the ``llvm-lit``
127 script which is built as part of LLVM. For example, to run the
128 ``Integer/BitPacked.ll`` test by itself you can run:
130 .. code-block:: bash
132     % llvm-lit ~/llvm/test/Integer/BitPacked.ll 
134 or to run all of the ARM CodeGen tests:
136 .. code-block:: bash
138     % llvm-lit ~/llvm/test/CodeGen/ARM
140 For more information on using the :program:`lit` tool, see ``llvm-lit --help``
141 or the :doc:`lit man page <CommandGuide/lit>`.
143 Debugging Information tests
144 ---------------------------
146 To run debugging information tests simply checkout the tests inside
147 clang/test directory.
149 .. code-block:: bash
151     % cd clang/test
152     % svn co http://llvm.org/svn/llvm-project/debuginfo-tests/trunk debuginfo-tests
154 These tests are already set up to run as part of clang regression tests.
156 Regression test structure
157 =========================
159 The LLVM regression tests are driven by :program:`lit` and are located in the
160 ``llvm/test`` directory.
162 This directory contains a large array of small tests that exercise
163 various features of LLVM and to ensure that regressions do not occur.
164 The directory is broken into several sub-directories, each focused on a
165 particular area of LLVM.
167 Writing new regression tests
168 ----------------------------
170 The regression test structure is very simple, but does require some
171 information to be set. This information is gathered via ``configure``
172 and is written to a file, ``test/lit.site.cfg`` in the build directory.
173 The ``llvm/test`` Makefile does this work for you.
175 In order for the regression tests to work, each directory of tests must
176 have a ``lit.local.cfg`` file. :program:`lit` looks for this file to determine
177 how to run the tests. This file is just Python code and thus is very
178 flexible, but we've standardized it for the LLVM regression tests. If
179 you're adding a directory of tests, just copy ``lit.local.cfg`` from
180 another directory to get running. The standard ``lit.local.cfg`` simply
181 specifies which files to look in for tests. Any directory that contains
182 only directories does not need the ``lit.local.cfg`` file. Read the :doc:`Lit
183 documentation <CommandGuide/lit>` for more information.
185 Each test file must contain lines starting with "RUN:" that tell :program:`lit`
186 how to run it. If there are no RUN lines, :program:`lit` will issue an error
187 while running a test.
189 RUN lines are specified in the comments of the test program using the
190 keyword ``RUN`` followed by a colon, and lastly the command (pipeline)
191 to execute. Together, these lines form the "script" that :program:`lit`
192 executes to run the test case. The syntax of the RUN lines is similar to a
193 shell's syntax for pipelines including I/O redirection and variable
194 substitution. However, even though these lines may *look* like a shell
195 script, they are not. RUN lines are interpreted by :program:`lit`.
196 Consequently, the syntax differs from shell in a few ways. You can specify
197 as many RUN lines as needed.
199 :program:`lit` performs substitution on each RUN line to replace LLVM tool names
200 with the full paths to the executable built for each tool (in
201 ``$(LLVM_OBJ_ROOT)/$(BuildMode)/bin)``. This ensures that :program:`lit` does
202 not invoke any stray LLVM tools in the user's path during testing.
204 Each RUN line is executed on its own, distinct from other lines unless
205 its last character is ``\``. This continuation character causes the RUN
206 line to be concatenated with the next one. In this way you can build up
207 long pipelines of commands without making huge line lengths. The lines
208 ending in ``\`` are concatenated until a RUN line that doesn't end in
209 ``\`` is found. This concatenated set of RUN lines then constitutes one
210 execution. :program:`lit` will substitute variables and arrange for the pipeline
211 to be executed. If any process in the pipeline fails, the entire line (and
212 test case) fails too.
214 Below is an example of legal RUN lines in a ``.ll`` file:
216 .. code-block:: llvm
218     ; RUN: llvm-as < %s | llvm-dis > %t1
219     ; RUN: llvm-dis < %s.bc-13 > %t2
220     ; RUN: diff %t1 %t2
222 As with a Unix shell, the RUN lines permit pipelines and I/O
223 redirection to be used.
225 There are some quoting rules that you must pay attention to when writing
226 your RUN lines. In general nothing needs to be quoted. :program:`lit` won't
227 strip off any quote characters so they will get passed to the invoked program.
228 To avoid this use curly braces to tell :program:`lit` that it should treat
229 everything enclosed as one value.
231 In general, you should strive to keep your RUN lines as simple as possible,
232 using them only to run tools that generate textual output you can then examine.
233 The recommended way to examine output to figure out if the test passes is using
234 the :doc:`FileCheck tool <CommandGuide/FileCheck>`. *[The usage of grep in RUN
235 lines is deprecated - please do not send or commit patches that use it.]*
237 Put related tests into a single file rather than having a separate file per
238 test. Check if there are files already covering your feature and consider
239 adding your code there instead of creating a new file.
241 Extra files
242 -----------
244 If your test requires extra files besides the file containing the ``RUN:``
245 lines, the idiomatic place to put them is in a subdirectory ``Inputs``.
246 You can then refer to the extra files as ``%S/Inputs/foo.bar``.
248 For example, consider ``test/Linker/ident.ll``. The directory structure is
249 as follows::
251   test/
252     Linker/
253       ident.ll
254       Inputs/
255         ident.a.ll
256         ident.b.ll
258 For convenience, these are the contents:
260 .. code-block:: llvm
262   ;;;;; ident.ll:
264   ; RUN: llvm-link %S/Inputs/ident.a.ll %S/Inputs/ident.b.ll -S | FileCheck %s
266   ; Verify that multiple input llvm.ident metadata are linked together.
268   ; CHECK-DAG: !llvm.ident = !{!0, !1, !2}
269   ; CHECK-DAG: "Compiler V1"
270   ; CHECK-DAG: "Compiler V2"
271   ; CHECK-DAG: "Compiler V3"
273   ;;;;; Inputs/ident.a.ll:
275   !llvm.ident = !{!0, !1}
276   !0 = metadata !{metadata !"Compiler V1"}
277   !1 = metadata !{metadata !"Compiler V2"}
279   ;;;;; Inputs/ident.b.ll:
281   !llvm.ident = !{!0}
282   !0 = metadata !{metadata !"Compiler V3"}
284 For symmetry reasons, ``ident.ll`` is just a dummy file that doesn't
285 actually participate in the test besides holding the ``RUN:`` lines.
287 .. note::
289   Some existing tests use ``RUN: true`` in extra files instead of just
290   putting the extra files in an ``Inputs/`` directory. This pattern is
291   deprecated.
293 Fragile tests
294 -------------
296 It is easy to write a fragile test that would fail spuriously if the tool being
297 tested outputs a full path to the input file.  For example, :program:`opt` by
298 default outputs a ``ModuleID``:
300 .. code-block:: console
302   $ cat example.ll
303   define i32 @main() nounwind {
304       ret i32 0
305   }
307   $ opt -S /path/to/example.ll
308   ; ModuleID = '/path/to/example.ll'
310   define i32 @main() nounwind {
311       ret i32 0
312   }
314 ``ModuleID`` can unexpectedly match against ``CHECK`` lines.  For example:
316 .. code-block:: llvm
318   ; RUN: opt -S %s | FileCheck
320   define i32 @main() nounwind {
321       ; CHECK-NOT: load
322       ret i32 0
323   }
325 This test will fail if placed into a ``download`` directory.
327 To make your tests robust, always use ``opt ... < %s`` in the RUN line.
328 :program:`opt` does not output a ``ModuleID`` when input comes from stdin.
330 Platform-Specific Tests
331 -----------------------
333 Whenever adding tests that require the knowledge of a specific platform,
334 either related to code generated, specific output or back-end features,
335 you must make sure to isolate the features, so that buildbots that
336 run on different architectures (and don't even compile all back-ends),
337 don't fail.
339 The first problem is to check for target-specific output, for example sizes
340 of structures, paths and architecture names, for example:
342 * Tests containing Windows paths will fail on Linux and vice-versa.
343 * Tests that check for ``x86_64`` somewhere in the text will fail anywhere else.
344 * Tests where the debug information calculates the size of types and structures.
346 Also, if the test rely on any behaviour that is coded in any back-end, it must
347 go in its own directory. So, for instance, code generator tests for ARM go
348 into ``test/CodeGen/ARM`` and so on. Those directories contain a special
349 ``lit`` configuration file that ensure all tests in that directory will
350 only run if a specific back-end is compiled and available.
352 For instance, on ``test/CodeGen/ARM``, the ``lit.local.cfg`` is:
354 .. code-block:: python
356   config.suffixes = ['.ll', '.c', '.cpp', '.test']
357   if not 'ARM' in config.root.targets:
358     config.unsupported = True
360 Other platform-specific tests are those that depend on a specific feature
361 of a specific sub-architecture, for example only to Intel chips that support ``AVX2``.
363 For instance, ``test/CodeGen/X86/psubus.ll`` tests three sub-architecture
364 variants:
366 .. code-block:: llvm
368   ; RUN: llc -mcpu=core2 < %s | FileCheck %s -check-prefix=SSE2
369   ; RUN: llc -mcpu=corei7-avx < %s | FileCheck %s -check-prefix=AVX1
370   ; RUN: llc -mcpu=core-avx2 < %s | FileCheck %s -check-prefix=AVX2
372 And the checks are different:
374 .. code-block:: llvm
376   ; SSE2: @test1
377   ; SSE2: psubusw LCPI0_0(%rip), %xmm0
378   ; AVX1: @test1
379   ; AVX1: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0
380   ; AVX2: @test1
381   ; AVX2: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0
383 So, if you're testing for a behaviour that you know is platform-specific or
384 depends on special features of sub-architectures, you must add the specific
385 triple, test with the specific FileCheck and put it into the specific
386 directory that will filter out all other architectures.
389 Constraining test execution
390 ---------------------------
392 Some tests can be run only in specific configurations, such as
393 with debug builds or on particular platforms. Use ``REQUIRES``
394 and ``UNSUPPORTED`` to control when the test is enabled.
396 Some tests are expected to fail. For example, there may be a known bug
397 that the test detect. Use ``XFAIL`` to mark a test as an expected failure.
398 An ``XFAIL`` test will be successful if its execution fails, and
399 will be a failure if its execution succeeds.
401 .. code-block:: llvm
403     ; This test will be only enabled in the build with asserts.
404     ; REQUIRES: asserts
405     ; This test is disabled on Linux.
406     ; UNSUPPORTED: -linux-
407     ; This test is expected to fail on PowerPC.
408     ; XFAIL: powerpc
410 ``REQUIRES`` and ``UNSUPPORTED`` and ``XFAIL`` all accept a comma-separated
411 list of boolean expressions. The values in each expression may be:
413 - Features added to ``config.available_features`` by 
414   configuration files such as ``lit.cfg``.
415 - Substrings of the target triple (``UNSUPPORTED`` and ``XFAIL`` only).
417 | ``REQUIRES`` enables the test if all expressions are true.
418 | ``UNSUPPORTED`` disables the test if any expression is true.
419 | ``XFAIL`` expects the test to fail if any expression is true.
421 As a special case, ``XFAIL: *`` is expected to fail everywhere.
423 .. code-block:: llvm
425     ; This test is disabled on Windows,
426     ; and is disabled on Linux, except for Android Linux.
427     ; UNSUPPORTED: windows, linux && !android
428     ; This test is expected to fail on both PowerPC and ARM.
429     ; XFAIL: powerpc || arm
432 Substitutions
433 -------------
435 Besides replacing LLVM tool names the following substitutions are performed in
436 RUN lines:
438 ``%%``
439    Replaced by a single ``%``. This allows escaping other substitutions.
441 ``%s``
442    File path to the test case's source. This is suitable for passing on the
443    command line as the input to an LLVM tool.
445    Example: ``/home/user/llvm/test/MC/ELF/foo_test.s``
447 ``%S``
448    Directory path to the test case's source.
450    Example: ``/home/user/llvm/test/MC/ELF``
452 ``%t``
453    File path to a temporary file name that could be used for this test case.
454    The file name won't conflict with other test cases. You can append to it
455    if you need multiple temporaries. This is useful as the destination of
456    some redirected output.
458    Example: ``/home/user/llvm.build/test/MC/ELF/Output/foo_test.s.tmp``
460 ``%T``
461    Directory of ``%t``. Deprecated. Shouldn't be used, because it can be easily
462    misused and cause race conditions between tests.
464    Use ``rm -rf %t && mkdir %t`` instead if a temporary directory is necessary.
466    Example: ``/home/user/llvm.build/test/MC/ELF/Output``
468 ``%{pathsep}``
470    Expands to the path separator, i.e. ``:`` (or ``;`` on Windows).
472 ``%/s, %/S, %/t, %/T:``
474   Act like the corresponding substitution above but replace any ``\``
475   character with a ``/``. This is useful to normalize path separators.
477    Example: ``%s:  C:\Desktop Files/foo_test.s.tmp``
478    
479    Example: ``%/s: C:/Desktop Files/foo_test.s.tmp``
481 ``%:s, %:S, %:t, %:T:``
483   Act like the corresponding substitution above but remove colons at
484   the beginning of Windows paths. This is useful to allow concatenation
485   of absolute paths on Windows to produce a legal path.
487    Example: ``%s:  C:\Desktop Files\foo_test.s.tmp``
489    Example: ``%:s: C\Desktop Files\foo_test.s.tmp``
492 **LLVM-specific substitutions:**
494 ``%shlibext``
495    The suffix for the host platforms shared library files. This includes the
496    period as the first character.
498    Example: ``.so`` (Linux), ``.dylib`` (OS X), ``.dll`` (Windows)
500 ``%exeext``
501    The suffix for the host platforms executable files. This includes the
502    period as the first character.
504    Example: ``.exe`` (Windows), empty on Linux.
506 ``%(line)``, ``%(line+<number>)``, ``%(line-<number>)``
507    The number of the line where this substitution is used, with an optional
508    integer offset. This can be used in tests with multiple RUN lines, which
509    reference test file's line numbers.
512 **Clang-specific substitutions:**
514 ``%clang``
515    Invokes the Clang driver.
517 ``%clang_cpp``
518    Invokes the Clang driver for C++.
520 ``%clang_cl``
521    Invokes the CL-compatible Clang driver.
523 ``%clangxx``
524    Invokes the G++-compatible Clang driver.
526 ``%clang_cc1``
527    Invokes the Clang frontend.
529 ``%itanium_abi_triple``, ``%ms_abi_triple``
530    These substitutions can be used to get the current target triple adjusted to
531    the desired ABI. For example, if the test suite is running with the
532    ``i686-pc-win32`` target, ``%itanium_abi_triple`` will expand to
533    ``i686-pc-mingw32``. This allows a test to run with a specific ABI without
534    constraining it to a specific triple.
536 To add more substituations, look at ``test/lit.cfg`` or ``lit.local.cfg``.
539 Options
540 -------
542 The llvm lit configuration allows to customize some things with user options:
544 ``llc``, ``opt``, ...
545     Substitute the respective llvm tool name with a custom command line. This
546     allows to specify custom paths and default arguments for these tools.
547     Example:
549     % llvm-lit "-Dllc=llc -verify-machineinstrs"
551 ``run_long_tests``
552     Enable the execution of long running tests.
554 ``llvm_site_config``
555     Load the specified lit configuration instead of the default one.
558 Other Features
559 --------------
561 To make RUN line writing easier, there are several helper programs. These
562 helpers are in the PATH when running tests, so you can just call them using
563 their name. For example:
565 ``not``
566    This program runs its arguments and then inverts the result code from it.
567    Zero result codes become 1. Non-zero result codes become 0.
569 To make the output more useful, :program:`lit` will scan
570 the lines of the test case for ones that contain a pattern that matches
571 ``PR[0-9]+``. This is the syntax for specifying a PR (Problem Report) number
572 that is related to the test case. The number after "PR" specifies the
573 LLVM bugzilla number. When a PR number is specified, it will be used in
574 the pass/fail reporting. This is useful to quickly get some context when
575 a test fails.
577 Finally, any line that contains "END." will cause the special
578 interpretation of lines to terminate. This is generally done right after
579 the last RUN: line. This has two side effects:
581 (a) it prevents special interpretation of lines that are part of the test
582     program, not the instructions to the test case, and
584 (b) it speeds things up for really big test cases by avoiding
585     interpretation of the remainder of the file.