• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (C) 2018 Valve Corporation
3#
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the "Software"),
6# to deal in the Software without restriction, including without limitation
7# the rights to use, copy, modify, merge, publish, distribute, sublicense,
8# and/or sell copies of the Software, and to permit persons to whom the
9# Software is furnished to do so, subject to the following conditions:
10#
11# The above copyright notice and this permission notice (including the next
12# paragraph) shall be included in all copies or substantial portions of the
13# Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21# IN THE SOFTWARE.
22
23import unittest
24
25import sys
26import os
27sys.path.insert(1, os.path.join(sys.path[0], '..'))
28
29from nir_algebraic import SearchAndReplace, AlgebraicPass
30
31# These tests check that the bitsize validator correctly rejects various
32# different kinds of malformed expressions, and documents what the error
33# message looks like.
34
35a = 'a'
36b = 'b'
37c = 'c'
38
39class ValidatorTests(unittest.TestCase):
40    pattern = ()
41    message = ''
42
43    algebraic_pass = AlgebraicPass("test", [])
44
45    def common(self, pattern, message):
46        with self.assertRaises(AssertionError) as context:
47            SearchAndReplace(pattern, self.algebraic_pass)
48
49        self.assertEqual(message, str(context.exception))
50
51    def test_wrong_src_count(self):
52        self.common((('iadd', a), ('fadd', a, a)),
53            "Expression ('iadd', 'a') has 1 sources, expected 2")
54
55    def test_var_bitsize(self):
56        self.common((('iadd', 'a@32', 'a@64'), ('fadd', a, a)),
57            "Variable a has conflicting bit size requirements: " \
58            "it must have bit size 32 and 64")
59
60    def test_var_bitsize_2(self):
61        self.common((('iadd', a, 'a@32'), ('fadd', 'a@64', a)),
62            "Variable a has conflicting bit size requirements: " \
63            "it must have bit size 32 and 64")
64
65    def test_search_src_bitsize(self):
66        self.common((('iadd', 'a@32', 'b@64'), ('fadd', a, b)),
67            "Source a@32 of ('iadd', 'a@32', 'b@64') must have bit size 32, " \
68            "while source b@64 must have incompatible bit size 64")
69
70    def test_replace_src_bitsize(self):
71        self.common((('iadd', a, ('b2i', b)), ('iadd', a, b)),
72            "Sources a (bit size of a) and b (bit size of b) " \
73            "of ('iadd', 'a', 'b') may not have the same bit size " \
74            "when building the replacement expression.")
75
76    def test_search_src_bitsize_fixed(self):
77        self.common((('ishl', a, 'b@64'), ('ishl', a, b)),
78            "b@64 must have 64 bits, but as a source of nir_op_ishl " \
79            "it must have 32 bits")
80
81    def test_replace_src_bitsize_fixed(self):
82        self.common((('iadd', a, b), ('ishl', a, b)),
83            "b has the bit size of b, but as a source of nir_op_ishl " \
84            "it must have 32 bits, which may not be the same")
85
86    def test_search_dst_bitsize(self):
87        self.common((('iadd@32', 'a@64', b), ('iadd', a, b)),
88            "('iadd@32', 'a@64', 'b') must have the bit size of 32, " \
89            "while its source a@64 must have incompatible bit size 64")
90
91    def test_replace_dst_bitsize(self):
92        self.common((('iadd', a, b), ('iadd@32', a, b)),
93            "('iadd@32', 'a', 'b') must have 32 bits, but its source a " \
94            "(bit size of b) may not have that bit size when building " \
95            "the replacement.")
96
97    def test_search_dst_bitsize_fixed(self):
98        self.common((('ufind_msb@64', a), ('ineg', a)),
99            "('ufind_msb@64', 'a') must have 64 bits, "\
100            "but as a destination of nir_op_ufind_msb it must have 32 bits")
101
102    def test_replace_dst_bitsize_fixed(self):
103        self.common((('ineg', 'a@64'), ('ufind_msb@64', a)),
104            "('ufind_msb@64', 'a') must have 64 bits, " \
105            "but as a destination of nir_op_ufind_msb it must have 32 bits")
106
107    def test_ambiguous_bitsize(self):
108        self.common((('ineg', 'a@32'), ('i2b', ('b2i', a))),
109            "Ambiguous bit size for replacement value ('b2i', 'a'): it "\
110            "cannot be deduced from a variable, a fixed bit size somewhere, "
111            "or the search expression.")
112
113    def test_search_replace_mismatch(self):
114        self.common((('b2i', ('i2b', a)), a),
115            "The search expression bit size ('b2i', ('i2b', 'a')) and " \
116            "replace expression bit size a may not be the same")
117
118unittest.main()
119