nir: Implement mul_high lowering for bit sizes other than 32
[mesa.git] / src / compiler / glsl / tests / optimization_test.py
1 # encoding=utf-8
2 # Copyright © 2018 Intel Corporation
3
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
10
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
13
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 # SOFTWARE.
21
22 """Script to generate and run glsl optimization tests."""
23
24 from __future__ import print_function
25 import argparse
26 import difflib
27 import errno
28 import os
29 import subprocess
30 import sys
31
32 import sexps
33 import lower_jump_cases
34
35 # The meson version handles windows paths better, but if it's not available
36 # fall back to shlex
37 try:
38 from meson.mesonlib import split_args
39 except ImportError:
40 from shlex import split as split_args
41
42
43 def arg_parser():
44 parser = argparse.ArgumentParser()
45 parser.add_argument(
46 '--test-runner',
47 required=True,
48 help='The glsl_test binary.')
49 return parser.parse_args()
50
51
52 def compare(actual, expected):
53 """Compare the s-expresions and return a diff if they are different."""
54 actual = sexps.sort_decls(sexps.parse_sexp(actual))
55 expected = sexps.sort_decls(sexps.parse_sexp(expected))
56
57 if actual == expected:
58 return None
59
60 actual = sexps.sexp_to_string(actual)
61 expected = sexps.sexp_to_string(expected)
62
63 return difflib.unified_diff(expected.splitlines(), actual.splitlines())
64
65
66 def get_test_runner(runner):
67 """Wrap the test runner in the exe wrapper if necessary."""
68 wrapper = os.environ.get('MESON_EXE_WRAPPER', None)
69 if wrapper is None:
70 return [runner]
71 return split_args(wrapper) + [runner]
72
73
74 def main():
75 """Generate each test and report pass or fail."""
76 args = arg_parser()
77
78 total = 0
79 passes = 0
80
81 runner = get_test_runner(args.test_runner)
82
83 for gen in lower_jump_cases.CASES:
84 for name, opt, source, expected in gen():
85 total += 1
86 print('{}: '.format(name), end='')
87 proc = subprocess.Popen(
88 runner + ['optpass', '--quiet', '--input-ir', opt],
89 stdout=subprocess.PIPE,
90 stderr=subprocess.PIPE,
91 stdin=subprocess.PIPE)
92 out, err = proc.communicate(source.encode('utf-8'))
93 out = out.decode('utf-8')
94 err = err.decode('utf-8')
95 if err:
96 print('FAIL')
97 print('Unexpected output on stderr: {}'.format(err),
98 file=sys.stdout)
99 continue
100
101 result = compare(out, expected)
102 if result is not None:
103 print('FAIL')
104 for l in result:
105 print(l, file=sys.stderr)
106 else:
107 print('PASS')
108 passes += 1
109
110 print('{}/{} tests returned correct results'.format(passes, total))
111 exit(0 if passes == total else 1)
112
113
114 if __name__ == '__main__':
115 try:
116 main()
117 except OSError as e:
118 if e.errno == errno.ENOEXEC:
119 print('Skipping due to inability to run host binaries', file=sys.stderr)
120 sys.exit(77)
121 raise