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.6 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 It is easy to write a fragile test that would fail spuriously if the tool being
440 tested outputs a full path to the input file. For example, :program:`opt` by
441 default outputs a ``ModuleID``:
443 .. code-block:: console
446 define i32 @main() nounwind {
450 $ opt -S /path/to/example.ll
451 ; ModuleID = '/path/to/example.ll'
453 define i32 @main() nounwind {
457 ``ModuleID`` can unexpectedly match against ``CHECK`` lines. For example:
461 ; RUN: opt -S %s | FileCheck
463 define i32 @main() nounwind {
468 This test will fail if placed into a ``download`` directory.
470 To make your tests robust, always use ``opt ... < %s`` in the RUN line.
471 :program:`opt` does not output a ``ModuleID`` when input comes from stdin.
473 Platform-Specific Tests
474 -----------------------
476 Whenever adding tests that require the knowledge of a specific platform,
477 either related to code generated, specific output or back-end features,
478 you must make sure to isolate the features, so that buildbots that
479 run on different architectures (and don't even compile all back-ends),
482 The first problem is to check for target-specific output, for example sizes
483 of structures, paths and architecture names, for example:
485 * Tests containing Windows paths will fail on Linux and vice-versa.
486 * Tests that check for ``x86_64`` somewhere in the text will fail anywhere else.
487 * Tests where the debug information calculates the size of types and structures.
489 Also, if the test rely on any behaviour that is coded in any back-end, it must
490 go in its own directory. So, for instance, code generator tests for ARM go
491 into ``test/CodeGen/ARM`` and so on. Those directories contain a special
492 ``lit`` configuration file that ensure all tests in that directory will
493 only run if a specific back-end is compiled and available.
495 For instance, on ``test/CodeGen/ARM``, the ``lit.local.cfg`` is:
497 .. code-block:: python
499 config.suffixes = ['.ll', '.c', '.cpp', '.test']
500 if not 'ARM' in config.root.targets:
501 config.unsupported = True
503 Other platform-specific tests are those that depend on a specific feature
504 of a specific sub-architecture, for example only to Intel chips that support ``AVX2``.
506 For instance, ``test/CodeGen/X86/psubus.ll`` tests three sub-architecture
511 ; RUN: llc -mcpu=core2 < %s | FileCheck %s -check-prefix=SSE2
512 ; RUN: llc -mcpu=corei7-avx < %s | FileCheck %s -check-prefix=AVX1
513 ; RUN: llc -mcpu=core-avx2 < %s | FileCheck %s -check-prefix=AVX2
515 And the checks are different:
520 ; SSE2: psubusw LCPI0_0(%rip), %xmm0
522 ; AVX1: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0
524 ; AVX2: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0
526 So, if you're testing for a behaviour that you know is platform-specific or
527 depends on special features of sub-architectures, you must add the specific
528 triple, test with the specific FileCheck and put it into the specific
529 directory that will filter out all other architectures.
532 Constraining test execution
533 ---------------------------
535 Some tests can be run only in specific configurations, such as
536 with debug builds or on particular platforms. Use ``REQUIRES``
537 and ``UNSUPPORTED`` to control when the test is enabled.
539 Some tests are expected to fail. For example, there may be a known bug
540 that the test detect. Use ``XFAIL`` to mark a test as an expected failure.
541 An ``XFAIL`` test will be successful if its execution fails, and
542 will be a failure if its execution succeeds.
546 ; This test will be only enabled in the build with asserts.
548 ; This test is disabled when running on Linux.
549 ; UNSUPPORTED: system-linux
550 ; This test is expected to fail when targeting PowerPC.
551 ; XFAIL: target=powerpc{{.*}}
553 ``REQUIRES`` and ``UNSUPPORTED`` and ``XFAIL`` all accept a comma-separated
554 list of boolean expressions. The values in each expression may be:
556 - Features added to ``config.available_features`` by configuration files such as ``lit.cfg``.
557 String comparison of features is case-sensitive. Furthermore, a boolean expression can
558 contain any Python regular expression enclosed in ``{{ }}``, in which case the boolean
559 expression is satisfied if any feature matches the regular expression. Regular
560 expressions can appear inside an identifier, so for example ``he{{l+}}o`` would match
561 ``helo``, ``hello``, ``helllo``, and so on.
562 - The default target triple, preceded by the string ``target=`` (for example,
563 ``target=x86_64-pc-windows-msvc``). Typically regular expressions are used
564 to match parts of the triple (for example, ``target={{.*}}-windows{{.*}}``
565 to match any Windows target triple).
567 | ``REQUIRES`` enables the test if all expressions are true.
568 | ``UNSUPPORTED`` disables the test if any expression is true.
569 | ``XFAIL`` expects the test to fail if any expression is true.
571 As a special case, ``XFAIL: *`` is expected to fail everywhere.
575 ; This test is disabled when running on Windows,
576 ; and is disabled when targeting Linux, except for Android Linux.
577 ; UNSUPPORTED: system-windows, target={{.*linux.*}} && !target={{.*android.*}}
578 ; This test is expected to fail when targeting PowerPC or running on Darwin.
579 ; XFAIL: target=powerpc{{.*}}, system-darwin
582 Tips for writing constraints
583 ----------------------------
585 **``REQUIRES`` and ``UNSUPPORTED``**
587 These are logical inverses. In principle, ``UNSUPPORTED`` isn't absolutely
588 necessary (the logical negation could be used with ``REQUIRES`` to get
589 exactly the same effect), but it can make these clauses easier to read and
590 understand. Generally, people use ``REQUIRES`` to state things that the test
591 depends on to operate correctly, and ``UNSUPPORTED`` to exclude cases where
592 the test is expected never to work.
594 **``UNSUPPORTED`` and ``XFAIL``**
596 Both of these indicate that the test isn't expected to work; however, they
597 have different effects. ``UNSUPPORTED`` causes the test to be skipped;
598 this saves execution time, but then you'll never know whether the test
599 actually would start working. Conversely, ``XFAIL`` actually runs the test
600 but expects a failure output, taking extra execution time but alerting you
601 if/when the test begins to behave correctly (an XPASS test result). You
602 need to decide which is more appropriate in each case.
604 **Using ``target=...``**
606 Checking the target triple can be tricky; it's easy to mis-specify. For
607 example, ``target=mips{{.*}}`` will match not only mips, but also mipsel,
608 mips64, and mips64el. ``target={{.*}}-linux-gnu`` will match
609 x86_64-unknown-linux-gnu, but not armv8l-unknown-linux-gnueabihf.
610 Prefer to use hyphens to delimit triple components (``target=mips-{{.*}}``)
611 and it's generally a good idea to use a trailing wildcard to allow for
614 Also, it's generally better to write regular expressions that use entire
615 triple components, than to do something clever to shorten them. For
616 example, to match both freebsd and netbsd in an expression, you could write
617 ``target={{.*(free|net)bsd.*}}`` and that would work. However, it would
618 prevent a ``grep freebsd`` from finding this test. Better to use:
619 ``target={{.+-freebsd.*}} || target={{.+-netbsd.*}}``
625 Besides replacing LLVM tool names the following substitutions are performed in
629 Replaced by a single ``%``. This allows escaping other substitutions.
632 File path to the test case's source. This is suitable for passing on the
633 command line as the input to an LLVM tool.
635 Example: ``/home/user/llvm/test/MC/ELF/foo_test.s``
638 Directory path to the test case's source.
640 Example: ``/home/user/llvm/test/MC/ELF``
643 File path to a temporary file name that could be used for this test case.
644 The file name won't conflict with other test cases. You can append to it
645 if you need multiple temporaries. This is useful as the destination of
646 some redirected output.
648 Example: ``/home/user/llvm.build/test/MC/ELF/Output/foo_test.s.tmp``
651 Directory of ``%t``. Deprecated. Shouldn't be used, because it can be easily
652 misused and cause race conditions between tests.
654 Use ``rm -rf %t && mkdir %t`` instead if a temporary directory is necessary.
656 Example: ``/home/user/llvm.build/test/MC/ELF/Output``
660 Expands to the path separator, i.e. ``:`` (or ``;`` on Windows).
663 Expands to the root component of file system paths for the source directory,
664 i.e. ``/`` on Unix systems or ``C:\`` (or another drive) on Windows.
667 Expands to the root component of file system paths for the test's temporary
668 directory, i.e. ``/`` on Unix systems or ``C:\`` (or another drive) on
672 Expands to the file system separator, i.e. ``/`` or ``\`` on Windows.
674 ``%/s, %/S, %/t, %/T``
676 Act like the corresponding substitution above but replace any ``\``
677 character with a ``/``. This is useful to normalize path separators.
679 Example: ``%s: C:\Desktop Files/foo_test.s.tmp``
681 Example: ``%/s: C:/Desktop Files/foo_test.s.tmp``
683 ``%{s:real}, %{S:real}, %{t:real}, %{T:real}``
684 ``%{/s:real}, %{/S:real}, %{/t:real}, %{/T:real}``
686 Act like the corresponding substitution, including with ``/``, but use
687 the real path by expanding all symbolic links and substitute drives.
689 Example: ``%s: S:\foo_test.s.tmp``
691 Example: ``%{/s:real}: C:/SDrive/foo_test.s.tmp``
693 ``%:s, %:S, %:t, %:T``
695 Act like the corresponding substitution above but remove colons at
696 the beginning of Windows paths. This is useful to allow concatenation
697 of absolute paths on Windows to produce a legal path.
699 Example: ``%s: C:\Desktop Files\foo_test.s.tmp``
701 Example: ``%:s: C\Desktop Files\foo_test.s.tmp``
705 Some error messages may be substituted to allow different spellings
706 based on the host platform.
708 The following error codes are currently supported:
709 ENOENT, EISDIR, EINVAL, EACCES.
711 Example: ``Linux %errc_ENOENT: No such file or directory``
713 Example: ``Windows %errc_ENOENT: no such file or directory``
715 ``%if feature %{<if branch>%} %else %{<else branch>%}``
717 Conditional substitution: if ``feature`` is available it expands to
718 ``<if branch>``, otherwise it expands to ``<else branch>``.
719 ``%else %{<else branch>%}`` is optional and treated like ``%else %{%}``
722 ``%(line)``, ``%(line+<number>)``, ``%(line-<number>)``
724 The number of the line where this substitution is used, with an
725 optional integer offset. These expand only if they appear
726 immediately in ``RUN:``, ``DEFINE:``, and ``REDEFINE:`` directives.
727 Occurrences in substitutions defined elsewhere are never expanded.
728 For example, this can be used in tests with multiple RUN lines,
729 which reference the test file's line numbers.
731 **LLVM-specific substitutions:**
734 The suffix for the host platforms shared library files. This includes the
735 period as the first character.
737 Example: ``.so`` (Linux), ``.dylib`` (macOS), ``.dll`` (Windows)
740 The suffix for the host platforms executable files. This includes the
741 period as the first character.
743 Example: ``.exe`` (Windows), empty on Linux.
745 **Clang-specific substitutions:**
748 Invokes the Clang driver.
751 Invokes the Clang driver for C++.
754 Invokes the CL-compatible Clang driver.
757 Invokes the G++-compatible Clang driver.
760 Invokes the Clang frontend.
762 ``%itanium_abi_triple``, ``%ms_abi_triple``
763 These substitutions can be used to get the current target triple adjusted to
764 the desired ABI. For example, if the test suite is running with the
765 ``i686-pc-win32`` target, ``%itanium_abi_triple`` will expand to
766 ``i686-pc-mingw32``. This allows a test to run with a specific ABI without
767 constraining it to a specific triple.
769 **FileCheck-specific substitutions:**
771 ``%ProtectFileCheckOutput``
772 This should precede a ``FileCheck`` call if and only if the call's textual
773 output affects test results. It's usually easy to tell: just look for
774 redirection or piping of the ``FileCheck`` call's stdout or stderr.
776 .. _Test-specific substitutions:
778 **Test-specific substitutions:**
780 Additional substitutions can be defined as follows:
782 - Lit configuration files (e.g., ``lit.cfg`` or ``lit.local.cfg``) can define
783 substitutions for all tests in a test directory. They do so by extending the
784 substitution list, ``config.substitutions``. Each item in the list is a tuple
785 consisting of a pattern and its replacement, which lit applies using python's
787 - To define substitutions within a single test file, lit supports the
788 ``DEFINE:`` and ``REDEFINE:`` directives, described in detail below. So that
789 they have no effect on other test files, these directives modify a copy of the
790 substitution list that is produced by lit configuration files.
792 For example, the following directives can be inserted into a test file to define
793 ``%{cflags}`` and ``%{fcflags}`` substitutions with empty initial values, which
794 serve as the parameters of another newly defined ``%{check}`` substitution:
798 ; DEFINE: %{cflags} =
799 ; DEFINE: %{fcflags} =
801 ; DEFINE: %{check} = \
802 ; DEFINE: %clang_cc1 -verify -fopenmp -fopenmp-version=51 %{cflags} \
803 ; DEFINE: -emit-llvm -o - %s | \
804 ; DEFINE: FileCheck %{fcflags} %s
806 Alternatively, the above substitutions can be defined in a lit configuration
807 file to be shared with other test files. Either way, the test file can then
808 specify directives like the following to redefine the parameter substitutions as
809 desired before each use of ``%{check}`` in a ``RUN:`` line:
813 ; REDEFINE: %{cflags} = -triple x86_64-apple-darwin10.6.0 -fopenmp-simd
814 ; REDEFINE: %{fcflags} = -check-prefix=SIMD
817 ; REDEFINE: %{cflags} = -triple x86_64-unknown-linux-gnu -fopenmp-simd
818 ; REDEFINE: %{fcflags} = -check-prefix=SIMD
821 ; REDEFINE: %{cflags} = -triple x86_64-apple-darwin10.6.0
822 ; REDEFINE: %{fcflags} = -check-prefix=NO-SIMD
825 ; REDEFINE: %{cflags} = -triple x86_64-unknown-linux-gnu
826 ; REDEFINE: %{fcflags} = -check-prefix=NO-SIMD
829 Besides providing initial values, the initial ``DEFINE:`` directives for the
830 parameter substitutions in the above example serve a second purpose: they
831 establish the substitution order so that both ``%{check}`` and its parameters
832 expand as desired. There's a simple way to remember the required definition
833 order in a test file: define a substitution before any substitution that might
836 In general, substitution expansion behaves as follows:
838 - Upon arriving at each ``RUN:`` line, lit expands all substitutions in that
839 ``RUN:`` line using their current values from the substitution list. No
840 substitution expansion is performed immediately at ``DEFINE:`` and
841 ``REDEFINE:`` directives except ``%(line)``, ``%(line+<number>)``, and
842 ``%(line-<number>)``.
843 - When expanding substitutions in a ``RUN:`` line, lit makes only one pass
844 through the substitution list by default. In this case, a substitution must
845 have been inserted earlier in the substitution list than any substitution
846 appearing in its value in order for the latter to expand. (For greater
847 flexibility, you can enable multiple passes through the substitution list by
848 setting `recursiveExpansionLimit`_ in a lit configuration file.)
849 - While lit configuration files can insert anywhere in the substitution list,
850 the insertion behavior of the ``DEFINE:`` and ``REDEFINE:`` directives is
851 specified below and is designed specifically for the use case presented in the
853 - Defining a substitution in terms of itself, whether directly or via other
854 substitutions, should be avoided. It usually produces an infinitely recursive
855 definition that cannot be fully expanded. It does *not* define the
856 substitution in terms of its previous value, even when using ``REDEFINE:``.
858 The relationship between the ``DEFINE:`` and ``REDEFINE:`` directive is
859 analogous to the relationship between a variable declaration and variable
860 assignment in many programming languages:
862 - ``DEFINE: %{name} = value``
864 This directive assigns the specified value to a new substitution whose
865 pattern is ``%{name}``, or it reports an error if there is already a
866 substitution whose pattern contains ``%{name}`` because that could produce
867 confusing expansions (e.g., a lit configuration file might define a
868 substitution with the pattern ``%{name}\[0\]``). The new substitution is
869 inserted at the start of the substitution list so that it will expand first.
870 Thus, its value can contain any substitution previously defined, whether in
871 the same test file or in a lit configuration file, and both will expand.
873 - ``REDEFINE: %{name} = value``
875 This directive assigns the specified value to an existing substitution whose
876 pattern is ``%{name}``, or it reports an error if there are no substitutions
877 with that pattern or if there are multiple substitutions whose patterns
878 contain ``%{name}``. The substitution's current position in the substitution
879 list does not change so that expansion order relative to other existing
880 substitutions is preserved.
882 The following properties apply to both the ``DEFINE:`` and ``REDEFINE:``
885 - **Substitution name**: In the directive, whitespace immediately before or
886 after ``%{name}`` is optional and discarded. ``%{name}`` must start with
887 ``%{``, it must end with ``}``, and the rest must start with a letter or
888 underscore and contain only alphanumeric characters, hyphens, underscores, and
889 colons. This syntax has a few advantages:
891 - It is impossible for ``%{name}`` to contain sequences that are special in
892 python's ``re.sub`` patterns. Otherwise, attempting to specify
893 ``%{name}`` as a substitution pattern in a lit configuration file could
894 produce confusing expansions.
895 - The braces help avoid the possibility that another substitution's pattern
896 will match part of ``%{name}`` or vice-versa, producing confusing
897 expansions. However, the patterns of substitutions defined by lit
898 configuration files and by lit itself are not restricted to this form, so
899 overlaps are still theoretically possible.
901 - **Substitution value**: The value includes all text from the first
902 non-whitespace character after ``=`` to the last non-whitespace character. If
903 there is no non-whitespace character after ``=``, the value is the empty
904 string. Escape sequences that can appear in python ``re.sub`` replacement
905 strings are treated as plain text in the value.
906 - **Line continuations**: If the last non-whitespace character on the line after
907 ``:`` is ``\``, then the next directive must use the same directive keyword
908 (e.g., ``DEFINE:``) , and it is an error if there is no additional directive.
909 That directive serves as a continuation. That is, before following the rules
910 above to parse the text after ``:`` in either directive, lit joins that text
911 together to form a single directive, replaces the ``\`` with a single space,
912 and removes any other whitespace that is now adjacent to that space. A
913 continuation can be continued in the same manner. A continuation containing
914 only whitespace after its ``:`` is an error.
916 .. _recursiveExpansionLimit:
918 **recursiveExpansionLimit:**
920 As described in the previous section, when expanding substitutions in a ``RUN:``
921 line, lit makes only one pass through the substitution list by default. Thus,
922 if substitutions are not defined in the proper order, some will remain in the
923 ``RUN:`` line unexpanded. For example, the following directives refer to
924 ``%{inner}`` within ``%{outer}`` but do not define ``%{inner}`` until after
929 ; By default, this definition order does not enable full expansion.
931 ; DEFINE: %{outer} = %{inner}
932 ; DEFINE: %{inner} = expanded
934 ; RUN: echo '%{outer}'
936 ``DEFINE:`` inserts substitutions at the start of the substitution list, so
937 ``%{inner}`` expands first but has no effect because the original ``RUN:`` line
938 does not contain ``%{inner}``. Next, ``%{outer}`` expands, and the output of
939 the ``echo`` command becomes:
941 .. code-block:: shell
945 Of course, one way to fix this simple case is to reverse the definitions of
946 ``%{outer}`` and ``%{inner}``. However, if a test has a complex set of
947 substitutions that can all reference each other, there might not exist a
948 sufficient substitution order.
950 To address such use cases, lit configuration files support
951 ``config.recursiveExpansionLimit``, which can be set to a non-negative integer
952 to specify the maximum number of passes through the substitution list. Thus, in
953 the above example, setting the limit to 2 would cause lit to make a second pass
954 that expands ``%{inner}`` in the ``RUN:`` line, and the output from the ``echo``
955 command when then be:
957 .. code-block:: shell
961 To improve performance, lit will stop making passes when it notices the ``RUN:``
962 line has stopped changing. In the above example, setting the limit higher than
965 To facilitate debugging, after reaching the limit, lit will make one extra pass
966 and report an error if the ``RUN:`` line changes again. In the above example,
967 setting the limit to 1 will thus cause lit to report an error instead of
968 producing incorrect output.
973 The llvm lit configuration allows to customize some things with user options:
975 ``llc``, ``opt``, ...
976 Substitute the respective llvm tool name with a custom command line. This
977 allows to specify custom paths and default arguments for these tools.
980 % llvm-lit "-Dllc=llc -verify-machineinstrs"
983 Enable the execution of long running tests.
986 Load the specified lit configuration instead of the default one.
992 To make RUN line writing easier, there are several helper programs. These
993 helpers are in the PATH when running tests, so you can just call them using
994 their name. For example:
997 This program runs its arguments and then inverts the result code from it.
998 Zero result codes become 1. Non-zero result codes become 0.
1000 To make the output more useful, :program:`lit` will scan
1001 the lines of the test case for ones that contain a pattern that matches
1002 ``PR[0-9]+``. This is the syntax for specifying a PR (Problem Report) number
1003 that is related to the test case. The number after "PR" specifies the
1004 LLVM Bugzilla number. When a PR number is specified, it will be used in
1005 the pass/fail reporting. This is useful to quickly get some context when
1008 Finally, any line that contains "END." will cause the special
1009 interpretation of lines to terminate. This is generally done right after
1010 the last RUN: line. This has two side effects:
1012 (a) it prevents special interpretation of lines that are part of the test
1013 program, not the instructions to the test case, and
1015 (b) it speeds things up for really big test cases by avoiding
1016 interpretation of the remainder of the file.