Skip to content

Commit 7464bbc

Browse files
authored
Add missing subsume uses in egraph rules (#7879)
* Fix a few egraph rules that needed `subsume` There were a few rules that dropped value references from the LHS without using subsume. I think they were probably benign as they produced constant results, but this change is in the spirit of our revised guidelines for egraph rules. * Augment egraph rule guideline 2 to talk about constants
1 parent 5b2ae83 commit 7464bbc

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

cranelift/codegen/src/opts/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ of it boils down to the fact that, unlike traditional e-graphs, our rules are
3838
place of `x` in such cases would introduce uses of `y` where it is not
3939
defined.
4040

41+
An exception to this rule is discarding constants, as they can be
42+
rematerialized anywhere without introducing correctness issues. For example,
43+
the (admittedly silly) rule `(select 1 x (iconst_u _)) => x` would be a good
44+
candidate for not using `subsume`, as it does not discard any non-constant
45+
values introduced in its LHS.
46+
4147
3. Avoid overly general rewrites like commutativity and associativity. Instead,
4248
prefer targeted instances of the rewrite (for example, canonicalizing adds
4349
where one operand is a constant such that the constant is always the add's

cranelift/codegen/src/opts/extends.isle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
(slt ty
3030
(uextend $I64 x @ (value_type $I32))
3131
(iconst_u _ 0)))
32-
(iconst_u ty 0))
32+
(subsume (iconst_u ty 0)))
3333
(rule (simplify
3434
(sge ty
3535
(uextend $I64 x @ (value_type $I32))
3636
(iconst_u _ 0)))
37-
(iconst_u ty 1))
37+
(subsume (iconst_u ty 1)))
3838

3939
;; Sign-extending can't change whether a number is zero nor how it signed-compares to zero
4040
(rule (simplify (eq _ (sextend _ x@(value_type ty)) (iconst_s _ 0)))

cranelift/codegen/src/opts/icmp.isle

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
;; `x == x` is always true for integers; `x != x` is false. Strict
44
;; inequalities are false, and loose inequalities are true.
5-
(rule (simplify (eq (ty_int ty) x x)) (iconst_u ty 1))
6-
(rule (simplify (ne (ty_int ty) x x)) (iconst_u ty 0))
7-
(rule (simplify (ugt (ty_int ty) x x)) (iconst_u ty 0))
8-
(rule (simplify (uge (ty_int ty) x x)) (iconst_u ty 1))
9-
(rule (simplify (sgt (ty_int ty) x x)) (iconst_u ty 0))
10-
(rule (simplify (sge (ty_int ty) x x)) (iconst_u ty 1))
11-
(rule (simplify (ult (ty_int ty) x x)) (iconst_u ty 0))
12-
(rule (simplify (ule (ty_int ty) x x)) (iconst_u ty 1))
13-
(rule (simplify (slt (ty_int ty) x x)) (iconst_u ty 0))
14-
(rule (simplify (sle (ty_int ty) x x)) (iconst_u ty 1))
5+
(rule (simplify (eq (ty_int ty) x x)) (subsume (iconst_u ty 1)))
6+
(rule (simplify (ne (ty_int ty) x x)) (subsume (iconst_u ty 0)))
7+
(rule (simplify (ugt (ty_int ty) x x)) (subsume (iconst_u ty 0)))
8+
(rule (simplify (uge (ty_int ty) x x)) (subsume (iconst_u ty 1)))
9+
(rule (simplify (sgt (ty_int ty) x x)) (subsume (iconst_u ty 0)))
10+
(rule (simplify (sge (ty_int ty) x x)) (subsume (iconst_u ty 1)))
11+
(rule (simplify (ult (ty_int ty) x x)) (subsume (iconst_u ty 0)))
12+
(rule (simplify (ule (ty_int ty) x x)) (subsume (iconst_u ty 1)))
13+
(rule (simplify (slt (ty_int ty) x x)) (subsume (iconst_u ty 0)))
14+
(rule (simplify (sle (ty_int ty) x x)) (subsume (iconst_u ty 1)))
1515

1616
;; Optimize icmp-of-icmp.
1717
(rule (simplify (ne ty

0 commit comments

Comments
 (0)