1from __future__
import annotations
3from collections.abc
import Sequence
5from copy
import copy, deepcopy
8An expression is an object with a value and a type. Since DSLHyPE
9operates predominantly over loops all expressions must provide an index_at
10function. Types that do not change between loop iterations (such as Elements)
11would return themselves whereas types that are being iterated over would
16 def print(self, printer, indent_level: int = 0):
24 def index_at(self, indices: Sequence[Element | Variable], get_address =
False) -> Expression:
28An Element is an object that can be used to build an Array.
29It provides an index_at function that returns itselfsince it will commonly appear
30in DataBlock operations.
31It is also used in index calculations, so methods to switch between Index and
32Integer types are provided. These exist to be used at parsing time since any parsed
33integer will be assumed to be Integers over Indices.
40 def index_at(self, indices: Sequence[Element | Variable], get_address =
False) -> Expression:
51 def __add__(self, rhs: Element | Variable) -> Element | Variable:
52 if isinstance(self, Index)
and isinstance(self.
value, int)
and isinstance(rhs, Index)
and isinstance(rhs.value, int):
54 if isinstance(self, Integer)
and isinstance(self.
value, int)
and isinstance(rhs, Integer)
and isinstance(rhs.value, int):
56 if isinstance(rhs, Integer)
and rhs.value == 0:
58 if isinstance(self, Integer)
and self.
value == 0:
62 def __sub__(self, rhs: Element | Variable) -> Element | Variable:
63 if isinstance(self, Index)
and isinstance(self.
value, int)
and isinstance(rhs, Index)
and isinstance(rhs.value, int):
65 if isinstance(self, Integer)
and isinstance(self.
value, int)
and isinstance(rhs, Integer)
and isinstance(rhs.value, int):
67 if isinstance(rhs, Integer)
and rhs.value == 0:
69 if isinstance(self, Integer)
and self.
value == 0:
71 if isinstance(rhs, ElementUnaryOperation):
72 return self + rhs.operand
76 if isinstance(self, Index)
and isinstance(self.
value, int):
78 if isinstance(self, Integer)
and isinstance(self.
value, int):
82 def __mul__(self, rhs: Element | Variable) -> Element | Variable:
83 if isinstance(self, Index)
and isinstance(self.
value, int)
and isinstance(rhs, Index)
and isinstance(rhs.value, int):
85 if isinstance(self, Integer)
and isinstance(self.
value, int)
and isinstance(rhs, Integer)
and isinstance(rhs.value, int):
87 if isinstance(rhs, Integer)
and rhs.value == 1:
89 if isinstance(self, Integer)
and self.
value == 1:
94An Array is an abstraction over DataBlocks and Matrices.
97 def __init__(self, element_type: TElement, size: Sequence[Element | Variable], flatten: bool):
112 def linearise_index(indices: Sequence[Element | Variable], dimensions: Sequence[Element | Variable]) -> Element | Variable:
114 if len(indices) < len(dimensions):
115 for length
in dimensions[0:-len(indices)]:
116 scaling = scaling * length
120 indices_subset = indices[-len(dimensions):]
121 dimensions_subset = dimensions[-len(indices):]
122 linearised_index =
Index(0)
125 for i, index
in enumerate(indices_subset):
126 interval = dimensions_subset[i]
127 if isinstance(interval, Index)
and interval.value == 1:
130 if isinstance(index, Variable):
134 assert(isinstance(node, ElementBinaryOperation))
135 while isinstance(node, ElementBinaryOperation):
137 assert(isinstance(node, Variable))
139 linearised_index = linearised_index + factor * (index - variable)
141 linearised_index = linearised_index + factor * index
142 factor = factor * dimensions_subset[i]
143 return linearised_index * scaling
146Expressions are by default not assignable, as they represent temporary values. For
147example, you cannot assign a value to (6 + 2). AssignableExpressions are those
148which can be assigned to, such as variables, subscripts and slices. They must refer
149to some variable to modify, either directly or indirectly, so AssignableExpressions
150must be able to retrieve the name of this variable.
158 def index_at(self, indices: Sequence[Element | Variable], get_address =
False) -> AssignableExpression:
162 symbol_map: dict[Name, Expression] = {}
171 return Variable.symbol_map[self.
name]
174 if self.
name in Variable.symbol_map:
176 raise Exception(f
"Variable name {self.name.id} is not stored in Variable.symbol_map")
178 def print(self, printer, indent_level: int = 0):
179 return printer.print_variable(self, indent_level)
183 assert(isinstance(value, Element))
184 value = value.convert_integers_to_indices()
185 Variable.symbol_map[self.
name] = value
190 assert(isinstance(value, Element))
191 value = value.convert_indices_to_integers()
192 Variable.symbol_map[self.
name] = value
195 def index_at(self, indices: Sequence[Element | Variable], get_address =
False) -> AssignableExpression:
196 if isinstance(self.
get_type(), Element):
200 assert (isinstance(array, Array))
201 return Subscript(self, array.calculate_indices(indices), flatten=array.flatten, get_address=get_address)
203 def __add__(self, rhs: Element | Variable) -> Variable | Element:
204 if isinstance(self.
get_type(), TElement)
and isinstance(rhs.get_type(), TElement):
205 if isinstance(rhs, Integer)
and rhs.value == 0:
208 raise Exception(
"DataBlockBinaryOperation is not supported yet")
210 def __sub__(self, rhs: Element | Variable) -> Variable | Element:
211 if isinstance(self.
get_type(), TElement)
and isinstance(rhs.get_type(), TElement):
212 if isinstance(rhs, Integer)
and rhs.value == 0:
214 if isinstance(self, Integer)
and self.
value == 0:
216 if isinstance(rhs, ElementUnaryOperation)
and rhs.operator == Operator.SUBTRACTION:
219 raise Exception(
"DataBlockBinaryOperation is not supported yet")
222 if isinstance(self.
get_type(), TElement):
224 raise Exception(
"DataBlockUnaryOperation is not supported yet")
226 def __mul__(self, rhs: Element | Variable) -> Variable | Element:
227 if isinstance(self.
get_type(), TElement)
and isinstance(rhs.get_type(), TElement):
228 if isinstance(rhs, Integer)
and rhs.value == 1:
231 raise Exception(
"DataBlockBinaryOperation is not supported yet")
233 def __div__(self, rhs: Element | Variable) -> Element:
234 if isinstance(self.
get_type(), TElement)
and isinstance(rhs.get_type(), TElement):
235 if not (isinstance(self.
get_type(), TFloat)
and isinstance(rhs.get_type(), TFloat)):
236 raise Exception(
"Division is only supported between expressions with type TFloat")
238 raise Exception(
"DataBlockBinaryOperation is not supported yet")
255 def print(self, printer, indent_level: int = 0):
256 return printer.print_integer(self, indent_level)
268 def print(self, printer, indent_level: int = 0):
269 return printer.print_index(self, indent_level)
284 def print(self, printer, indent_level: int = 0):
285 return printer.print_float(self, indent_level)
295 def __init__(self, operator: Operator, lhs: Element | Variable | Subscript, rhs: Element | Variable | Subscript):
296 if isinstance(lhs, Variable)
and not isinstance(lhs.get_value(), Element)
or \
297 isinstance(rhs, Variable)
and not isinstance(rhs.get_value(), Element):
298 raise Exception(
"Variable operands of ElementBinaryOperation must be Elements")
317 def print(self, printer, indent_level: int = 0):
318 return printer.print_element_binary_operation(self, indent_level)
322 def __init__(self, operator: Operator, operand: Element | Variable):
334 assert(isinstance(type, TElement))
337 def print(self, printer, indent_level: int = 0):
338 return printer.print_element_unary_operation(self, indent_level)
341 def __init__(self, element: Element, index: Index):
354 def print(self, printer, indent_level: int = 0):
355 return printer.print_element_index(self, indent_level)
361 def index_at(self, indices: Sequence[Element], get_address =
False) ->
None:
362 raise Exception(
"External references should not be indexed")
365 raise Exception(
"External references have no type to obtain")
367 def print(self, printer, indent_level: int = 0):
368 raise Exception(
"External references should not have print called on them")
371 def __init__(self, struct_name: Name, struct_type: TStruct, field_name: Name):
381 def __init__(self, iteration_range: Sequence[Sequence[Element | Variable]], value: Element | ExternalStructField | FunctionCall |
None, flatten):
385 size_in_memory: list[Element | Variable] = []
387 size_in_memory.append(interval[1] - interval[0])
390 if isinstance(value, Element):
391 element_type = value.get_type()
392 elif isinstance(value, ExternalStructField):
393 external_type = value.get_type()
394 assert(isinstance(external_type, TArray))
395 element_type = external_type.element_type
400 Array.__init__(self, element_type, size_in_memory,
False)
402 flattened_size =
Index(1)
404 flattened_size = flattened_size * size
405 Array.__init__(self, element_type, [self.
size_in_memory[-1], flattened_size], flatten=flatten)
412 outer_index = indices[-1]
413 inner_index = Array.linearise_index(indices[0:-1], self.
size_in_memory[:-1])
414 return [outer_index, inner_index]
419 def print(self, printer, indent_level: int = 0):
422 def index_at(self, indices: Sequence[Element | Variable], get_address =
False) -> Expression:
423 raise Exception(
"A DataBlock should not be indexed directly, only its slices or variables should be indexed.")
426 def __init__(self, original_data_block: Variable, iteration_range: list[list[Element | Variable]]):
428 DataBlock.__init__(self, iteration_range,
None, original_data_block.get_value().flatten)
431 def index_at(self, indices: Sequence[Element | Variable], get_address =
False) -> Expression:
433 assert (isinstance(data_block, DataBlock)
or isinstance(data_block, DataBlockSlice))
435 offset_indices: list[Element | Variable] = []
436 for i, index
in enumerate(indices):
437 offset_indices.append(index + self.
iteration_range[i + len(self.
iteration_range) - len(indices)][0] - data_block.iteration_range[i + len(data_block.iteration_range) - len(indices)][0])
445 assert(isinstance(data_block, DataBlock))
448 def print(self, printer, indent_level: int = 0):
452 def __init__(self, operator: Operator, lhs: Expression, rhs: Expression):
458 if isinstance(self.
lhs, Variable):
459 lhs_value = self.
lhs.get_value()
460 assert(isinstance(lhs_value, DataBlock))
461 lhs_iteration_range = lhs_value.iteration_range
464 lhs_iteration_range = self.
lhs.iteration_range
466 if isinstance(self.
rhs, Variable):
467 rhs_value = self.
rhs.get_value()
468 assert(isinstance(rhs_value, DataBlock))
469 rhs_iteration_range = rhs_value.iteration_range
472 rhs_iteration_range = self.
rhs.iteration_range
476 iteration_range = deepcopy(lhs_iteration_range)
477 if len(iteration_range) == 1:
479 iteration_range = deepcopy(rhs_iteration_range)
481 lhs_range = iteration_range[0][1] - iteration_range[0][0]
482 rhs_range = rhs_iteration_range[0][1] - rhs_iteration_range[0][0]
483 if isinstance(lhs_range, Index)
and isinstance(rhs_range, Index):
484 assert(isinstance(lhs_range.value, int))
485 assert(isinstance(rhs_range.value, int))
486 if rhs_range.value > lhs_range.value:
487 iteration_range[0][0] = rhs_iteration_range[0][0]
488 iteration_range[0][1] = rhs_iteration_range[0][1]
489 elif isinstance(lhs_range, Index)
and isinstance(lhs_range.value, int)
and lhs_range.value == 1:
491 iteration_range[0][0] = rhs_iteration_range[0][0]
492 iteration_range[0][1] = rhs_iteration_range[0][1]
495 if isinstance(self.
lhs, Variable):
496 lhs_value = self.
lhs.get_value()
497 assert(isinstance(lhs_value, DataBlock))
498 lhs_iteration_range = lhs_value.iteration_range
501 lhs_iteration_range = self.
lhs.iteration_range
502 iteration_range = deepcopy(lhs_iteration_range)
504 if isinstance(self.
rhs, Variable):
505 rhs_value = self.
rhs.get_value()
506 assert(isinstance(rhs_value, DataBlock))
507 rhs_iteration_range = rhs_value.iteration_range
510 rhs_iteration_range = self.
rhs.iteration_range
511 iteration_range = deepcopy(rhs_iteration_range)
513 super().
__init__(iteration_range,
None, TArray.flatten_temporaries)
515 def index_at(self, indices: Sequence[Element | Variable], get_address =
False) -> Expression:
518 assert(isinstance(lhs, Element)
or isinstance(lhs, Variable)
or isinstance(lhs, Subscript))
519 assert(isinstance(rhs, Element)
or isinstance(rhs, Variable)
or isinstance(rhs, Subscript))
523 def __init__(self, operator: Operator, operand: Expression):
524 assert(isinstance(operand.get_type(), TDataBlock))
525 if isinstance(operand, Variable):
526 iteration_range = operand.get_value().iteration_range
527 flatten = operand.get_value().flatten
529 iteration_range = operand.iteration_range
530 flatten = operand.flatten
534 super().
__init__(iteration_range,
None, flatten)
537 def index_at(self, indices: Sequence[Element | Variable], get_address =
False) -> Expression:
539 assert(isinstance(element, Element)
or isinstance(element, Variable)
or isinstance(element, Subscript))
543 def __init__(self, vector_data_block: Variable, index: Index):
546 data_block_type = vector_data_block.get_type()
547 assert(isinstance(data_block_type, TDataBlock))
548 assert(isinstance(data_block_type.element_type, TVector))
549 super().
__init__(vector_data_block.get_value().iteration_range,
None, data_block_type.flatten)
551 def index_at(self, indices: Sequence[Element | Variable], get_address =
False) -> Expression:
555 def __init__(self, base: AssignableExpression, indices: list[Element | Variable], flatten =
False, get_address =
False):
556 assert(len(indices) == 1
or len(indices) == 2)
557 for index
in indices:
558 if not isinstance(index.get_type(), TIndex):
559 raise Exception(
"Index of subscript must be of index type")
566 assert (isinstance(self.
base, Variable)
and isinstance(self.
base.get_value(), Array))
575 def index_at(self, indices: Sequence[Element], get_address =
False) -> Subscript:
578 def print(self, printer, indent_level: int = 0):
579 return printer.print_subscript(self, indent_level)
Element|Variable linearise_index(Sequence[Element|Variable] indices, Sequence[Element|Variable] dimensions)
list[Element|Variable] calculate_indices(self, Sequence[Element|Variable] indices)
__init__(self, TElement element_type, Sequence[Element|Variable] size, bool flatten)
AssignableExpression index_at(self, Sequence[Element|Variable] indices, get_address=False)
__init__(self, Operator operator, Expression lhs, Expression rhs)
Expression index_at(self, Sequence[Element|Variable] indices, get_address=False)
__init__(self, Variable vector_data_block, Index index)
Expression index_at(self, Sequence[Element|Variable] indices, get_address=False)
Expression index_at(self, Sequence[Element|Variable] indices, get_address=False)
__init__(self, Variable original_data_block, list[list[Element|Variable]] iteration_range)
TDataBlock get_type(self)
print(self, printer, int indent_level=0)
__init__(self, Operator operator, Expression operand)
Expression index_at(self, Sequence[Element|Variable] indices, get_address=False)
print(self, printer, int indent_level=0)
Expression index_at(self, Sequence[Element|Variable] indices, get_address=False)
list[Element|Variable] calculate_indices(self, Sequence[Element|Variable] indices)
TDataBlock get_type(self)
__init__(self, Sequence[Sequence[Element|Variable]] iteration_range, Element|ExternalStructField|FunctionCall|None value, flatten)
ElementBinaryOperation convert_indices_to_integers(self)
__init__(self, Operator operator, Element|Variable|Subscript lhs, Element|Variable|Subscript rhs)
ElementBinaryOperation convert_integers_to_indices(self)
print(self, printer, int indent_level=0)
__init__(self, Element element, Index index)
ElementUnaryOperation convert_indices_to_integers(self)
ElementUnaryOperation convert_integers_to_indices(self)
print(self, printer, int indent_level=0)
print(self, printer, int indent_level=0)
ElementUnaryOperation convert_integers_to_indices(self)
__init__(self, Operator operator, Element|Variable operand)
ElementUnaryOperation convert_indices_to_integers(self)
Element convert_integers_to_indices(self)
Element|Variable __mul__(self, Element|Variable rhs)
Element|Variable __sub__(self, Element|Variable rhs)
Element|Variable __add__(self, Element|Variable rhs)
Element|Variable __neg__(self)
Expression index_at(self, Sequence[Element|Variable] indices, get_address=False)
Element convert_indices_to_integers(self)
print(self, printer, int indent_level=0)
Expression index_at(self, Sequence[Element|Variable] indices, get_address=False)
__init__(self, Name struct_name, TStruct struct_type, Name field_name)
__init__(self, str reference)
print(self, printer, int indent_level=0)
None index_at(self, Sequence[Element] indices, get_address=False)
print(self, printer, int indent_level=0)
Float convert_indices_to_integers(self)
__init__(self, float|External value)
Float convert_integers_to_indices(self)
Index convert_integers_to_indices(self)
Integer convert_indices_to_integers(self)
print(self, printer, int indent_level=0)
print(self, printer, int indent_level=0)
Integer convert_indices_to_integers(self)
__init__(self, int|External value)
Index convert_integers_to_indices(self)
print(self, printer, int indent_level=0)
Subscript index_at(self, Sequence[Element] indices, get_address=False)
__init__(self, AssignableExpression base, list[Element|Variable] indices, flatten=False, get_address=False)
Variable|Element __mul__(self, Element|Variable rhs)
Variable|Element __add__(self, Element|Variable rhs)
print(self, printer, int indent_level=0)
Variable convert_indices_to_integers(self)
Expression get_value(self)
Variable convert_integers_to_indices(self)
Element __div__(self, Element|Variable rhs)
AssignableExpression index_at(self, Sequence[Element|Variable] indices, get_address=False)
Variable|Element __sub__(self, Element|Variable rhs)
__init__(self, Name name)