This is an archive of the discontinued LLVM Phabricator instance.

[WinEH] Ignore dynamic allocas of catch objects
AcceptedPublic

Authored by pengfei on Feb 10 2023, 7:31 AM.

Details

Reviewers
majnemer
rnk
Summary

It seems D17823 assumes catch objects are always fixed objects.
Though dynamic alloca is rare, it exists in manually written IR, e.g., https://godbolt.org/z/a8xY91seG
We may not need to support it in WinEH, but we should ignore it to prevent later crash.

Diff Detail

Event Timeline

pengfei created this revision.Feb 10 2023, 7:31 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 10 2023, 7:31 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
pengfei requested review of this revision.Feb 10 2023, 7:31 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 10 2023, 7:31 AM
rnk added a subscriber: hans.May 10 2023, 9:50 AM

Sorry for the delay, I was OOO, and David Majnemer has moved on to other projects. In the future, consider adding @hans for more visibility for WinEH related changes to help find a reviewer.

I think maybe it would be best to handle this in WinEHPrepare, which is where we check if the third argument operand of the catchpad instruction is an alloca. We could check there that the alloca is static if it exists, and it might also be healthy to report a backend error if some non-null, non-alloca value exists. Currently we silently ignore the value if it is not an alloca. It would be consistent with our current behavior to silently ignore dynamic allocas there, if you don't want to go through the trouble of reporting an error.

The next best place to report an error would be the verifier, but the validity of the arguments to the catchpad depend on the personality, so that would require teaching the verifier about more WinEH semantics.

llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp
173–174 ↗(On Diff #496468)

This seems overly strict, I think it will assert if there is any dynamic alloca in a function using WinEH. I don't think that's what we want, we only want to assert if the catch object itself is a dynamic alloca, right?

pengfei planned changes to this revision.May 10 2023, 11:10 PM

@rnk Glad to see you back and thanks for the suggestions! I'm busy in other work recently, will update when I have time. Thanks!

pengfei updated this revision to Diff 524076.May 20 2023, 10:02 PM
pengfei marked an inline comment as done.

Sorry for the delay, I was OOO, and David Majnemer has moved on to other projects. In the future, consider adding @hans for more visibility for WinEH related changes to help find a reviewer.

I think maybe it would be best to handle this in WinEHPrepare, which is where we check if the third argument operand of the catchpad instruction is an alloca. We could check there that the alloca is static if it exists, and it might also be healthy to report a backend error if some non-null, non-alloca value exists. Currently we silently ignore the value if it is not an alloca. It would be consistent with our current behavior to silently ignore dynamic allocas there, if you don't want to go through the trouble of reporting an error.

The next best place to report an error would be the verifier, but the validity of the arguments to the catchpad depend on the personality, so that would require teaching the verifier about more WinEH semantics.

Thanks for the guidances! Changed WinEHPrepare to ignore dynamic allocas.

pengfei retitled this revision from [WinEH][NFC] Assert for dynamic alloca in WinEH BBs to [WinEH] Ignore dynamic allocas of catch objects.May 20 2023, 10:05 PM
pengfei edited the summary of this revision. (Show Details)
rnk accepted this revision.May 22 2023, 11:04 AM

lgtm

This revision is now accepted and ready to land.May 22 2023, 11:04 AM

I'm not sure if I understand the fix issue correctly, given my very limited knowledge with WinEH. But wouldn't this result in a silent failure if DynAlloca is used?

rnk added a comment.May 22 2023, 7:58 PM

I'm not sure if I understand the fix issue correctly, given my very limited knowledge with WinEH. But wouldn't this result in a silent failure if DynAlloca is used?

That's true, but it's consistent with passing other non-alloca values to the catchpad. The next best thing would be to provide a diagnostic in the verifier here, but it depends on knowledge of the personality function:
https://github.com/llvm/llvm-project/blob/main/llvm/lib/IR/Verifier.cpp#L4261

Naville added a comment.EditedMay 22 2023, 11:14 PM

Wouldn't that still results in unwind failure like 60914? Or what am I missing here?

EDIT:
What exactly does "going through the trouble or reporting an error" means? I do have time dedicated to this topic but I'm not entirely sure what to do here

Wouldn't that still results in unwind failure like 60914? Or what am I missing here?

EDIT:
What exactly does "going through the trouble or reporting an error" means? I do have time dedicated to this topic but I'm not entirely sure what to do here

Guess D151209 is what you expected. It does catch a lot ill formated IR tests which would be crash with llc.