1=================================
2LLVM Testing Infrastructure Guide
3=================================
4
5.. contents::
6   :local:
7
8.. toctree::
9   :hidden:
10
11   TestSuiteGuide
12   TestSuiteMakefileGuide
13
14Overview
15========
16
17This document is the reference manual for the LLVM testing
18infrastructure. It documents the structure of the LLVM testing
19infrastructure, the tools needed to use it, and how to add and run
20tests.
21
22Requirements
23============
24
25In order to use the LLVM testing infrastructure, you will need all of the
26software required to build LLVM, as well as `Python <http://python.org>`_ 2.7 or
27later.
28
29LLVM Testing Infrastructure Organization
30========================================
31
32The LLVM testing infrastructure contains two major categories of tests:
33regression tests and whole programs. The regression tests are contained
34inside the LLVM repository itself under ``llvm/test`` and are expected
35to always pass -- they should be run before every commit.
36
37The 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
39historical reasons, these tests are also referred to as the "nightly
40tests" in places, which is less ambiguous than "test-suite" and remains
41in use although we run them much more often than nightly.
42
43Regression tests
44----------------
45
46The regression tests are small pieces of code that test a specific
47feature of LLVM or trigger a specific bug in LLVM. The language they are
48written in depends on the part of LLVM being tested. These tests are driven by
49the :doc:`Lit <CommandGuide/lit>` testing tool (which is part of LLVM), and
50are located in the ``llvm/test`` directory.
51
52Typically when a bug is found in LLVM, a regression test containing just
53enough code to reproduce the problem should be written and placed
54somewhere underneath this directory. For example, it can be a small
55piece of LLVM IR distilled from an actual application or benchmark.
56
57``test-suite``
58--------------
59
60The test suite contains whole programs, which are pieces of code which
61can be compiled and linked into a stand-alone program that can be
62executed. These programs are generally written in high level languages
63such as C or C++.
64
65These programs are compiled using a user specified compiler and set of
66flags, and then executed to capture the program output and timing
67information. The output of these programs is compared to a reference
68output to ensure that the program is being compiled correctly.
69
70In addition to compiling and executing programs, whole program tests
71serve as a way of benchmarking LLVM performance, both in terms of the
72efficiency of the programs generated as well as the speed with which
73LLVM compiles, optimizes, and generates code.
74
75The test-suite is located in the ``test-suite`` Subversion module.
76
77See the :doc:`TestSuiteGuide` for details.
78
79Debugging Information tests
80---------------------------
81
82The test suite contains tests to check quality of debugging information.
83The test are written in C based languages or in LLVM assembly language.
84
85These tests are compiled and run under a debugger. The debugger output
86is checked to validate of debugging information. See README.txt in the
87test suite for more information . This test suite is located in the
88``debuginfo-tests`` Subversion module.
89
90Quick start
91===========
92
93The tests are located in two separate Subversion modules. The
94regressions 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).
96Use ``make check-all`` to run the regression tests after building LLVM.
97
98The ``test-suite`` module contains more comprehensive tests including whole C
99and C++ programs. See the :doc:`TestSuiteGuide` for details.
100
101Regression tests
102----------------
103
104To run all of the LLVM regression tests use the check-llvm target:
105
106.. code-block:: bash
107
108    % make check-llvm
109
110If you have `Clang <http://clang.llvm.org/>`_ checked out and built, you
111can run the LLVM and Clang tests simultaneously using:
112
113.. code-block:: bash
114
115    % make check-all
116
117To run the tests with Valgrind (Memcheck by default), use the ``LIT_ARGS`` make
118variable to pass the required options to lit. For example, you can use:
119
120.. code-block:: bash
121
122    % make check LIT_ARGS="-v --vg --vg-leak"
123
124to enable testing with valgrind and with leak checking enabled.
125
126To run individual tests or subsets of tests, you can use the ``llvm-lit``
127script which is built as part of LLVM. For example, to run the
128``Integer/BitPacked.ll`` test by itself you can run:
129
130.. code-block:: bash
131
132    % llvm-lit ~/llvm/test/Integer/BitPacked.ll
133
134or to run all of the ARM CodeGen tests:
135
136.. code-block:: bash
137
138    % llvm-lit ~/llvm/test/CodeGen/ARM
139
140For more information on using the :program:`lit` tool, see ``llvm-lit --help``
141or the :doc:`lit man page <CommandGuide/lit>`.
142
143Debugging Information tests
144---------------------------
145
146To run debugging information tests simply checkout the tests inside
147clang/test directory.
148
149.. code-block:: bash
150
151    % cd clang/test
152    % svn co http://llvm.org/svn/llvm-project/debuginfo-tests/trunk debuginfo-tests
153
154These tests are already set up to run as part of clang regression tests.
155
156Regression test structure
157=========================
158
159The LLVM regression tests are driven by :program:`lit` and are located in the
160``llvm/test`` directory.
161
162This directory contains a large array of small tests that exercise
163various features of LLVM and to ensure that regressions do not occur.
164The directory is broken into several sub-directories, each focused on a
165particular area of LLVM.
166
167Writing new regression tests
168----------------------------
169
170The regression test structure is very simple, but does require some
171information to be set. This information is gathered via ``configure``
172and is written to a file, ``test/lit.site.cfg`` in the build directory.
173The ``llvm/test`` Makefile does this work for you.
174
175In order for the regression tests to work, each directory of tests must
176have a ``lit.local.cfg`` file. :program:`lit` looks for this file to determine
177how to run the tests. This file is just Python code and thus is very
178flexible, but we've standardized it for the LLVM regression tests. If
179you're adding a directory of tests, just copy ``lit.local.cfg`` from
180another directory to get running. The standard ``lit.local.cfg`` simply
181specifies which files to look in for tests. Any directory that contains
182only directories does not need the ``lit.local.cfg`` file. Read the :doc:`Lit
183documentation <CommandGuide/lit>` for more information.
184
185Each test file must contain lines starting with "RUN:" that tell :program:`lit`
186how to run it. If there are no RUN lines, :program:`lit` will issue an error
187while running a test.
188
189RUN lines are specified in the comments of the test program using the
190keyword ``RUN`` followed by a colon, and lastly the command (pipeline)
191to execute. Together, these lines form the "script" that :program:`lit`
192executes to run the test case. The syntax of the RUN lines is similar to a
193shell's syntax for pipelines including I/O redirection and variable
194substitution. However, even though these lines may *look* like a shell
195script, they are not. RUN lines are interpreted by :program:`lit`.
196Consequently, the syntax differs from shell in a few ways. You can specify
197as many RUN lines as needed.
198
199:program:`lit` performs substitution on each RUN line to replace LLVM tool names
200with the full paths to the executable built for each tool (in
201``$(LLVM_OBJ_ROOT)/$(BuildMode)/bin)``. This ensures that :program:`lit` does
202not invoke any stray LLVM tools in the user's path during testing.
203
204Each RUN line is executed on its own, distinct from other lines unless
205its last character is ``\``. This continuation character causes the RUN
206line to be concatenated with the next one. In this way you can build up
207long pipelines of commands without making huge line lengths. The lines
208ending in ``\`` are concatenated until a RUN line that doesn't end in
209``\`` is found. This concatenated set of RUN lines then constitutes one
210execution. :program:`lit` will substitute variables and arrange for the pipeline
211to be executed. If any process in the pipeline fails, the entire line (and
212test case) fails too.
213
214Below is an example of legal RUN lines in a ``.ll`` file:
215
216.. code-block:: llvm
217
218    ; RUN: llvm-as < %s | llvm-dis > %t1
219    ; RUN: llvm-dis < %s.bc-13 > %t2
220    ; RUN: diff %t1 %t2
221
222As with a Unix shell, the RUN lines permit pipelines and I/O
223redirection to be used.
224
225There are some quoting rules that you must pay attention to when writing
226your RUN lines. In general nothing needs to be quoted. :program:`lit` won't
227strip off any quote characters so they will get passed to the invoked program.
228To avoid this use curly braces to tell :program:`lit` that it should treat
229everything enclosed as one value.
230
231In general, you should strive to keep your RUN lines as simple as possible,
232using them only to run tools that generate textual output you can then examine.
233The recommended way to examine output to figure out if the test passes is using
234the :doc:`FileCheck tool <CommandGuide/FileCheck>`. *[The usage of grep in RUN
235lines is deprecated - please do not send or commit patches that use it.]*
236
237Put related tests into a single file rather than having a separate file per
238test. Check if there are files already covering your feature and consider
239adding your code there instead of creating a new file.
240
241Extra files
242-----------
243
244If your test requires extra files besides the file containing the ``RUN:``
245lines, the idiomatic place to put them is in a subdirectory ``Inputs``.
246You can then refer to the extra files as ``%S/Inputs/foo.bar``.
247
248For example, consider ``test/Linker/ident.ll``. The directory structure is
249as follows::
250
251  test/
252    Linker/
253      ident.ll
254      Inputs/
255        ident.a.ll
256        ident.b.ll
257
258For convenience, these are the contents:
259
260.. code-block:: llvm
261
262  ;;;;; ident.ll:
263
264  ; RUN: llvm-link %S/Inputs/ident.a.ll %S/Inputs/ident.b.ll -S | FileCheck %s
265
266  ; Verify that multiple input llvm.ident metadata are linked together.
267
268  ; CHECK-DAG: !llvm.ident = !{!0, !1, !2}
269  ; CHECK-DAG: "Compiler V1"
270  ; CHECK-DAG: "Compiler V2"
271  ; CHECK-DAG: "Compiler V3"
272
273  ;;;;; Inputs/ident.a.ll:
274
275  !llvm.ident = !{!0, !1}
276  !0 = metadata !{metadata !"Compiler V1"}
277  !1 = metadata !{metadata !"Compiler V2"}
278
279  ;;;;; Inputs/ident.b.ll:
280
281  !llvm.ident = !{!0}
282  !0 = metadata !{metadata !"Compiler V3"}
283
284For symmetry reasons, ``ident.ll`` is just a dummy file that doesn't
285actually participate in the test besides holding the ``RUN:`` lines.
286
287.. note::
288
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.
292
293Fragile tests
294-------------
295
296It is easy to write a fragile test that would fail spuriously if the tool being
297tested outputs a full path to the input file.  For example, :program:`opt` by
298default outputs a ``ModuleID``:
299
300.. code-block:: console
301
302  $ cat example.ll
303  define i32 @main() nounwind {
304      ret i32 0
305  }
306
307  $ opt -S /path/to/example.ll
308  ; ModuleID = '/path/to/example.ll'
309
310  define i32 @main() nounwind {
311      ret i32 0
312  }
313
314``ModuleID`` can unexpectedly match against ``CHECK`` lines.  For example:
315
316.. code-block:: llvm
317
318  ; RUN: opt -S %s | FileCheck
319
320  define i32 @main() nounwind {
321      ; CHECK-NOT: load
322      ret i32 0
323  }
324
325This test will fail if placed into a ``download`` directory.
326
327To 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.
329
330Platform-Specific Tests
331-----------------------
332
333Whenever adding tests that require the knowledge of a specific platform,
334either related to code generated, specific output or back-end features,
335you must make sure to isolate the features, so that buildbots that
336run on different architectures (and don't even compile all back-ends),
337don't fail.
338
339The first problem is to check for target-specific output, for example sizes
340of structures, paths and architecture names, for example:
341
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.
345
346Also, if the test rely on any behaviour that is coded in any back-end, it must
347go in its own directory. So, for instance, code generator tests for ARM go
348into ``test/CodeGen/ARM`` and so on. Those directories contain a special
349``lit`` configuration file that ensure all tests in that directory will
350only run if a specific back-end is compiled and available.
351
352For instance, on ``test/CodeGen/ARM``, the ``lit.local.cfg`` is:
353
354.. code-block:: python
355
356  config.suffixes = ['.ll', '.c', '.cpp', '.test']
357  if not 'ARM' in config.root.targets:
358    config.unsupported = True
359
360Other platform-specific tests are those that depend on a specific feature
361of a specific sub-architecture, for example only to Intel chips that support ``AVX2``.
362
363For instance, ``test/CodeGen/X86/psubus.ll`` tests three sub-architecture
364variants:
365
366.. code-block:: llvm
367
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
371
372And the checks are different:
373
374.. code-block:: llvm
375
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
382
383So, if you're testing for a behaviour that you know is platform-specific or
384depends on special features of sub-architectures, you must add the specific
385triple, test with the specific FileCheck and put it into the specific
386directory that will filter out all other architectures.
387
388
389Constraining test execution
390---------------------------
391
392Some tests can be run only in specific configurations, such as
393with debug builds or on particular platforms. Use ``REQUIRES``
394and ``UNSUPPORTED`` to control when the test is enabled.
395
396Some tests are expected to fail. For example, there may be a known bug
397that the test detect. Use ``XFAIL`` to mark a test as an expected failure.
398An ``XFAIL`` test will be successful if its execution fails, and
399will be a failure if its execution succeeds.
400
401.. code-block:: llvm
402
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
409
410``REQUIRES`` and ``UNSUPPORTED`` and ``XFAIL`` all accept a comma-separated
411list of boolean expressions. The values in each expression may be:
412
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).
416
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.
420
421As a special case, ``XFAIL: *`` is expected to fail everywhere.
422
423.. code-block:: llvm
424
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
430
431
432Substitutions
433-------------
434
435Besides replacing LLVM tool names the following substitutions are performed in
436RUN lines:
437
438``%%``
439   Replaced by a single ``%``. This allows escaping other substitutions.
440
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.
444
445   Example: ``/home/user/llvm/test/MC/ELF/foo_test.s``
446
447``%S``
448   Directory path to the test case's source.
449
450   Example: ``/home/user/llvm/test/MC/ELF``
451
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.
457
458   Example: ``/home/user/llvm.build/test/MC/ELF/Output/foo_test.s.tmp``
459
460``%T``
461   Directory of ``%t``. Deprecated. Shouldn't be used, because it can be easily
462   misused and cause race conditions between tests.
463
464   Use ``rm -rf %t && mkdir %t`` instead if a temporary directory is necessary.
465
466   Example: ``/home/user/llvm.build/test/MC/ELF/Output``
467
468``%{pathsep}``
469
470   Expands to the path separator, i.e. ``:`` (or ``;`` on Windows).
471
472``%/s, %/S, %/t, %/T:``
473
474  Act like the corresponding substitution above but replace any ``\``
475  character with a ``/``. This is useful to normalize path separators.
476
477   Example: ``%s:  C:\Desktop Files/foo_test.s.tmp``
478
479   Example: ``%/s: C:/Desktop Files/foo_test.s.tmp``
480
481``%:s, %:S, %:t, %:T:``
482
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.
486
487   Example: ``%s:  C:\Desktop Files\foo_test.s.tmp``
488
489   Example: ``%:s: C\Desktop Files\foo_test.s.tmp``
490
491
492**LLVM-specific substitutions:**
493
494``%shlibext``
495   The suffix for the host platforms shared library files. This includes the
496   period as the first character.
497
498   Example: ``.so`` (Linux), ``.dylib`` (OS X), ``.dll`` (Windows)
499
500``%exeext``
501   The suffix for the host platforms executable files. This includes the
502   period as the first character.
503
504   Example: ``.exe`` (Windows), empty on Linux.
505
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.
510
511
512**Clang-specific substitutions:**
513
514``%clang``
515   Invokes the Clang driver.
516
517``%clang_cpp``
518   Invokes the Clang driver for C++.
519
520``%clang_cl``
521   Invokes the CL-compatible Clang driver.
522
523``%clangxx``
524   Invokes the G++-compatible Clang driver.
525
526``%clang_cc1``
527   Invokes the Clang frontend.
528
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.
535
536To add more substituations, look at ``test/lit.cfg`` or ``lit.local.cfg``.
537
538
539Options
540-------
541
542The llvm lit configuration allows to customize some things with user options:
543
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:
548
549    % llvm-lit "-Dllc=llc -verify-machineinstrs"
550
551``run_long_tests``
552    Enable the execution of long running tests.
553
554``llvm_site_config``
555    Load the specified lit configuration instead of the default one.
556
557
558Other Features
559--------------
560
561To make RUN line writing easier, there are several helper programs. These
562helpers are in the PATH when running tests, so you can just call them using
563their name. For example:
564
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.
568
569To make the output more useful, :program:`lit` will scan
570the 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
572that is related to the test case. The number after "PR" specifies the
573LLVM bugzilla number. When a PR number is specified, it will be used in
574the pass/fail reporting. This is useful to quickly get some context when
575a test fails.
576
577Finally, any line that contains "END." will cause the special
578interpretation of lines to terminate. This is generally done right after
579the last RUN: line. This has two side effects:
580
581(a) it prevents special interpretation of lines that are part of the test
582    program, not the instructions to the test case, and
583
584(b) it speeds things up for really big test cases by avoiding
585    interpretation of the remainder of the file.
586