|
1 |
| -from vyper.venom.analysis import DFGAnalysis, LivenessAnalysis, PhiReachingAnalysis |
2 |
| -from vyper.venom.basicblock import IRInstruction |
| 1 | +from vyper.venom.analysis import DFGAnalysis, LivenessAnalysis |
| 2 | +from vyper.venom.basicblock import IRInstruction, IRVariable |
3 | 3 | from vyper.venom.passes.base_pass import InstUpdater, IRPass
|
4 | 4 |
|
5 | 5 |
|
6 | 6 | class PhiEliminationPass(IRPass):
|
7 |
| - phi_reach: PhiReachingAnalysis |
| 7 | + phi_to_origins: dict[IRInstruction, set[IRInstruction]] |
8 | 8 |
|
9 | 9 | def run_pass(self):
|
10 | 10 | self.dfg = self.analyses_cache.request_analysis(DFGAnalysis)
|
11 | 11 | self.updater = InstUpdater(self.dfg)
|
12 |
| - self.phi_reach = self.analyses_cache.request_analysis(PhiReachingAnalysis) |
| 12 | + self._calculate_phi_origins() |
13 | 13 |
|
14 | 14 | for _, inst in self.dfg.outputs.copy().items():
|
15 | 15 | if inst.opcode != "phi":
|
16 | 16 | continue
|
17 | 17 | self._process_phi(inst)
|
18 | 18 |
|
| 19 | + # sort phis to top of basic block |
19 | 20 | for bb in self.function.get_basic_blocks():
|
20 | 21 | bb.ensure_well_formed()
|
21 | 22 |
|
22 | 23 | self.analyses_cache.invalidate_analysis(LivenessAnalysis)
|
23 | 24 |
|
24 | 25 | def _process_phi(self, inst: IRInstruction):
|
25 |
| - srcs = self.phi_reach.phi_to_origins[inst] |
| 26 | + srcs = self.phi_to_origins[inst] |
26 | 27 |
|
27 | 28 | if len(srcs) == 1:
|
28 |
| - src = next(iter(srcs)) |
| 29 | + src = srcs.pop() |
| 30 | + if src == inst: |
| 31 | + return |
29 | 32 | assert src.output is not None
|
30 | 33 | self.updater.store(inst, src.output)
|
| 34 | + |
| 35 | + def _calculate_phi_origins(self): |
| 36 | + self.dfg = self.analyses_cache.request_analysis(DFGAnalysis) |
| 37 | + self.phi_to_origins = dict() |
| 38 | + |
| 39 | + for bb in self.function.get_basic_blocks(): |
| 40 | + for inst in bb.instructions: |
| 41 | + if inst.opcode != "phi": |
| 42 | + break |
| 43 | + self._get_phi_origins(inst) |
| 44 | + |
| 45 | + def _get_phi_origins(self, inst: IRInstruction): |
| 46 | + assert inst.opcode == "phi" # sanity |
| 47 | + visited: set[IRInstruction] = set() |
| 48 | + self.phi_to_origins[inst] = self._get_phi_origins_r(inst, visited) |
| 49 | + |
| 50 | + # traverse chains of phis and stores to get the "root" instructions |
| 51 | + # for phis. |
| 52 | + def _get_phi_origins_r( |
| 53 | + self, inst: IRInstruction, visited: set[IRInstruction] |
| 54 | + ) -> set[IRInstruction]: |
| 55 | + if inst.opcode == "phi": |
| 56 | + if inst in self.phi_to_origins: |
| 57 | + return self.phi_to_origins[inst] |
| 58 | + |
| 59 | + if inst in visited: |
| 60 | + # we have hit a dfg cycle. break the recursion. |
| 61 | + # if it is only visited we have found a self |
| 62 | + # reference, and we won't find anything more by |
| 63 | + # continuing the recursion. |
| 64 | + return set() |
| 65 | + |
| 66 | + visited.add(inst) |
| 67 | + |
| 68 | + res: set[IRInstruction] = set() |
| 69 | + |
| 70 | + for _, var in inst.phi_operands: |
| 71 | + next_inst = self.dfg.get_producing_instruction(var) |
| 72 | + assert next_inst is not None, (inst, var) |
| 73 | + res |= self._get_phi_origins_r(next_inst, visited) |
| 74 | + |
| 75 | + if len(res) > 1: |
| 76 | + # if this phi has more than one origin, then for future |
| 77 | + # phis, it is better to treat this as a barrier in the |
| 78 | + # graph traversal. for example (without basic blocks) |
| 79 | + # %a = 1 |
| 80 | + # %b = 2 |
| 81 | + # %c = phi %a, %b ; has two origins |
| 82 | + # %d = %c |
| 83 | + # %e = %d |
| 84 | + # %f = phi %d, %e |
| 85 | + # in this case, %f should reduce to %c. |
| 86 | + return set([inst]) |
| 87 | + return res |
| 88 | + |
| 89 | + if inst.opcode == "store" and isinstance(inst.operands[0], IRVariable): |
| 90 | + # traverse store chain |
| 91 | + var = inst.operands[0] |
| 92 | + next_inst = self.dfg.get_producing_instruction(var) |
| 93 | + assert next_inst is not None |
| 94 | + return self._get_phi_origins_r(next_inst, visited) |
| 95 | + |
| 96 | + # root of the phi/store chain |
| 97 | + return set([inst]) |
0 commit comments