From 97c28cb0823a043e660e97197525251830cd89ed Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Fri, 8 Dec 2017 17:45:03 -0800 Subject: [PATCH] glsl/tests: Convert optimization-test.sh to pure python This patch converts optimization-test.sh to python, in this process it removes external shell dependencies including diff. It replaces the python script that generates shell scripts with a python library that generates test cases and runs them using subprocess. v2: - use $PYTHON2 to be consistent with other tests in mesa Signed-off-by: Dylan Baker --- ...eate_test_cases.py => lower_jump_cases.py} | 266 ++++++++---------- .../glsl/tests/lower_jumps/.gitignore | 3 - src/compiler/glsl/tests/optimization-test.sh | 87 +----- src/compiler/glsl/tests/optimization_test.py | 95 +++++++ 4 files changed, 214 insertions(+), 237 deletions(-) rename src/compiler/glsl/tests/{lower_jumps/create_test_cases.py => lower_jump_cases.py} (70%) delete mode 100644 src/compiler/glsl/tests/lower_jumps/.gitignore create mode 100755 src/compiler/glsl/tests/optimization_test.py diff --git a/src/compiler/glsl/tests/lower_jumps/create_test_cases.py b/src/compiler/glsl/tests/lower_jump_cases.py similarity index 70% rename from src/compiler/glsl/tests/lower_jumps/create_test_cases.py rename to src/compiler/glsl/tests/lower_jump_cases.py index 88a74a3c4ad..b50ab734798 100644 --- a/src/compiler/glsl/tests/lower_jumps/create_test_cases.py +++ b/src/compiler/glsl/tests/lower_jump_cases.py @@ -1,6 +1,6 @@ # coding=utf-8 # -# Copyright © 2011 Intel Corporation +# Copyright © 2011, 2018 Intel Corporation # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), @@ -21,18 +21,8 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -import argparse -import os -import os.path -import re -import subprocess -import sys - -sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) # For access to sexps.py, which is in parent dir from sexps import * -runner = ":" -outdir = "." def make_test_case(f_name, ret_type, body): """Create a simple optimization test case consisting of a single function with the given name, return type, and body. @@ -280,40 +270,27 @@ def bash_quote(*args): return "'{0}'".format(word.replace("'", "'\"'\"'")) return ' '.join(quote_word(word) for word in args) -def create_test_case(doc_string, input_sexp, expected_sexp, test_name, +def create_test_case(input_sexp, expected_sexp, test_name, pull_out_jumps=False, lower_sub_return=False, lower_main_return=False, lower_continue=False, lower_break=False): """Create a test case that verifies that do_lower_jumps transforms the given code in the expected way. """ - doc_lines = [line.strip() for line in doc_string.splitlines()] - doc_string = ''.join('# {0}\n'.format(line) for line in doc_lines if line != '') check_sexp(input_sexp) check_sexp(expected_sexp) input_str = sexp_to_string(sort_decls(input_sexp)) - expected_output = sexp_to_string(sort_decls(expected_sexp)) - + expected_output = sexp_to_string(sort_decls(expected_sexp)) # XXX: don't stringify this optimization = ( 'do_lower_jumps({0:d}, {1:d}, {2:d}, {3:d}, {4:d})'.format( pull_out_jumps, lower_sub_return, lower_main_return, lower_continue, lower_break)) - args = [runner, 'optpass', '--quiet', '--input-ir', optimization] - test_file = os.path.join(outdir, '{0}.opt_test'.format(test_name)) - with open(test_file, 'w') as f: - f.write('#!/usr/bin/env bash\n#\n# This file was generated by create_test_cases.py.\n#\n') - f.write(doc_string) - f.write('{0} </dev/null -if [ $? -ne 0 ]; then - echo "Could not find python2. Make sure that PYTHON2 variable is correctly set." - exit 1 -fi - -if [ -z "$srcdir" -o -z "$abs_builddir" ]; then - echo "" - echo "Warning: you're invoking the script manually and things may fail." - echo "Attempting to determine/set srcdir and abs_builddir variables." - echo "" - - # Variable should point to the Makefile.glsl.am - srcdir=./../../ - cd `dirname "$0"` - # Variable should point to the folder two levels above glsl_test - abs_builddir=`pwd`/../../ -fi - -compare_ir=$srcdir/glsl/tests/compare_ir.py - -total=0 -pass=0 -has_tests=0 - -# Store our location before we start diving into subdirectories. -ORIGDIR=`pwd` -echo "====== Generating tests ======" -for dir in $srcdir/glsl/tests/*/; do - if [ -e "${dir}create_test_cases.py" ]; then - echo "$dir" - # construct the correct builddir - completedir="$abs_builddir/glsl/tests/`echo ${dir} | sed 's|.*/glsl/tests/||g'`" - mkdir -p $completedir - cd $dir; - $PYTHON2 create_test_cases.py --runner $abs_builddir/glsl/glsl_test --outdir $completedir; - if [ $? -eq 0 ]; then - has_tests=1 - fi - cd .. - fi -done -cd "$ORIGDIR" - -if [ $has_tests -eq 0 ]; then - echo "Could not generate any tests." - exit 1 -fi - -if [ ! -f "$compare_ir" ]; then - echo "Could not find compare_ir. Make sure that srcdir variable is correctly set." - exit 1 -fi - -echo "====== Testing optimization passes ======" -for test in `find . -iname '*.opt_test'`; do - echo -n "Testing `echo $test| sed 's|.*/glsl/tests/||g'`..." - ./$test > "$test.out" 2>&1 - total=$((total+1)) - if $PYTHON2 $PYTHON_FLAGS $compare_ir "$test.expected" "$test.out" >/dev/null 2>&1; then - echo "PASS" - pass=$((pass+1)) - else - echo "FAIL" - $PYTHON2 $PYTHON_FLAGS $compare_ir "$test.expected" "$test.out" - fi -done - -if [ $total -eq 0 ]; then - echo "Could not find any tests." - exit 1 -fi - -echo "" -echo "$pass/$total tests returned correct results" -echo "" - -if [ $pass = $total ]; then - exit 0 -else - exit 1 -fi +$PYTHON2 $srcdir/glsl/tests/optimization_test.py --test-runner $abs_builddir/glsl/glsl_test diff --git a/src/compiler/glsl/tests/optimization_test.py b/src/compiler/glsl/tests/optimization_test.py new file mode 100755 index 00000000000..577d2dfc20f --- /dev/null +++ b/src/compiler/glsl/tests/optimization_test.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python2 +# encoding=utf-8 +# Copyright © 2018 Intel Corporation + +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: + +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +"""Script to generate and run glsl optimization tests.""" + +from __future__ import print_function +import argparse +import difflib +import subprocess +import sys + +import sexps +import lower_jump_cases + + +def arg_parser(): + parser = argparse.ArgumentParser() + parser.add_argument( + '--test-runner', + required=True, + help='The glsl_test binary.') + return parser.parse_args() + + +def compare(actual, expected): + """Compare the s-expresions and return a diff if they are different.""" + actual = sexps.sort_decls(sexps.parse_sexp(actual)) + expected = sexps.sort_decls(sexps.parse_sexp(expected)) + + if actual == expected: + return None + + actual = sexps.sexp_to_string(actual) + expected = sexps.sexp_to_string(expected) + + return difflib.unified_diff(expected.splitlines(), actual.splitlines()) + + +def main(): + """Generate each test and report pass or fail.""" + args = arg_parser() + + total = 0 + passes = 0 + + for gen in lower_jump_cases.CASES: + for name, opt, source, expected in gen(): + total += 1 + print('{}: '.format(name), end='') + proc = subprocess.Popen( + [args.test_runner, 'optpass', '--quiet', '--input-ir', opt], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + stdin=subprocess.PIPE) + out, err = proc.communicate(source) + if err: + print('FAIL') + print('Unexpected output on stderr: {}'.format(err), + file=sys.stdout) + continue + + result = compare(out, expected) + if result is not None: + print('FAIL') + for l in result: + print(l, file=sys.stderr) + else: + print('PASS') + passes += 1 + + print('{}/{} tests returned correct results'.format(passes, total)) + exit(0 if passes == total else 1) + + +if __name__ == '__main__': + main()