Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ne, eq, fix fold bug. #106

Merged
merged 12 commits into from
Jan 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion examples/lvn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Numbering(dict):
"""A dict mapping anything to numbers that can generate new numbers
for you when adding new values.
"""

def __init__(self, init={}):
super(Numbering, self).__init__(init)
self._next_fresh = 0
Expand Down Expand Up @@ -160,7 +161,7 @@ def lvn_block(block, lookup, canonicalize, fold):
if val:
# Is this value foldable to a constant?
const = fold(num2const, val)
if const:
if const is not None:
num2const[newnum] = const
instr.update({
'op': 'const',
Expand Down Expand Up @@ -196,6 +197,8 @@ def _lookup(value2num, value):
'lt': lambda a, b: a < b,
'ge': lambda a, b: a >= b,
'le': lambda a, b: a <= b,
'ne': lambda a, b: a != b,
'eq': lambda a, b: a == b,
}


Expand All @@ -205,6 +208,10 @@ def _fold(num2const, value):
const_args = [num2const[n] for n in value.args]
return FOLDABLE_OPS[value.op](*const_args)
except KeyError: # At least one argument is not a constant.
if value.op in {'eq', 'ne', 'le', 'ge'} and value.args[0] == value.args[1]:
# Equivalent arguments may be evaluated for equality.
cgyurgyik marked this conversation as resolved.
Show resolved Hide resolved
# E.g. `eq x x`, where `x` is not a constant evaluates to `true`.
return value.op != 'ne'
return None
except ZeroDivisionError: # If we hit a dynamic error, bail!
return None
Expand Down
25 changes: 25 additions & 0 deletions examples/test/lvn/fold-comparisons.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ARGS: -f

@main(arg1: int, arg2: int) {
a: int = const 4;
b: int = const 3;
constant_fold1: bool = ne b a;
constant_fold2: bool = eq a b;
constant_fold3: bool = le a b;
constant_fold4: bool = lt b a;
constant_fold5: bool = gt b a;
constant_fold6: bool = ge b a;

should_fold1: bool = eq arg1 arg1;
should_fold2: bool = ne arg1 arg1;
should_fold3: bool = le arg1 arg1;
should_fold4: bool = ge arg1 arg1;

no_fold1: bool = eq arg1 arg2;
no_fold2: bool = ne arg1 arg2;
no_fold3: bool = le arg1 arg2;
no_fold4: bool = ge arg1 arg2;

no_fold5: bool = lt arg1 arg1;
no_fold6: bool = gt arg2 arg2;
}
20 changes: 20 additions & 0 deletions examples/test/lvn/fold-comparisons.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@main(arg1: int, arg2: int) {
a: int = const 4;
b: int = const 3;
constant_fold1: bool = const true;
constant_fold2: bool = const false;
constant_fold3: bool = const false;
constant_fold4: bool = const true;
constant_fold5: bool = const false;
constant_fold6: bool = const false;
should_fold1: bool = const true;
should_fold2: bool = const false;
should_fold3: bool = const true;
should_fold4: bool = const true;
no_fold1: bool = eq arg1 arg2;
no_fold2: bool = ne arg1 arg2;
no_fold3: bool = le arg1 arg2;
no_fold4: bool = ge arg1 arg2;
no_fold5: bool = lt arg1 arg1;
no_fold6: bool = gt arg2 arg2;
}