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