|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | from dataclasses import dataclass |
2 | 4 |
|
3 | | -from kirin.ir import Pure, Block, Statement |
| 5 | +from kirin import ir |
4 | 6 | from kirin.rewrite.abc import RewriteRule, RewriteResult |
5 | 7 |
|
6 | 8 |
|
| 9 | +@dataclass |
| 10 | +class Info: |
| 11 | + """An object to hold the comparison information of a statement.""" |
| 12 | + |
| 13 | + head: type[ir.Statement] |
| 14 | + args: tuple[ir.SSAValue, ...] |
| 15 | + attributes: tuple[ir.Attribute, ...] |
| 16 | + successors: tuple[ir.Block, ...] |
| 17 | + regions: tuple[ir.Region, ...] |
| 18 | + |
| 19 | + def __hash__(self) -> int: |
| 20 | + return hash( |
| 21 | + (id(self.head),) |
| 22 | + + tuple(id(ssa) for ssa in self.args) |
| 23 | + + tuple(hash(attr) for attr in self.attributes) |
| 24 | + + tuple(id(succ) for succ in self.successors) |
| 25 | + + tuple(id(region) for region in self.regions) |
| 26 | + ) |
| 27 | + |
| 28 | + def __eq__(self, other: object) -> bool: |
| 29 | + if not isinstance(other, Info): |
| 30 | + return False |
| 31 | + |
| 32 | + return ( |
| 33 | + self.head == other.head |
| 34 | + and self.args == other.args |
| 35 | + and self.attributes == other.attributes |
| 36 | + and self.successors == other.successors |
| 37 | + and self.regions == other.regions |
| 38 | + ) |
| 39 | + |
| 40 | + |
7 | 41 | @dataclass |
8 | 42 | class CommonSubexpressionElimination(RewriteRule): |
9 | 43 |
|
10 | | - def rewrite_Block(self, node: Block) -> RewriteResult: |
11 | | - seen: dict[int, Statement] = {} |
| 44 | + def rewrite_Block(self, node: ir.Block) -> RewriteResult: |
| 45 | + seen: dict[Info, ir.Statement] = {} |
12 | 46 |
|
13 | 47 | for stmt in node.stmts: |
14 | | - if not stmt.has_trait(Pure): |
| 48 | + if not stmt.has_trait(ir.Pure): |
15 | 49 | continue |
16 | 50 |
|
17 | 51 | if stmt.regions: |
18 | 52 | continue |
19 | 53 |
|
20 | | - hash_value = hash( |
21 | | - (type(stmt),) |
22 | | - + tuple(stmt.args) |
23 | | - + tuple(stmt.attributes.values()) |
24 | | - + tuple(stmt.successors) |
25 | | - + tuple(stmt.regions) |
| 54 | + info = Info( |
| 55 | + head=type(stmt), |
| 56 | + args=tuple(stmt.args), |
| 57 | + attributes=tuple(stmt.attributes.values()), |
| 58 | + successors=tuple(stmt.successors), |
| 59 | + regions=tuple(stmt.regions), |
26 | 60 | ) |
27 | | - if hash_value in seen: |
28 | | - old_stmt = seen[hash_value] |
| 61 | + if info in seen: |
| 62 | + old_stmt = seen[info] |
29 | 63 | for result, old_result in zip(stmt._results, old_stmt.results): |
30 | 64 | result.replace_by(old_result) |
31 | 65 | stmt.delete() |
32 | 66 | return RewriteResult(has_done_something=True) |
33 | 67 | else: |
34 | | - seen[hash_value] = stmt |
| 68 | + seen[info] = stmt |
35 | 69 | return RewriteResult() |
36 | 70 |
|
37 | | - def rewrite_Statement(self, node: Statement) -> RewriteResult: |
| 71 | + def rewrite_Statement(self, node: ir.Statement) -> RewriteResult: |
38 | 72 | if not node.regions: |
39 | 73 | return RewriteResult() |
40 | 74 |
|
|
0 commit comments