CIRCT  20.0.0git
comb.py
Go to the documentation of this file.
1 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2 # See https://llvm.org/LICENSE.txt for license information.
3 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4 
5 from . import comb
6 from ..dialects._ods_common import _cext as _ods_cext
7 from ..ir import IntegerAttr, IntegerType, OpView
8 from ..support import NamedValueOpView, get_value
9 from ._comb_ops_gen import *
10 from ._comb_ops_gen import _Dialect
11 
12 
13 # Sugar classes for the various possible verions of ICmpOp.
14 class ICmpOpBuilder(NamedValueOpView):
15 
16  def operand_names(self):
17  return ["lhs", "rhs"]
18 
19  def result_names(self):
20  return ["result"]
21 
22  def __init__(self, predicate, data_type, input_port_mapping={}, **kwargs):
23  predicate = IntegerAttr.get(IntegerType.get_signless(64), predicate)
24  super().__init__(ICmpOp, data_type, input_port_mapping, [predicate],
25  **kwargs)
26 
27 
28 def CompareOp(predicate):
29 
30  def decorated(cls):
31 
32  class _Class(cls, OpView):
33 
34  @staticmethod
35  def create(lhs=None, rhs=None):
36  mapping = {}
37  if lhs:
38  mapping["lhs"] = lhs
39  if rhs:
40  mapping["rhs"] = rhs
41  if len(mapping) == 0:
42  result_type = IntegerType.get_signless(1)
43  else:
44  result_type = None
45  return ICmpOpBuilder(predicate, result_type, mapping)
46 
47  return _Class
48 
49  return decorated
50 
51 
52 @CompareOp(0)
53 class EqOp:
54  pass
55 
56 
57 @CompareOp(1)
58 class NeOp:
59  pass
60 
61 
62 @CompareOp(2)
63 class LtSOp:
64  pass
65 
66 
67 @CompareOp(3)
68 class LeSOp:
69  pass
70 
71 
72 @CompareOp(4)
73 class GtSOp:
74  pass
75 
76 
77 @CompareOp(5)
78 class GeSOp:
79  pass
80 
81 
82 @CompareOp(6)
83 class LtUOp:
84  pass
85 
86 
87 @CompareOp(7)
88 class LeUOp:
89  pass
90 
91 
92 @CompareOp(8)
93 class GtUOp:
94  pass
95 
96 
97 @CompareOp(9)
98 class GeUOp:
99  pass
100 
101 
102 # Builder base classes for non-variadic unary and binary ops.
103 class UnaryOpBuilder(NamedValueOpView):
104 
105  def operand_names(self):
106  return ["input"]
107 
108  def result_names(self):
109  return ["result"]
110 
111 
112 def UnaryOp(base):
113 
114  class _Class(base):
115 
116  @classmethod
117  def create(cls, input=None, result_type=None):
118  mapping = {"input": input} if input else {}
119  return UnaryOpBuilder(cls, result_type, mapping)
120 
121  return _Class
122 
123 
125 
126  def __init__(self, low_bit, data_type, input_port_mapping={}, **kwargs):
127  low_bit = IntegerAttr.get(IntegerType.get_signless(32), low_bit)
128  super().__init__(comb.ExtractOp, data_type, input_port_mapping, [],
129  [low_bit], **kwargs)
130 
131 
132 class BinaryOpBuilder(NamedValueOpView):
133 
134  def operand_names(self):
135  return ["lhs", "rhs"]
136 
137  def result_names(self):
138  return ["result"]
139 
140 
141 def BinaryOp(base):
142 
143  class _Class(base):
144 
145  @classmethod
146  def create(cls, lhs=None, rhs=None, result_type=None):
147  mapping = {}
148  if lhs:
149  mapping["lhs"] = lhs
150  if rhs:
151  mapping["rhs"] = rhs
152  return BinaryOpBuilder(cls, result_type, mapping)
153 
154  return _Class
155 
156 
157 # Base classes for the variadic ops.
158 def VariadicOp(base):
159 
160  class _Class(base):
161 
162  @classmethod
163  def create(cls, *args):
164  return cls([get_value(a) for a in args])
165 
166  return _Class
167 
168 
169 # Base class for miscellaneous ops that can't be abstracted but should provide a
170 # create method for uniformity.
171 def CreatableOp(base):
172 
173  class _Class(base):
174 
175  @classmethod
176  def create(cls, *args, **kwargs):
177  return cls(*args, **kwargs)
178 
179  return _Class
180 
181 
182 # Sugar classes for the various non-variadic unary ops.
183 @_ods_cext.register_operation(_Dialect, replace=True)
185 
186  @staticmethod
187  def create(low_bit, result_type, input=None):
188  mapping = {"input": input} if input else {}
189  return ExtractOpBuilder(low_bit,
190  result_type,
191  mapping,
192  needs_result_type=True)
193 
194 
195 @_ods_cext.register_operation(_Dialect, replace=True)
196 @UnaryOp
198  pass
199 
200 
201 # Sugar classes for the various non-variadic binary ops.
202 @_ods_cext.register_operation(_Dialect, replace=True)
203 @BinaryOp
204 class DivSOp(DivSOp):
205  pass
206 
207 
208 @_ods_cext.register_operation(_Dialect, replace=True)
209 @BinaryOp
210 class DivUOp(DivUOp):
211  pass
212 
213 
214 @_ods_cext.register_operation(_Dialect, replace=True)
215 @BinaryOp
216 class ModSOp(ModSOp):
217  pass
218 
219 
220 @_ods_cext.register_operation(_Dialect, replace=True)
221 @BinaryOp
222 class ModUOp(ModUOp):
223  pass
224 
225 
226 @_ods_cext.register_operation(_Dialect, replace=True)
227 @BinaryOp
228 class ShlOp(ShlOp):
229  pass
230 
231 
232 @_ods_cext.register_operation(_Dialect, replace=True)
233 @BinaryOp
234 class ShrSOp(ShrSOp):
235  pass
236 
237 
238 @_ods_cext.register_operation(_Dialect, replace=True)
239 @BinaryOp
240 class ShrUOp(ShrUOp):
241  pass
242 
243 
244 @_ods_cext.register_operation(_Dialect, replace=True)
245 @BinaryOp
246 class SubOp(SubOp):
247  pass
248 
249 
250 # Sugar classes for the variadic ops.
251 @_ods_cext.register_operation(_Dialect, replace=True)
252 @VariadicOp
253 class AddOp(AddOp):
254  pass
255 
256 
257 @_ods_cext.register_operation(_Dialect, replace=True)
258 @VariadicOp
259 class MulOp(MulOp):
260  pass
261 
262 
263 @_ods_cext.register_operation(_Dialect, replace=True)
264 @VariadicOp
265 class AndOp(AndOp):
266  pass
267 
268 
269 @_ods_cext.register_operation(_Dialect, replace=True)
270 @VariadicOp
271 class OrOp(OrOp):
272  pass
273 
274 
275 @_ods_cext.register_operation(_Dialect, replace=True)
276 @VariadicOp
277 class XorOp(XorOp):
278  pass
279 
280 
281 @_ods_cext.register_operation(_Dialect, replace=True)
282 @VariadicOp
284  pass
285 
286 
287 # Sugar classes for miscellaneous ops.
288 @_ods_cext.register_operation(_Dialect, replace=True)
289 @CreatableOp
290 class MuxOp(MuxOp):
291  pass
def result_names(self)
Definition: comb.py:137
def operand_names(self)
Definition: comb.py:134
def __init__(self, low_bit, data_type, input_port_mapping={}, **kwargs)
Definition: comb.py:126
def create(low_bit, result_type, input=None)
Definition: comb.py:187
def result_names(self)
Definition: comb.py:19
def operand_names(self)
Definition: comb.py:16
def __init__(self, predicate, data_type, input_port_mapping={}, **kwargs)
Definition: comb.py:22
def result_names(self)
Definition: comb.py:108
def operand_names(self)
Definition: comb.py:105
ir.Value get_value(obj)
Definition: support.py:27
def VariadicOp(base)
Definition: comb.py:158
def CompareOp(predicate)
Definition: comb.py:28
def UnaryOp(base)
Definition: comb.py:112
def CreatableOp(base)
Definition: comb.py:171
def BinaryOp(base)
Definition: comb.py:141