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>`_ 3.6 or
27later.
28
29LLVM Testing Infrastructure Organization
30========================================
31
32The LLVM testing infrastructure contains three major categories of tests:
33unit tests, regression tests and whole programs. The unit tests and regression
34tests are contained inside the LLVM repository itself under ``llvm/unittests``
35and ``llvm/test`` respectively and are expected to always pass -- they should be
36run before every commit.
37
38The 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>`_.
41For historical reasons, these tests are also referred to as the "nightly
42tests" in places, which is less ambiguous than "test-suite" and remains
43in use although we run them much more often than nightly.
44
45Unit tests
46----------
47
48Unit tests are written using `Google Test <https://github.com/google/googletest/blob/master/docs/primer.md>`_
49and `Google Mock <https://github.com/google/googletest/blob/master/docs/gmock_for_dummies.md>`_
50and are located in the ``llvm/unittests`` directory.
51In general unit tests are reserved for targeting the support library and other
52generic data structure, we prefer relying on regression tests for testing
53transformations and analysis on the IR.
54
55Regression tests
56----------------
57
58The regression tests are small pieces of code that test a specific
59feature of LLVM or trigger a specific bug in LLVM. The language they are
60written in depends on the part of LLVM being tested. These tests are driven by
61the :doc:`Lit <CommandGuide/lit>` testing tool (which is part of LLVM), and
62are located in the ``llvm/test`` directory.
63
64Typically when a bug is found in LLVM, a regression test containing just
65enough code to reproduce the problem should be written and placed
66somewhere underneath this directory. For example, it can be a small
67piece of LLVM IR distilled from an actual application or benchmark.
68
69Testing Analysis
70----------------
71
72An analysis is a pass that infer properties on some part of the IR and not
73transforming it. They are tested in general using the same infrastructure as the
74regression tests, by creating a separate "Printer" pass to consume the analysis
75result and print it on the standard output in a textual format suitable for
76FileCheck.
77See `llvm/test/Analysis/BranchProbabilityInfo/loop.ll <https://github.com/llvm/llvm-project/blob/main/llvm/test/Analysis/BranchProbabilityInfo/loop.ll>`_
78for an example of such test.
79
80``test-suite``
81--------------
82
83The test suite contains whole programs, which are pieces of code which
84can be compiled and linked into a stand-alone program that can be
85executed. These programs are generally written in high level languages
86such as C or C++.
87
88These programs are compiled using a user specified compiler and set of
89flags, and then executed to capture the program output and timing
90information. The output of these programs is compared to a reference
91output to ensure that the program is being compiled correctly.
92
93In addition to compiling and executing programs, whole program tests
94serve as a way of benchmarking LLVM performance, both in terms of the
95efficiency of the programs generated as well as the speed with which
96LLVM compiles, optimizes, and generates code.
97
98The test-suite is located in the ``test-suite``
99`repository on GitHub <https://github.com/llvm/llvm-test-suite.git>`_.
100
101See the :doc:`TestSuiteGuide` for details.
102
103Debugging Information tests
104---------------------------
105
106The test suite contains tests to check quality of debugging information.
107The test are written in C based languages or in LLVM assembly language.
108
109These tests are compiled and run under a debugger. The debugger output
110is checked to validate of debugging information. See README.txt in the
111test suite for more information. This test suite is located in the
112``cross-project-tests/debuginfo-tests`` directory.
113
114Quick start
115===========
116
117The tests are located in two separate repositories. The unit and
118regression 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
120main LLVM tree). Use ``make check-all`` to run the unit and regression tests
121after building LLVM.
122
123The ``test-suite`` module contains more comprehensive tests including whole C
124and C++ programs. See the :doc:`TestSuiteGuide` for details.
125
126Unit and Regression tests
127-------------------------
128
129To run all of the LLVM unit tests use the check-llvm-unit target:
130
131.. code-block:: bash
132
133    % make check-llvm-unit
134
135To run all of the LLVM regression tests use the check-llvm target:
136
137.. code-block:: bash
138
139    % make check-llvm
140
141In order to get reasonable testing performance, build LLVM and subprojects
142in release mode, i.e.
143
144.. code-block:: bash
145
146    % cmake -DCMAKE_BUILD_TYPE="Release" -DLLVM_ENABLE_ASSERTIONS=On
147
148If you have `Clang <https://clang.llvm.org/>`_ checked out and built, you
149can run the LLVM and Clang tests simultaneously using:
150
151.. code-block:: bash
152
153    % make check-all
154
155To run the tests with Valgrind (Memcheck by default), use the ``LIT_ARGS`` make
156variable to pass the required options to lit. For example, you can use:
157
158.. code-block:: bash
159
160    % make check LIT_ARGS="-v --vg --vg-leak"
161
162to enable testing with valgrind and with leak checking enabled.
163
164To run individual tests or subsets of tests, you can use the ``llvm-lit``
165script which is built as part of LLVM. For example, to run the
166``Integer/BitPacked.ll`` test by itself you can run:
167
168.. code-block:: bash
169
170    % llvm-lit ~/llvm/test/Integer/BitPacked.ll
171
172or to run all of the ARM CodeGen tests:
173
174.. code-block:: bash
175
176    % llvm-lit ~/llvm/test/CodeGen/ARM
177
178The 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
180environment. Under Windows, install Python for all users and then run
181``pip install psutil`` in an elevated command prompt.
182
183For more information on using the :program:`lit` tool, see ``llvm-lit --help``
184or the :doc:`lit man page <CommandGuide/lit>`.
185
186Debugging Information tests
187---------------------------
188
189To run debugging information tests simply add the ``cross-project-tests``
190project to your ``LLVM_ENABLE_PROJECTS`` define on the cmake
191command-line.
192
193Regression test structure
194=========================
195
196The LLVM regression tests are driven by :program:`lit` and are located in the
197``llvm/test`` directory.
198
199This directory contains a large array of small tests that exercise
200various features of LLVM and to ensure that regressions do not occur.
201The directory is broken into several sub-directories, each focused on a
202particular area of LLVM.
203
204Writing new regression tests
205----------------------------
206
207The regression test structure is very simple, but does require some
208information to be set. This information is gathered via ``cmake``
209and is written to a file, ``test/lit.site.cfg`` in the build directory.
210The ``llvm/test`` Makefile does this work for you.
211
212In order for the regression tests to work, each directory of tests must
213have a ``lit.local.cfg`` file. :program:`lit` looks for this file to determine
214how to run the tests. This file is just Python code and thus is very
215flexible, but we've standardized it for the LLVM regression tests. If
216you're adding a directory of tests, just copy ``lit.local.cfg`` from
217another directory to get running. The standard ``lit.local.cfg`` simply
218specifies which files to look in for tests. Any directory that contains
219only directories does not need the ``lit.local.cfg`` file. Read the :doc:`Lit
220documentation <CommandGuide/lit>` for more information.
221
222Each test file must contain lines starting with "RUN:" that tell :program:`lit`
223how to run it. If there are no RUN lines, :program:`lit` will issue an error
224while running a test.
225
226RUN lines are specified in the comments of the test program using the
227keyword ``RUN`` followed by a colon, and lastly the command (pipeline)
228to execute. Together, these lines form the "script" that :program:`lit`
229executes to run the test case. The syntax of the RUN lines is similar to a
230shell's syntax for pipelines including I/O redirection and variable
231substitution. However, even though these lines may *look* like a shell
232script, they are not. RUN lines are interpreted by :program:`lit`.
233Consequently, the syntax differs from shell in a few ways. You can specify
234as many RUN lines as needed.
235
236:program:`lit` performs substitution on each RUN line to replace LLVM tool names
237with the full paths to the executable built for each tool (in
238``$(LLVM_OBJ_ROOT)/$(BuildMode)/bin)``. This ensures that :program:`lit` does
239not invoke any stray LLVM tools in the user's path during testing.
240
241Each RUN line is executed on its own, distinct from other lines unless
242its last character is ``\``. This continuation character causes the RUN
243line to be concatenated with the next one. In this way you can build up
244long pipelines of commands without making huge line lengths. The lines
245ending in ``\`` are concatenated until a RUN line that doesn't end in
246``\`` is found. This concatenated set of RUN lines then constitutes one
247execution. :program:`lit` will substitute variables and arrange for the pipeline
248to be executed. If any process in the pipeline fails, the entire line (and
249test case) fails too.
250
251Below is an example of legal RUN lines in a ``.ll`` file:
252
253.. code-block:: llvm
254
255    ; RUN: llvm-as < %s | llvm-dis > %t1
256    ; RUN: llvm-dis < %s.bc-13 > %t2
257    ; RUN: diff %t1 %t2
258
259As with a Unix shell, the RUN lines permit pipelines and I/O
260redirection to be used.
261
262There are some quoting rules that you must pay attention to when writing
263your RUN lines. In general nothing needs to be quoted. :program:`lit` won't
264strip off any quote characters so they will get passed to the invoked program.
265To avoid this use curly braces to tell :program:`lit` that it should treat
266everything enclosed as one value.
267
268In general, you should strive to keep your RUN lines as simple as possible,
269using them only to run tools that generate textual output you can then examine.
270The recommended way to examine output to figure out if the test passes is using
271the :doc:`FileCheck tool <CommandGuide/FileCheck>`. *[The usage of grep in RUN
272lines is deprecated - please do not send or commit patches that use it.]*
273
274Put related tests into a single file rather than having a separate file per
275test. Check if there are files already covering your feature and consider
276adding your code there instead of creating a new file.
277
278Extra files
279-----------
280
281If your test requires extra files besides the file containing the ``RUN:`` lines
282and the extra files are small, consider specifying them in the same file and
283using ``split-file`` to extract them. For example,
284
285.. code-block:: llvm
286
287  ; RUN: split-file %s %t
288  ; RUN: llvm-link -S %t/a.ll %t/b.ll | FileCheck %s
289
290  ; CHECK: ...
291
292  ;--- a.ll
293  ...
294  ;--- b.ll
295  ...
296
297The parts are separated by the regex ``^(.|//)--- <part>``. By default the
298extracted content has leading empty lines to preserve line numbers. Specify
299``--no-leading-lines`` to drop leading lines.
300
301If the extra files are large, the idiomatic place to put them is in a subdirectory ``Inputs``.
302You can then refer to the extra files as ``%S/Inputs/foo.bar``.
303
304For example, consider ``test/Linker/ident.ll``. The directory structure is
305as follows::
306
307  test/
308    Linker/
309      ident.ll
310      Inputs/
311        ident.a.ll
312        ident.b.ll
313
314For convenience, these are the contents:
315
316.. code-block:: llvm
317
318  ;;;;; ident.ll:
319
320  ; RUN: llvm-link %S/Inputs/ident.a.ll %S/Inputs/ident.b.ll -S | FileCheck %s
321
322  ; Verify that multiple input llvm.ident metadata are linked together.
323
324  ; CHECK-DAG: !llvm.ident = !{!0, !1, !2}
325  ; CHECK-DAG: "Compiler V1"
326  ; CHECK-DAG: "Compiler V2"
327  ; CHECK-DAG: "Compiler V3"
328
329  ;;;;; Inputs/ident.a.ll:
330
331  !llvm.ident = !{!0, !1}
332  !0 = metadata !{metadata !"Compiler V1"}
333  !1 = metadata !{metadata !"Compiler V2"}
334
335  ;;;;; Inputs/ident.b.ll:
336
337  !llvm.ident = !{!0}
338  !0 = metadata !{metadata !"Compiler V3"}
339
340For symmetry reasons, ``ident.ll`` is just a dummy file that doesn't
341actually participate in the test besides holding the ``RUN:`` lines.
342
343.. note::
344
345  Some existing tests use ``RUN: true`` in extra files instead of just
346  putting the extra files in an ``Inputs/`` directory. This pattern is
347  deprecated.
348
349Fragile tests
350-------------
351
352It is easy to write a fragile test that would fail spuriously if the tool being
353tested outputs a full path to the input file.  For example, :program:`opt` by
354default outputs a ``ModuleID``:
355
356.. code-block:: console
357
358  $ cat example.ll
359  define i32 @main() nounwind {
360      ret i32 0
361  }
362
363  $ opt -S /path/to/example.ll
364  ; ModuleID = '/path/to/example.ll'
365
366  define i32 @main() nounwind {
367      ret i32 0
368  }
369
370``ModuleID`` can unexpectedly match against ``CHECK`` lines.  For example:
371
372.. code-block:: llvm
373
374  ; RUN: opt -S %s | FileCheck
375
376  define i32 @main() nounwind {
377      ; CHECK-NOT: load
378      ret i32 0
379  }
380
381This test will fail if placed into a ``download`` directory.
382
383To make your tests robust, always use ``opt ... < %s`` in the RUN line.
384:program:`opt` does not output a ``ModuleID`` when input comes from stdin.
385
386Platform-Specific Tests
387-----------------------
388
389Whenever adding tests that require the knowledge of a specific platform,
390either related to code generated, specific output or back-end features,
391you must make sure to isolate the features, so that buildbots that
392run on different architectures (and don't even compile all back-ends),
393don't fail.
394
395The first problem is to check for target-specific output, for example sizes
396of structures, paths and architecture names, for example:
397
398* Tests containing Windows paths will fail on Linux and vice-versa.
399* Tests that check for ``x86_64`` somewhere in the text will fail anywhere else.
400* Tests where the debug information calculates the size of types and structures.
401
402Also, if the test rely on any behaviour that is coded in any back-end, it must
403go in its own directory. So, for instance, code generator tests for ARM go
404into ``test/CodeGen/ARM`` and so on. Those directories contain a special
405``lit`` configuration file that ensure all tests in that directory will
406only run if a specific back-end is compiled and available.
407
408For instance, on ``test/CodeGen/ARM``, the ``lit.local.cfg`` is:
409
410.. code-block:: python
411
412  config.suffixes = ['.ll', '.c', '.cpp', '.test']
413  if not 'ARM' in config.root.targets:
414    config.unsupported = True
415
416Other platform-specific tests are those that depend on a specific feature
417of a specific sub-architecture, for example only to Intel chips that support ``AVX2``.
418
419For instance, ``test/CodeGen/X86/psubus.ll`` tests three sub-architecture
420variants:
421
422.. code-block:: llvm
423
424  ; RUN: llc -mcpu=core2 < %s | FileCheck %s -check-prefix=SSE2
425  ; RUN: llc -mcpu=corei7-avx < %s | FileCheck %s -check-prefix=AVX1
426  ; RUN: llc -mcpu=core-avx2 < %s | FileCheck %s -check-prefix=AVX2
427
428And the checks are different:
429
430.. code-block:: llvm
431
432  ; SSE2: @test1
433  ; SSE2: psubusw LCPI0_0(%rip), %xmm0
434  ; AVX1: @test1
435  ; AVX1: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0
436  ; AVX2: @test1
437  ; AVX2: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0
438
439So, if you're testing for a behaviour that you know is platform-specific or
440depends on special features of sub-architectures, you must add the specific
441triple, test with the specific FileCheck and put it into the specific
442directory that will filter out all other architectures.
443
444
445Constraining test execution
446---------------------------
447
448Some tests can be run only in specific configurations, such as
449with debug builds or on particular platforms. Use ``REQUIRES``
450and ``UNSUPPORTED`` to control when the test is enabled.
451
452Some tests are expected to fail. For example, there may be a known bug
453that the test detect. Use ``XFAIL`` to mark a test as an expected failure.
454An ``XFAIL`` test will be successful if its execution fails, and
455will be a failure if its execution succeeds.
456
457.. code-block:: llvm
458
459    ; This test will be only enabled in the build with asserts.
460    ; REQUIRES: asserts
461    ; This test is disabled on Linux.
462    ; UNSUPPORTED: -linux-
463    ; This test is expected to fail on PowerPC.
464    ; XFAIL: powerpc
465
466``REQUIRES`` and ``UNSUPPORTED`` and ``XFAIL`` all accept a comma-separated
467list of boolean expressions. The values in each expression may be:
468
469- Features added to ``config.available_features`` by configuration files such as ``lit.cfg``.
470  String comparison of features is case-sensitive. Furthermore, a boolean expression can
471  contain any Python regular expression enclosed in ``{{ }}``, in which case the boolean
472  expression is satisfied if any feature matches the regular expression. Regular
473  expressions can appear inside an identifier, so for example ``he{{l+}}o`` would match
474  ``helo``, ``hello``, ``helllo``, and so on.
475- Substrings of the target triple (``UNSUPPORTED`` and ``XFAIL`` only).
476
477| ``REQUIRES`` enables the test if all expressions are true.
478| ``UNSUPPORTED`` disables the test if any expression is true.
479| ``XFAIL`` expects the test to fail if any expression is true.
480
481As a special case, ``XFAIL: *`` is expected to fail everywhere.
482
483.. code-block:: llvm
484
485    ; This test is disabled on Windows,
486    ; and is disabled on Linux, except for Android Linux.
487    ; UNSUPPORTED: windows, linux && !android
488    ; This test is expected to fail on both PowerPC and ARM.
489    ; XFAIL: powerpc || arm
490
491
492Substitutions
493-------------
494
495Besides replacing LLVM tool names the following substitutions are performed in
496RUN lines:
497
498``%%``
499   Replaced by a single ``%``. This allows escaping other substitutions.
500
501``%s``
502   File path to the test case's source. This is suitable for passing on the
503   command line as the input to an LLVM tool.
504
505   Example: ``/home/user/llvm/test/MC/ELF/foo_test.s``
506
507``%S``
508   Directory path to the test case's source.
509
510   Example: ``/home/user/llvm/test/MC/ELF``
511
512``%t``
513   File path to a temporary file name that could be used for this test case.
514   The file name won't conflict with other test cases. You can append to it
515   if you need multiple temporaries. This is useful as the destination of
516   some redirected output.
517
518   Example: ``/home/user/llvm.build/test/MC/ELF/Output/foo_test.s.tmp``
519
520``%T``
521   Directory of ``%t``. Deprecated. Shouldn't be used, because it can be easily
522   misused and cause race conditions between tests.
523
524   Use ``rm -rf %t && mkdir %t`` instead if a temporary directory is necessary.
525
526   Example: ``/home/user/llvm.build/test/MC/ELF/Output``
527
528``%{pathsep}``
529
530   Expands to the path separator, i.e. ``:`` (or ``;`` on Windows).
531
532``%/s, %/S, %/t, %/T:``
533
534  Act like the corresponding substitution above but replace any ``\``
535  character with a ``/``. This is useful to normalize path separators.
536
537   Example: ``%s:  C:\Desktop Files/foo_test.s.tmp``
538
539   Example: ``%/s: C:/Desktop Files/foo_test.s.tmp``
540
541``%:s, %:S, %:t, %:T:``
542
543  Act like the corresponding substitution above but remove colons at
544  the beginning of Windows paths. This is useful to allow concatenation
545  of absolute paths on Windows to produce a legal path.
546
547   Example: ``%s:  C:\Desktop Files\foo_test.s.tmp``
548
549   Example: ``%:s: C\Desktop Files\foo_test.s.tmp``
550
551``%errc_<ERRCODE>``
552
553 Some error messages may be substituted to allow different spellings
554 based on the host platform.
555
556   The following error codes are currently supported:
557   ENOENT, EISDIR, EINVAL, EACCES.
558
559   Example: ``Linux %errc_ENOENT: No such file or directory``
560
561   Example: ``Windows %errc_ENOENT: no such file or directory``
562
563**LLVM-specific substitutions:**
564
565``%shlibext``
566   The suffix for the host platforms shared library files. This includes the
567   period as the first character.
568
569   Example: ``.so`` (Linux), ``.dylib`` (macOS), ``.dll`` (Windows)
570
571``%exeext``
572   The suffix for the host platforms executable files. This includes the
573   period as the first character.
574
575   Example: ``.exe`` (Windows), empty on Linux.
576
577``%(line)``, ``%(line+<number>)``, ``%(line-<number>)``
578   The number of the line where this substitution is used, with an optional
579   integer offset. This can be used in tests with multiple RUN lines, which
580   reference test file's line numbers.
581
582
583**Clang-specific substitutions:**
584
585``%clang``
586   Invokes the Clang driver.
587
588``%clang_cpp``
589   Invokes the Clang driver for C++.
590
591``%clang_cl``
592   Invokes the CL-compatible Clang driver.
593
594``%clangxx``
595   Invokes the G++-compatible Clang driver.
596
597``%clang_cc1``
598   Invokes the Clang frontend.
599
600``%itanium_abi_triple``, ``%ms_abi_triple``
601   These substitutions can be used to get the current target triple adjusted to
602   the desired ABI. For example, if the test suite is running with the
603   ``i686-pc-win32`` target, ``%itanium_abi_triple`` will expand to
604   ``i686-pc-mingw32``. This allows a test to run with a specific ABI without
605   constraining it to a specific triple.
606
607**FileCheck-specific substitutions:**
608
609``%ProtectFileCheckOutput``
610   This should precede a ``FileCheck`` call if and only if the call's textual
611   output affects test results.  It's usually easy to tell: just look for
612   redirection or piping of the ``FileCheck`` call's stdout or stderr.
613
614To add more substitutions, look at ``test/lit.cfg`` or ``lit.local.cfg``.
615
616
617Options
618-------
619
620The llvm lit configuration allows to customize some things with user options:
621
622``llc``, ``opt``, ...
623    Substitute the respective llvm tool name with a custom command line. This
624    allows to specify custom paths and default arguments for these tools.
625    Example:
626
627    % llvm-lit "-Dllc=llc -verify-machineinstrs"
628
629``run_long_tests``
630    Enable the execution of long running tests.
631
632``llvm_site_config``
633    Load the specified lit configuration instead of the default one.
634
635
636Other Features
637--------------
638
639To make RUN line writing easier, there are several helper programs. These
640helpers are in the PATH when running tests, so you can just call them using
641their name. For example:
642
643``not``
644   This program runs its arguments and then inverts the result code from it.
645   Zero result codes become 1. Non-zero result codes become 0.
646
647To make the output more useful, :program:`lit` will scan
648the lines of the test case for ones that contain a pattern that matches
649``PR[0-9]+``. This is the syntax for specifying a PR (Problem Report) number
650that is related to the test case. The number after "PR" specifies the
651LLVM Bugzilla number. When a PR number is specified, it will be used in
652the pass/fail reporting. This is useful to quickly get some context when
653a test fails.
654
655Finally, any line that contains "END." will cause the special
656interpretation of lines to terminate. This is generally done right after
657the last RUN: line. This has two side effects:
658
659(a) it prevents special interpretation of lines that are part of the test
660    program, not the instructions to the test case, and
661
662(b) it speeds things up for really big test cases by avoiding
663    interpretation of the remainder of the file.
664