This is an archive of the discontinued LLVM Phabricator instance.

[mlir][llvm] Verify consistency of llvm.resume and llvm.landingpad types
ClosedPublic

Authored by victor-eds on Mar 27 2023, 7:58 AM.

Details

Summary

Following the steps of the LLVM verifier
(https://github.com/llvm/llvm-project/blob/b2c48559c882fd558f91e471c4d23ea7b0c6e718/llvm/lib/IR/Verifier.cpp#L4195),
checks in llvm.func verifier are added to ensure consistency of
llvm.landingpad operations' result types and llvm.resume operations'
input types.

As in LLVM, we will allow llvm.resume operations with input values
defined by operations other than llvm.landingpad.

Signed-off-by: Victor Perez <victor.perez@codeplay.com>

Diff Detail

Event Timeline

victor-eds created this revision.Mar 27 2023, 7:58 AM
Herald added a reviewer: dcaballe. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
victor-eds requested review of this revision.Mar 27 2023, 7:58 AM

It seems a bit wasteful to walk all the landingpads for each of the landingpads, but there might not be another way. It would be nice if one could compare the types with the result type of the personality function, but it does not exist for non-llvm functions.

What are the expected semantics of a landingpad operation that is part of something else than an llvm.func?

It seems a bit wasteful to walk all the landingpads for each of the landingpads, but there might not be another way.

Yes, I don't think there is unfortunately...

It would be nice if one could compare the types with the result type of the personality function, but it does not exist for non-llvm functions.

Also, AFAIK, this mapping is target specific (from the spec):

As with calling conventions, how the personality function results are represented in LLVM IR is target specific.

What are the expected semantics of a landingpad operation that is part of something else than an llvm.func?

There is no documentation on this operation. However, following the dialect [[documentation|https://mlir.llvm.org/docs/Dialects/LLVM/]]:

Unless explicitly stated otherwise, the semantics of the LLVM dialect operations must correspond to the semantics of LLVM IR instructions and any divergence is considered a bug.

I assume the semantics will remain unchanged regardless of the parent function operation (be it llvm.func or, e.g., func.func).

There is no documentation on this operation. However, following the dialect [[documentation|https://mlir.llvm.org/docs/Dialects/LLVM/]]:

Unless explicitly stated otherwise, the semantics of the LLVM dialect operations must correspond to the semantics of LLVM IR instructions and any divergence is considered a bug.

I'm not sure if this can directly apply to llvm dialect operations mixed into something like a func.func. Thus, one could argue that the llvm.landingpad type invariant might be violated in that case. If we do so, the type equality check should move into the llvm.func to ensure that we only walk the region once instead of N times.

Maybe @gysit has an opinion on this.

the type equality check should move into the llvm.func

I think moving the verifier to llvm.func sounds like a good idea. Then only a single pass is needed and there is no need to handle possible issues with non-existing parent functions. I would even consider moving the logic in https://reviews.llvm.org/D145802 to the same verifier. LLVM's verifier also works with a single pass and a global variable that is used for checking the types of landing pad and resume ops at once:

https://github.com/llvm/llvm-project/blob/15f52c1502e6aa2f7553393d76da92b21c7cf493/llvm/lib/IR/Verifier.cpp#L309

An additional advantage of the solution is that the verifier only runs if the landing pad operations are part of an llvm.func. If not, for example, during testing or while the IR is in a transient state in between lowering passes the verifier does not run. This behavior is in line with the existing verifier that only checks for the personality function if the landing pad op is part of a llvm.func.

victor-eds retitled this revision from [mlir][llvm] Verify all llvm.landingpad ops in a function share type to [mlir][llvm] Verify consistency of llvm.resume and llvm.landingpad types.
victor-eds edited the summary of this revision. (Show Details)

Check type consistency in llvm.func verifier

the type equality check should move into the llvm.func

I think moving the verifier to llvm.func sounds like a good idea. Then only a single pass is needed and there is no need to handle possible issues with non-existing parent functions. I would even consider moving the logic in https://reviews.llvm.org/D145802 to the same verifier. LLVM's verifier also works with a single pass and a global variable that is used for checking the types of landing pad and resume ops at once:

https://github.com/llvm/llvm-project/blob/15f52c1502e6aa2f7553393d76da92b21c7cf493/llvm/lib/IR/Verifier.cpp#L309

An additional advantage of the solution is that the verifier only runs if the landing pad operations are part of an llvm.func. If not, for example, during testing or while the IR is in a transient state in between lowering passes the verifier does not run. This behavior is in line with the existing verifier that only checks for the personality function if the landing pad op is part of a llvm.func.

I agree we can move this logic to the llvm.func verifier if the semantics are not to be preserved in other function operations. Now this patch mimics LLVM behavior.

zero9178 added inline comments.Mar 28 2023, 3:46 AM
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2179

nit: please just use capture-all & for the lambdas here

2179

Could you extract the walk call out of the if condition? I think it makes the logic harder to see and increases the indentation within walk.

2193

No need to specify the type here. Case deduces it from the lambda parameter

2199
gysit accepted this revision.Mar 28 2023, 3:51 AM

Thanks for the changes LGTM modulo @zero9178 's comments.

mlir/test/Dialect/LLVMIR/invalid-typed-pointers.mlir
141

ultra nit: there is now a expected-error@below for matching errors on the next line.

Same holds for the tests below.

This revision is now accepted and ready to land.Mar 28 2023, 3:51 AM
Dinistro accepted this revision.Mar 28 2023, 3:58 AM

+1 on zero's comments. Once they are implemented: LGTM!

Address comments

victor-eds marked 2 inline comments as done.

Rename var

Improve readability

victor-eds marked 3 inline comments as done.Mar 28 2023, 8:21 AM

Thanks for the comments everyone! I'll land this now.