Skip to content

Commit db557be

Browse files
authored
[clang][bytecode][NFC] Add Block::getBlockDesc<T>() (#172218)
Which returns the block-level descriptor. This way we don't have to do the reinterpret_cast dance everywhere.
1 parent ce1b047 commit db557be

File tree

6 files changed

+20
-21
lines changed

6 files changed

+20
-21
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4798,8 +4798,7 @@ VarCreationState Compiler<Emitter>::visitDecl(const VarDecl *VD,
47984798
if (!R && Context::shouldBeGloballyIndexed(VD)) {
47994799
if (auto GlobalIndex = P.getGlobal(VD)) {
48004800
Block *GlobalBlock = P.getGlobal(*GlobalIndex);
4801-
GlobalInlineDescriptor &GD =
4802-
*reinterpret_cast<GlobalInlineDescriptor *>(GlobalBlock->rawData());
4801+
auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
48034802

48044803
GD.InitState = GlobalInitState::InitializerFailed;
48054804
GlobalBlock->invokeDtor();
@@ -4860,8 +4859,7 @@ bool Compiler<Emitter>::visitDeclAndReturn(const VarDecl *VD, const Expr *Init,
48604859
auto GlobalIndex = P.getGlobal(VD);
48614860
assert(GlobalIndex);
48624861
Block *GlobalBlock = P.getGlobal(*GlobalIndex);
4863-
GlobalInlineDescriptor &GD =
4864-
*reinterpret_cast<GlobalInlineDescriptor *>(GlobalBlock->rawData());
4862+
auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
48654863

48664864
GD.InitState = GlobalInitState::InitializerFailed;
48674865
GlobalBlock->invokeDtor();

clang/lib/AST/ByteCode/EvalEmitter.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Scope::Local EvalEmitter::createLocal(Descriptor *D) {
110110
B->invokeCtor();
111111

112112
// Initialize local variable inline descriptor.
113-
InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
113+
auto &Desc = B->getBlockDesc<InlineDescriptor>();
114114
Desc.Desc = D;
115115
Desc.Offset = sizeof(InlineDescriptor);
116116
Desc.IsActive = false;
@@ -304,7 +304,7 @@ bool EvalEmitter::emitSetLocal(uint32_t I, SourceInfo Info) {
304304

305305
Block *B = getLocal(I);
306306
*reinterpret_cast<T *>(B->data()) = S.Stk.pop<T>();
307-
InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
307+
auto &Desc = B->getBlockDesc<InlineDescriptor>();
308308
Desc.IsInitialized = true;
309309

310310
return true;
@@ -327,8 +327,7 @@ bool EvalEmitter::emitGetLocalEnabled(uint32_t I, SourceInfo Info) {
327327
return true;
328328

329329
Block *B = getLocal(I);
330-
const InlineDescriptor &Desc =
331-
*reinterpret_cast<InlineDescriptor *>(B->rawData());
330+
const auto &Desc = B->getBlockDesc<InlineDescriptor>();
332331

333332
S.Stk.push<bool>(Desc.IsActive);
334333
return true;
@@ -344,7 +343,7 @@ bool EvalEmitter::emitEnableLocal(uint32_t I, SourceInfo Info) {
344343
// probably use a different struct than InlineDescriptor for the block-level
345344
// inline descriptor of local varaibles.
346345
Block *B = getLocal(I);
347-
InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData());
346+
auto &Desc = B->getBlockDesc<InlineDescriptor>();
348347
Desc.IsActive = true;
349348
return true;
350349
}

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,7 @@ static bool CheckWeak(InterpState &S, CodePtr OpPC, const Block *B) {
736736
// For example, since those can't be members of structs, they also can't
737737
// be mutable.
738738
bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B) {
739-
const auto &Desc =
740-
*reinterpret_cast<const GlobalInlineDescriptor *>(B->rawData());
739+
const auto &Desc = B->getBlockDesc<GlobalInlineDescriptor>();
741740
if (!B->isAccessible()) {
742741
if (!CheckExtern(S, OpPC, Pointer(const_cast<Block *>(B))))
743742
return false;

clang/lib/AST/ByteCode/Interp.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,8 +1455,7 @@ bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
14551455
template <PrimType Name, class T = typename PrimConv<Name>::T>
14561456
bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I) {
14571457
const Block *B = S.P.getGlobal(I);
1458-
const auto &Desc =
1459-
*reinterpret_cast<const GlobalInlineDescriptor *>(B->rawData());
1458+
const auto &Desc = B->getBlockDesc<GlobalInlineDescriptor>();
14601459
if (Desc.InitState != GlobalInitState::Initialized)
14611460
return DiagnoseUninitialized(S, OpPC, B->isExtern(), B->getDescriptor(),
14621461
AK_Read);

clang/lib/AST/ByteCode/InterpBlock.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ class Block final {
122122
}
123123
template <typename T> T &deref() { return *reinterpret_cast<T *>(data()); }
124124

125+
template <typename T> T &getBlockDesc() {
126+
assert(sizeof(T) == getDescriptor()->getMetadataSize());
127+
return *reinterpret_cast<T *>(rawData());
128+
}
129+
template <typename T> const T &getBlockDesc() const {
130+
return const_cast<Block *>(this)->getBlockDesc<T>();
131+
}
132+
125133
/// Invokes the constructor.
126134
void invokeCtor() {
127135
assert(!IsInitialized);

clang/lib/AST/ByteCode/Pointer.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,7 @@ bool Pointer::isInitialized() const {
444444

445445
if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) &&
446446
Offset == BS.Base) {
447-
const GlobalInlineDescriptor &GD =
448-
*reinterpret_cast<const GlobalInlineDescriptor *>(block()->rawData());
447+
const auto &GD = block()->getBlockDesc<GlobalInlineDescriptor>();
449448
return GD.InitState == GlobalInitState::Initialized;
450449
}
451450

@@ -473,8 +472,7 @@ bool Pointer::isElementInitialized(unsigned Index) const {
473472

474473
if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) &&
475474
Offset == BS.Base) {
476-
const GlobalInlineDescriptor &GD =
477-
*reinterpret_cast<const GlobalInlineDescriptor *>(block()->rawData());
475+
const auto &GD = block()->getBlockDesc<GlobalInlineDescriptor>();
478476
return GD.InitState == GlobalInitState::Initialized;
479477
}
480478

@@ -499,8 +497,7 @@ void Pointer::initialize() const {
499497

500498
if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) &&
501499
Offset == BS.Base) {
502-
GlobalInlineDescriptor &GD = *reinterpret_cast<GlobalInlineDescriptor *>(
503-
asBlockPointer().Pointee->rawData());
500+
auto &GD = BS.Pointee->getBlockDesc<GlobalInlineDescriptor>();
504501
GD.InitState = GlobalInitState::Initialized;
505502
return;
506503
}
@@ -565,8 +562,7 @@ bool Pointer::allElementsInitialized() const {
565562

566563
if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) &&
567564
Offset == BS.Base) {
568-
const GlobalInlineDescriptor &GD =
569-
*reinterpret_cast<const GlobalInlineDescriptor *>(block()->rawData());
565+
const auto &GD = block()->getBlockDesc<GlobalInlineDescriptor>();
570566
return GD.InitState == GlobalInitState::Initialized;
571567
}
572568

0 commit comments

Comments
 (0)