-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[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
tbaederr
wants to merge
1
commit into
llvm:main
Choose a base branch
from
tbaederr:union-deactivate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+136
−11
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesFirst, don't forget to also activate fields which are bitfields on assignment. Second, when deactivating fields, recurse into records. Full diff: https://github.com/llvm/llvm-project/pull/146824.diff 3 Files Affected:
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index f9623131809e5..ddd0b44bb37bf 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -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
@@ -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
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 0ad47645d39cc..31be77a5b936b 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -481,8 +481,17 @@ 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);
+ }
+ }
};
// Unions might be nested etc., so find the topmost Pointer that's
@@ -492,21 +501,19 @@ void Pointer::activate() const {
UnionPtr = UnionPtr.getBase();
assert(UnionPtr.getFieldDesc()->isUnion());
-
const Record *UnionRecord = UnionPtr.getRecord();
for (const Record::Field &F : UnionRecord->fields()) {
Pointer FieldPtr = UnionPtr.atField(F.Offset);
if (FieldPtr == *this) {
+ // 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();
}
}
diff --git a/clang/test/AST/ByteCode/unions.cpp b/clang/test/AST/ByteCode/unions.cpp
index 36f4b72335af3..c65c1e11cb1d9 100644
--- a/clang/test/AST/ByteCode/unions.cpp
+++ b/clang/test/AST/ByteCode/unions.cpp
@@ -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;
@@ -600,6 +600,55 @@ namespace MoveOrAssignOp {
}
static_assert(foo());
}
+
+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);
+ }
#endif
namespace AddressComparison {
|
184dea8
to
f25ea5c
Compare
First, don't forget to also activate fields which are bitfields on assignment. Second, when deactivating fields, recurse into records.
f25ea5c
to
26b5446
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:bytecode
Issues for the clang bytecode constexpr interpreter
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
First, don't forget to also activate fields which are bitfields on assignment.
Second, when deactivating fields, recurse into records.