Skip to content

[clang][Interp] Remove GlobalDecl flag #101995

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 1 commit into from
Aug 5, 2024
Merged
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
6 changes: 1 addition & 5 deletions clang/lib/AST/Interp/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ template <class Emitter> class DeclScope final : public LocalScope<Emitter> {
public:
DeclScope(Compiler<Emitter> *Ctx, const ValueDecl *VD)
: LocalScope<Emitter>(Ctx, VD), Scope(Ctx->P, VD),
OldGlobalDecl(Ctx->GlobalDecl),
OldInitializingDecl(Ctx->InitializingDecl) {
Ctx->GlobalDecl = Context::shouldBeGloballyIndexed(VD);
Ctx->InitializingDecl = VD;
Ctx->InitStack.push_back(InitLink::Decl(VD));
}
Expand All @@ -41,14 +39,12 @@ template <class Emitter> class DeclScope final : public LocalScope<Emitter> {
}

~DeclScope() {
this->Ctx->GlobalDecl = OldGlobalDecl;
this->Ctx->InitializingDecl = OldInitializingDecl;
this->Ctx->InitStack.pop_back();
}

private:
Program::DeclScope Scope;
bool OldGlobalDecl;
const ValueDecl *OldInitializingDecl;
};

Expand Down Expand Up @@ -2265,7 +2261,7 @@ bool Compiler<Emitter>::VisitMaterializeTemporaryExpr(
// the temporary is explicitly static, create a global variable.
std::optional<PrimType> SubExprT = classify(SubExpr);
bool IsStatic = E->getStorageDuration() == SD_Static;
if (GlobalDecl || IsStatic) {
if (IsStatic) {
std::optional<unsigned> GlobalIndex = P.createGlobal(E);
if (!GlobalIndex)
return false;
Expand Down
3 changes: 0 additions & 3 deletions clang/lib/AST/Interp/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,6 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
llvm::SmallVector<InitLink> InitStack;
bool InitStackActive = false;

/// Flag indicating if we're initializing a global variable.
bool GlobalDecl = false;

/// Type of the expression returned by the function.
std::optional<PrimType> ReturnType;

Expand Down
3 changes: 3 additions & 0 deletions clang/lib/AST/Interp/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {

if (!Recursing) {
assert(Stk.empty());
C.cleanup();
#ifndef NDEBUG
// Make sure we don't rely on some value being still alive in
// InterpStack memory.
Expand All @@ -82,6 +83,7 @@ bool Context::evaluate(State &Parent, const Expr *E, APValue &Result) {

if (!Recursing) {
assert(Stk.empty());
C.cleanup();
#ifndef NDEBUG
// Make sure we don't rely on some value being still alive in
// InterpStack memory.
Expand Down Expand Up @@ -111,6 +113,7 @@ bool Context::evaluateAsInitializer(State &Parent, const VarDecl *VD,

if (!Recursing) {
assert(Stk.empty());
C.cleanup();
#ifndef NDEBUG
// Make sure we don't rely on some value being still alive in
// InterpStack memory.
Expand Down
19 changes: 14 additions & 5 deletions clang/lib/AST/Interp/Descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,21 @@ static void dtorRecord(Block *B, std::byte *Ptr, const Descriptor *D) {

static void moveRecord(Block *B, const std::byte *Src, std::byte *Dst,
const Descriptor *D) {
for (const auto &F : D->ElemRecord->fields()) {
auto FieldOff = F.Offset;
auto *FieldDesc = F.Desc;
assert(D);
assert(D->ElemRecord);

if (auto Fn = FieldDesc->MoveFn)
Fn(B, Src + FieldOff, Dst + FieldOff, FieldDesc);
// FIXME: There might be cases where we need to move over the (v)bases as
// well.
for (const auto &F : D->ElemRecord->fields()) {
auto FieldOffset = F.Offset;
const auto *SrcDesc =
reinterpret_cast<const InlineDescriptor *>(Src + FieldOffset) - 1;
auto *DestDesc =
reinterpret_cast<InlineDescriptor *>(Dst + FieldOffset) - 1;
std::memcpy(DestDesc, SrcDesc, sizeof(InlineDescriptor));

if (auto Fn = F.Desc->MoveFn)
Fn(B, Src + FieldOffset, Dst + FieldOffset, F.Desc);
}
}

Expand Down
10 changes: 10 additions & 0 deletions clang/test/AST/Interp/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@ namespace reinterpretcast {
// CHECK: define {{.*}} ptr @_ZN15reinterpretcast2f1Em
// CHECK: inttoptr
}

namespace Bitfield {
struct S { int a : 5; ~S(); };
// CHECK: alloca
// CHECK: call {{.*}}memset
// CHECK: store i32 {{.*}}, ptr @_ZGRN8Bitfield1rE_
// CHECK: call void @_ZN8Bitfield1SD1
// CHECK: store ptr @_ZGRN8Bitfield1rE_, ptr @_ZN8Bitfield1rE, align 8
int &&r = S().a;
}
Loading