This is an archive of the discontinued LLVM Phabricator instance.

[WinEH] Update LCSSA to handle catchswitch with handlers inside and outside a loop
ClosedPublic

Authored by andrew.w.kaylor on Dec 17 2015, 4:41 PM.

Details

Summary

When a try block occurs inside a loop with one catch handler that continues in the loop and another that exits the loop, the catchswitch instruction (which returns a token value) will be used by blocks inside and outside the loop. This causes the LCSSA pass to want to create a PHI node in the block outside the loop to reference the catchswitch, but that isn't allowed.

This patch updates the LCSSA pass and the Loop::isLCSSAForm function to skip over token values.

Diff Detail

Repository
rL LLVM

Event Timeline

andrew.w.kaylor retitled this revision from to [WinEH] Update LCSSA to handle catchswitch with handlers inside and outside a loop.
andrew.w.kaylor updated this object.
andrew.w.kaylor set the repository for this revision to rL LLVM.
andrew.w.kaylor added a subscriber: llvm-commits.
majnemer added inline comments.Dec 17 2015, 4:49 PM
test/CodeGen/X86/catch-lcssa.ll
1 ↗(On Diff #43195)

Can we move this test to "test/Transforms/LCSSA" and just use "opt -lcssa -S" ?

mzolotukhin added inline comments.
test/CodeGen/X86/catch-lcssa.ll
1 ↗(On Diff #43195)

Also, could you please run "opt -instnamer" on the test?

test/CodeGen/X86/catch-lcssa.ll
1 ↗(On Diff #43195)

The reason I put it here was so that it wouldn't choke the non-X86 buildbots. I know I could add a REQUIRES line to the test, but it doesn't seem like that is the typical way this is handled.

I'll see if I can strip it down enough to work without referencing anything X86-specific.

rnk added inline comments.Dec 17 2015, 5:54 PM
test/CodeGen/X86/catch-lcssa.ll
1 ↗(On Diff #43195)

You can use an x86 target triple in opt without the x86 backend just fine. There should be other examples of this.

test/CodeGen/X86/catch-lcssa.ll
1 ↗(On Diff #43195)

I would have thought so, but I broke a bunch of buildbots that way recently. In that case I was using the triple in the command line to run the test. Is it possible that just having it embedded in the IR yields a different result?

majnemer added inline comments.Dec 17 2015, 6:42 PM
test/CodeGen/X86/catch-lcssa.ll
1 ↗(On Diff #43195)

IIRC, the test you added was for CodeGenPrep which is fundamentally a target-specific test. This is test is not so it should be fine to keep it generic.

Moved and cleaned up test.

majnemer accepted this revision.Dec 17 2015, 8:22 PM
majnemer edited edge metadata.

LGTM

This revision is now accepted and ready to land.Dec 17 2015, 8:22 PM
This revision was automatically updated to reflect the committed changes.
reames added a subscriber: reames.Dec 18 2015, 10:16 AM

I'm a bit concerned by this change. Specifically, I'm worried that various optimization passes have built in assumptions about what LCSSA means. If an optimization pass expects to have to only update PHIs in loop exists, this change could lead to crashes or potentially silent miscompiles. Have you reviewed the loop transform passes to understand the implications of the changed invariant?

I'm a bit concerned by this change. Specifically, I'm worried that various optimization passes have built in assumptions about what LCSSA means. If an optimization pass expects to have to only update PHIs in loop exists, this change could lead to crashes or potentially silent miscompiles. Have you reviewed the loop transform passes to understand the implications of the changed invariant?

David tells me that CodeMetrics::analyzeBasicBlock already disables loop optimizations involving live-out token type SSA values.

I just reviewed all the passes that are calling either LoopInfo::isLCSSAForm() or LoopInfo::isRecursivelyLCSSAForm(), and it does appear to me that they will be safe with this change. Loop unroll and loop unswitch both call CodeMetrics::analyzeBasicBlock and I verified experimentally that the sort of code involved in this patch won't be unrolled. LCIM is slightly more involved, but I believe that it won't move code involving tokens. Finally, IndVarSimplify does exactly what you are suggesting (i.e. uses the existence of PHI nodes to determine whether a value is used on an exit path) but again I don't think there is a risk of it operating on token values.

Thanks for checking. SGTM

Philip