-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[clang][bytecode][NFC] Add Block::getBlockDesc<T>() #172218
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
Merged
Merged
+20
−21
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
Which returns the block-level descriptor. This way we don't have to do the reinterpret_cast dance everywhere.
Member
|
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesWhich returns the block-level descriptor. This way we don't have to do the reinterpret_cast dance everywhere. Full diff: https://github.com/llvm/llvm-project/pull/172218.diff 6 Files Affected:
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index ed5493c315dd7..4daab0702f147 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -4798,8 +4798,7 @@ VarCreationState Compiler<Emitter>::visitDecl(const VarDecl *VD,
if (!R && Context::shouldBeGloballyIndexed(VD)) {
if (auto GlobalIndex = P.getGlobal(VD)) {
Block *GlobalBlock = P.getGlobal(*GlobalIndex);
- GlobalInlineDescriptor &GD =
- *reinterpret_cast<GlobalInlineDescriptor *>(GlobalBlock->rawData());
+ auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
GD.InitState = GlobalInitState::InitializerFailed;
GlobalBlock->invokeDtor();
@@ -4860,8 +4859,7 @@ bool Compiler<Emitter>::visitDeclAndReturn(const VarDecl *VD, const Expr *Init,
auto GlobalIndex = P.getGlobal(VD);
assert(GlobalIndex);
Block *GlobalBlock = P.getGlobal(*GlobalIndex);
- GlobalInlineDescriptor &GD =
- *reinterpret_cast<GlobalInlineDescriptor *>(GlobalBlock->rawData());
+ auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
GD.InitState = GlobalInitState::InitializerFailed;
GlobalBlock->invokeDtor();
diff --git a/clang/lib/AST/ByteCode/EvalEmitter.cpp b/clang/lib/AST/ByteCode/EvalEmitter.cpp
index a2e01efc8ffd9..2ed5147a15491 100644
--- a/clang/lib/AST/ByteCode/EvalEmitter.cpp
+++ b/clang/lib/AST/ByteCode/EvalEmitter.cpp
@@ -110,7 +110,7 @@ Scope::Local EvalEmitter::createLocal(Descriptor *D) {
B->invokeCtor();
// Initialize local variable inline descriptor.
- InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
+ auto &Desc = B->getBlockDesc<InlineDescriptor>();
Desc.Desc = D;
Desc.Offset = sizeof(InlineDescriptor);
Desc.IsActive = false;
@@ -304,7 +304,7 @@ bool EvalEmitter::emitSetLocal(uint32_t I, SourceInfo Info) {
Block *B = getLocal(I);
*reinterpret_cast<T *>(B->data()) = S.Stk.pop<T>();
- InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
+ auto &Desc = B->getBlockDesc<InlineDescriptor>();
Desc.IsInitialized = true;
return true;
@@ -327,8 +327,7 @@ bool EvalEmitter::emitGetLocalEnabled(uint32_t I, SourceInfo Info) {
return true;
Block *B = getLocal(I);
- const InlineDescriptor &Desc =
- *reinterpret_cast<InlineDescriptor *>(B->rawData());
+ const auto &Desc = B->getBlockDesc<InlineDescriptor>();
S.Stk.push<bool>(Desc.IsActive);
return true;
@@ -344,7 +343,7 @@ bool EvalEmitter::emitEnableLocal(uint32_t I, SourceInfo Info) {
// probably use a different struct than InlineDescriptor for the block-level
// inline descriptor of local varaibles.
Block *B = getLocal(I);
- InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
+ auto &Desc = B->getBlockDesc<InlineDescriptor>();
Desc.IsActive = true;
return true;
}
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 80ef656dc6285..889ac1e1a9a7e 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -736,8 +736,7 @@ static bool CheckWeak(InterpState &S, CodePtr OpPC, const Block *B) {
// For example, since those can't be members of structs, they also can't
// be mutable.
bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B) {
- const auto &Desc =
- *reinterpret_cast<const GlobalInlineDescriptor *>(B->rawData());
+ const auto &Desc = B->getBlockDesc<GlobalInlineDescriptor>();
if (!B->isAccessible()) {
if (!CheckExtern(S, OpPC, Pointer(const_cast<Block *>(B))))
return false;
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index d8b8b209fa927..427f694319b6c 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -1455,8 +1455,7 @@ bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
template <PrimType Name, class T = typename PrimConv<Name>::T>
bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I) {
const Block *B = S.P.getGlobal(I);
- const auto &Desc =
- *reinterpret_cast<const GlobalInlineDescriptor *>(B->rawData());
+ const auto &Desc = B->getBlockDesc<GlobalInlineDescriptor>();
if (Desc.InitState != GlobalInitState::Initialized)
return DiagnoseUninitialized(S, OpPC, B->isExtern(), B->getDescriptor(),
AK_Read);
diff --git a/clang/lib/AST/ByteCode/InterpBlock.h b/clang/lib/AST/ByteCode/InterpBlock.h
index 73fdc8d85da11..57f9e7ec3714d 100644
--- a/clang/lib/AST/ByteCode/InterpBlock.h
+++ b/clang/lib/AST/ByteCode/InterpBlock.h
@@ -122,6 +122,14 @@ class Block final {
}
template <typename T> T &deref() { return *reinterpret_cast<T *>(data()); }
+ template <typename T> T &getBlockDesc() {
+ assert(sizeof(T) == getDescriptor()->getMetadataSize());
+ return *reinterpret_cast<T *>(rawData());
+ }
+ template <typename T> const T &getBlockDesc() const {
+ return const_cast<Block *>(this)->getBlockDesc<T>();
+ }
+
/// Invokes the constructor.
void invokeCtor() {
assert(!IsInitialized);
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 00e74db5655d6..90f41bed39440 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -444,8 +444,7 @@ bool Pointer::isInitialized() const {
if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) &&
Offset == BS.Base) {
- const GlobalInlineDescriptor &GD =
- *reinterpret_cast<const GlobalInlineDescriptor *>(block()->rawData());
+ const auto &GD = block()->getBlockDesc<GlobalInlineDescriptor>();
return GD.InitState == GlobalInitState::Initialized;
}
@@ -473,8 +472,7 @@ bool Pointer::isElementInitialized(unsigned Index) const {
if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) &&
Offset == BS.Base) {
- const GlobalInlineDescriptor &GD =
- *reinterpret_cast<const GlobalInlineDescriptor *>(block()->rawData());
+ const auto &GD = block()->getBlockDesc<GlobalInlineDescriptor>();
return GD.InitState == GlobalInitState::Initialized;
}
@@ -499,8 +497,7 @@ void Pointer::initialize() const {
if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) &&
Offset == BS.Base) {
- GlobalInlineDescriptor &GD = *reinterpret_cast<GlobalInlineDescriptor *>(
- asBlockPointer().Pointee->rawData());
+ auto &GD = BS.Pointee->getBlockDesc<GlobalInlineDescriptor>();
GD.InitState = GlobalInitState::Initialized;
return;
}
@@ -565,8 +562,7 @@ bool Pointer::allElementsInitialized() const {
if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) &&
Offset == BS.Base) {
- const GlobalInlineDescriptor &GD =
- *reinterpret_cast<const GlobalInlineDescriptor *>(block()->rawData());
+ const auto &GD = block()->getBlockDesc<GlobalInlineDescriptor>();
return GD.InitState == GlobalInitState::Initialized;
}
|
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.
Which returns the block-level descriptor. This way we don't have to do the reinterpret_cast dance everywhere.