Page MenuHomePhabricator

Expose constructing a virtual method dispatch through the include/clang/CodeGen APIs.
Needs ReviewPublic

Authored by pschuh on Aug 16 2019, 12:53 PM.

Details

Reviewers
rjmccall
Summary

In swift, we want to be able to call c++ virtual methods by looking up in the virtual table. There appears to exist no way currently, via the standard APIs, to take a CXXMethodDecl and a llvm::Value representing a this-ptr and construct the llvm instructions necessary to extract the method from the virtual table as well as adjust the this-ptr.

For reference, this is used in https://github.com/apple/swift/pull/26658.

Diff Detail

Repository
rC Clang

Event Timeline

pschuh created this revision.Aug 16 2019, 12:53 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 16 2019, 12:53 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
rjmccall added inline comments.Aug 26 2019, 10:22 PM
include/clang/CodeGen/SwiftCallingConv.h
190 ↗(On Diff #215655)

This isn't the right header for this. More thoughts on this point below.

The documentation should describe the expected relationship between this and the method. I imagine that thisPtr is expected to already be a pointer to the type which declares the method (i.e. MD->getParent()), and in the appropriate address space.

Please name this something like emitLoadOfCXXVirtualMethod.

This particular case will probably never need control flow or temporary memory, but I wonder if we should go ahead and introduce an abstraction that bundles the CGM and the builder so that we can teach it to handle those in the future. If you do, you could just define it in its own header and then make this a method on that.

I think it would be totally reasonable to pull Clang's Address.h up to include/clang/CodeGen instead of breaking it down here.

Also, this really needs to return a CGCallee, which might require pulling that up as well. The reason why should be clear in a few weeks.

pschuh updated this revision to Diff 217566.Aug 27 2019, 7:07 PM

Your suggestions of pulling the builder out as an API and exposing CGCallee made everything a lot simpler. Unfortunately, I had to split the CGCallee header because it threatened to pull EHScopeStack.h and CGValue.h to be public (and transitively CodeGenTBAA.h). The old CGCall.h header now just contains the Args and ReturnValueSlot.

pschuh added a comment.Sep 5 2019, 4:26 PM

Hi John, maybe take a look?