Documentation: add a section to prevent spurious test failures like the one
[oota-llvm.git] / docs / TestingGuide.rst
1 =================================
2 LLVM Testing Infrastructure Guide
3 =================================
4
5 Written by John T. Criswell, Daniel Dunbar, Reid Spencer, and Tanya
6 Lattner
7
8 .. contents::
9    :local:
10
11 .. toctree::
12    :hidden:
13
14    TestSuiteMakefileGuide
15
16 Overview
17 ========
18
19 This document is the reference manual for the LLVM testing
20 infrastructure. It documents the structure of the LLVM testing
21 infrastructure, the tools needed to use it, and how to add and run
22 tests.
23
24 Requirements
25 ============
26
27 In order to use the LLVM testing infrastructure, you will need all of
28 the software required to build LLVM, as well as
29 `Python <http://python.org>`_ 2.4 or later.
30
31 LLVM testing infrastructure organization
32 ========================================
33
34 The LLVM testing infrastructure contains two major categories of tests:
35 regression tests and whole programs. The regression tests are contained
36 inside the LLVM repository itself under ``llvm/test`` and are expected
37 to always pass -- they should be run before every commit.
38
39 The whole programs tests are referred to as the "LLVM test suite" (or
40 "test-suite") and are in the ``test-suite`` module in subversion. For
41 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.
44
45 Regression tests
46 ----------------
47
48 The regression tests are small pieces of code that test a specific
49 feature of LLVM or trigger a specific bug in LLVM. The language they are
50 written in depends on the part of LLVM being tested. These tests are driven by
51 the :doc:`Lit <CommandGuide/lit>` testing tool (which is part of LLVM), and
52 are located in the ``llvm/test`` directory.
53
54 Typically when a bug is found in LLVM, a regression test containing just
55 enough code to reproduce the problem should be written and placed
56 somewhere underneath this directory. For example, it can be a small
57 piece of LLVM IR distilled from an actual application or benchmark.
58
59 ``test-suite``
60 --------------
61
62 The test suite contains whole programs, which are pieces of code which
63 can be compiled and linked into a stand-alone program that can be
64 executed. These programs are generally written in high level languages
65 such as C or C++.
66
67 These programs are compiled using a user specified compiler and set of
68 flags, and then executed to capture the program output and timing
69 information. The output of these programs is compared to a reference
70 output to ensure that the program is being compiled correctly.
71
72 In addition to compiling and executing programs, whole program tests
73 serve as a way of benchmarking LLVM performance, both in terms of the
74 efficiency of the programs generated as well as the speed with which
75 LLVM compiles, optimizes, and generates code.
76
77 The test-suite is located in the ``test-suite`` Subversion module.
78
79 Debugging Information tests
80 ---------------------------
81
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.
84
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.
89
90 Quick start
91 ===========
92
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.
97
98 The more comprehensive test suite that includes whole programs in C and C++
99 is in the ``test-suite`` module. See :ref:`test-suite Quickstart
100 <test-suite-quickstart>` for more information on running these tests.
101
102 Regression tests
103 ----------------
104
105 To run all of the LLVM regression tests, use the master Makefile in the
106 ``llvm/test`` directory. LLVM Makefiles require GNU Make (read the :doc:`LLVM
107 Makefile Guide <MakefileGuide>` for more details):
108
109 .. code-block:: bash
110
111     % make -C llvm/test
112
113 or:
114
115 .. code-block:: bash
116
117     % make check
118
119 If you have `Clang <http://clang.llvm.org/>`_ checked out and built, you
120 can run the LLVM and Clang tests simultaneously using:
121
122 .. code-block:: bash
123
124     % make check-all
125
126 To run the tests with Valgrind (Memcheck by default), just append
127 ``VG=1`` to the commands above, e.g.:
128
129 .. code-block:: bash
130
131     % make check VG=1
132
133 To run individual tests or subsets of tests, you can use the ``llvm-lit``
134 script which is built as part of LLVM. For example, to run the
135 ``Integer/BitPacked.ll`` test by itself you can run:
136
137 .. code-block:: bash
138
139     % llvm-lit ~/llvm/test/Integer/BitPacked.ll 
140
141 or to run all of the ARM CodeGen tests:
142
143 .. code-block:: bash
144
145     % llvm-lit ~/llvm/test/CodeGen/ARM
146
147 For more information on using the :program:`lit` tool, see ``llvm-lit --help``
148 or the :doc:`lit man page <CommandGuide/lit>`.
149
150 Debugging Information tests
151 ---------------------------
152
153 To run debugging information tests simply checkout the tests inside
154 clang/test directory.
155
156 .. code-block:: bash
157
158     % cd clang/test
159     % svn co http://llvm.org/svn/llvm-project/debuginfo-tests/trunk debuginfo-tests
160
161 These tests are already set up to run as part of clang regression tests.
162
163 Regression test structure
164 =========================
165
166 The LLVM regression tests are driven by :program:`lit` and are located in the
167 ``llvm/test`` directory.
168
169 This directory contains a large array of small tests that exercise
170 various features of LLVM and to ensure that regressions do not occur.
171 The directory is broken into several sub-directories, each focused on a
172 particular area of LLVM.
173
174 Writing new regression tests
175 ----------------------------
176
177 The regression test structure is very simple, but does require some
178 information to be set. This information is gathered via ``configure``
179 and is written to a file, ``test/lit.site.cfg`` in the build directory.
180 The ``llvm/test`` Makefile does this work for you.
181
182 In order for the regression tests to work, each directory of tests must
183 have a ``lit.local.cfg`` file. :program:`lit` looks for this file to determine
184 how to run the tests. This file is just Python code and thus is very
185 flexible, but we've standardized it for the LLVM regression tests. If
186 you're adding a directory of tests, just copy ``lit.local.cfg`` from
187 another directory to get running. The standard ``lit.local.cfg`` simply
188 specifies which files to look in for tests. Any directory that contains
189 only directories does not need the ``lit.local.cfg`` file. Read the :doc:`Lit
190 documentation <CommandGuide/lit>` for more information.
191
192 Each test file must contain lines starting with "RUN:" that tell :program:`lit`
193 how to run it. If there are no RUN lines, :program:`lit` will issue an error
194 while running a test.
195
196 RUN lines are specified in the comments of the test program using the
197 keyword ``RUN`` followed by a colon, and lastly the command (pipeline)
198 to execute. Together, these lines form the "script" that :program:`lit`
199 executes to run the test case. The syntax of the RUN lines is similar to a
200 shell's syntax for pipelines including I/O redirection and variable
201 substitution. However, even though these lines may *look* like a shell
202 script, they are not. RUN lines are interpreted by :program:`lit`.
203 Consequently, the syntax differs from shell in a few ways. You can specify
204 as many RUN lines as needed.
205
206 :program:`lit` performs substitution on each RUN line to replace LLVM tool names
207 with the full paths to the executable built for each tool (in
208 ``$(LLVM_OBJ_ROOT)/$(BuildMode)/bin)``. This ensures that :program:`lit` does
209 not invoke any stray LLVM tools in the user's path during testing.
210
211 Each RUN line is executed on its own, distinct from other lines unless
212 its last character is ``\``. This continuation character causes the RUN
213 line to be concatenated with the next one. In this way you can build up
214 long pipelines of commands without making huge line lengths. The lines
215 ending in ``\`` are concatenated until a RUN line that doesn't end in
216 ``\`` is found. This concatenated set of RUN lines then constitutes one
217 execution. :program:`lit` will substitute variables and arrange for the pipeline
218 to be executed. If any process in the pipeline fails, the entire line (and
219 test case) fails too.
220
221 Below is an example of legal RUN lines in a ``.ll`` file:
222
223 .. code-block:: llvm
224
225     ; RUN: llvm-as < %s | llvm-dis > %t1
226     ; RUN: llvm-dis < %s.bc-13 > %t2
227     ; RUN: diff %t1 %t2
228
229 As with a Unix shell, the RUN lines permit pipelines and I/O
230 redirection to be used. However, the usage is slightly different than
231 for Bash. In general, it's useful to read the code of other tests to figure out
232 what you can use in yours. The major differences are:
233
234 -  You can't do ``2>&1``. That will cause :program:`lit` to write to a file
235    named ``&1``. Usually this is done to get stderr to go through a pipe. You
236    can do that with ``|&`` so replace this idiom:
237    ``... 2>&1 | grep`` with ``... |& grep``
238 -  You can only redirect to a file, not to another descriptor and not
239    from a here document.
240
241 There are some quoting rules that you must pay attention to when writing
242 your RUN lines. In general nothing needs to be quoted. :program:`lit` won't
243 strip off any quote characters so they will get passed to the invoked program.
244 For example:
245
246 .. code-block:: bash
247
248     ... | grep 'find this string'
249
250 This will fail because the ``'`` characters are passed to ``grep``. This would
251 make ``grep`` to look for ``'find`` in the files ``this`` and
252 ``string'``. To avoid this use curly braces to tell :program:`lit` that it
253 should treat everything enclosed as one value. So our example would become:
254
255 .. code-block:: bash
256
257     ... | grep {find this string}
258
259 In general, you should strive to keep your RUN lines as simple as possible,
260 using them only to run tools that generate the output you can then examine. The
261 recommended way to examine output to figure out if the test passes it using the
262 :doc:`FileCheck tool <CommandGuide/FileCheck>`. The usage of ``grep`` in RUN
263 lines is discouraged.
264
265 Fragile tests
266 -------------
267
268 It is easy to write a fragile test that would fail spuriously if the tool being
269 tested outputs a full path to the input file.  For example, :program:`opt` by
270 default outputs a ``ModuleID``:
271
272 .. code-block:: console
273
274   $ cat example.ll
275   define i32 @main() nounwind {
276       ret i32 0
277   }
278
279   $ opt -S /path/to/example.ll
280   ; ModuleID = '/path/to/example.ll'
281
282   define i32 @main() nounwind {
283       ret i32 0
284   }
285
286 ``ModuleID`` can unexpetedly match against ``CHECK`` lines.  For example:
287
288 .. code-block:: llvm
289
290   ; RUN: opt -S %s | FileCheck
291
292   define i32 @main() nounwind {
293       ; CHECK-NOT: load
294       ret i32 0
295   }
296
297 This test will fail if placed into a ``download`` directory.
298
299 To make your tests robust, always use ``opt ... < %s`` in the RUN line.
300 :program:`opt` does not output a ``ModuleID`` when input comes from stdin.
301
302 The FileCheck utility
303 ---------------------
304
305 A powerful feature of the RUN lines is that it allows any arbitrary
306 commands to be executed as part of the test harness. While standard
307 (portable) unix tools like ``grep`` work fine on run lines, as you see
308 above, there are a lot of caveats due to interaction with shell syntax,
309 and we want to make sure the run lines are portable to a wide range of
310 systems. Another major problem is that ``grep`` is not very good at checking
311 to verify that the output of a tools contains a series of different
312 output in a specific order. The :program:`FileCheck` tool was designed to
313 help with these problems.
314
315 :program:`FileCheck` is designed to read a file to check from standard input,
316 and the set of things to verify from a file specified as a command line
317 argument. :program:`FileCheck` is described in :doc:`the FileCheck man page
318 <CommandGuide/FileCheck>`.
319
320 Variables and substitutions
321 ---------------------------
322
323 With a RUN line there are a number of substitutions that are permitted.
324 To make a substitution just write the variable's name preceded by a ``$``.
325 Additionally, for compatibility reasons with previous versions of the
326 test library, certain names can be accessed with an alternate syntax: a
327 % prefix. These alternates are deprecated and may go away in a future
328 version.
329
330 Here are the available variable names. The alternate syntax is listed in
331 parentheses.
332
333 ``$test`` (``%s``)
334    The full path to the test case's source. This is suitable for passing on
335    the command line as the input to an LLVM tool.
336
337 ``%(line)``, ``%(line+<number>)``, ``%(line-<number>)``
338    The number of the line where this variable is used, with an optional
339    integer offset. This can be used in tests with multiple RUN lines,
340    which reference test file's line numbers.
341
342 ``$srcdir``
343    The source directory from where the ``make check`` was run.
344
345 ``objdir``
346    The object directory that corresponds to the ``$srcdir``.
347
348 ``subdir``
349    A partial path from the ``test`` directory that contains the
350    sub-directory that contains the test source being executed.
351
352 ``srcroot``
353    The root directory of the LLVM src tree.
354
355 ``objroot``
356    The root directory of the LLVM object tree. This could be the same as
357    the srcroot.
358
359 ``path``
360    The path to the directory that contains the test case source. This is
361    for locating any supporting files that are not generated by the test,
362    but used by the test.
363
364 ``tmp``
365    The path to a temporary file name that could be used for this test case.
366    The file name won't conflict with other test cases. You can append to it
367    if you need multiple temporaries. This is useful as the destination of
368    some redirected output.
369
370 ``target_triplet`` (``%target_triplet``)
371    The target triplet that corresponds to the current host machine (the one
372    running the test cases). This should probably be called "host".
373
374 ``link`` (``%link``)
375    This full link command used to link LLVM executables. This has all the
376    configured ``-I``, ``-L`` and ``-l`` options.
377
378 ``shlibext`` (``%shlibext``)
379    The suffix for the host platforms shared library (DLL) files. This
380    includes the period as the first character.
381
382 To add more variables, look at ``test/lit.cfg``.
383
384 Other Features
385 --------------
386
387 To make RUN line writing easier, there are several helper scripts and programs
388 in the ``llvm/test/Scripts`` directory. This directory is in the PATH
389 when running tests, so you can just call these scripts using their name.
390 For example:
391
392 ``ignore``
393    This script runs its arguments and then always returns 0. This is useful
394    in cases where the test needs to cause a tool to generate an error (e.g.
395    to check the error output). However, any program in a pipeline that
396    returns a non-zero result will cause the test to fail.  This script
397    overcomes that issue and nicely documents that the test case is
398    purposefully ignoring the result code of the tool
399 ``not``
400    This script runs its arguments and then inverts the result code from it.
401    Zero result codes become 1. Non-zero result codes become 0.
402
403 Sometimes it is necessary to mark a test case as "expected fail" or
404 XFAIL. You can easily mark a test as XFAIL just by including ``XFAIL:``
405 on a line near the top of the file. This signals that the test case
406 should succeed if the test fails. Such test cases are counted separately
407 by the testing tool. To specify an expected fail, use the XFAIL keyword
408 in the comments of the test program followed by a colon and one or more
409 failure patterns. Each failure pattern can be either ``*`` (to specify
410 fail everywhere), or a part of a target triple (indicating the test
411 should fail on that platform), or the name of a configurable feature
412 (for example, ``loadable_module``). If there is a match, the test is
413 expected to fail. If not, the test is expected to succeed. To XFAIL
414 everywhere just specify ``XFAIL: *``. Here is an example of an ``XFAIL``
415 line:
416
417 .. code-block:: llvm
418
419     ; XFAIL: darwin,sun
420
421 To make the output more useful, :program:`lit` will scan
422 the lines of the test case for ones that contain a pattern that matches
423 ``PR[0-9]+``. This is the syntax for specifying a PR (Problem Report) number
424 that is related to the test case. The number after "PR" specifies the
425 LLVM bugzilla number. When a PR number is specified, it will be used in
426 the pass/fail reporting. This is useful to quickly get some context when
427 a test fails.
428
429 Finally, any line that contains "END." will cause the special
430 interpretation of lines to terminate. This is generally done right after
431 the last RUN: line. This has two side effects:
432
433 (a) it prevents special interpretation of lines that are part of the test
434     program, not the instructions to the test case, and
435
436 (b) it speeds things up for really big test cases by avoiding
437     interpretation of the remainder of the file.
438
439 ``test-suite`` Overview
440 =======================
441
442 The ``test-suite`` module contains a number of programs that can be
443 compiled and executed. The ``test-suite`` includes reference outputs for
444 all of the programs, so that the output of the executed program can be
445 checked for correctness.
446
447 ``test-suite`` tests are divided into three types of tests: MultiSource,
448 SingleSource, and External.
449
450 -  ``test-suite/SingleSource``
451
452    The SingleSource directory contains test programs that are only a
453    single source file in size. These are usually small benchmark
454    programs or small programs that calculate a particular value. Several
455    such programs are grouped together in each directory.
456
457 -  ``test-suite/MultiSource``
458
459    The MultiSource directory contains subdirectories which contain
460    entire programs with multiple source files. Large benchmarks and
461    whole applications go here.
462
463 -  ``test-suite/External``
464
465    The External directory contains Makefiles for building code that is
466    external to (i.e., not distributed with) LLVM. The most prominent
467    members of this directory are the SPEC 95 and SPEC 2000 benchmark
468    suites. The ``External`` directory does not contain these actual
469    tests, but only the Makefiles that know how to properly compile these
470    programs from somewhere else. When using ``LNT``, use the
471    ``--test-externals`` option to include these tests in the results.
472
473 .. _test-suite-quickstart:
474
475 ``test-suite`` Quickstart
476 -------------------------
477
478 The modern way of running the ``test-suite`` is focused on testing and
479 benchmarking complete compilers using the
480 `LNT <http://llvm.org/docs/lnt>`_ testing infrastructure.
481
482 For more information on using LNT to execute the ``test-suite``, please
483 see the `LNT Quickstart <http://llvm.org/docs/lnt/quickstart.html>`_
484 documentation.
485
486 ``test-suite`` Makefiles
487 ------------------------
488
489 Historically, the ``test-suite`` was executed using a complicated setup
490 of Makefiles. The LNT based approach above is recommended for most
491 users, but there are some testing scenarios which are not supported by
492 the LNT approach. In addition, LNT currently uses the Makefile setup
493 under the covers and so developers who are interested in how LNT works
494 under the hood may want to understand the Makefile based setup.
495
496 For more information on the ``test-suite`` Makefile setup, please see
497 the :doc:`Test Suite Makefile Guide <TestSuiteMakefileGuide>`.