xref: /aosp_15_r20/external/mesa3d/src/amd/compiler/tests/check_output.py (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1# Copyright (c) 2020 Valve Corporation
2#
3# SPDX-License-Identifier: MIT
4
5import re
6import sys
7import os.path
8import struct
9import string
10import copy
11from math import floor
12
13if os.isatty(sys.stdout.fileno()):
14    set_red = "\033[31m"
15    set_green = "\033[1;32m"
16    set_normal = "\033[0m"
17else:
18    set_red = ''
19    set_green = ''
20    set_normal = ''
21
22initial_code = '''
23import re
24
25def insert_code(code):
26    insert_queue.append(CodeCheck(code, current_position))
27
28def insert_pattern(pattern):
29    insert_queue.append(PatternCheck(pattern, False, current_position))
30
31def vector_gpr(prefix, name, size, align):
32    insert_code(f'{name} = {name}0')
33    for i in range(size):
34        insert_code(f'{name}{i} = {name}0 + {i}')
35    insert_code(f'success = {name}0 + {size - 1} == {name}{size - 1}')
36    insert_code(f'success = {name}0 % {align} == 0')
37    return f'{prefix}[#{name}0:#{name}{size - 1}]'
38
39def sgpr_vector(name, size, align):
40    return vector_gpr('s', name, size, align)
41
42funcs.update({
43    's64': lambda name: vector_gpr('s', name, 2, 2),
44    's96': lambda name: vector_gpr('s', name, 3, 2),
45    's128': lambda name: vector_gpr('s', name, 4, 4),
46    's256': lambda name: vector_gpr('s', name, 8, 4),
47    's512': lambda name: vector_gpr('s', name, 16, 4),
48})
49for i in range(2, 14):
50    funcs['v%d' % (i * 32)] = lambda name: vector_gpr('v', name, i, 1)
51
52def _match_func(names):
53    for name in names.split(' '):
54        insert_code(f'funcs["{name}"] = lambda _: {name}')
55    return ' '.join(f'${name}' for name in names.split(' '))
56
57funcs['match_func'] = _match_func
58
59def search_re(pattern):
60    global success
61    success = re.search(pattern, output.read_line()) != None and success
62
63'''
64
65class Check:
66    def __init__(self, data, position):
67        self.data = data.rstrip()
68        self.position = position
69
70    def run(self, state):
71        pass
72
73class CodeCheck(Check):
74    def run(self, state):
75        indent = 0
76        first_line = [l for l in self.data.split('\n') if l.strip() != ''][0]
77        indent_amount = len(first_line) - len(first_line.lstrip())
78        indent = first_line[:indent_amount]
79        new_lines = []
80        for line in self.data.split('\n'):
81            if line.strip() == '':
82                new_lines.append('')
83                continue
84            if line[:indent_amount] != indent:
85                state.result.log += 'unexpected indent in code check:\n'
86                state.result.log += self.data + '\n'
87                return False
88            new_lines.append(line[indent_amount:])
89        code = '\n'.join(new_lines)
90
91        try:
92            exec(code, state.g)
93            state.result.log += state.g['log']
94            state.g['log'] = ''
95        except BaseException as e:
96            state.result.log += 'code check at %s raised exception:\n' % self.position
97            state.result.log += code + '\n'
98            state.result.log += str(e)
99            return False
100        if not state.g['success']:
101            state.result.log += 'code check at %s failed:\n' % self.position
102            state.result.log += code + '\n'
103            return False
104        return True
105
106class StringStream:
107    class Pos:
108        def __init__(self):
109            self.line = 1
110            self.column = 1
111
112    def __init__(self, data, name):
113        self.name = name
114        self.data = data
115        self.offset = 0
116        self.pos = StringStream.Pos()
117
118    def reset(self):
119        self.offset = 0
120        self.pos = StringStream.Pos()
121
122    def peek(self, num=1):
123        return self.data[self.offset:self.offset+num]
124
125    def peek_test(self, chars):
126        c = self.peek(1)
127        return c != '' and c in chars
128
129    def read(self, num=4294967296):
130        res = self.peek(num)
131        self.offset += len(res)
132        for c in res:
133            if c == '\n':
134                self.pos.line += 1
135                self.pos.column = 1
136            else:
137                self.pos.column += 1
138        return res
139
140    def get_line(self, num):
141        return self.data.split('\n')[num - 1].rstrip()
142
143    def read_line(self):
144        line = ''
145        while self.peek(1) not in ['\n', '']:
146            line += self.read(1)
147        self.read(1)
148        return line
149
150    def skip_whitespace(self, inc_line):
151        chars = [' ', '\t'] + (['\n'] if inc_line else [])
152        while self.peek(1) in chars:
153            self.read(1)
154
155    def get_number(self):
156        num = ''
157        while self.peek() in string.digits:
158            num += self.read(1)
159        return num
160
161    def check_identifier(self):
162        return self.peek_test(string.ascii_letters + '_')
163
164    def get_identifier(self):
165        res = ''
166        if self.check_identifier():
167            while self.peek_test(string.ascii_letters + string.digits + '_'):
168                res += self.read(1)
169        return res
170
171def format_error_lines(at, line_num, column_num, ctx, line):
172    pred = '%s line %d, column %d of %s: "' % (at, line_num, column_num, ctx)
173    return [pred + line + '"',
174            '-' * (column_num - 1 + len(pred)) + '^']
175
176class MatchResult:
177    def __init__(self, pattern):
178        self.success = True
179        self.func_res = None
180        self.pattern = pattern
181        self.pattern_pos = StringStream.Pos()
182        self.output_pos = StringStream.Pos()
183        self.fail_message = ''
184
185    def set_pos(self, pattern, output):
186        self.pattern_pos.line = pattern.pos.line
187        self.pattern_pos.column = pattern.pos.column
188        self.output_pos.line = output.pos.line
189        self.output_pos.column = output.pos.column
190
191    def fail(self, msg):
192        self.success = False
193        self.fail_message = msg
194
195    def format_pattern_pos(self):
196        pat_pos = self.pattern_pos
197        pat_line = self.pattern.get_line(pat_pos.line)
198        res = format_error_lines('at', pat_pos.line, pat_pos.column, 'pattern', pat_line)
199        func_res = self.func_res
200        while func_res:
201            pat_pos = func_res.pattern_pos
202            pat_line = func_res.pattern.get_line(pat_pos.line)
203            res += format_error_lines('in', pat_pos.line, pat_pos.column, func_res.pattern.name, pat_line)
204            func_res = func_res.func_res
205        return '\n'.join(res)
206
207def do_match(g, pattern, output, skip_lines, in_func=False):
208    assert(not in_func or not skip_lines)
209
210    if not in_func:
211        output.skip_whitespace(False)
212    pattern.skip_whitespace(False)
213
214    old_g = copy.copy(g)
215    old_g_keys = list(g.keys())
216    res = MatchResult(pattern)
217    escape = False
218    while True:
219        res.set_pos(pattern, output)
220
221        c = pattern.read(1)
222        fail = False
223        if c == '':
224            break
225        elif output.peek() == '':
226            res.fail('unexpected end of output')
227        elif c == '\\':
228            escape = True
229            continue
230        elif c == '\n':
231            old_line = output.pos.line
232            output.skip_whitespace(True)
233            if output.pos.line == old_line:
234                res.fail('expected newline in output')
235        elif not escape and c == '#':
236            num = output.get_number()
237            if num == '':
238                res.fail('expected number in output')
239            elif pattern.check_identifier():
240                name = pattern.get_identifier()
241                if name in g and int(num) != g[name]:
242                    res.fail('unexpected number for \'%s\': %d (expected %d)' % (name, int(num), g[name]))
243                elif name != '_':
244                    g[name] = int(num)
245        elif not escape and c == '$':
246            name = pattern.get_identifier()
247
248            val = ''
249            while not output.peek_test(string.whitespace):
250                val += output.read(1)
251
252            if name in g and val != g[name]:
253                res.fail('unexpected value for \'%s\': \'%s\' (expected \'%s\')' % (name, val, g[name]))
254            elif name != '_':
255                g[name] = val
256        elif not escape and c == '%' and pattern.check_identifier():
257            if output.read(1) != '%':
258                res.fail('expected \'%\' in output')
259            else:
260                num = output.get_number()
261                if num == '':
262                    res.fail('expected number in output')
263                else:
264                    name = pattern.get_identifier()
265                    if name in g and int(num) != g[name]:
266                        res.fail('unexpected number for \'%s\': %d (expected %d)' % (name, int(num), g[name]))
267                    elif name != '_':
268                        g[name] = int(num)
269        elif not escape and c == '@' and pattern.check_identifier():
270            name = pattern.get_identifier()
271            args = ''
272            if pattern.peek_test('('):
273                pattern.read(1)
274                while pattern.peek() not in ['', ')']:
275                    args += pattern.read(1)
276                assert(pattern.read(1) == ')')
277            func_res = g['funcs'][name](args)
278            match_res = do_match(g, StringStream(func_res, 'expansion of "%s(%s)"' % (name, args)), output, False, True)
279            if not match_res.success:
280                res.func_res = match_res
281                res.output_pos = match_res.output_pos
282                res.fail(match_res.fail_message)
283        elif not escape and c == ' ':
284            while pattern.peek_test(' '):
285                pattern.read(1)
286
287            read_whitespace = False
288            while output.peek_test(' \t'):
289                output.read(1)
290                read_whitespace = True
291            if not read_whitespace:
292                res.fail('expected whitespace in output, got %r' % (output.peek(1)))
293        else:
294            outc = output.peek(1)
295            if outc != c:
296                res.fail('expected %r in output, got %r' % (c, outc))
297            else:
298                output.read(1)
299        if not res.success:
300            if skip_lines and output.peek() != '':
301                g.clear()
302                g.update(old_g)
303                res.success = True
304                output.read_line()
305                pattern.reset()
306                output.skip_whitespace(False)
307                pattern.skip_whitespace(False)
308            else:
309                return res
310
311        escape = False
312
313    if not in_func:
314        while output.peek() in [' ', '\t']:
315            output.read(1)
316
317        if output.read(1) not in ['', '\n']:
318            res.fail('expected end of output')
319            return res
320
321    return res
322
323class PatternCheck(Check):
324    def __init__(self, data, search, position):
325        Check.__init__(self, data, position)
326        self.search = search
327
328    def run(self, state):
329        pattern_stream = StringStream(self.data.rstrip(), 'pattern')
330        res = do_match(state.g, pattern_stream, state.g['output'], self.search)
331        if not res.success:
332            state.result.log += 'pattern at %s failed: %s\n' % (self.position, res.fail_message)
333            state.result.log += res.format_pattern_pos() + '\n\n'
334            if not self.search:
335                out_line = state.g['output'].get_line(res.output_pos.line)
336                state.result.log += '\n'.join(format_error_lines('at', res.output_pos.line, res.output_pos.column, 'output', out_line))
337            else:
338                state.result.log += 'output was:\n'
339                state.result.log += state.g['output'].data.rstrip() + '\n'
340            return False
341        return True
342
343class CheckState:
344    def __init__(self, result, variant, checks, output):
345        self.result = result
346        self.variant = variant
347        self.checks = checks
348
349        self.checks.insert(0, CodeCheck(initial_code, None))
350        self.insert_queue = []
351
352        self.g = {'success': True, 'funcs': {}, 'insert_queue': self.insert_queue,
353                  'variant': variant, 'log': '', 'output': StringStream(output, 'output'),
354                  'CodeCheck': CodeCheck, 'PatternCheck': PatternCheck,
355                  'current_position': ''}
356
357class TestResult:
358    def __init__(self, expected):
359        self.result = ''
360        self.expected = expected
361        self.log = ''
362
363def check_output(result, variant, checks, output):
364    state = CheckState(result, variant, checks, output)
365
366    while len(state.checks):
367        check = state.checks.pop(0)
368        state.current_position = check.position
369        if not check.run(state):
370            result.result = 'failed'
371            return
372
373        for check in state.insert_queue[::-1]:
374            state.checks.insert(0, check)
375        state.insert_queue.clear()
376
377    result.result = 'passed'
378    return
379
380def parse_check(variant, line, checks, pos):
381    if line.startswith(';'):
382        line = line[1:]
383        if len(checks) and isinstance(checks[-1], CodeCheck):
384            checks[-1].data += '\n' + line
385        else:
386            checks.append(CodeCheck(line, pos))
387    elif line.startswith('!'):
388        checks.append(PatternCheck(line[1:], False, pos))
389    elif line.startswith('>>'):
390        checks.append(PatternCheck(line[2:], True, pos))
391    elif line.startswith('~'):
392        end = len(line)
393        start = len(line)
394        for c in [';', '!', '>>']:
395            if line.find(c) != -1 and line.find(c) < end:
396                end = line.find(c)
397        if end != len(line):
398            match = re.match(line[1:end], variant)
399            if match and match.end() == len(variant):
400                parse_check(variant, line[end:], checks, pos)
401
402def parse_test_source(test_name, variant, fname):
403    in_test = False
404    test = []
405    expected_result = 'passed'
406    line_num = 1
407    for line in open(fname, 'r').readlines():
408        if line.startswith('BEGIN_TEST(%s)' % test_name):
409            in_test = True
410        elif line.startswith('BEGIN_TEST_TODO(%s)' % test_name):
411            in_test = True
412            expected_result = 'todo'
413        elif line.startswith('BEGIN_TEST_FAIL(%s)' % test_name):
414            in_test = True
415            expected_result = 'failed'
416        elif line.startswith('END_TEST'):
417            in_test = False
418        elif in_test:
419            test.append((line_num, line.strip()))
420        line_num += 1
421
422    checks = []
423    for line_num, check in [(line_num, l[2:]) for line_num, l in test if l.startswith('//')]:
424         parse_check(variant, check, checks, 'line %d of %s' % (line_num, os.path.split(fname)[1]))
425
426    return checks, expected_result
427
428def parse_and_check_test(test_name, variant, test_file, output, current_result):
429    checks, expected = parse_test_source(test_name, variant, test_file)
430
431    result = TestResult(expected)
432    if len(checks) == 0:
433        result.result = 'empty'
434        result.log = 'no checks found'
435    elif current_result != None:
436        result.result, result.log = current_result
437    else:
438        check_output(result, variant, checks, output)
439        if result.result == 'failed' and expected == 'todo':
440            result.result = 'todo'
441
442    return result
443
444def print_results(results, output, expected):
445    results = {name: result for name, result in results.items() if result.result == output}
446    results = {name: result for name, result in results.items() if (result.result == result.expected) == expected}
447
448    if not results:
449        return 0
450
451    print('%s tests (%s):' % (output, 'expected' if expected else 'unexpected'))
452    for test, result in results.items():
453        color = '' if expected else set_red
454        print('   %s%s%s' % (color, test, set_normal))
455        if result.log.strip() != '':
456            for line in result.log.rstrip().split('\n'):
457                print('      ' + line.rstrip())
458    print('')
459
460    return len(results)
461
462def get_cstr(fp):
463    res = b''
464    while True:
465        c = fp.read(1)
466        if c == b'\x00':
467            return res.decode('utf-8')
468        else:
469            res += c
470
471if __name__ == "__main__":
472   results = {}
473
474   stdin = sys.stdin.buffer
475   while True:
476       packet_type = stdin.read(4)
477       if packet_type == b'':
478           break;
479
480       test_name = get_cstr(stdin)
481       test_variant = get_cstr(stdin)
482       if test_variant != '':
483           full_name = test_name + '/' + test_variant
484       else:
485           full_name = test_name
486
487       test_source_file = get_cstr(stdin)
488       current_result = None
489       if ord(stdin.read(1)):
490           current_result = (get_cstr(stdin), get_cstr(stdin))
491       code_size = struct.unpack("=L", stdin.read(4))[0]
492       code = stdin.read(code_size).decode('utf-8')
493
494       results[full_name] = parse_and_check_test(test_name, test_variant, test_source_file, code, current_result)
495
496   result_types = ['passed', 'failed', 'todo', 'empty']
497   num_expected = 0
498   num_unexpected = 0
499   for t in result_types:
500       num_expected += print_results(results, t, True)
501   for t in result_types:
502       num_unexpected += print_results(results, t, False)
503   num_expected_skipped = print_results(results, 'skipped', True)
504   num_unexpected_skipped = print_results(results, 'skipped', False)
505
506   num_unskipped = len(results) - num_expected_skipped - num_unexpected_skipped
507   color = set_red if num_unexpected else set_green
508   print('%s%d (%.0f%%) of %d unskipped tests had an expected result%s' % (color, num_expected, floor(num_expected / num_unskipped * 100), num_unskipped, set_normal))
509   if num_unexpected_skipped:
510       print('%s%d tests had been unexpectedly skipped%s' % (set_red, num_unexpected_skipped, set_normal))
511
512   if num_unexpected:
513       sys.exit(1)
514