This is an archive of the discontinued LLVM Phabricator instance.

[TailDuplicator] Fix merging block with terminator
ClosedPublic

Authored by sebastian-ne on Oct 21 2021, 8:15 AM.

Details

Summary

The TailDuplicator merged two blocks, even if the first one ended with
a terminator, resulting in invalid MIR, where a terminator is in the
middle of a block.

Abort merging if the first block ends with a terminator.

Diff Detail

Event Timeline

sebastian-ne created this revision.Oct 21 2021, 8:15 AM
sebastian-ne requested review of this revision.Oct 21 2021, 8:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 21 2021, 8:15 AM
foad added inline comments.Oct 21 2021, 8:41 AM
llvm/lib/CodeGen/TailDuplicator.cpp
974

Isn't there a danger that this will fall through to the code that updates phi nodes, even though we didn't merge anything?

sebastian-ne added inline comments.Oct 22 2021, 3:20 AM
llvm/lib/CodeGen/TailDuplicator.cpp
974

No, this is just another condition for the if conditions above. I couldn’t add it there because TII->removeBranch needs to run first.
The phi updates only run for blocks in TDBBs and PrevBB is only added there inside the if.

foad added inline comments.Oct 22 2021, 3:31 AM
llvm/lib/CodeGen/TailDuplicator.cpp
974

But if TII->removeBranch returns true and then there are still tail instructions, this function will not return at line 984. What am I missing? Maybe it's impossible for there to be tail instructions after a branch is removed?

sebastian-ne marked an inline comment as done.

Maybe it's impossible for there to be tail instructions after a branch is removed?

It’s not impossible, which is exactly what this patch fixes (because S_MOV_B32_term are terminators but not branch instructions, so they stay and should prevent merging even after branch instructions are removed).

The phi updates only run for blocks in TDBBs…

Ups, you’re right, I got it wrong, it actually updates them for blocks *not* in TDBBs.

I changed it, so it returns if Changed was false before.

foad accepted this revision.Oct 28 2021, 3:28 AM

Looks reasonable to me, thanks.

This revision is now accepted and ready to land.Oct 28 2021, 3:28 AM
This revision was automatically updated to reflect the committed changes.