Skip to content

SILGen: Refactor key path component lowering. #14893

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
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
4 changes: 2 additions & 2 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -4909,7 +4909,7 @@ class KeyPathExpr : public Expr {
case Kind::OptionalForce:
case Kind::UnresolvedProperty:
case Kind::Property:
llvm_unreachable("no index expr for this kind");
return nullptr;
}
}

Expand Down Expand Up @@ -4942,7 +4942,7 @@ class KeyPathExpr : public Expr {
case Kind::OptionalForce:
case Kind::UnresolvedProperty:
case Kind::Property:
llvm_unreachable("no hashable conformances for this kind");
return {};
}
}

Expand Down
47 changes: 47 additions & 0 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,53 @@ void SILGenModule::visitVarDecl(VarDecl *vd) {
if (auto setter = vd->getSetter())
emitFunction(setter);
}

tryEmitPropertyDescriptor(vd);
}

static bool doesPropertyNeedDescriptor(AbstractStorageDecl *decl) {
// The storage needs a descriptor if it sits at a module's ABI boundary,
// meaning it has public linkage.

// Any property that's potentially resilient should have accessors
// synthesized.
if (!decl->getGetter())
return false;

// TODO: If previous versions of an ABI-stable binary needed the descriptor,
// then we still do.

auto getter = SILDeclRef(decl->getGetter());
auto getterLinkage = getter.getLinkage(ForDefinition);

switch (getterLinkage) {
case SILLinkage::Public:
case SILLinkage::PublicNonABI:
// We may need a descriptor.
break;

case SILLinkage::Shared:
case SILLinkage::Private:
case SILLinkage::Hidden:
// Don't need a public descriptor.
return false;

case SILLinkage::HiddenExternal:
case SILLinkage::PrivateExternal:
case SILLinkage::PublicExternal:
case SILLinkage::SharedExternal:
llvm_unreachable("should be definition linkage?");
}

// TODO: We might be able to avoid a descriptor if the property is committed
// to being implemented a certain way, such as if it's promised to remain
// stored, or is computed with inlinable accessors, and can't change its
// mutability (because it's already promised to be mutable or fully immutable).
return true;
}

void SILGenModule::tryEmitPropertyDescriptor(AbstractStorageDecl *decl) {
// TODO
}

void SILGenModule::emitPropertyBehavior(VarDecl *vd) {
Expand Down
9 changes: 9 additions & 0 deletions lib/SILGen/SILGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
/// Emit a global initialization.
void emitGlobalInitialization(PatternBindingDecl *initializer, unsigned elt);

SILDeclRef getGetterDeclRef(AbstractStorageDecl *decl);
SILDeclRef getSetterDeclRef(AbstractStorageDecl *decl);
SILDeclRef getAddressorDeclRef(AbstractStorageDecl *decl,
AccessKind accessKind);
SILDeclRef getMaterializeForSetDeclRef(AbstractStorageDecl *decl);

/// Known functions for bridging.
SILDeclRef getStringToNSStringFn();
SILDeclRef getNSStringToStringFn();
Expand Down Expand Up @@ -421,6 +427,9 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
SubstitutionList
getNonMemberVarDeclSubstitutions(VarDecl *var);

/// Emit a property descriptor for the given storage decl if it needs one.
void tryEmitPropertyDescriptor(AbstractStorageDecl *decl);

private:
/// Emit the deallocator for a class that uses the objc allocator.
void emitObjCAllocatorDestructor(ClassDecl *cd, DestructorDecl *dd);
Expand Down
10 changes: 5 additions & 5 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5015,7 +5015,7 @@ ArgumentSource SILGenFunction::prepareAccessorBaseArg(SILLocation loc,
return Preparer.prepare();
}

SILDeclRef SILGenFunction::getGetterDeclRef(AbstractStorageDecl *storage) {
SILDeclRef SILGenModule::getGetterDeclRef(AbstractStorageDecl *storage) {
auto *getter = storage->getGetter();
return SILDeclRef(getter, SILDeclRef::Kind::Func)
.asForeign(requiresForeignEntryPoint(getter));
Expand Down Expand Up @@ -5054,7 +5054,7 @@ emitGetAccessor(SILLocation loc, SILDeclRef get,
return emission.apply(c);
}

SILDeclRef SILGenFunction::getSetterDeclRef(AbstractStorageDecl *storage) {
SILDeclRef SILGenModule::getSetterDeclRef(AbstractStorageDecl *storage) {
auto *setter = storage->getSetter();
return SILDeclRef(setter, SILDeclRef::Kind::Func)
.asForeign(requiresForeignEntryPoint(setter));
Expand Down Expand Up @@ -5120,7 +5120,7 @@ void SILGenFunction::emitSetAccessor(SILLocation loc, SILDeclRef set,
}

SILDeclRef
SILGenFunction::getMaterializeForSetDeclRef(AbstractStorageDecl *storage) {
SILGenModule::getMaterializeForSetDeclRef(AbstractStorageDecl *storage) {
return SILDeclRef(storage->getMaterializeForSetFunc(),
SILDeclRef::Kind::Func);
}
Expand Down Expand Up @@ -5197,8 +5197,8 @@ emitMaterializeForSetAccessor(SILLocation loc, SILDeclRef materializeForSet,
optionalCallback, callbackStorage);
}

SILDeclRef SILGenFunction::getAddressorDeclRef(AbstractStorageDecl *storage,
AccessKind accessKind) {
SILDeclRef SILGenModule::getAddressorDeclRef(AbstractStorageDecl *storage,
AccessKind accessKind) {
FuncDecl *addressorFunc = storage->getAddressorForAccess(accessKind);
return SILDeclRef(addressorFunc, SILDeclRef::Kind::Func);
}
Expand Down
Loading