Skip to content

Commit

Permalink
Tolerate recursively-useless phi-nodes
Browse files Browse the repository at this point in the history
Also, make to_ssa more deterministic.
  • Loading branch information
sampsyo committed Sep 30, 2020
1 parent 37cfa64 commit 0b4ae8c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions bril-ts/brili.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,17 @@ function evalInstr(instr: bril.Instruction, state: State): Action {
// Last label not handled. Leave uninitialized.
state.env.delete(instr.dest);
} else {
let val = getArgument(instr, state.env, idx);
state.env.set(instr.dest, val);
// Copy the right argument (including an undefined one).
if (!instr.args || idx >= instr.args.length) {
throw error(`phi node needed at least ${idx+1} arguments`);
}
let src = instr.args[idx];
let val = state.env.get(src);
if (val === undefined) {
state.env.delete(instr.dest);
} else {
state.env.set(instr.dest, val);
}
}
return NEXT;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/to_ssa.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _rename(block):
phi_args[s][p].append((block, stack[p][0]))

# Recursive calls.
for b in domtree[block]:
for b in sorted(domtree[block]):
_rename(b)

# Restore stacks.
Expand All @@ -90,7 +90,7 @@ def _rename(block):

def insert_phis(blocks, phi_args, phi_dests, types):
for block, instrs in blocks.items():
for dest, pairs in phi_args[block].items():
for dest, pairs in sorted(phi_args[block].items()):
phi = {
'op': 'phi',
'dest': phi_dests[block][dest],
Expand Down

0 comments on commit 0b4ae8c

Please sign in to comment.