Skip to content

[clang][bytecode] Misc union fixes #146824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1948,8 +1948,10 @@ bool StoreBitField(InterpState &S, CodePtr OpPC) {
const Pointer &Ptr = S.Stk.peek<Pointer>();
if (!CheckStore(S, OpPC, Ptr))
return false;
if (Ptr.canBeInitialized())
if (Ptr.canBeInitialized()) {
Ptr.initialize();
Ptr.activate();
}
if (const auto *FD = Ptr.getField())
Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
else
Expand All @@ -1963,8 +1965,10 @@ bool StoreBitFieldPop(InterpState &S, CodePtr OpPC) {
const Pointer &Ptr = S.Stk.pop<Pointer>();
if (!CheckStore(S, OpPC, Ptr))
return false;
if (Ptr.canBeInitialized())
if (Ptr.canBeInitialized()) {
Ptr.initialize();
Ptr.activate();
}
if (const auto *FD = Ptr.getField())
Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
else
Expand Down
31 changes: 26 additions & 5 deletions clang/lib/AST/ByteCode/Pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,19 @@ void Pointer::activate() const {
auto activate = [](Pointer &P) -> void {
P.getInlineDesc()->IsActive = true;
};
auto deactivate = [](Pointer &P) -> void {

std::function<void(Pointer &)> deactivate;
deactivate = [&deactivate](Pointer &P) -> void {
P.getInlineDesc()->IsActive = false;

if (const Record *R = P.getRecord()) {
for (const Record::Field &F : R->fields()) {
Pointer FieldPtr = P.atField(F.Offset);
if (FieldPtr.getInlineDesc()->IsActive)
deactivate(FieldPtr);
}
// FIXME: Bases?
}
};

// Unions might be nested etc., so find the topmost Pointer that's
Expand All @@ -492,21 +503,31 @@ void Pointer::activate() const {
UnionPtr = UnionPtr.getBase();

assert(UnionPtr.getFieldDesc()->isUnion());

const Record *UnionRecord = UnionPtr.getRecord();

// The direct child pointer of the union that's on the path from
// this pointer to the union.
Pointer ChildPtr = *this;
assert(ChildPtr != UnionPtr);
while (true) {
if (ChildPtr.getBase() == UnionPtr)
break;
ChildPtr = ChildPtr.getBase();
}
assert(ChildPtr.getBase() == UnionPtr);

for (const Record::Field &F : UnionRecord->fields()) {
Pointer FieldPtr = UnionPtr.atField(F.Offset);
if (FieldPtr == *this) {
if (FieldPtr == ChildPtr) {
// No need to deactivate, will be activated in the next loop.
} else {
deactivate(FieldPtr);
// FIXME: Recurse.
}
}

Pointer B = *this;
while (B != UnionPtr) {
activate(B);
// FIXME: Need to de-activate other fields of parent records.
B = B.getBase();
}
}
Expand Down
108 changes: 104 additions & 4 deletions clang/test/AST/ByteCode/unions.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s
// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s
// RUN: %clang_cc1 -verify=ref,both %s
// RUN: %clang_cc1 -std=c++20 -verify=ref,both %s
// RUN: %clang_cc1 -verify=expected,both %s -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -std=c++20 -verify=expected,both %s -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -verify=ref,both %s
// RUN: %clang_cc1 -std=c++20 -verify=ref,both %s

union U {
int a;
Expand Down Expand Up @@ -612,6 +612,106 @@ namespace CopyEmptyUnion {
}
static_assert(foo() == 1);
}

namespace BitFields {
constexpr bool simple() {
union U {
unsigned a : 1;
unsigned b : 1;
};

U u{1};
u.b = 1;
return u.b;
}
static_assert(simple());
}

namespace deactivateRecurses {

constexpr int foo() {
struct A {
struct {
int a;
};
int b;
};
struct B {
struct {
int a;
int b;
};
};

union U {
A a;
B b;
} u;

u.b.a = 10;
++u.b.a;

u.a.a = 10;
++u.a.a;

if (__builtin_constant_p(u.b.a))
return 10;

return 1;
}
static_assert(foo() == 1);
}

namespace AnonymousUnion {
struct Long {
struct {
unsigned is_long;
};
unsigned Size;
};

struct Short {
struct {
unsigned is_long;
unsigned Size;
};
char data;
};

union Rep {
Short S;
Long L;
};

#define assert_active(F) if (!__builtin_is_within_lifetime(&F)) (1/0);
#define assert_inactive(F) if ( __builtin_is_within_lifetime(&F)) (1/0);
consteval int test() {
union UU {
struct {
Rep R;
int a;
};
} U;

U.R.S.Size = 10;
assert_active(U);
assert_active(U.R);
assert_active(U.R.S);
assert_active(U.R.S.Size);

U.a = 10;
assert_active(U.a);
assert_active(U);

assert_active(U);
assert_active(U.R);
assert_active(U.R.S);
assert_active(U.R.S.Size);

return 1;
}
static_assert(test() == 1);
}
#endif

namespace AddressComparison {
Expand Down
Loading