LLVM Testing Infrastructure Guide#

Overview#

This document is the reference manual for the LLVM testing infrastructure. It documents the structure of the LLVM testing infrastructure, the tools needed to use it, and how to add and run tests.

Requirements#

In order to use the LLVM testing infrastructure, you will need all of the software required to build LLVM, as well as Python 3.8 or later.

LLVM Testing Infrastructure Organization#

The LLVM testing infrastructure contains three major categories of tests: unit tests, regression tests, and whole programs. The unit tests and regression tests are contained inside the LLVM repository itself under llvm/unittests and llvm/test respectively and are expected to always pass. They should be run before every commit.

The whole-program tests are referred to as the “LLVM test suite” (or “test-suite”) and are in the test-suite repository on GitHub. For historical reasons, these tests are also referred to as the “nightly tests” in places, which is less ambiguous than “test-suite” and remains in use although we run them much more often than nightly.

Unit tests#

Unit tests are written using Google Test and Google Mock and are located in the llvm/unittests directory. In general, unit tests are reserved for targeting the support library and other generic data structure. We prefer relying on regression tests for testing transformations and analysis on the IR.

Regression tests#

The regression tests are small pieces of code that test a specific feature of LLVM or trigger a specific bug in LLVM. The language they are written in depends on the part of LLVM being tested. These tests are driven by the Lit testing tool (which is part of LLVM), and are located in the llvm/test directory.

Typically, when a bug is found in LLVM, a regression test containing just enough code to reproduce the problem should be written and placed somewhere underneath this directory. For example, it can be a small piece of LLVM IR distilled from an actual application or benchmark.

Testing Analysis#

An analysis is a pass to infer properties on some part of the IR without transforming it. They are tested in general using the same infrastructure as the regression tests, by creating a separate “Printer” pass to consume the analysis result and print it on the standard output in a textual format suitable for FileCheck. See llvm/test/Analysis/BranchProbabilityInfo/loop.ll for an example of such test.

test-suite#

The test suite contains whole programs, which are pieces of code which can be compiled and linked into a stand-alone program that can be executed. These programs are generally written in high-level languages, such as C and C++.

These programs are compiled using a user-specified compiler and set of flags, and then executed to capture the program output and timing information. The output of these programs is compared to a reference output to ensure that the program is being compiled correctly.

In addition to compiling and executing programs, whole-program tests serve as a way of benchmarking LLVM performance, both in terms of the efficiency of the programs generated as well as the speed with which LLVM compiles, optimizes, and generates code.

The test-suite is located in the test-suite repository on GitHub.

See the test-suite Guide for details.

Debugging Information tests#

The test suite contains tests to check the quality of debugging information. The tests are written in C-based languages or in LLVM assembly language.

These tests are compiled and run under a debugger. The debugger output is checked to validate the debugging information. See README.txt in the test suite for more information. This test suite is located in the cross-project-tests/debuginfo-tests directory.

Quick start#

The tests are located in two separate repositories. The unit and regression tests are in the main “llvm”/ directory under the directories llvm/unittests and llvm/test (so you get these tests for free with the main LLVM tree). Use make check-all to run the unit and regression tests after building LLVM.

The test-suite module contains more comprehensive tests including whole C and C++ programs. See the test-suite Guide for details.

Unit and Regression tests#

To run all of the LLVM unit tests, use the check-llvm-unit target:

% make check-llvm-unit

To run all of the LLVM regression tests, use the check-llvm target:

% make check-llvm

In order to get reasonable testing performance, build LLVM and subprojects in release mode, i.e.,

% cmake -DCMAKE_BUILD_TYPE="Release" -DLLVM_ENABLE_ASSERTIONS=On

If you have Clang checked out and built, you can run the LLVM and Clang tests simultaneously using:

% make check-all

To run the tests with Valgrind (Memcheck by default), use the LIT_OPTS make variable to pass the required options to lit. For example, you can use:

% make check LIT_OPTS="-v --vg --vg-leak"

to enable testing with Valgrind and with leak checking enabled.

To run individual tests or subsets of tests, you can use the llvm-lit script which is built as part of LLVM. For example, to run the Integer/BitPacked.ll test by itself, you can run:

% llvm-lit <path to llvm-project>/llvm/test/Integer/BitPacked.ll

Note

The test files are in the llvm-project directory, not the directory you are building LLVM in.

Or you can run a whole folder of tests. To run all of the ARM CodeGen tests:

% llvm-lit <path to llvm-project>/llvm/test/CodeGen/ARM

The regression tests will use the Python psutil module only if installed in a non-user location. Under Linux, install with sudo or within a virtual environment. Under Windows, install Python for all users and then run pip install psutil in an elevated command prompt.

For more information on using the lit tool, see llvm-lit --help or the lit man page.

Debugging Information tests#

To run debugging information tests simply add the cross-project-tests project to your LLVM_ENABLE_PROJECTS define on the cmake command-line.

Regression test structure#

The LLVM regression tests are driven by lit and are located in the llvm/test directory.

This directory contains a large array of small tests that exercise various features of LLVM and to ensure that regressions do not occur. The directory is broken into several subdirectories, each focused on a particular area of LLVM.

Writing new regression tests#

The regression test structure is very simple but does require some information to be set. This information is gathered via cmake and is written to a file, test/lit.site.cfg.py in the build directory. The llvm/test Makefile does this work for you.

In order for the regression tests to work, each directory of tests must have a lit.local.cfg file. lit looks for this file to determine how to run the tests. This file is just Python code and thus is very flexible, but we’ve standardized it for the LLVM regression tests. If you’re adding a directory of tests, just copy lit.local.cfg from another directory to get running. The standard lit.local.cfg simply specifies which files to look in for tests. Any directory that contains only directories does not need the lit.local.cfg file. Read the Lit documentation for more information.

Each test file must contain lines starting with “RUN:” that tell lit how to run it. If there are no RUN lines, lit will issue an error while running a test.

RUN lines are specified in the comments of the test program using the keyword RUN followed by a colon, and lastly the command (pipeline) to execute. Together, these lines form the “script” that lit executes to run the test case. The syntax of the RUN lines is similar to a shell’s syntax for pipelines including I/O redirection and variable substitution. However, even though these lines may look like a shell script, they are not. RUN lines are interpreted by lit. Consequently, the syntax differs from shell in a few ways. You can specify as many RUN lines as needed.

lit performs substitution on each RUN line to replace LLVM tool names with the full paths to the executable built for each tool (in $(LLVM_OBJ_ROOT)/bin). This ensures that lit does not invoke any stray LLVM tools in the user’s path during testing.

Each RUN line is executed on its own, distinct from other lines unless its last character is \. This continuation character causes the RUN line to be concatenated with the next one. In this way, you can build up long pipelines of commands without making huge line lengths. The lines ending in \ are concatenated until a RUN line that doesn’t end in \ is found. This concatenated set of RUN lines then constitutes one execution. lit will substitute variables and arrange for the pipeline to be executed. If any process in the pipeline fails, the entire line (and test case) fails too.

Below is an example of legal RUN lines in a .ll file:

; RUN: llvm-as < %s | llvm-dis > %t1
; RUN: llvm-dis < %s.bc-13 > %t2
; RUN: diff %t1 %t2

As with a Unix shell, the RUN lines permit pipelines and I/O redirection to be used.

There are some quoting rules that you must pay attention to when writing your RUN lines. In general, nothing needs to be quoted. lit won’t strip off any quote characters, so they will get passed to the invoked program. To avoid this use curly braces to tell lit that it should treat everything enclosed as one value.

In general, you should strive to keep your RUN lines as simple as possible, using them only to run tools that generate textual output you can then examine. The recommended way to examine output to figure out if the test passes is using the FileCheck tool. [The usage of grep in RUN lines is deprecated - please do not send or commit patches that use it.]

Put related tests into a single file rather than having a separate file per test. Check if there are files already covering your feature and consider adding your code there instead of creating a new file.

If new tests depend on a separate LLVM pull request, wait at least a day after merging the LLVM PR before merging the test-suite PR. This avoids test failures when the merged test suite runs before the compiler updates.

Generating assertions in regression tests#

Some regression test cases are very large and complex to write/update by hand. In that case, to reduce the manual work, we can use the scripts available in llvm/utils/ to generate the assertions.

For example, to generate assertions in an llc-based test, after adding one or more RUN lines, use:

% llvm/utils/update_llc_test_checks.py --llc-binary build/bin/llc test.ll

This will generate FileCheck assertions, and insert a NOTE: line at the top to indicate that assertions were automatically generated.

If you want to update assertions in an existing test case, pass the -u option which first checks the NOTE: line exists and matches the script name.

Sometimes, a test absolutely depends on hand-written assertions and should not have assertions automatically generated. In that case, add the text NOTE: Do not autogenerate to the first line, and the scripts will skip that test. It is a good idea to explain why generated assertions will not work for the test so future developers will understand what is going on.

These are the most common scripts and their purposes/applications in generating assertions:

update_analyze_test_checks.py
opt -passes='print<cost-model>'

update_cc_test_checks.py
C/C++, or clang/clang++ (IR checks)

update_llc_test_checks.py
llc (assembly checks)

update_mca_test_checks.py
llvm-mca

update_mir_test_checks.py
llc (MIR checks)

update_test_checks.py
opt

update_llubi_test_checks.py
llubi

Precommit workflow for tests#

If the test does not crash, assert, or infinite loop, commit the test with baseline check-lines first. That is, the test will show a miscompile or missing optimization. Add a “TODO” or “FIXME” comment to indicate that something is expected to change in a test.

A follow-up patch with code changes to the compiler will then show check-line differences to the tests, so it is easier to see the effect of the patch. Remove TODO/FIXME comments added in the previous step if a problem is solved.

Baseline tests (no-functional-change or NFC patch) may be pushed to main without pre-commit review if you have commit access.

Best practices for regression tests#

  • Use auto-generated check lines (produced by the scripts mentioned above) whenever feasible.

  • Include comments about what is tested/expected in a particular test. If there are relevant issues in the bug tracker, add references to those bug reports (for example, “See PR999 for more details”).

  • Avoid undefined behavior and poison/undef values unless necessary. For example, do not use patterns like br i1 undef, which are likely to break as a result of future optimizations.

  • Minimize tests by removing unnecessary instructions, metadata, attributes, etc. Tools like llvm-reduce can partially automate this, but usually some manual cleanup is still required.

  • Outside PhaseOrdering tests, only run a minimal set of passes. For example, prefer opt -S -passes=instcombine over opt -S -O3.

  • Avoid unnamed instructions/blocks (such as %0 or 1:), because they may require renumbering on future test modifications. These can be removed by running the test through opt -S -passes=instnamer.

  • Try to give values (including variables, blocks and functions) meaningful names, and avoid retaining complex names generated by the optimization pipeline (such as %foo.0.0.0.0.0.0).

  • Omit target triples and datalayouts when possible. If the test case only exercises the code path of interest with a specific target triple and/or datalayout, then they should be in the test, but otherwise they should be omitted to allow the test to run without requiring a specific backend to be built.

  • Put tests in the right place. Prefer to add additional test cases to an existing file rather than creating a new one. If your test requires a specific target, prefer putting it in a target-specific test subdirectory rather than requiring a specific backend to be built with a REQUIRES: line.

  • Use a minimal number of non-default (CHECK-<prefix>) check prefixes. Avoid setting a check prefix if all FileCheck invocations in the test expect the same output.

Extra files