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