This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] Support alignof()
ClosedPublic

Authored by tbaeder on Nov 2 2022, 3:24 AM.

Details

Summary

The test is just a copy of clang/test/SemaCXX/class-layout.cpp, but with the __builtin_offsetof calls commented out.

Not sure if this is the best way to test it, but adding special cases for the new interpreter to existing test cases doesn't seem like a good option either.

Diff Detail

Event Timeline

tbaeder created this revision.Nov 2 2022, 3:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 2 2022, 3:24 AM
tbaeder requested review of this revision.Nov 2 2022, 3:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 2 2022, 3:24 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tbaeder updated this revision to Diff 472562.Nov 2 2022, 3:27 AM
tbaeder updated this revision to Diff 472563.Nov 2 2022, 3:28 AM
tbaeder updated this revision to Diff 472564.Nov 2 2022, 3:34 AM
aaron.ballman added inline comments.Nov 2 2022, 12:25 PM
clang/lib/AST/Interp/ByteCodeExprGen.cpp
438

You're missing the rest of the standards quote from http://eel.is/c++draft/expr.alignof#3. There is some logic missing here for array types. Getting the alignment of an array gives you the alignment of its element type. (And there don't seem to be any tests for calling alignof on an array, so we should add some.)

tbaeder added inline comments.Nov 3 2022, 12:12 AM
clang/lib/AST/Interp/ByteCodeExprGen.cpp
438

This seems to be handled by ASTContext? It "just works" at least and this added code block is almost copy/paste from the current constant interpreter. I'll add some tests though.

tbaeder updated this revision to Diff 472855.Nov 3 2022, 12:19 AM
aaron.ballman accepted this revision.Nov 3 2022, 10:49 AM

LGTM

clang/lib/AST/Interp/ByteCodeExprGen.cpp
438

Ahhh, so it is! Interesting that the AST handles half of the requirements and expects callers to handle the other half.

This revision is now accepted and ready to land.Nov 3 2022, 10:49 AM

Oh, this doesn't work:

void test_alignas_operand() {
  alignas(8) char dummy;
  static_assert(__alignof(dummy) == 8);
}

Oh, this doesn't work:

void test_alignas_operand() {
  alignas(8) char dummy;
  static_assert(__alignof(dummy) == 8);
}

That's interesting! Based on the code, I would have thought that should work (you have support for getting the preferred alignment). I'm curious to see what fixes it!

tbaeder updated this revision to Diff 473316.Nov 4 2022, 12:43 PM

It's not really all that interesting since the current interpreter already does all the work, but it works like this.

aaron.ballman accepted this revision.Nov 7 2022, 8:26 AM

It's not really all that interesting since the current interpreter already does all the work, but it works like this.

Ahhh okay, that makes sense, thanks!

clang/lib/AST/Interp/ByteCodeExprGen.cpp
404

No need for the else because of the unconditional return in the if branch.

tbaeder updated this revision to Diff 473707.Nov 7 2022, 9:07 AM
tbaeder updated this revision to Diff 473708.
tbaeder marked an inline comment as done.
This revision was landed with ongoing or failed builds.Nov 10 2022, 11:38 PM
This revision was automatically updated to reflect the committed changes.