TestingGuide.rst 20 KB

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