This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] Basic support for bit fields
ClosedPublic

Authored by tbaeder on Jul 14 2023, 1:19 AM.

Diff Detail

Event Timeline

tbaeder created this revision.Jul 14 2023, 1:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 14 2023, 1:19 AM
tbaeder requested review of this revision.Jul 14 2023, 1:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 14 2023, 1:19 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tbaeder updated this revision to Diff 540927.Jul 17 2023, 2:59 AM
shafik added inline comments.Jul 17 2023, 3:33 PM
clang/test/AST/Interp/bitfields.cpp
37

This is an interesting test case:

struct A {int c:3;};

constexpr int f() {
  A a1{3};
  return a1.c++;
}

void g() {
    constexpr int x = f();
}

We are overflowing the bit-field value but it is implementation defined what the value is 😱 CC @aaron.ballman

Might be worth checking the result of conditional operator and comma etc are bit-fields when they are supposed to be and therefore sizeof fails for those cases.

Precommit CI is currently crashing on the newly introduced test case.

clang/test/AST/Interp/bitfields.cpp
37

Yeah, the bit-field overflow case is http://eel.is/c++draft/expr.ass#4 -- we don't document what we do in this case (so this is a good opportunity to improve our docs), but we appear to treat it the same as overflow for a non-bit-field integer type: https://godbolt.org/z/4Ta7KoP58

Precommit CI is currently crashing on the newly introduced test case.

It's missing https://reviews.llvm.org/D155548 :)

That's of course just a workaround. At the beginning of the evalute* fuctions in the new interpreter, there's a assert(S.Stk.empty()), to ensure that a previous interpret call didn't leave someting on the stack. But that doesn't work anymore since FieldDecl::getBitWidthValue() calls in the interpreter again while we're already evaluating something, so that assertion triggers.

aaron.ballman accepted this revision.Aug 1 2023, 8:06 AM

Precommit CI is currently crashing on the newly introduced test case.

It's missing https://reviews.llvm.org/D155548 :)

That's of course just a workaround. At the beginning of the evalute* fuctions in the new interpreter, there's a assert(S.Stk.empty()), to ensure that a previous interpret call didn't leave someting on the stack. But that doesn't work anymore since FieldDecl::getBitWidthValue() calls in the interpreter again while we're already evaluating something, so that assertion triggers.

Ah, I see, thank you for the explanation!

There's more work left here for supporting other operators and uses, but as far as these changes go, they seem reasonable to me. It'd be good to add Shafik's suggested test cases even if they're surrounded with FIXME comments, just so we don't lose track of the request.

This revision is now accepted and ready to land.Aug 1 2023, 8:06 AM
This revision was landed with ongoing or failed builds.Aug 9 2023, 11:50 PM
This revision was automatically updated to reflect the committed changes.