• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1Writing and running NIR unit tests
2==================================
3
4NIR uses `gtest <https://github.com/google/googletest>`__
5for unit testing lowering and optimization passes. Tests
6should declare a class to use for all test cases:
7
8.. code:: c++
9
10   class nir_my_pass_test : public nir_test {
11   protected:
12      nir_my_pass_test();
13
14      void run_pass(nir_reference_shader expected);
15
16      /* Resources used by the test */
17   }
18
19   nir_my_pass_test::nir_my_pass_test()
20      : nir_test::nir_test("nir_my_pass_test")
21   {
22      /* Create resources used by the test */
23   }
24
25   void nir_my_pass_test::run_pass(nir_reference_shader expected)
26   {
27      nir_validate_shader(b->shader, "before nir_my_pass");
28      NIR_PASS(_, b->shader, nir_my_pass);
29      check_nir_string(expected);
30   }
31
32With this setup, the individual test cases can use ``nir_builder``
33to initialize a shader, run the pass that should be tested on it
34and compare it against a string, containing the expected pass
35output:
36
37.. code:: c++
38
39   TEST_F(nir_my_pass_test, basic)
40   {
41      run_pass(NIR_REFERENCE_SHADER(R"(
42         shader: MESA_SHADER_COMPUTE
43         name: nir_my_pass_test
44         workgroup_size: 1, 1, 1
45         subgroup_size: 0
46         decl_function main () (entrypoint)
47
48         impl main {
49             block b0:  // preds:
50         }
51      )"));
52   }
53
54The expected string can be managed using the
55``bin/nir-test-runner.py`` script which builds and runs NIR
56unit tests. The output of the tests are compared against the
57expectations. The runner can then optionally update the
58expectations.
59
60.. code::
61
62   user@distro:~/mesa$ python bin/nir-test-runner.py -Bbuild
63   INFO: autodetecting backend as ninja
64   INFO: calculating backend command to run: /usr/bin/ninja -C /home/konstantin/dev/mesa/build
65   ninja: Entering directory `/home/konstantin/dev/mesa/build'
66   [1/1] Generating src/git_sha1.h with a custom command
67   diff --git a/src/compiler/nir/tests/opt_loop_tests.cpp b/src/compiler/nir/tests/opt_loop_tests.cpp
68   index 05d3c6357c6..0d4810c5f85 100644
69   --- a/src/compiler/nir/tests/opt_loop_tests.cpp
70   +++ b/src/compiler/nir/tests/opt_loop_tests.cpp
71   @@ -136,6 +136,7 @@ TEST_F(nir_opt_loop_test, opt_loop_merge_terminators_break_in_then)
72       check_nir_string(NIR_REFERENCE_SHADER(R"(
73          shader: MESA_SHADER_FRAGMENT
74          name: nir_opt_loop_test
75   +      subgroup_size: 0
76          decl_var shader_in INTERP_MODE_SMOOTH none int in (VARYING_SLOT_POS.x, 0, 0)
77          decl_var shader_out INTERP_MODE_NONE none int out (FRAG_RESULT_DEPTH.x, 0, 0)
78          decl_var ubo INTERP_MODE_NONE none int ubo1 (0, 0, 0)
79
80   Apply the changes listed above? [Y/n]y
81