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.8 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``
40 `repository on GitHub <https://github.com/llvm/llvm-test-suite.git>`_.
41 For 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.
48 Unit tests are written using `Google Test <https://github.com/google/googletest/blob/master/docs/primer.md>`_
49 and `Google Mock <https://github.com/google/googletest/blob/master/docs/gmock_for_dummies.md>`_
50 and are located in the ``llvm/unittests`` directory.
51 In general unit tests are reserved for targeting the support library and other
52 generic data structure, we prefer relying on regression tests for testing
53 transformations and analysis on the IR.
58 The regression tests are small pieces of code that test a specific
59 feature of LLVM or trigger a specific bug in LLVM. The language they are
60 written in depends on the part of LLVM being tested. These tests are driven by
61 the :doc:`Lit <CommandGuide/lit>` testing tool (which is part of LLVM), and
62 are located in the ``llvm/test`` directory.
64 Typically when a bug is found in LLVM, a regression test containing just
65 enough code to reproduce the problem should be written and placed
66 somewhere underneath this directory. For example, it can be a small
67 piece of LLVM IR distilled from an actual application or benchmark.
72 An analysis is a pass that infer properties on some part of the IR and not
73 transforming it. They are tested in general using the same infrastructure as the
74 regression tests, by creating a separate "Printer" pass to consume the analysis
75 result and print it on the standard output in a textual format suitable for
77 See `llvm/test/Analysis/BranchProbabilityInfo/loop.ll <https://github.com/llvm/llvm-project/blob/main/llvm/test/Analysis/BranchProbabilityInfo/loop.ll>`_
78 for an example of such test.
83 The test suite contains whole programs, which are pieces of code which
84 can be compiled and linked into a stand-alone program that can be
85 executed. These programs are generally written in high level languages
88 These programs are compiled using a user specified compiler and set of
89 flags, and then executed to capture the program output and timing
90 information. The output of these programs is compared to a reference
91 output to ensure that the program is being compiled correctly.
93 In addition to compiling and executing programs, whole program tests
94 serve as a way of benchmarking LLVM performance, both in terms of the
95 efficiency of the programs generated as well as the speed with which
96 LLVM compiles, optimizes, and generates code.
98 The test-suite is located in the ``test-suite``
99 `repository on GitHub <https://github.com/llvm/llvm-test-suite.git>`_.
101 See the :doc:`TestSuiteGuide` for details.
103 Debugging Information tests
104 ---------------------------
106 The test suite contains tests to check quality of debugging information.
107 The test are written in C based languages or in LLVM assembly language.
109 These tests are compiled and run under a debugger. The debugger output
110 is checked to validate of debugging information. See README.txt in the
111 test suite for more information. This test suite is located in the
112 ``cross-project-tests/debuginfo-tests`` directory.
117 The tests are located in two separate repositories. The unit and
118 regression 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
120 main LLVM tree). Use ``make check-all`` to run the unit and regression tests
123 The ``test-suite`` module contains more comprehensive tests including whole C
124 and C++ programs. See the :doc:`TestSuiteGuide` for details.
126 Unit and Regression tests
127 -------------------------
129 To run all of the LLVM unit tests use the check-llvm-unit target:
133 % make check-llvm-unit
135 To run all of the LLVM regression tests use the check-llvm target:
141 In order to get reasonable testing performance, build LLVM and subprojects
142 in release mode, i.e.
146 % cmake -DCMAKE_BUILD_TYPE="Release" -DLLVM_ENABLE_ASSERTIONS=On
148 If you have `Clang <https://clang.llvm.org/>`_ checked out and built, you
149 can run the LLVM and Clang tests simultaneously using:
155 To run the tests with Valgrind (Memcheck by default), use the ``LIT_ARGS`` make
156 variable to pass the required options to lit. For example, you can use:
160 % make check LIT_ARGS="-v --vg --vg-leak"
162 to enable testing with valgrind and with leak checking enabled.
164 To run individual tests or subsets of tests, you can use the ``llvm-lit``
165 script which is built as part of LLVM. For example, to run the
166 ``Integer/BitPacked.ll`` test by itself you can run:
170 % llvm-lit ~/llvm/test/Integer/BitPacked.ll
172 or to run all of the ARM CodeGen tests:
176 % llvm-lit ~/llvm/test/CodeGen/ARM
178 The 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
180 environment. Under Windows, install Python for all users and then run
181 ``pip install psutil`` in an elevated command prompt.
183 For more information on using the :program:`lit` tool, see ``llvm-lit --help``
184 or the :doc:`lit man page <CommandGuide/lit>`.
186 Debugging Information tests
187 ---------------------------
189 To run debugging information tests simply add the ``cross-project-tests``
190 project to your ``LLVM_ENABLE_PROJECTS`` define on the cmake
193 Regression test structure
194 =========================
196 The LLVM regression tests are driven by :program:`lit` and are located in the
197 ``llvm/test`` directory.
199 This directory contains a large array of small tests that exercise
200 various features of LLVM and to ensure that regressions do not occur.
201 The directory is broken into several sub-directories, each focused on a
202 particular area of LLVM.
204 Writing new regression tests
205 ----------------------------
207 The regression test structure is very simple, but does require some
208 information to be set. This information is gathered via ``cmake``
209 and is written to a file, ``test/lit.site.cfg.py`` in the build directory.
210 The ``llvm/test`` Makefile does this work for you.
212 In order for the regression tests to work, each directory of tests must
213 have a ``lit.local.cfg`` file. :program:`lit` looks for this file to determine
214 how to run the tests. This file is just Python code and thus is very
215 flexible, but we've standardized it for the LLVM regression tests. If
216 you're adding a directory of tests, just copy ``lit.local.cfg`` from
217 another directory to get running. The standard ``lit.local.cfg`` simply
218 specifies which files to look in for tests. Any directory that contains
219 only directories does not need the ``lit.local.cfg`` file. Read the :doc:`Lit
220 documentation <CommandGuide/lit>` for more information.
222 Each test file must contain lines starting with "RUN:" that tell :program:`lit`
223 how to run it. If there are no RUN lines, :program:`lit` will issue an error
224 while running a test.
226 RUN lines are specified in the comments of the test program using the
227 keyword ``RUN`` followed by a colon, and lastly the command (pipeline)
228 to execute. Together, these lines form the "script" that :program:`lit`
229 executes to run the test case. The syntax of the RUN lines is similar to a
230 shell's syntax for pipelines including I/O redirection and variable
231 substitution. However, even though these lines may *look* like a shell
232 script, they are not. RUN lines are interpreted by :program:`lit`.
233 Consequently, the syntax differs from shell in a few ways. You can specify
234 as many RUN lines as needed.
236 :program:`lit` performs substitution on each RUN line to replace LLVM tool names
237 with the full paths to the executable built for each tool (in
238 ``$(LLVM_OBJ_ROOT)/bin``). This ensures that :program:`lit` does
239 not invoke any stray LLVM tools in the user's path during testing.
241 Each RUN line is executed on its own, distinct from other lines unless
242 its last character is ``\``. This continuation character causes the RUN
243 line to be concatenated with the next one. In this way you can build up
244 long pipelines of commands without making huge line lengths. The lines
245 ending in ``\`` are concatenated until a RUN line that doesn't end in
246 ``\`` is found. This concatenated set of RUN lines then constitutes one
247 execution. :program:`lit` will substitute variables and arrange for the pipeline
248 to be executed. If any process in the pipeline fails, the entire line (and
249 test case) fails too.
251 Below is an example of legal RUN lines in a ``.ll`` file:
255 ; RUN: llvm-as < %s | llvm-dis > %t1
256 ; RUN: llvm-dis < %s.bc-13 > %t2
259 As with a Unix shell, the RUN lines permit pipelines and I/O
260 redirection to be used.
262 There are some quoting rules that you must pay attention to when writing
263 your RUN lines. In general nothing needs to be quoted. :program:`lit` won't
264 strip off any quote characters so they will get passed to the invoked program.
265 To avoid this use curly braces to tell :program:`lit` that it should treat
266 everything enclosed as one value.
268 In general, you should strive to keep your RUN lines as simple as possible,
269 using them only to run tools that generate textual output you can then examine.
270 The recommended way to examine output to figure out if the test passes is using
271 the :doc:`FileCheck tool <CommandGuide/FileCheck>`. *[The usage of grep in RUN
272 lines is deprecated - please do not send or commit patches that use it.]*
274 Put related tests into a single file rather than having a separate file per
275 test. Check if there are files already covering your feature and consider
276 adding your code there instead of creating a new file.
278 Generating assertions in regression tests
279 -----------------------------------------
281 Some regression test cases are very large and complex to write/update by hand.
282 In that case to reduce the human work we can use the scripts available in
283 llvm/utils/ to generate the assertions.
285 For example to generate assertions in an :program:`llc`-based test, after
286 adding one or more RUN lines use:
290 % llvm/utils/update_llc_test_checks.py --llc-binary build/bin/llc test.ll
292 This will generate FileCheck assertions, and insert a ``NOTE:`` line at the
293 top to indicate that assertions were automatically generated.
295 If you want to update assertions in an existing test case, pass the `-u` option
296 which first checks the ``NOTE:`` line exists and matches the script name.
298 Sometimes a test absolutely depends on hand-written assertions and should not
299 have assertions automatically generated. In that case, add the text ``NOTE: Do
300 not autogenerate`` to the first line, and the scripts will skip that test. It
301 is a good idea to explain why generated assertions will not work for the test
302 so future developers will understand what is going on.
304 These are the most common scripts and their purposes/applications in generating
309 update_analyze_test_checks.py
310 opt -passes='print<cost-model>'
312 update_cc_test_checks.py
313 C/C++, or clang/clang++ (IR checks)
315 update_llc_test_checks.py
316 llc (assembly checks)
318 update_mca_test_checks.py
321 update_mir_test_checks.py
324 update_test_checks.py
327 Precommit workflow for tests
328 ----------------------------
330 If the test does not crash, assert, or infinite loop, commit the test with
331 baseline check-lines first. That is, the test will show a miscompile or
332 missing optimization. Add a "TODO" or "FIXME" comment to indicate that
333 something is expected to change in a test.
335 A follow-up patch with code changes to the compiler will then show check-line
336 differences to the tests, so it is easier to see the effect of the patch.
337 Remove TODO/FIXME comments added in the previous step if a problem is solved.
339 Baseline tests (no-functional-change or NFC patch) may be pushed to main
340 without pre-commit review if you have commit access.
342 Best practices for regression tests
343 -----------------------------------
345 - Use auto-generated check lines (produced by the scripts mentioned above)
347 - Include comments about what is tested/expected in a particular test. If there
348 are relevant issues in the bug tracker, add references to those bug reports
349 (for example, "See PR999 for more details").
350 - Avoid undefined behavior and poison/undef values unless necessary. For
351 example, do not use patterns like ``br i1 undef``, which are likely to break
352 as a result of future optimizations.
353 - Minimize tests by removing unnecessary instructions, metadata, attributes,
354 etc. Tools like ``llvm-reduce`` can help automate this.
355 - Outside PhaseOrdering tests, only run a minimal set of passes. For example,
356 prefer ``opt -S -passes=instcombine`` over ``opt -S -O3``.
357 - Avoid unnamed instructions/blocks (such as ``%0`` or ``1:``), because they may
358 require renumbering on future test modifications. These can be removed by
359 running the test through ``opt -S -passes=instnamer``.
360 - Try to give values (including variables, blocks and functions) meaningful
361 names, and avoid retaining complex names generated by the optimization
362 pipeline (such as ``%foo.0.0.0.0.0.0``).
367 If your test requires extra files besides the file containing the ``RUN:`` lines
368 and the extra files are small, consider specifying them in the same file and
369 using ``split-file`` to extract them. For example,
373 ; RUN: split-file %s %t
374 ; RUN: llvm-link -S %t/a.ll %t/b.ll | FileCheck %s
383 The parts are separated by the regex ``^(.|//)--- <part>``.
385 If you want to test relative line numbers like ``[[#@LINE+1]]``, specify
386 ``--leading-lines`` to add leading empty lines to preserve line numbers.
388 If the extra files are large, the idiomatic place to put them is in a subdirectory ``Inputs``.
389 You can then refer to the extra files as ``%S/Inputs/foo.bar``.
391 For example, consider ``test/Linker/ident.ll``. The directory structure is
401 For convenience, these are the contents:
407 ; RUN: llvm-link %S/Inputs/ident.a.ll %S/Inputs/ident.b.ll -S | FileCheck %s
409 ; Verify that multiple input llvm.ident metadata are linked together.
411 ; CHECK-DAG: !llvm.ident = !{!0, !1, !2}
412 ; CHECK-DAG: "Compiler V1"
413 ; CHECK-DAG: "Compiler V2"
414 ; CHECK-DAG: "Compiler V3"
416 ;;;;; Inputs/ident.a.ll:
418 !llvm.ident = !{!0, !1}
419 !0 = metadata !{metadata !"Compiler V1"}
420 !1 = metadata !{metadata !"Compiler V2"}
422 ;;;;; Inputs/ident.b.ll:
425 !0 = metadata !{metadata !"Compiler V3"}
427 For symmetry reasons, ``ident.ll`` is just a dummy file that doesn't
428 actually participate in the test besides holding the ``RUN:`` lines.
432 Some existing tests use ``RUN: true`` in extra files instead of just
433 putting the extra files in an ``Inputs/`` directory. This pattern is
439 Generally, IR and assembly test files benefit from being cleaned to remove
440 unnecessary details. However, for tests requiring elaborate IR or assembly
441 files where cleanup is less practical (e.g., large amount of debug information
442 output from Clang), you can include generation instructions within
443 ``split-file`` part called ``gen``. Then, run
444 ``llvm/utils/update_test_body.py`` on the test file to generate the needed
449 ; RUN: rm -rf %t && split-file %s %t && cd %t
450 ; RUN: opt -S a.ll ... | FileCheck %s
457 clang --target=x86_64-linux -S -emit-llvm -g a.cc -o -
460 # content generated by the script 'gen'
464 PATH=/path/to/clang_build/bin:$PATH llvm/utils/update_test_body.py path/to/test.ll
466 The script will prepare extra files with ``split-file``, invoke ``gen``, and
467 then rewrite the part after ``gen`` with its stdout.
469 For convenience, if the test needs one single assembly file, you can also wrap
470 ``gen`` and its required files with ``.ifdef`` and ``.endif``. Then you can
471 skip ``split-file`` in RUN lines.
475 # RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o a.o
476 # RUN: ... | FileCheck %s
484 clang --target=x86_64-linux -S -g a.cc -o -
486 # content generated by the script 'gen'
490 Consider specifying an explicit target triple to avoid differences when
491 regeneration is needed on another machine.
493 ``gen`` is invoked with ``PWD`` set to ``/proc/self/cwd``. Clang commands
494 don't need ``-fdebug-compilation-dir=`` since its default value is ``PWD``.
496 Check prefixes should be placed before ``.endif`` since the part after
497 ``.endif`` is replaced.
499 If the test body contains multiple files, you can print ``---`` separators and
500 utilize ``split-file`` in ``RUN`` lines.
504 # RUN: rm -rf %t && split-file %s %t && cd %t
512 clang --target=x86_64-linux -S -O1 -g a.cc -o -
514 clang --target=x86_64-linux -S -O1 -g b.cc -o -
520 It is easy to write a fragile test that would fail spuriously if the tool being
521 tested outputs a full path to the input file. For example, :program:`opt` by
522 default outputs a ``ModuleID``:
524 .. code-block:: console
527 define i32 @main() nounwind {
531 $ opt -S /path/to/example.ll
532 ; ModuleID = '/path/to/example.ll'
534 define i32 @main() nounwind {
538 ``ModuleID`` can unexpectedly match against ``CHECK`` lines. For example:
542 ; RUN: opt -S %s | FileCheck
544 define i32 @main() nounwind {
549 This test will fail if placed into a ``download`` directory.
551 To make your tests robust, always use ``opt ... < %s`` in the RUN line.
552 :program:`opt` does not output a ``ModuleID`` when input comes from stdin.
554 Platform-Specific Tests
555 -----------------------
557 Whenever adding tests that require the knowledge of a specific platform,
558 either related to code generated, specific output or back-end features,
559 you must make sure to isolate the features, so that buildbots that
560 run on different architectures (and don't even compile all back-ends),
563 The first problem is to check for target-specific output, for example sizes
564 of structures, paths and architecture names, for example:
566 * Tests containing Windows paths will fail on Linux and vice-versa.
567 * Tests that check for ``x86_64`` somewhere in the text will fail anywhere else.
568 * Tests where the debug information calculates the size of types and structures.
570 Also, if the test rely on any behaviour that is coded in any back-end, it must
571 go in its own directory. So, for instance, code generator tests for ARM go
572 into ``test/CodeGen/ARM`` and so on. Those directories contain a special
573 ``lit`` configuration file that ensure all tests in that directory will
574 only run if a specific back-end is compiled and available.
576 For instance, on ``test/CodeGen/ARM``, the ``lit.local.cfg`` is:
578 .. code-block:: python
580 config.suffixes = ['.ll', '.c', '.cpp', '.test']
581 if not 'ARM' in config.root.targets:
582 config.unsupported = True
584 Other platform-specific tests are those that depend on a specific feature
585 of a specific sub-architecture, for example only to Intel chips that support ``AVX2``.
587 For instance, ``test/CodeGen/X86/psubus.ll`` tests three sub-architecture
592 ; RUN: llc -mcpu=core2 < %s | FileCheck %s -check-prefix=SSE2
593 ; RUN: llc -mcpu=corei7-avx < %s | FileCheck %s -check-prefix=AVX1
594 ; RUN: llc -mcpu=core-avx2 < %s | FileCheck %s -check-prefix=AVX2
596 And the checks are different:
601 ; SSE2: psubusw LCPI0_0(%rip), %xmm0
603 ; AVX1: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0
605 ; AVX2: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0
607 So, if you're testing for a behaviour that you know is platform-specific or
608 depends on special features of sub-architectures, you must add the specific
609 triple, test with the specific FileCheck and put it into the specific
610 directory that will filter out all other architectures.
613 Constraining test execution
614 ---------------------------
616 Some tests can be run only in specific configurations, such as
617 with debug builds or on particular platforms. Use ``REQUIRES``
618 and ``UNSUPPORTED`` to control when the test is enabled.
620 Some tests are expected to fail. For example, there may be a known bug
621 that the test detect. Use ``XFAIL`` to mark a test as an expected failure.
622 An ``XFAIL`` test will be successful if its execution fails, and
623 will be a failure if its execution succeeds.
627 ; This test will be only enabled in the build with asserts.
629 ; This test is disabled when running on Linux.
630 ; UNSUPPORTED: system-linux
631 ; This test is expected to fail when targeting PowerPC.
632 ; XFAIL: target=powerpc{{.*}}
634 ``REQUIRES`` and ``UNSUPPORTED`` and ``XFAIL`` all accept a comma-separated
635 list of boolean expressions. The values in each expression may be:
637 - Features added to ``config.available_features`` by configuration files such as ``lit.cfg``.
638 String comparison of features is case-sensitive. Furthermore, a boolean expression can
639 contain any Python regular expression enclosed in ``{{ }}``, in which case the boolean
640 expression is satisfied if any feature matches the regular expression. Regular
641 expressions can appear inside an identifier, so for example ``he{{l+}}o`` would match
642 ``helo``, ``hello``, ``helllo``, and so on.
643 - The default target triple, preceded by the string ``target=`` (for example,
644 ``target=x86_64-pc-windows-msvc``). Typically regular expressions are used
645 to match parts of the triple (for example, ``target={{.*}}-windows{{.*}}``
646 to match any Windows target triple).
648 | ``REQUIRES`` enables the test if all expressions are true.
649 | ``UNSUPPORTED`` disables the test if any expression is true.
650 | ``XFAIL`` expects the test to fail if any expression is true.
652 Use, ``XFAIL: *`` if the test is expected to fail everywhere. Similarly, use
653 ``UNSUPPORTED: target={{.*}}`` to disable the test everywhere.
657 ; This test is disabled when running on Windows,
658 ; and is disabled when targeting Linux, except for Android Linux.
659 ; UNSUPPORTED: system-windows, target={{.*linux.*}} && !target={{.*android.*}}
660 ; This test is expected to fail when targeting PowerPC or running on Darwin.
661 ; XFAIL: target=powerpc{{.*}}, system-darwin
664 Tips for writing constraints
665 ----------------------------
667 **``REQUIRES`` and ``UNSUPPORTED``**
669 These are logical inverses. In principle, ``UNSUPPORTED`` isn't absolutely
670 necessary (the logical negation could be used with ``REQUIRES`` to get
671 exactly the same effect), but it can make these clauses easier to read and
672 understand. Generally, people use ``REQUIRES`` to state things that the test
673 depends on to operate correctly, and ``UNSUPPORTED`` to exclude cases where
674 the test is expected never to work.
676 **``UNSUPPORTED`` and ``XFAIL``**
678 Both of these indicate that the test isn't expected to work; however, they
679 have different effects. ``UNSUPPORTED`` causes the test to be skipped;
680 this saves execution time, but then you'll never know whether the test
681 actually would start working. Conversely, ``XFAIL`` actually runs the test
682 but expects a failure output, taking extra execution time but alerting you
683 if/when the test begins to behave correctly (an XPASS test result). You
684 need to decide which is more appropriate in each case.
686 **Using ``target=...``**
688 Checking the target triple can be tricky; it's easy to mis-specify. For
689 example, ``target=mips{{.*}}`` will match not only mips, but also mipsel,
690 mips64, and mips64el. ``target={{.*}}-linux-gnu`` will match
691 x86_64-unknown-linux-gnu, but not armv8l-unknown-linux-gnueabihf.
692 Prefer to use hyphens to delimit triple components (``target=mips-{{.*}}``)
693 and it's generally a good idea to use a trailing wildcard to allow for
696 Also, it's generally better to write regular expressions that use entire
697 triple components, than to do something clever to shorten them. For
698 example, to match both freebsd and netbsd in an expression, you could write
699 ``target={{.*(free|net)bsd.*}}`` and that would work. However, it would
700 prevent a ``grep freebsd`` from finding this test. Better to use:
701 ``target={{.+-freebsd.*}} || target={{.+-netbsd.*}}``
707 Besides replacing LLVM tool names the following substitutions are performed in
711 Replaced by a single ``%``. This allows escaping other substitutions.
714 File path to the test case's source. This is suitable for passing on the
715 command line as the input to an LLVM tool.
717 Example: ``/home/user/llvm/test/MC/ELF/foo_test.s``
720 Directory path to the test case's source.
722 Example: ``/home/user/llvm/test/MC/ELF``
725 File path to a temporary file name that could be used for this test case.
726 The file name won't conflict with other test cases. You can append to it
727 if you need multiple temporaries. This is useful as the destination of
728 some redirected output.
730 Example: ``/home/user/llvm.build/test/MC/ELF/Output/foo_test.s.tmp``
733 Directory of ``%t``. Deprecated. Shouldn't be used, because it can be easily
734 misused and cause race conditions between tests.
736 Use ``rm -rf %t && mkdir %t`` instead if a temporary directory is necessary.
738 Example: ``/home/user/llvm.build/test/MC/ELF/Output``
742 Expands to the path separator, i.e. ``:`` (or ``;`` on Windows).
745 Expands to the root component of file system paths for the source directory,
746 i.e. ``/`` on Unix systems or ``C:\`` (or another drive) on Windows.
749 Expands to the root component of file system paths for the test's temporary
750 directory, i.e. ``/`` on Unix systems or ``C:\`` (or another drive) on
754 Expands to the file system separator, i.e. ``/`` or ``\`` on Windows.
756 ``%/s, %/S, %/t, %/T``
758 Act like the corresponding substitution above but replace any ``\``
759 character with a ``/``. This is useful to normalize path separators.
761 Example: ``%s: C:\Desktop Files/foo_test.s.tmp``
763 Example: ``%/s: C:/Desktop Files/foo_test.s.tmp``
765 ``%{s:real}, %{S:real}, %{t:real}, %{T:real}``
766 ``%{/s:real}, %{/S:real}, %{/t:real}, %{/T:real}``
768 Act like the corresponding substitution, including with ``/``, but use
769 the real path by expanding all symbolic links and substitute drives.
771 Example: ``%s: S:\foo_test.s.tmp``
773 Example: ``%{/s:real}: C:/SDrive/foo_test.s.tmp``
775 ``%:s, %:S, %:t, %:T``
777 Act like the corresponding substitution above but remove colons at
778 the beginning of Windows paths. This is useful to allow concatenation
779 of absolute paths on Windows to produce a legal path.
781 Example: ``%s: C:\Desktop Files\foo_test.s.tmp``
783 Example: ``%:s: C\Desktop Files\foo_test.s.tmp``
787 Some error messages may be substituted to allow different spellings
788 based on the host platform.
790 The following error codes are currently supported:
791 ENOENT, EISDIR, EINVAL, EACCES.
793 Example: ``Linux %errc_ENOENT: No such file or directory``
795 Example: ``Windows %errc_ENOENT: no such file or directory``
797 ``%if feature %{<if branch>%} %else %{<else branch>%}``
799 Conditional substitution: if ``feature`` is available it expands to
800 ``<if branch>``, otherwise it expands to ``<else branch>``.
801 ``%else %{<else branch>%}`` is optional and treated like ``%else %{%}``
804 ``%(line)``, ``%(line+<number>)``, ``%(line-<number>)``
806 The number of the line where this substitution is used, with an
807 optional integer offset. These expand only if they appear
808 immediately in ``RUN:``, ``DEFINE:``, and ``REDEFINE:`` directives.
809 Occurrences in substitutions defined elsewhere are never expanded.
810 For example, this can be used in tests with multiple RUN lines,
811 which reference the test file's line numbers.
813 **LLVM-specific substitutions:**
816 The suffix for the host platforms shared library files. This includes the
817 period as the first character.
819 Example: ``.so`` (Linux), ``.dylib`` (macOS), ``.dll`` (Windows)
822 The suffix for the host platforms executable files. This includes the
823 period as the first character.
825 Example: ``.exe`` (Windows), empty on Linux.
827 **Clang-specific substitutions:**
830 Invokes the Clang driver.
833 Invokes the Clang driver for C++.
836 Invokes the CL-compatible Clang driver.
839 Invokes the G++-compatible Clang driver.
842 Invokes the Clang frontend.
844 ``%itanium_abi_triple``, ``%ms_abi_triple``
845 These substitutions can be used to get the current target triple adjusted to
846 the desired ABI. For example, if the test suite is running with the
847 ``i686-pc-win32`` target, ``%itanium_abi_triple`` will expand to
848 ``i686-pc-mingw32``. This allows a test to run with a specific ABI without
849 constraining it to a specific triple.
851 **FileCheck-specific substitutions:**
853 ``%ProtectFileCheckOutput``
854 This should precede a ``FileCheck`` call if and only if the call's textual
855 output affects test results. It's usually easy to tell: just look for
856 redirection or piping of the ``FileCheck`` call's stdout or stderr.
858 .. _Test-specific substitutions:
860 **Test-specific substitutions:**
862 Additional substitutions can be defined as follows:
864 - Lit configuration files (e.g., ``lit.cfg`` or ``lit.local.cfg``) can define
865 substitutions for all tests in a test directory. They do so by extending the
866 substitution list, ``config.substitutions``. Each item in the list is a tuple
867 consisting of a pattern and its replacement, which lit applies as plain text
868 (even if it contains sequences that python's ``re.sub`` considers to be
870 - To define substitutions within a single test file, lit supports the
871 ``DEFINE:`` and ``REDEFINE:`` directives, described in detail below. So that
872 they have no effect on other test files, these directives modify a copy of the
873 substitution list that is produced by lit configuration files.
875 For example, the following directives can be inserted into a test file to define
876 ``%{cflags}`` and ``%{fcflags}`` substitutions with empty initial values, which
877 serve as the parameters of another newly defined ``%{check}`` substitution:
881 ; DEFINE: %{cflags} =
882 ; DEFINE: %{fcflags} =
884 ; DEFINE: %{check} = \
885 ; DEFINE: %clang_cc1 -verify -fopenmp -fopenmp-version=51 %{cflags} \
886 ; DEFINE: -emit-llvm -o - %s | \
887 ; DEFINE: FileCheck %{fcflags} %s
889 Alternatively, the above substitutions can be defined in a lit configuration
890 file to be shared with other test files. Either way, the test file can then
891 specify directives like the following to redefine the parameter substitutions as
892 desired before each use of ``%{check}`` in a ``RUN:`` line:
896 ; REDEFINE: %{cflags} = -triple x86_64-apple-darwin10.6.0 -fopenmp-simd
897 ; REDEFINE: %{fcflags} = -check-prefix=SIMD
900 ; REDEFINE: %{cflags} = -triple x86_64-unknown-linux-gnu -fopenmp-simd
901 ; REDEFINE: %{fcflags} = -check-prefix=SIMD
904 ; REDEFINE: %{cflags} = -triple x86_64-apple-darwin10.6.0
905 ; REDEFINE: %{fcflags} = -check-prefix=NO-SIMD
908 ; REDEFINE: %{cflags} = -triple x86_64-unknown-linux-gnu
909 ; REDEFINE: %{fcflags} = -check-prefix=NO-SIMD
912 Besides providing initial values, the initial ``DEFINE:`` directives for the
913 parameter substitutions in the above example serve a second purpose: they
914 establish the substitution order so that both ``%{check}`` and its parameters
915 expand as desired. There's a simple way to remember the required definition
916 order in a test file: define a substitution before any substitution that might
919 In general, substitution expansion behaves as follows:
921 - Upon arriving at each ``RUN:`` line, lit expands all substitutions in that
922 ``RUN:`` line using their current values from the substitution list. No
923 substitution expansion is performed immediately at ``DEFINE:`` and
924 ``REDEFINE:`` directives except ``%(line)``, ``%(line+<number>)``, and
925 ``%(line-<number>)``.
926 - When expanding substitutions in a ``RUN:`` line, lit makes only one pass
927 through the substitution list by default. In this case, a substitution must
928 have been inserted earlier in the substitution list than any substitution
929 appearing in its value in order for the latter to expand. (For greater
930 flexibility, you can enable multiple passes through the substitution list by
931 setting `recursiveExpansionLimit`_ in a lit configuration file.)
932 - While lit configuration files can insert anywhere in the substitution list,
933 the insertion behavior of the ``DEFINE:`` and ``REDEFINE:`` directives is
934 specified below and is designed specifically for the use case presented in the
936 - Defining a substitution in terms of itself, whether directly or via other
937 substitutions, should be avoided. It usually produces an infinitely recursive
938 definition that cannot be fully expanded. It does *not* define the
939 substitution in terms of its previous value, even when using ``REDEFINE:``.
941 The relationship between the ``DEFINE:`` and ``REDEFINE:`` directive is
942 analogous to the relationship between a variable declaration and variable
943 assignment in many programming languages:
945 - ``DEFINE: %{name} = value``
947 This directive assigns the specified value to a new substitution whose
948 pattern is ``%{name}``, or it reports an error if there is already a
949 substitution whose pattern contains ``%{name}`` because that could produce
950 confusing expansions (e.g., a lit configuration file might define a
951 substitution with the pattern ``%{name}\[0\]``). The new substitution is
952 inserted at the start of the substitution list so that it will expand first.
953 Thus, its value can contain any substitution previously defined, whether in
954 the same test file or in a lit configuration file, and both will expand.
956 - ``REDEFINE: %{name} = value``
958 This directive assigns the specified value to an existing substitution whose
959 pattern is ``%{name}``, or it reports an error if there are no substitutions
960 with that pattern or if there are multiple substitutions whose patterns
961 contain ``%{name}``. The substitution's current position in the substitution
962 list does not change so that expansion order relative to other existing
963 substitutions is preserved.
965 The following properties apply to both the ``DEFINE:`` and ``REDEFINE:``
968 - **Substitution name**: In the directive, whitespace immediately before or
969 after ``%{name}`` is optional and discarded. ``%{name}`` must start with
970 ``%{``, it must end with ``}``, and the rest must start with a letter or
971 underscore and contain only alphanumeric characters, hyphens, underscores, and
972 colons. This syntax has a few advantages:
974 - It is impossible for ``%{name}`` to contain sequences that are special in
975 python's ``re.sub`` patterns. Otherwise, attempting to specify
976 ``%{name}`` as a substitution pattern in a lit configuration file could
977 produce confusing expansions.
978 - The braces help avoid the possibility that another substitution's pattern
979 will match part of ``%{name}`` or vice-versa, producing confusing
980 expansions. However, the patterns of substitutions defined by lit
981 configuration files and by lit itself are not restricted to this form, so
982 overlaps are still theoretically possible.
984 - **Substitution value**: The value includes all text from the first
985 non-whitespace character after ``=`` to the last non-whitespace character. If
986 there is no non-whitespace character after ``=``, the value is the empty
987 string. Escape sequences that can appear in python ``re.sub`` replacement
988 strings are treated as plain text in the value.
989 - **Line continuations**: If the last non-whitespace character on the line after
990 ``:`` is ``\``, then the next directive must use the same directive keyword
991 (e.g., ``DEFINE:``) , and it is an error if there is no additional directive.
992 That directive serves as a continuation. That is, before following the rules
993 above to parse the text after ``:`` in either directive, lit joins that text
994 together to form a single directive, replaces the ``\`` with a single space,
995 and removes any other whitespace that is now adjacent to that space. A
996 continuation can be continued in the same manner. A continuation containing
997 only whitespace after its ``:`` is an error.
999 .. _recursiveExpansionLimit:
1001 **recursiveExpansionLimit:**
1003 As described in the previous section, when expanding substitutions in a ``RUN:``
1004 line, lit makes only one pass through the substitution list by default. Thus,
1005 if substitutions are not defined in the proper order, some will remain in the
1006 ``RUN:`` line unexpanded. For example, the following directives refer to
1007 ``%{inner}`` within ``%{outer}`` but do not define ``%{inner}`` until after
1010 .. code-block:: llvm
1012 ; By default, this definition order does not enable full expansion.
1014 ; DEFINE: %{outer} = %{inner}
1015 ; DEFINE: %{inner} = expanded
1017 ; RUN: echo '%{outer}'
1019 ``DEFINE:`` inserts substitutions at the start of the substitution list, so
1020 ``%{inner}`` expands first but has no effect because the original ``RUN:`` line
1021 does not contain ``%{inner}``. Next, ``%{outer}`` expands, and the output of
1022 the ``echo`` command becomes:
1024 .. code-block:: shell
1028 Of course, one way to fix this simple case is to reverse the definitions of
1029 ``%{outer}`` and ``%{inner}``. However, if a test has a complex set of
1030 substitutions that can all reference each other, there might not exist a
1031 sufficient substitution order.
1033 To address such use cases, lit configuration files support
1034 ``config.recursiveExpansionLimit``, which can be set to a non-negative integer
1035 to specify the maximum number of passes through the substitution list. Thus, in
1036 the above example, setting the limit to 2 would cause lit to make a second pass
1037 that expands ``%{inner}`` in the ``RUN:`` line, and the output from the ``echo``
1038 command when then be:
1040 .. code-block:: shell
1044 To improve performance, lit will stop making passes when it notices the ``RUN:``
1045 line has stopped changing. In the above example, setting the limit higher than
1048 To facilitate debugging, after reaching the limit, lit will make one extra pass
1049 and report an error if the ``RUN:`` line changes again. In the above example,
1050 setting the limit to 1 will thus cause lit to report an error instead of
1051 producing incorrect output.
1056 The llvm lit configuration allows to customize some things with user options:
1058 ``llc``, ``opt``, ...
1059 Substitute the respective llvm tool name with a custom command line. This
1060 allows to specify custom paths and default arguments for these tools.
1063 % llvm-lit "-Dllc=llc -verify-machineinstrs"
1066 Enable the execution of long running tests.
1068 ``llvm_site_config``
1069 Load the specified lit configuration instead of the default one.
1075 To make RUN line writing easier, there are several helper programs. These
1076 helpers are in the PATH when running tests, so you can just call them using
1077 their name. For example:
1080 This program runs its arguments and then inverts the result code from it.
1081 Zero result codes become 1. Non-zero result codes become 0.
1083 To make the output more useful, :program:`lit` will scan
1084 the lines of the test case for ones that contain a pattern that matches
1085 ``PR[0-9]+``. This is the syntax for specifying a PR (Problem Report) number
1086 that is related to the test case. The number after "PR" specifies the
1087 LLVM Bugzilla number. When a PR number is specified, it will be used in
1088 the pass/fail reporting. This is useful to quickly get some context when
1091 Finally, any line that contains "END." will cause the special
1092 interpretation of lines to terminate. This is generally done right after
1093 the last RUN: line. This has two side effects:
1095 (a) it prevents special interpretation of lines that are part of the test
1096 program, not the instructions to the test case, and
1098 (b) it speeds things up for really big test cases by avoiding
1099 interpretation of the remainder of the file.