1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 # See Notices.txt for copyright information
5 Copyright (C) 2020 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
7 dynamic-partitionable class similar to Signal, which, when the partition
8 is fully open will be identical to Signal. when partitions are closed,
9 the class turns into a SIMD variant of Signal. *this is dynamic*.
11 the basic fundamental idea is: write code once, and if you want a SIMD
12 version of it, use SimdSignal in place of Signal. job done.
13 this however requires the code to *not* be designed to use nmigen.If,
14 nmigen.Case, or other constructs: only Mux and other logic.
16 * http://bugs.libre-riscv.org/show_bug.cgi?id=132
19 from ieee754
.part_mul_add
.adder
import PartitionedAdder
20 from ieee754
.part_cmp
.eq_gt_ge
import PartitionedEqGtGe
21 from ieee754
.part_bits
.xor
import PartitionedXOR
22 from ieee754
.part_bits
.bool import PartitionedBool
23 from ieee754
.part_bits
.all
import PartitionedAll
24 from ieee754
.part_shift
.part_shift_dynamic
import PartitionedDynamicShift
25 from ieee754
.part_shift
.part_shift_scalar
import PartitionedScalarShift
26 from ieee754
.part_mul_add
.partpoints
import make_partition2
, PartitionPoints
27 from ieee754
.part_mux
.part_mux
import PMux
28 from ieee754
.part_ass
.passign
import PAssign
29 from ieee754
.part_cat
.pcat
import PCat
30 from ieee754
.part_repl
.prepl
import PRepl
31 from ieee754
.part
.simd_scope
import SimdScope
32 from ieee754
.part
.layout_experiment
import layout
33 from operator
import or_
, xor
, and_
, not_
35 from nmigen
import (Signal
, Const
, Cat
)
36 from nmigen
.hdl
.ast
import UserValue
, Shape
40 if isinstance(op1
, SimdSignal
):
45 def applyop(op1
, op2
, op
):
46 if isinstance(op1
, SimdSignal
):
47 result
= SimdSignal
.like(op1
)
49 result
= SimdSignal
.like(op2
)
50 result
.m
.d
.comb
+= result
.sig
.eq(op(getsig(op1
), getsig(op2
)))
56 # for sub-modules to be created on-demand. Mux is done slightly
57 # differently (has its own global)
58 for name
in ['add', 'eq', 'gt', 'ge', 'ls', 'xor', 'bool', 'all']:
62 def get_runlengths(pbit
, size
):
65 # identify where the 1s are, which indicates "start of a new partition"
66 # we want a list of the lengths of all partitions
68 if pbit
& (1 << i
): # it's a 1: ends old partition, starts new
69 res
.append(count
) # add partition
70 count
= 1 # start again
73 # end reached, add whatever is left. could have done this by creating
74 # "fake" extra bit on the partitions, but hey
80 # Prototype https://bugs.libre-soc.org/show_bug.cgi?id=713#c53
81 # this provides a "compatibility" layer with existing SimdSignal
82 # behaviour. the idea is that this interface defines which "combinations"
83 # of partition selections are relevant, and as an added bonus it says
84 # which partition lanes are completely irrelevant (padding, blank).
85 class PartType
: # TODO decide name
86 def __init__(self
, psig
):
90 return list(self
.psig
.partpoints
.values())
93 return Cat(self
.get_mask())
96 return range(1 << len(self
.get_mask()))
98 def get_num_elements(self
, pbit
):
99 keys
= list(self
.psig
.partpoints
.keys())
100 return len(get_runlengths(pbit
, len(keys
)))
102 def get_el_range(self
, pbit
, el_num
):
103 """based on the element number and the current elwid/pbit (case)
104 return the range start/end of the element within its underlying signal
105 this function is not at all designed to be efficient.
107 keys
= list(self
.psig
.partpoints
.keys())
108 runs
= get_runlengths(pbit
, len(keys
))
109 keys
= [0] + keys
+ [len(self
.psig
.sig
)]
111 for i
in range(el_num
):
114 numparts
= runs
[el_num
]
115 return range(keys
[y
], keys
[y
+numparts
])
118 def blanklanes(self
):
122 # this one would be an elwidth version
123 # see https://bugs.libre-soc.org/show_bug.cgi?id=713#c34
124 # it requires an "adapter" which is the layout() function
125 # where the PartitionPoints was *created* by the layout()
126 # function and this class then "understands" the relationship
127 # between elwidth and the PartitionPoints that were created
129 class ElwidPartType
: # TODO decide name
130 def __init__(self
, psig
):
134 return list(self
.psig
._shape
.partpoints
.values()) # i think
136 def get_switch(self
):
137 return self
.psig
.scope
.elwid
# switch on elwid: match get_cases()
140 return self
.psig
._shape
.bitp
.keys() # all possible values of elwid
143 def blanklanes(self
):
144 return self
.psig
.shape
.blankmask
147 class SimdShape(Shape
):
148 """a SIMD variant of Shape. supports:
149 * fixed overall width with variable (maxed-out) element lengths
150 * fixed element widths with overall size auto-determined
151 * both fixed overall width and fixed element widths
153 naming is preserved to be compatible with Shape().
156 def __init__(self
, scope
, width
=None, # this is actually widths_at_elwid
158 fixed_width
=None): # fixed overall width
159 widths_at_elwid
= width
160 print("SimdShape width", width
, "fixed_width", fixed_width
)
161 # this check is done inside layout but do it again here anyway
162 assert fixed_width
!= None or widths_at_elwid
!= None, \
163 "both width (widths_at_elwid) and fixed_width cannot be None"
164 (pp
, bitp
, lpoints
, bmask
, fixed_width
, lane_shapes
, part_wid
) = \
170 self
.bitp
= bitp
# binary values for partpoints at each elwidth
171 self
.lpoints
= lpoints
# layout ranges
172 self
.blankmask
= bmask
# blanking mask (partitions always padding)
173 self
.partwid
= part_wid
# smallest alignment start point for elements
175 # pass through the calculated width to Shape() so that when/if
176 # objects using this Shape are downcast, they know exactly how to
177 # get *all* bits and need know absolutely nothing about SIMD at all
178 Shape
.__init
__(self
, fixed_width
, signed
)
181 class SimdSignal(UserValue
):
182 # XXX ################################################### XXX
183 # XXX Keep these functions in the same order as ast.Value XXX
184 # XXX ################################################### XXX
185 def __init__(self
, mask
, shape
=None, *args
,
186 src_loc_at
=0, fixed_width
=None, **kwargs
):
187 super().__init
__(src_loc_at
=src_loc_at
)
188 print("SimdSignal shape", shape
)
189 # create partition points
190 if isinstance(mask
, SimdScope
): # mask parameter is a SimdScope
192 self
.ptype
= ElwidPartType(self
)
193 # adapt shape to a SimdShape
194 if not isinstance(shape
, SimdShape
):
195 shape
= SimdShape(self
.scope
, shape
, fixed_width
=fixed_width
)
197 self
.sig
= Signal(shape
, *args
, **kwargs
)
198 # get partpoints from SimdShape
199 self
.partpoints
= shape
.partpoints
201 self
.sig
= Signal(shape
, *args
, **kwargs
)
202 width
= len(self
.sig
) # get signal width
203 if isinstance(mask
, PartitionPoints
):
204 self
.partpoints
= mask
206 self
.partpoints
= make_partition2(mask
, width
)
207 self
.ptype
= PartType(self
)
209 def set_module(self
, m
):
212 def get_modname(self
, category
):
213 modnames
[category
] += 1
214 return "%s_%d" % (category
, modnames
[category
])
217 def like(other
, *args
, **kwargs
):
218 """Builds a new SimdSignal with the same PartitionPoints and
219 Signal properties as the other"""
220 result
= SimdSignal(PartitionPoints(other
.partpoints
))
221 result
.sig
= Signal
.like(other
.sig
, *args
, **kwargs
)
228 # nmigen-redirected constructs (Mux, Cat, Switch, Assign)
230 # TODO, http://bugs.libre-riscv.org/show_bug.cgi?id=716
231 # def __Part__(self, offset, width, stride=1, *, src_loc_at=0):
232 raise NotImplementedError("TODO: implement as "
233 "(self>>(offset*stride)[:width]")
234 # TODO, http://bugs.libre-riscv.org/show_bug.cgi?id=716
236 def __Slice__(self
, start
, stop
, *, src_loc_at
=0):
237 # NO. Swizzled shall NOT be deployed, it violates
238 # Project Development Practices
239 raise NotImplementedError("TODO: need PartitionedSlice")
241 def __Repl__(self
, count
, *, src_loc_at
=0):
242 return PRepl(self
.m
, self
, count
, self
.ptype
)
244 def __Cat__(self
, *args
, src_loc_at
=0):
245 print("partsig cat", self
, args
)
246 # TODO: need SwizzledSimdValue-aware Cat
247 args
= [self
] + list(args
)
249 assert isinstance(sig
, SimdSignal
), \
250 "All SimdSignal.__Cat__ arguments must be " \
251 "a SimdSignal. %s is not." % repr(sig
)
252 return PCat(self
.m
, args
, self
.ptype
)
254 def __Mux__(self
, val1
, val2
):
255 # print ("partsig mux", self, val1, val2)
256 assert len(val1
) == len(val2
), \
257 "SimdSignal width sources must be the same " \
258 "val1 == %d, val2 == %d" % (len(val1
), len(val2
))
259 return PMux(self
.m
, self
.partpoints
, self
, val1
, val2
, self
.ptype
)
261 def __Assign__(self
, val
, *, src_loc_at
=0):
262 print("partsig assign", self
, val
)
263 # this is a truly awful hack, outlined here:
264 # https://bugs.libre-soc.org/show_bug.cgi?id=731#c13
265 # during the period between constructing Simd-aware sub-modules
266 # and the elaborate() being called on them there is a window of
267 # opportunity to indicate which of those submodules is LHS and
268 # which is RHS. manic laughter is permitted. *gibber*.
269 if hasattr(self
, "_hack_submodule"):
270 self
._hack
_submodule
.set_lhs_mode(True)
271 if hasattr(val
, "_hack_submodule"):
272 val
._hack
_submodule
.set_lhs_mode(False)
273 return PAssign(self
.m
, self
, val
, self
.ptype
)
275 # TODO, http://bugs.libre-riscv.org/show_bug.cgi?id=458
276 # def __Switch__(self, cases, *, src_loc=None, src_loc_at=0,
279 # no override needed, Value.__bool__ sufficient
280 # def __bool__(self):
282 # unary ops that do not require partitioning
284 def __invert__(self
):
285 result
= SimdSignal
.like(self
)
286 self
.m
.d
.comb
+= result
.sig
.eq(~self
.sig
)
289 # unary ops that require partitioning
292 z
= Const(0, len(self
.sig
))
293 result
, _
= self
.sub_op(z
, self
)
296 # binary ops that need partitioning
298 def add_op(self
, op1
, op2
, carry
):
301 pa
= PartitionedAdder(len(op1
), self
.partpoints
)
302 setattr(self
.m
.submodules
, self
.get_modname('add'), pa
)
306 comb
+= pa
.carry_in
.eq(carry
)
307 result
= SimdSignal
.like(self
)
308 comb
+= result
.sig
.eq(pa
.output
)
309 return result
, pa
.carry_out
311 def sub_op(self
, op1
, op2
, carry
=~
0):
314 pa
= PartitionedAdder(len(op1
), self
.partpoints
)
315 setattr(self
.m
.submodules
, self
.get_modname('add'), pa
)
318 comb
+= pa
.b
.eq(~op2
)
319 comb
+= pa
.carry_in
.eq(carry
)
320 result
= SimdSignal
.like(self
)
321 comb
+= result
.sig
.eq(pa
.output
)
322 return result
, pa
.carry_out
324 def __add__(self
, other
):
325 result
, _
= self
.add_op(self
, other
, carry
=0)
328 def __radd__(self
, other
):
329 # https://bugs.libre-soc.org/show_bug.cgi?id=718
330 result
, _
= self
.add_op(other
, self
)
333 def __sub__(self
, other
):
334 result
, _
= self
.sub_op(self
, other
)
337 def __rsub__(self
, other
):
338 # https://bugs.libre-soc.org/show_bug.cgi?id=718
339 result
, _
= self
.sub_op(other
, self
)
342 def __mul__(self
, other
):
343 raise NotImplementedError # too complicated at the moment
344 return Operator("*", [self
, other
])
346 def __rmul__(self
, other
):
347 raise NotImplementedError # too complicated at the moment
348 return Operator("*", [other
, self
])
350 # not needed: same as Value.__check_divisor
351 # def __check_divisor(self):
353 def __mod__(self
, other
):
354 raise NotImplementedError
355 other
= Value
.cast(other
)
356 other
.__check
_divisor
()
357 return Operator("%", [self
, other
])
359 def __rmod__(self
, other
):
360 raise NotImplementedError
361 self
.__check
_divisor
()
362 return Operator("%", [other
, self
])
364 def __floordiv__(self
, other
):
365 raise NotImplementedError
366 other
= Value
.cast(other
)
367 other
.__check
_divisor
()
368 return Operator("//", [self
, other
])
370 def __rfloordiv__(self
, other
):
371 raise NotImplementedError
372 self
.__check
_divisor
()
373 return Operator("//", [other
, self
])
375 # not needed: same as Value.__check_shamt
376 # def __check_shamt(self):
378 # TODO: detect if the 2nd operand is a Const, a Signal or a
379 # SimdSignal. if it's a Const or a Signal, a global shift
380 # can occur. if it's a SimdSignal, that's much more interesting.
381 def ls_op(self
, op1
, op2
, carry
, shr_flag
=0):
383 if isinstance(op2
, Const
) or isinstance(op2
, Signal
):
385 pa
= PartitionedScalarShift(len(op1
), self
.partpoints
)
389 pa
= PartitionedDynamicShift(len(op1
), self
.partpoints
)
391 # TODO: case where the *shifter* is a SimdSignal but
392 # the thing *being* Shifted is a scalar (Signal, expression)
393 # https://bugs.libre-soc.org/show_bug.cgi?id=718
394 setattr(self
.m
.submodules
, self
.get_modname('ls'), pa
)
397 comb
+= pa
.data
.eq(op1
)
398 comb
+= pa
.shifter
.eq(op2
)
399 comb
+= pa
.shift_right
.eq(shr_flag
)
403 comb
+= pa
.shift_right
.eq(shr_flag
)
404 # XXX TODO: carry-in, carry-out (for arithmetic shift)
405 #comb += pa.carry_in.eq(carry)
406 return (pa
.output
, 0)
408 def __lshift__(self
, other
):
409 z
= Const(0, len(self
.partpoints
)+1)
410 result
, _
= self
.ls_op(self
, other
, carry
=z
) # TODO, carry
413 def __rlshift__(self
, other
):
414 # https://bugs.libre-soc.org/show_bug.cgi?id=718
415 raise NotImplementedError
416 return Operator("<<", [other
, self
])
418 def __rshift__(self
, other
):
419 z
= Const(0, len(self
.partpoints
)+1)
420 result
, _
= self
.ls_op(self
, other
, carry
=z
, shr_flag
=1) # TODO, carry
423 def __rrshift__(self
, other
):
424 # https://bugs.libre-soc.org/show_bug.cgi?id=718
425 raise NotImplementedError
426 return Operator(">>", [other
, self
])
428 # binary ops that don't require partitioning
430 def __and__(self
, other
):
431 return applyop(self
, other
, and_
)
433 def __rand__(self
, other
):
434 return applyop(other
, self
, and_
)
436 def __or__(self
, other
):
437 return applyop(self
, other
, or_
)
439 def __ror__(self
, other
):
440 return applyop(other
, self
, or_
)
442 def __xor__(self
, other
):
443 return applyop(self
, other
, xor
)
445 def __rxor__(self
, other
):
446 return applyop(other
, self
, xor
)
448 # binary comparison ops that need partitioning
450 def _compare(self
, width
, op1
, op2
, opname
, optype
):
451 # print (opname, op1, op2)
452 pa
= PartitionedEqGtGe(width
, self
.partpoints
)
453 setattr(self
.m
.submodules
, self
.get_modname(opname
), pa
)
455 comb
+= pa
.opcode
.eq(optype
) # set opcode
456 if isinstance(op1
, SimdSignal
):
457 comb
+= pa
.a
.eq(op1
.sig
)
460 if isinstance(op2
, SimdSignal
):
461 comb
+= pa
.b
.eq(op2
.sig
)
466 def __eq__(self
, other
):
467 width
= len(self
.sig
)
468 return self
._compare
(width
, self
, other
, "eq", PartitionedEqGtGe
.EQ
)
470 def __ne__(self
, other
):
471 width
= len(self
.sig
)
472 eq
= self
._compare
(width
, self
, other
, "eq", PartitionedEqGtGe
.EQ
)
473 ne
= Signal(eq
.width
)
474 self
.m
.d
.comb
+= ne
.eq(~eq
)
477 def __lt__(self
, other
):
478 width
= len(self
.sig
)
479 # swap operands, use gt to do lt
480 return self
._compare
(width
, other
, self
, "gt", PartitionedEqGtGe
.GT
)
482 def __le__(self
, other
):
483 width
= len(self
.sig
)
484 # swap operands, use ge to do le
485 return self
._compare
(width
, other
, self
, "ge", PartitionedEqGtGe
.GE
)
487 def __gt__(self
, other
):
488 width
= len(self
.sig
)
489 return self
._compare
(width
, self
, other
, "gt", PartitionedEqGtGe
.GT
)
491 def __ge__(self
, other
):
492 width
= len(self
.sig
)
493 return self
._compare
(width
, self
, other
, "ge", PartitionedEqGtGe
.GE
)
495 # no override needed: Value.__abs__ is general enough it does the job
501 # TODO, http://bugs.libre-riscv.org/show_bug.cgi?id=716
502 # def __getitem__(self, key):
504 def __new_sign(self
, signed
):
505 # XXX NO - SimdShape not Shape
506 print("XXX requires SimdShape not Shape")
507 shape
= Shape(len(self
), signed
=signed
)
508 result
= SimdSignal
.like(self
, shape
=shape
)
509 self
.m
.d
.comb
+= result
.sig
.eq(self
.sig
)
512 # http://bugs.libre-riscv.org/show_bug.cgi?id=719
513 def as_unsigned(self
):
514 return self
.__new
_sign
(False)
517 return self
.__new
_sign
(True)
522 """Conversion to boolean.
527 ``1`` if any bits are set, ``0`` otherwise.
529 width
= len(self
.sig
)
530 pa
= PartitionedBool(width
, self
.partpoints
)
531 setattr(self
.m
.submodules
, self
.get_modname("bool"), pa
)
532 self
.m
.d
.comb
+= pa
.a
.eq(self
.sig
)
536 """Check if any bits are ``1``.
541 ``1`` if any bits are set, ``0`` otherwise.
543 return self
!= Const(0) # leverage the __ne__ operator here
544 return Operator("r|", [self
])
547 """Check if all bits are ``1``.
552 ``1`` if all bits are set, ``0`` otherwise.
554 # something wrong with PartitionedAll, but self == Const(-1)"
555 # XXX https://bugs.libre-soc.org/show_bug.cgi?id=176#c17
556 #width = len(self.sig)
557 #pa = PartitionedAll(width, self.partpoints)
558 #setattr(self.m.submodules, self.get_modname("all"), pa)
559 #self.m.d.comb += pa.a.eq(self.sig)
561 return self
== Const(-1) # leverage the __eq__ operator here
564 """Compute pairwise exclusive-or of every bit.
569 ``1`` if an odd number of bits are set, ``0`` if an
570 even number of bits are set.
572 width
= len(self
.sig
)
573 pa
= PartitionedXOR(width
, self
.partpoints
)
574 setattr(self
.m
.submodules
, self
.get_modname("xor"), pa
)
575 self
.m
.d
.comb
+= pa
.a
.eq(self
.sig
)
578 # not needed: Value.implies does the job
579 # def implies(premise, conclusion):
581 # TODO. contains a Value.cast which means an override is needed (on both)
582 # def bit_select(self, offset, width):
583 # def word_select(self, offset, width):
585 # not needed: Value.matches, amazingly, should do the job
586 # def matches(self, *patterns):
588 # TODO, http://bugs.libre-riscv.org/show_bug.cgi?id=713
590 return self
.sig
.shape()