This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] Implement DecompositionDecls
ClosedPublic

Authored by tbaeder on Nov 28 2022, 5:53 AM.

Details

Summary

Factor out the actual variable allocation logic into a separate function, so we can call it multiple times from visitVarDecl() if the variable declaration is a DecompositionDecl().

Diff Detail

Event Timeline

tbaeder created this revision.Nov 28 2022, 5:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 28 2022, 5:53 AM
tbaeder requested review of this revision.Nov 28 2022, 5:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 28 2022, 5:53 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
shafik added inline comments.Jan 11 2023, 9:33 PM
clang/lib/AST/Interp/ByteCodeExprGen.h
269–270
clang/test/AST/Interp/cxx17.cpp
8

It would also be good to test references, bit-fields, volatile and tuple-like types if possible, if not then leave a FIXME.

tbaeder marked an inline comment as done.Jan 12 2023, 11:25 PM
tbaeder added inline comments.
clang/test/AST/Interp/cxx17.cpp
8

Clang doesn't seem to support volatile in constant expressions at all right now(?). Can you come up with a reproducer that works? All I get is errors about a non-literal type: https://godbolt.org/z/PbevGf3E8

tbaeder updated this revision to Diff 488883.Jan 12 2023, 11:53 PM

Add more tests and fix decomposition decls of reference type.

shafik added inline comments.Jan 13 2023, 8:55 AM
clang/test/AST/Interp/cxx17.cpp
8

Apologies, my mistake. In my enthusiasm I forgot that while constexpr volatile is a thing the variable is not usable in a constant expression.

aaron.ballman added inline comments.Jan 23 2023, 7:01 AM
clang/lib/AST/Interp/ByteCodeExprGen.cpp
34–35

The destructor for LocalScope already calls emitDestruction() which is a virtual function, so is this necessary?

1537–1539
1548–1551
1571–1572

<uncertain>Is this correct? IIRC, the decomposition declaration is its own object, but the bindings themselves are references back to the decomposition declaration object directly and so they're not distinct objects themselves (they're more like aliases).</uncertain>

tbaeder added inline comments.Jan 24 2023, 1:33 AM
clang/lib/AST/Interp/ByteCodeExprGen.cpp
1571–1572

Is this not reflected in the individual bindings? What does it mean that the bindings aren't "distinct objects themselves"?

aaron.ballman added inline comments.Jan 30 2023, 12:38 PM
clang/lib/AST/Interp/ByteCodeExprGen.cpp
1571–1572

Bindings are basically a label back to an object. Taking the easy case of a structure being bound:

struct S { int i, j; } s;

int main() {
  auto [val1, val2] = s;
  return val1 + val2;
}

The way this works under the hood is akin to:

struct S { int i, j; } s;

int main() {
  S __s = s; // This is the decomposition declaration
  return __s.i + __s.j; // And the structured bindings give alternative names to the fields in the decomposition declaration
}

so there's no allocation made for val1 or val2 because they're not really objects, just names. You can see that in: https://godbolt.org/z/sdj3Mvqhb (note the LLVM IR, which is identical between foo and bar aside from debug info).

tbaeder added inline comments.Feb 2 2023, 1:34 AM
clang/lib/AST/Interp/ByteCodeExprGen.cpp
1571–1572

Ah, I see. I guess my way works as well, but yours is better, so I'll change the patch. I can basically replace the added stuff in VisitDeclRefExpr() by just return this->visit(BD->getBinding()) and that will give me the right thing. I hope that way I can remove some of the BindingDecl special cases I've added as well.

Thanks!

tbaeder updated this revision to Diff 494235.Feb 2 2023, 3:10 AM
aaron.ballman added inline comments.Feb 2 2023, 9:16 AM
clang/lib/AST/Interp/ByteCodeExprGen.cpp
34–35

Still wondering about this

1536

Spurious whitespace change

clang/test/AST/Interp/cxx17.cpp
31

It's be helpful to have a test where a and b are modified to demonstrate we change the values in f as expected. e.g.,
`
F f = getF();
auto &[a, b] = f;
a += 1;
b += 2;

return f.a + f.b;
`

54–56

Spurious whitespace

tbaeder updated this revision to Diff 494517.Feb 2 2023, 11:09 PM
tbaeder marked 7 inline comments as done.
tbaeder marked 2 inline comments as done.Feb 3 2023, 9:40 AM
tbaeder added inline comments.
clang/lib/AST/Interp/ByteCodeExprGen.cpp
34–35

You're right, removed it again. Thanks.

This revision is now accepted and ready to land.Feb 28 2023, 6:12 AM
This revision was automatically updated to reflect the committed changes.