Debian Codegraph @

tensilelite/Tensile/Properties.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2022 Advanced Micro Devices, Inc. All rights reserved.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the 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 THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 #
23 ################################################################################
24
25 from .Common import hash_objs, state
26
27
28 class Property:
29
30 @classmethod
31 def FromOriginalState(cls, d):
32 return cls(d.get('type'), d.get('index'), d.get('value'))
33
34 def __init__(self, tag=None, index=None, value=None):
35 self._tag = tag
36 self._index = index
37 self._value = value
38
39 @property
40 def tag(self):
41 return self._tag
42
43 @property
44 def index(self):
45 return self._index
46
47 @property
48 def value(self):
49 return self._value
50
51 def state(self):
52 rv = {'type': self.tag}
53 if self.index is not None: rv['index'] = state(self.index)
54 if self.value is not None: rv['value'] = state(self.value)
55 return rv
56
57 def __eq__(self, other):
58 return self.__class__ == other.__class__ and \
59 self.tag == other.tag and \
60 self.value == other.value and \
61 self.index == other.index
62
63 def __hash__(self):
64 #return hash(self.tag) ^ hash(self.value) ^ hash(self.index)
65 return hash_objs(self.tag, self.value, self.index)
66
67
68 class Predicate(Property):
69
70 @classmethod
71 def And(cls, predicates):
72 predicates = tuple(predicates)
73 if len(predicates) == 0:
74 return cls('TruePred')
75 if len(predicates) == 1:
76 return predicates[0]
77 return cls('And', value=predicates)
78
79 @classmethod
80 def Or(cls, predicates):
81 predicates = tuple(predicates)
82 if len(predicates) == 0:
83 return cls('TruePred')
84 if len(predicates) == 1:
85 return predicates[0]
86 return cls('Or', value=predicates)
87
88 def __lt__(self, other):
89 # Ensure TruePred appears last.
90 if other.tag == 'TruePred':
91 if self.tag == 'TruePred':
92 return False
93 return True
94 if self.tag == 'TruePred':
95 return False
96
97 # EqualityMatching < RangeMatching < FreeSizeMatching
98 if self.tag == 'EqualityMatching':
99 if other.tag == 'EqualityMatching':
100 return False
101 if other.tag == 'RangeMatching':
102 return True
103 if other.tag == 'FreeSizeMatching':
104 return True
105 if self.tag == 'RangeMatching':
106 if other.tag == 'EqualityMatching':
107 return False
108 if other.tag == 'RangeMatching':
109 return False
110 if other.tag == 'FreeSizeMatching':
111 return True
112 if self.tag == 'FreeSizeMatching':
113 if other.tag == 'EqualityMatching':
114 return False
115 if other.tag == 'RangeMatching':
116 return False
117 if other.tag == 'FreeSizeMatching':
118 return False
119
120 selfValue = self.value
121 otherValue = other.value
122
123 if type(selfValue) is dict:
124 selfTup = {v for _, v in selfValue.items()}
125 selfValue = selfTup
126
127 if type(otherValue) is dict:
128 otherTup = {v for _, v in otherValue.items()}
129 otherValue = otherTup
130
131 # If neither is a TruePred then just use the default comparison.
132 return (self.tag, self.index, selfValue) < (other.tag, other.index, otherValue)