This is an archive of the discontinued LLVM Phabricator instance.

[GlobalISel] correctly record PHI sources coming from switch instructions
ClosedPublic

Authored by t.p.northover on Jan 11 2017, 2:09 PM.

Details

Reviewers
qcolombet
Summary

GlobalISel lowers switches to a sequence of conditional branches spanning multiple basic blocks. This means that a single CFG edge at the IR level can change when the MIR has been generated. Successors are still well-behaved, but when querying the MachineBasicBlock predecessors (e.g. to translate a PHI), a more sophisticated analysis than getOrCreateBB is needed.

This adds a callback to IRTranslator that every translation which modifies the CFG (currently only switch translation) should be aware of. finishPendingPHIs then uses this extra information to create a correct PHI at the end of translation. If a switch instruction isn't involved in any of the PHI's predecessors then we fall back to the usually sufficient bijective map from BasicBlocks to MachineBasicBlocks.

This should fix at least one failure in the test-suite bot under GlobalISel (halide).

Diff Detail

Repository
rL LLVM

Event Timeline

t.p.northover retitled this revision from to [GlobalISel] correctly record PHI sources coming from switch instructions.
t.p.northover updated this object.
t.p.northover added a reviewer: qcolombet.
t.p.northover set the repository for this revision to rL LLVM.
t.p.northover added a subscriber: llvm-commits.
rovka added inline comments.Jan 12 2017, 2:03 AM
include/llvm/CodeGen/GlobalISel/IRTranslator.h
408

Typo: predecessor

qcolombet edited edge metadata.Jan 12 2017, 10:01 AM

Hi Tim,

Looks mostly good to me, the only thing I think we miss is clearing the map for the edges at the end of the function.
Nothing incorrect, just wasted memory :).

Cheers,
-Quentin

include/llvm/CodeGen/GlobalISel/IRTranslator.h
405

const CFGEdge &, MachineBasicBlock &

408

typo: predecessor

411

const CFGEdge &?

t.p.northover added inline comments.Jan 13 2017, 1:33 PM
include/llvm/CodeGen/GlobalISel/IRTranslator.h
405

I did think about this at the time, but decided a pair of pointers was simple enough to pass by value. I can change it, but I'm not convinced there's a strong argument either way.

t.p.northover edited edge metadata.

Fix typo & clear data between functions. Uploading while I've got it handy.

t.p.northover marked 2 inline comments as done.Jan 13 2017, 1:53 PM
qcolombet accepted this revision.Jan 13 2017, 1:58 PM
qcolombet edited edge metadata.

Hi Tim,

LGTM, one nitpick below.

Cheers,
-Quentin

include/llvm/CodeGen/GlobalISel/IRTranslator.h
405

Fine by me. I would still turn the pointer to NewPred into a reference though. That makes it obvious nullptr is not a valid argument :).

This revision is now accepted and ready to land.Jan 13 2017, 1:58 PM
t.p.northover added inline comments.Jan 13 2017, 2:59 PM
include/llvm/CodeGen/GlobalISel/IRTranslator.h
405

I'm not so sure about that one either. The problem is that maps can't contain references, which means the querying interface essentially has to use pointers. Having those out of sync seems a bit worse than passing a raw pointer to me.

qcolombet added inline comments.Jan 13 2017, 3:16 PM
include/llvm/CodeGen/GlobalISel/IRTranslator.h
405

Fair enough.
I would add an assert that NewPred is not null in that method.

t.p.northover closed this revision.Jan 13 2017, 3:22 PM

Thanks Quentin. Committed as r291973. Hopefully the bot should be green soonish.

include/llvm/CodeGen/GlobalISel/IRTranslator.h
405

Good idea.