This is an archive of the discontinued LLVM Phabricator instance.

[reg2mem] Add special handling to CatchSwitchInst
ClosedPublic

Authored by Naville on Nov 24 2022, 12:45 AM.

Diff Detail

Event Timeline

Naville created this revision.Nov 24 2022, 12:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 24 2022, 12:45 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
Naville requested review of this revision.Nov 24 2022, 12:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 24 2022, 12:45 AM
Naville updated this revision to Diff 477698.Nov 24 2022, 12:45 AM
Naville updated this revision to Diff 477699.Nov 24 2022, 12:54 AM
Naville updated this revision to Diff 477731.Nov 24 2022, 3:48 AM

Add TestCase

nikic added a comment.Dec 8 2022, 5:42 AM

Can you please upload the patch with full context (-U99999)?

llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
100

You can probably combine handlers and unwind dest by iterating over successors(CSI).

158

Why does this insert a load before uses, rather than doing the same as in the previous function (insert load in each successor)?

llvm/test/Transforms/Reg2Mem/catchswitch-crach2.ll
1 ↗(On Diff #477731)

Typo in file name: crach -> crash?

8 ↗(On Diff #477731)

Can you please convert the test to use opaque pointers? This will remove these types.

I'd also replace the invoke undef with an invoke of a real function, to avoid UB. Something like this:

declare i32 @__CxxFrameHandler3(...)
declare i64 @fn() 

define void @testreg2mem(ptr %_Val) personality ptr @__CxxFrameHandler3 {
entry:
  %call.i166167 = invoke i64 @fn()
  to label %if.end56 unwind label %catch.dispatch

if.end56:                                         ; preds = %entry
  invoke void @"extfunc1"()
  to label %invoke.cont75 unwind label %catch.dispatch

invoke.cont75:                                    ; preds = %if.end56
  unreachable

catch.dispatch:                                   ; preds = %if.end56, %entry
  %_State.3 = phi i32 [ undef, %if.end56 ], [ 0, %entry ]
  %0 = catchswitch within none [label %catch] unwind label %ehcleanup105

catch:                                            ; preds = %catch.dispatch
  %1 = catchpad within %0 [ptr null, i32 64, ptr null]
  invoke void @"extfunc2"() [ "funclet"(token %1) ]
  to label %invoke.cont98 unwind label %ehcleanup105

invoke.cont98:                                    ; preds = %catch
  catchret from %1 to label %if.end99

if.end99:                                         ; preds = %invoke.cont98
  %or.i = or i32 undef, %_State.3
  unreachable

ehcleanup105:                                     ; preds = %catch, %catch.dispatch
  %2 = cleanuppad within none []
  cleanupret from %2 unwind to caller
}

declare void @"extfunc1"()

declare void @"extfunc2"()
Naville added inline comments.Dec 9 2022, 12:59 AM
llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
158

Because otherwise we need another Map to record successor and the loadinst in it?

Don't we need :

  • Insert loadinst in each immediate successor, map each successor BasicBlock with the load inst
  • For each user(which might not be inside the immediate successor), query the dominator tree, check which immediate successor dominates the user and replace the use with the loadinst in that block?
Naville added a comment.EditedDec 9 2022, 1:12 AM

DemoteReg does the same already in U->replaceUsesOfWith(&I, V);, so I think we're all good here

Naville updated this revision to Diff 481553.Dec 9 2022, 1:20 AM
nikic accepted this revision.Dec 16 2022, 1:24 AM

LGTM with some nits.

llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
100

nit: Omit braces for single-line for.

152

This should be just isa<>, you are not using CSI.

154

nit: users -> Users.

158

Okay, I get it now: If we insert in successor, we need to replace dominating uses. This is possible, but your way is much simpler.

This revision is now accepted and ready to land.Dec 16 2022, 1:24 AM
Naville updated this revision to Diff 483458.Dec 16 2022, 2:05 AM

Fix style issues

This revision was landed with ongoing or failed builds.Dec 16 2022, 7:07 AM
This revision was automatically updated to reflect the committed changes.