Skip to content

[clang][bytecode] Add support for creating dummies for expressions #108394

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
Sep 12, 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
8 changes: 6 additions & 2 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3335,7 +3335,11 @@ bool Compiler<Emitter>::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
if (!E->isExpressibleAsConstantInitializer())
return this->discard(SubExpr) && this->emitInvalid(E);

return this->delegate(SubExpr);
assert(classifyPrim(E) == PT_Ptr);
if (std::optional<unsigned> I = P.getOrCreateDummy(E))
return this->emitGetPtrGlobal(*I, E);

return false;
}

template <class Emitter>
Expand Down Expand Up @@ -4118,7 +4122,7 @@ bool Compiler<Emitter>::VisitBuiltinCallExpr(const CallExpr *E,
BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString ||
BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant ||
BuiltinID == Builtin::BI__builtin_function_start) {
if (std::optional<unsigned> GlobalOffset = P.createGlobal(E)) {
if (std::optional<unsigned> GlobalOffset = P.getOrCreateDummy(E)) {
if (!this->emitGetPtrGlobal(*GlobalOffset, E))
return false;

Expand Down
29 changes: 20 additions & 9 deletions clang/lib/AST/ByteCode/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,33 @@ std::optional<unsigned> Program::getOrCreateGlobal(const ValueDecl *VD,
return std::nullopt;
}

std::optional<unsigned> Program::getOrCreateDummy(const ValueDecl *VD) {
std::optional<unsigned> Program::getOrCreateDummy(const DeclTy &D) {
assert(D);
// Dedup blocks since they are immutable and pointers cannot be compared.
if (auto It = DummyVariables.find(VD); It != DummyVariables.end())
if (auto It = DummyVariables.find(D.getOpaqueValue());
It != DummyVariables.end())
return It->second;

QualType QT = VD->getType();
if (const auto *RT = QT->getAs<ReferenceType>())
QT = RT->getPointeeType();
QualType QT;
if (const auto *E = D.dyn_cast<const Expr *>()) {
QT = E->getType();
} else {
const ValueDecl *VD = cast<ValueDecl>(D.get<const Decl *>());
QT = VD->getType();
if (const auto *RT = QT->getAs<ReferenceType>())
QT = RT->getPointeeType();
}
assert(!QT.isNull());

Descriptor *Desc;
if (std::optional<PrimType> T = Ctx.classify(QT))
Desc = createDescriptor(VD, *T, std::nullopt, true, false);
Desc = createDescriptor(D, *T, std::nullopt, /*IsTemporary=*/true,
/*IsMutable=*/false);
else
Desc = createDescriptor(VD, QT.getTypePtr(), std::nullopt, true, false);
Desc = createDescriptor(D, QT.getTypePtr(), std::nullopt,
/*IsTemporary=*/true, /*IsMutable=*/false);
if (!Desc)
Desc = allocateDescriptor(VD);
Desc = allocateDescriptor(D);

assert(Desc);
Desc->makeDummy();
Expand All @@ -175,7 +186,7 @@ std::optional<unsigned> Program::getOrCreateDummy(const ValueDecl *VD) {
G->block()->invokeCtor();

Globals.push_back(G);
DummyVariables[VD] = I;
DummyVariables[D.getOpaqueValue()] = I;
return I;
}

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/ByteCode/Program.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Program final {
const Expr *Init = nullptr);

/// Returns or creates a dummy value for unknown declarations.
std::optional<unsigned> getOrCreateDummy(const ValueDecl *VD);
std::optional<unsigned> getOrCreateDummy(const DeclTy &D);

/// Creates a global and returns its index.
std::optional<unsigned> createGlobal(const ValueDecl *VD, const Expr *Init);
Expand Down Expand Up @@ -209,7 +209,7 @@ class Program final {
llvm::DenseMap<const RecordDecl *, Record *> Records;

/// Dummy parameter to generate pointers from.
llvm::DenseMap<const ValueDecl *, unsigned> DummyVariables;
llvm::DenseMap<const void *, unsigned> DummyVariables;

/// Creates a new descriptor.
template <typename... Ts> Descriptor *allocateDescriptor(Ts &&...Args) {
Expand Down
1 change: 1 addition & 0 deletions clang/test/CodeGenObjC/boxing.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s -fexperimental-new-constant-interpreter | FileCheck %s

typedef long NSInteger;
typedef unsigned long NSUInteger;
Expand Down
Loading