gitlab-ci: Add ppc64el and s390x cross-build jobs
[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
36 def arg_parser():
37 parser = argparse.ArgumentParser()
38 parser.add_argument(
39 '--test-runner',
40 required=True,
41 help='The glsl_test binary.')
42 return parser.parse_args()
43
44
45 def compare(actual, expected):
46 """Compare the s-expresions and return a diff if they are different."""
47 actual = sexps.sort_decls(sexps.parse_sexp(actual))
48 expected = sexps.sort_decls(sexps.parse_sexp(expected))
49
50 if actual == expected:
51 return None
52
53 actual = sexps.sexp_to_string(actual)
54 expected = sexps.sexp_to_string(expected)
55
56 return difflib.unified_diff(expected.splitlines(), actual.splitlines())
57
58
59 def get_test_runner(runner):
60 """Wrap the test runner in the exe wrapper if necessary."""
61 wrapper = os.environ.get('MESON_EXE_WRAPPER', None)
62 if wrapper is None:
63 return [runner]
64 return [wrapper, runner]
65
66
67 def main():
68 """Generate each test and report pass or fail."""
69 args = arg_parser()
70
71 total = 0
72 passes = 0
73
74 runner = get_test_runner(args.test_runner)
75
76 for gen in lower_jump_cases.CASES:
77 for name, opt, source, expected in gen():
78 total += 1
79 print('{}: '.format(name), end='')
80 proc = subprocess.Popen(
81 runner + ['optpass', '--quiet', '--input-ir', opt],
82 stdout=subprocess.PIPE,
83 stderr=subprocess.PIPE,
84 stdin=subprocess.PIPE)
85 out, err = proc.communicate(source.encode('utf-8'))
86 out = out.decode('utf-8')
87 err = err.decode('utf-8')
88 if err:
89 print('FAIL')
90 print('Unexpected output on stderr: {}'.format(err),
91 file=sys.stdout)
92 continue
93
94 result = compare(out, expected)
95 if result is not None:
96 print('FAIL')
97 for l in result:
98 print(l, file=sys.stderr)
99 else:
100 print('PASS')
101 passes += 1
102
103 print('{}/{} tests returned correct results'.format(passes, total))
104 exit(0 if passes == total else 1)
105
106
107 if __name__ == '__main__':
108 try:
109 main()
110 except OSError as e:
111 if e.errno == errno.ENOEXEC:
112 print('Skipping due to lack of exe_wrapper.', file=sys.stderr)
113 sys.exit(77)
114 else:
115 raise