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:`` 275lines, the idiomatic place to put them is in a subdirectory ``Inputs``. 276You can then refer to the extra files as ``%S/Inputs/foo.bar``. 277 278For example, consider ``test/Linker/ident.ll``. The directory structure is 279as follows:: 280 281 test/ 282 Linker/ 283 ident.ll 284 Inputs/ 285 ident.a.ll 286 ident.b.ll 287 288For convenience, these are the contents: 289 290.. code-block:: llvm 291 292 ;;;;; ident.ll: 293 294 ; RUN: llvm-link %S/Inputs/ident.a.ll %S/Inputs/ident.b.ll -S | FileCheck %s 295 296 ; Verify that multiple input llvm.ident metadata are linked together. 297 298 ; CHECK-DAG: !llvm.ident = !{!0, !1, !2} 299 ; CHECK-DAG: "Compiler V1" 300 ; CHECK-DAG: "Compiler V2" 301 ; CHECK-DAG: "Compiler V3" 302 303 ;;;;; Inputs/ident.a.ll: 304 305 !llvm.ident = !{!0, !1} 306 !0 = metadata !{metadata !"Compiler V1"} 307 !1 = metadata !{metadata !"Compiler V2"} 308 309 ;;;;; Inputs/ident.b.ll: 310 311 !llvm.ident = !{!0} 312 !0 = metadata !{metadata !"Compiler V3"} 313 314For symmetry reasons, ``ident.ll`` is just a dummy file that doesn't 315actually participate in the test besides holding the ``RUN:`` lines. 316 317.. note:: 318 319 Some existing tests use ``RUN: true`` in extra files instead of just 320 putting the extra files in an ``Inputs/`` directory. This pattern is 321 deprecated. 322 323Fragile tests 324------------- 325 326It is easy to write a fragile test that would fail spuriously if the tool being 327tested outputs a full path to the input file. For example, :program:`opt` by 328default outputs a ``ModuleID``: 329 330.. code-block:: console 331 332 $ cat example.ll 333 define i32 @main() nounwind { 334 ret i32 0 335 } 336 337 $ opt -S /path/to/example.ll 338 ; ModuleID = '/path/to/example.ll' 339 340 define i32 @main() nounwind { 341 ret i32 0 342 } 343 344``ModuleID`` can unexpectedly match against ``CHECK`` lines. For example: 345 346.. code-block:: llvm 347 348 ; RUN: opt -S %s | FileCheck 349 350 define i32 @main() nounwind { 351 ; CHECK-NOT: load 352 ret i32 0 353 } 354 355This test will fail if placed into a ``download`` directory. 356 357To make your tests robust, always use ``opt ... < %s`` in the RUN line. 358:program:`opt` does not output a ``ModuleID`` when input comes from stdin. 359 360Platform-Specific Tests 361----------------------- 362 363Whenever adding tests that require the knowledge of a specific platform, 364either related to code generated, specific output or back-end features, 365you must make sure to isolate the features, so that buildbots that 366run on different architectures (and don't even compile all back-ends), 367don't fail. 368 369The first problem is to check for target-specific output, for example sizes 370of structures, paths and architecture names, for example: 371 372* Tests containing Windows paths will fail on Linux and vice-versa. 373* Tests that check for ``x86_64`` somewhere in the text will fail anywhere else. 374* Tests where the debug information calculates the size of types and structures. 375 376Also, if the test rely on any behaviour that is coded in any back-end, it must 377go in its own directory. So, for instance, code generator tests for ARM go 378into ``test/CodeGen/ARM`` and so on. Those directories contain a special 379``lit`` configuration file that ensure all tests in that directory will 380only run if a specific back-end is compiled and available. 381 382For instance, on ``test/CodeGen/ARM``, the ``lit.local.cfg`` is: 383 384.. code-block:: python 385 386 config.suffixes = ['.ll', '.c', '.cpp', '.test'] 387 if not 'ARM' in config.root.targets: 388 config.unsupported = True 389 390Other platform-specific tests are those that depend on a specific feature 391of a specific sub-architecture, for example only to Intel chips that support ``AVX2``. 392 393For instance, ``test/CodeGen/X86/psubus.ll`` tests three sub-architecture 394variants: 395 396.. code-block:: llvm 397 398 ; RUN: llc -mcpu=core2 < %s | FileCheck %s -check-prefix=SSE2 399 ; RUN: llc -mcpu=corei7-avx < %s | FileCheck %s -check-prefix=AVX1 400 ; RUN: llc -mcpu=core-avx2 < %s | FileCheck %s -check-prefix=AVX2 401 402And the checks are different: 403 404.. code-block:: llvm 405 406 ; SSE2: @test1 407 ; SSE2: psubusw LCPI0_0(%rip), %xmm0 408 ; AVX1: @test1 409 ; AVX1: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0 410 ; AVX2: @test1 411 ; AVX2: vpsubusw LCPI0_0(%rip), %xmm0, %xmm0 412 413So, if you're testing for a behaviour that you know is platform-specific or 414depends on special features of sub-architectures, you must add the specific 415triple, test with the specific FileCheck and put it into the specific 416directory that will filter out all other architectures. 417 418 419Constraining test execution 420--------------------------- 421 422Some tests can be run only in specific configurations, such as 423with debug builds or on particular platforms. Use ``REQUIRES`` 424and ``UNSUPPORTED`` to control when the test is enabled. 425 426Some tests are expected to fail. For example, there may be a known bug 427that the test detect. Use ``XFAIL`` to mark a test as an expected failure. 428An ``XFAIL`` test will be successful if its execution fails, and 429will be a failure if its execution succeeds. 430 431.. code-block:: llvm 432 433 ; This test will be only enabled in the build with asserts. 434 ; REQUIRES: asserts 435 ; This test is disabled on Linux. 436 ; UNSUPPORTED: -linux- 437 ; This test is expected to fail on PowerPC. 438 ; XFAIL: powerpc 439 440``REQUIRES`` and ``UNSUPPORTED`` and ``XFAIL`` all accept a comma-separated 441list of boolean expressions. The values in each expression may be: 442 443- Features added to ``config.available_features`` by 444 configuration files such as ``lit.cfg``. 445- Substrings of the target triple (``UNSUPPORTED`` and ``XFAIL`` only). 446 447| ``REQUIRES`` enables the test if all expressions are true. 448| ``UNSUPPORTED`` disables the test if any expression is true. 449| ``XFAIL`` expects the test to fail if any expression is true. 450 451As a special case, ``XFAIL: *`` is expected to fail everywhere. 452 453.. code-block:: llvm 454 455 ; This test is disabled on Windows, 456 ; and is disabled on Linux, except for Android Linux. 457 ; UNSUPPORTED: windows, linux && !android 458 ; This test is expected to fail on both PowerPC and ARM. 459 ; XFAIL: powerpc || arm 460 461 462Substitutions 463------------- 464 465Besides replacing LLVM tool names the following substitutions are performed in 466RUN lines: 467 468``%%`` 469 Replaced by a single ``%``. This allows escaping other substitutions. 470 471``%s`` 472 File path to the test case's source. This is suitable for passing on the 473 command line as the input to an LLVM tool. 474 475 Example: ``/home/user/llvm/test/MC/ELF/foo_test.s`` 476 477``%S`` 478 Directory path to the test case's source. 479 480 Example: ``/home/user/llvm/test/MC/ELF`` 481 482``%t`` 483 File path to a temporary file name that could be used for this test case. 484 The file name won't conflict with other test cases. You can append to it 485 if you need multiple temporaries. This is useful as the destination of 486 some redirected output. 487 488 Example: ``/home/user/llvm.build/test/MC/ELF/Output/foo_test.s.tmp`` 489 490``%T`` 491 Directory of ``%t``. Deprecated. Shouldn't be used, because it can be easily 492 misused and cause race conditions between tests. 493 494 Use ``rm -rf %t && mkdir %t`` instead if a temporary directory is necessary. 495 496 Example: ``/home/user/llvm.build/test/MC/ELF/Output`` 497 498``%{pathsep}`` 499 500 Expands to the path separator, i.e. ``:`` (or ``;`` on Windows). 501 502``%/s, %/S, %/t, %/T:`` 503 504 Act like the corresponding substitution above but replace any ``\`` 505 character with a ``/``. This is useful to normalize path separators. 506 507 Example: ``%s: C:\Desktop Files/foo_test.s.tmp`` 508 509 Example: ``%/s: C:/Desktop Files/foo_test.s.tmp`` 510 511``%:s, %:S, %:t, %:T:`` 512 513 Act like the corresponding substitution above but remove colons at 514 the beginning of Windows paths. This is useful to allow concatenation 515 of absolute paths on Windows to produce a legal path. 516 517 Example: ``%s: C:\Desktop Files\foo_test.s.tmp`` 518 519 Example: ``%:s: C\Desktop Files\foo_test.s.tmp`` 520 521 522**LLVM-specific substitutions:** 523 524``%shlibext`` 525 The suffix for the host platforms shared library files. This includes the 526 period as the first character. 527 528 Example: ``.so`` (Linux), ``.dylib`` (macOS), ``.dll`` (Windows) 529 530``%exeext`` 531 The suffix for the host platforms executable files. This includes the 532 period as the first character. 533 534 Example: ``.exe`` (Windows), empty on Linux. 535 536``%(line)``, ``%(line+<number>)``, ``%(line-<number>)`` 537 The number of the line where this substitution is used, with an optional 538 integer offset. This can be used in tests with multiple RUN lines, which 539 reference test file's line numbers. 540 541 542**Clang-specific substitutions:** 543 544``%clang`` 545 Invokes the Clang driver. 546 547``%clang_cpp`` 548 Invokes the Clang driver for C++. 549 550``%clang_cl`` 551 Invokes the CL-compatible Clang driver. 552 553``%clangxx`` 554 Invokes the G++-compatible Clang driver. 555 556``%clang_cc1`` 557 Invokes the Clang frontend. 558 559``%itanium_abi_triple``, ``%ms_abi_triple`` 560 These substitutions can be used to get the current target triple adjusted to 561 the desired ABI. For example, if the test suite is running with the 562 ``i686-pc-win32`` target, ``%itanium_abi_triple`` will expand to 563 ``i686-pc-mingw32``. This allows a test to run with a specific ABI without 564 constraining it to a specific triple. 565 566**FileCheck-specific substitutions:** 567 568``%ProtectFileCheckOutput`` 569 This should precede a ``FileCheck`` call if and only if the call's textual 570 output affects test results. It's usually easy to tell: just look for 571 redirection or piping of the ``FileCheck`` call's stdout or stderr. 572 573To add more substitutions, look at ``test/lit.cfg`` or ``lit.local.cfg``. 574 575 576Options 577------- 578 579The llvm lit configuration allows to customize some things with user options: 580 581``llc``, ``opt``, ... 582 Substitute the respective llvm tool name with a custom command line. This 583 allows to specify custom paths and default arguments for these tools. 584 Example: 585 586 % llvm-lit "-Dllc=llc -verify-machineinstrs" 587 588``run_long_tests`` 589 Enable the execution of long running tests. 590 591``llvm_site_config`` 592 Load the specified lit configuration instead of the default one. 593 594 595Other Features 596-------------- 597 598To make RUN line writing easier, there are several helper programs. These 599helpers are in the PATH when running tests, so you can just call them using 600their name. For example: 601 602``not`` 603 This program runs its arguments and then inverts the result code from it. 604 Zero result codes become 1. Non-zero result codes become 0. 605 606To make the output more useful, :program:`lit` will scan 607the lines of the test case for ones that contain a pattern that matches 608``PR[0-9]+``. This is the syntax for specifying a PR (Problem Report) number 609that is related to the test case. The number after "PR" specifies the 610LLVM Bugzilla number. When a PR number is specified, it will be used in 611the pass/fail reporting. This is useful to quickly get some context when 612a test fails. 613 614Finally, any line that contains "END." will cause the special 615interpretation of lines to terminate. This is generally done right after 616the last RUN: line. This has two side effects: 617 618(a) it prevents special interpretation of lines that are part of the test 619 program, not the instructions to the test case, and 620 621(b) it speeds things up for really big test cases by avoiding 622 interpretation of the remainder of the file. 623