Skip to content

Commit

Permalink
[Expression Simplifcation] Relax checks in CollapseNestedConstants to…
Browse files Browse the repository at this point in the history
… allow more simplifcations (#425)

* Relaxe checks in collapse_nested_constants.py to allow more simplifications

* Fix exception catching

* black

* Update comment

Co-authored-by: Manuel Blatt <[email protected]>

---------

Co-authored-by: Manuel Blatt <[email protected]>
  • Loading branch information
rihi and blattm authored Jul 8, 2024
1 parent 6613dd2 commit 3ed0094
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ def apply(self, operation: Operation) -> list[tuple[Expression, Expression]]:
first, *rest = constants

# We don't need to catch UnsupportedOperationType, because check that operation is in _COLLAPSIBLE_OPERATIONS
# We don't need to catch UnsupportedMismatchedSizes, because '_collect_constants' only returns constants of the same type
# We don't need to catch UnsupportedMismatchedSizes, because '_collect_constants' only returns constants of the same size
# We don't need to catch UnsupportedValueType, because '_collect_constants' only returns constants with supported value types
try:
folded_constant = reduce(lambda c0, c1: constant_fold(operation.operation, [c0, c1], operation.type), rest, first)
except UnsupportedValueType:
return []
except IncompatibleOperandCount as e:
raise MalformedData() from e

Expand All @@ -61,14 +60,18 @@ def _collect_constants(operation: Operation) -> Iterator[Constant]:
current_operation = context_stack.pop()

for i, operand in enumerate(current_operation.operands):
if operand.type != operand_type: # This check could potentially be relaxed to only check for equal size
if operand.type.size != operand_type.size:
continue

if isinstance(operand, Operation):
if operand.operation == operation_type:
context_stack.append(operand)
continue
elif isinstance(operand, Constant) and _identity_constant(operation_type, operand_type).value != operand.value:
elif (
isinstance(operand, Constant)
and isinstance(operand.value, int)
and _identity_constant(operation_type, operand_type).value != operand.value
):
yield operand


Expand Down

0 comments on commit 3ed0094

Please sign in to comment.