This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] Implement This pointer passing to methods
ClosedPublic

Authored by tbaeder on Sep 26 2022, 9:38 PM.

Details

Summary

Seems like I completely forgot to post this to phab!

Implement passing the this pointer to member functions and constructors. The this pointer is passed via the stack. This changes the functions to explicitly track whether they have a RVO pointer and a this pointer.

Diff Detail

Event Timeline

tbaeder created this revision.Sep 26 2022, 9:38 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 26 2022, 9:38 PM
tbaeder requested review of this revision.Sep 26 2022, 9:38 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 26 2022, 9:38 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tbaeder updated this revision to Diff 465279.Oct 4 2022, 9:47 PM

@aaron.ballman This one has working precommit CI \o/ (array filler patch is not needed for it though)

aaron.ballman added inline comments.Oct 6 2022, 7:36 AM
clang/lib/AST/Interp/ByteCodeEmitter.cpp
47
clang/lib/AST/Interp/ByteCodeExprGen.cpp
637–638

What happens if Func is null?

668

Any reason not to use cast here instead, given that getFunction() expects a nonnull pointer anyway?

clang/test/AST/Interp/records.cpp
106

We're missing a fair amount of test coverage here in terms of calling member functions. Can you add some tests for that, as well as a test where the this pointer is invalid and causes UB? e.g.,

struct S {
  constexpr void member() {}
};

constexpr void func(S *s) {
  s->member(); // Should not be a constant expression
}

int main() {
  func(nullptr);
}
tbaeder marked an inline comment as done.Oct 6 2022, 8:08 AM
tbaeder added inline comments.
clang/lib/AST/Interp/ByteCodeExprGen.cpp
637–638

We need to bail out!

668

Not particularly, I guess the generated assertion output is a little nicer if it reaches the one in getFunction().

tbaeder added inline comments.Oct 6 2022, 8:49 AM
clang/test/AST/Interp/records.cpp
106

I just checked and we indeed don't reject that example. There are already functions to detect such scenarios in Interp.h/.cpp. But actually using them seems to require a bigger surgery since Call/CallVoid are currenly in both EvalEmitter.cpp and Interp.cpp.

I've written this down but I think for this patch it makes sense to add the failing test case and ignore the failure for now.

aaron.ballman added inline comments.Oct 6 2022, 11:33 AM
clang/test/AST/Interp/records.cpp
106

I'm okay with adding a failure test case with a fixme comment on it for now.

tbaeder updated this revision to Diff 465966.Oct 6 2022, 9:42 PM
aaron.ballman added inline comments.Oct 11 2022, 10:56 AM
clang/test/AST/Interp/records.cpp
139

The other thing I think we need some tests for are constructor and destructor calls where the this pointer may be a bit surprising because it needs adjustments. For example, with multiple inheritance where the this pointer may need to be adjusted to get to the fields of the object, and ensuring the correct constructors are called in the correct order.

Another case is with virtual functions (I'm assuming there's no vtable support yet and so that's less interesting, but it will become interesting once we get there so you may want to keep it in mind).

tbaeder added inline comments.Oct 11 2022, 11:48 PM
clang/test/AST/Interp/records.cpp
139

For constructors, see https://reviews.llvm.org/D135025. Although it doesn't test any sort of order I believe and destructors aren't part of that either.

aaron.ballman accepted this revision.Oct 13 2022, 11:40 AM

LGTM

clang/lib/AST/Interp/EvalEmitter.cpp
108

Not related to this review: it'd be nice to fix the interface so we get const correct behavior rather than needing to use const_cast like this.

This revision is now accepted and ready to land.Oct 13 2022, 11:40 AM
tbaeder marked an inline comment as done.Oct 13 2022, 11:05 PM
tbaeder added inline comments.
clang/lib/AST/Interp/EvalEmitter.cpp
108

Already have a local patch for this!

This revision was landed with ongoing or failed builds.Oct 14 2022, 1:22 AM
This revision was automatically updated to reflect the committed changes.
tbaeder marked an inline comment as done.