Skip to content

SILGen: Emit "trivial" property descriptors for properties that withhold no information about their implementation. #17404

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
Jun 22, 2018
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
22 changes: 21 additions & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4228,6 +4228,25 @@ class AbstractStorageDecl : public ValueDecl {
llvm_unreachable("bad storage kind");
}

/// \brief Return true if this is a VarDecl that has storage associated with
/// it which can be trivially accessed.
bool hasTrivialStorage() const {
switch (getStorageKind()) {
case Stored:
case StoredWithTrivialAccessors:
return true;
case StoredWithObservers:
case InheritedWithObservers:
case Computed:
case ComputedWithMutableAddress:
case Addressed:
case AddressedWithTrivialAccessors:
case AddressedWithObservers:
return false;
}
llvm_unreachable("bad storage kind");
}

/// \brief Return true if this object has a getter (and, if mutable,
/// a setter and a materializeForSet).
bool hasAccessorFunctions() const {
Expand Down Expand Up @@ -4456,7 +4475,8 @@ class AbstractStorageDecl : public ValueDecl {

/// Determine how this storage declaration should actually be accessed.
AccessStrategy getAccessStrategy(AccessSemantics semantics,
AccessKind accessKind) const;
AccessKind accessKind,
DeclContext *accessFromDC = nullptr) const;

/// \brief Should this declaration behave as if it must be accessed
/// resiliently, even when we're building a non-resilient module?
Expand Down
14 changes: 10 additions & 4 deletions include/swift/SIL/SILProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,31 @@ class SILProperty : public llvm::ilist_node<SILProperty>,
AbstractStorageDecl *Decl;

/// The key path component that represents its implementation.
KeyPathPatternComponent Component;
Optional<KeyPathPatternComponent> Component;

SILProperty(bool Serialized,
AbstractStorageDecl *Decl,
KeyPathPatternComponent Component)
Optional<KeyPathPatternComponent> Component)
: Serialized(Serialized), Decl(Decl), Component(Component)
{}

public:
static SILProperty *create(SILModule &M,
bool Serialized,
AbstractStorageDecl *Decl,
KeyPathPatternComponent Component);
Optional<KeyPathPatternComponent> Component);

bool isSerialized() const { return Serialized; }

AbstractStorageDecl *getDecl() const { return Decl; }

const KeyPathPatternComponent &getComponent() const { return Component; }
bool isTrivial() const {
return !Component.hasValue();
}

const Optional<KeyPathPatternComponent> &getComponent() const {
return Component;
}

void print(SILPrintContext &Ctx) const;
void dump() const;
Expand Down
2 changes: 2 additions & 0 deletions include/swift/SIL/TypeLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,8 @@ class TypeConverter {
CanSILBoxType getBoxTypeForEnumElement(SILType enumType,
EnumElementDecl *elt);

bool canStorageUseStoredKeyPathComponent(AbstractStorageDecl *decl);

private:
CanType getLoweredRValueType(AbstractionPattern origType, CanType substType);

Expand Down
12 changes: 10 additions & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,8 @@ ValueDecl::getAccessSemanticsFromContext(const DeclContext *UseDC,

AccessStrategy
AbstractStorageDecl::getAccessStrategy(AccessSemantics semantics,
AccessKind accessKind) const {
AccessKind accessKind,
DeclContext *accessFromDC) const {
switch (semantics) {
case AccessSemantics::DirectToStorage:
switch (getStorageKind()) {
Expand Down Expand Up @@ -1481,7 +1482,14 @@ AbstractStorageDecl::getAccessStrategy(AccessSemantics semantics,
// This is done by using DirectToStorage semantics above, with the
// understanding that the access semantics are with respect to the
// resilience domain of the accessor's caller.
if (isResilient())
bool resilient;
if (accessFromDC)
resilient = isResilient(accessFromDC->getParentModule(),
ResilienceExpansion::Maximal);
else
resilient = isResilient();

if (resilient)
return AccessStrategy::DirectToAccessor;

if (storageKind == StoredWithObservers ||
Expand Down
30 changes: 29 additions & 1 deletion lib/IRGen/GenKeyPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,33 @@ IRGenModule::getAddrOfKeyPathPattern(KeyPathPattern *pattern,
}

void IRGenModule::emitSILProperty(SILProperty *prop) {
if (prop->isTrivial()) {
// All trivial property descriptors can share a single definition in the
// translation unit.
if (!TheTrivialPropertyDescriptor) {
// Emit a definition if we don't have one yet.
ConstantInitBuilder builder(*this);
ConstantStructBuilder fields = builder.beginStruct();
fields.addInt32(
_SwiftKeyPathComponentHeader_TrivialPropertyDescriptorMarker);
auto var = cast<llvm::GlobalVariable>(
getAddrOfPropertyDescriptor(prop->getDecl(),
fields.finishAndCreateFuture()));
var->setConstant(true);
var->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
var->setAlignment(4);

TheTrivialPropertyDescriptor = var;
} else {
auto entity = LinkEntity::forPropertyDescriptor(prop->getDecl());
auto linkInfo = LinkInfo::get(*this, entity, ForDefinition);
llvm::GlobalAlias::create(linkInfo.getLinkage(),
linkInfo.getName(),
TheTrivialPropertyDescriptor);
}
return;
}

ConstantInitBuilder builder(*this);
ConstantStructBuilder fields = builder.beginStruct();
fields.setPacked(true);
Expand Down Expand Up @@ -1245,7 +1272,7 @@ void IRGenModule::emitSILProperty(SILProperty *prop) {
[&](GenericRequirement reqt) { requirements.push_back(reqt); });
}

emitKeyPathComponent(*this, fields, prop->getComponent(),
emitKeyPathComponent(*this, fields, *prop->getComponent(),
isInstantiableInPlace, genericEnv, requirements,
prop->getDecl()->getInnermostDeclContext()
->getInnermostTypeContext()
Expand All @@ -1260,6 +1287,7 @@ void IRGenModule::emitSILProperty(SILProperty *prop) {
getAddrOfPropertyDescriptor(prop->getDecl(),
fields.finishAndCreateFuture()));
var->setConstant(true);
var->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
// A simple stored component descriptor can fit in four bytes. Anything else
// needs pointer alignment.
if (size <= Size(4))
Expand Down
2 changes: 2 additions & 0 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,8 @@ class IRGenModule {
llvm::PointerType *OpenedErrorTriplePtrTy; /// { %swift.opaque*, %swift.type*, i8** }*
llvm::PointerType *WitnessTablePtrPtrTy; /// i8***

llvm::GlobalVariable *TheTrivialPropertyDescriptor = nullptr;

/// Used to create unique names for class layout types with tail allocated
/// elements.
unsigned TailElemTypeID = 0;
Expand Down
23 changes: 15 additions & 8 deletions lib/ParseSIL/ParseSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5464,19 +5464,26 @@ bool SILParserTUState::parseSILProperty(Parser &P) {
}

Identifier ComponentKind;
KeyPathPatternComponent Component;
Optional<KeyPathPatternComponent> Component;
SourceLoc ComponentLoc;
SmallVector<SILType, 4> OperandTypes;

if (P.parseToken(tok::l_paren, diag::expected_tok_in_sil_instr, "(")
|| P.parseIdentifier(ComponentKind, ComponentLoc,
diag::expected_tok_in_sil_instr, "component kind")
|| SP.parseKeyPathPatternComponent(Component, OperandTypes,
ComponentLoc, ComponentKind, InstLoc,
patternEnv)
|| P.parseToken(tok::r_paren, diag::expected_tok_in_sil_instr, ")"))
if (P.parseToken(tok::l_paren, diag::expected_tok_in_sil_instr, "("))
return true;

if (!P.consumeIf(tok::r_paren)) {
KeyPathPatternComponent parsedComponent;
if (P.parseIdentifier(ComponentKind, ComponentLoc,
diag::expected_tok_in_sil_instr, "component kind")
|| SP.parseKeyPathPatternComponent(parsedComponent, OperandTypes,
ComponentLoc, ComponentKind, InstLoc,
patternEnv)
|| P.parseToken(tok::r_paren, diag::expected_tok_in_sil_instr, ")"))
return true;

Component = std::move(parsedComponent);
}

SILProperty::create(M, Serialized,
cast<AbstractStorageDecl>(VD), Component);
return false;
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/SILModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ void SILModule::setOptRecordStream(
SILProperty *SILProperty::create(SILModule &M,
bool Serialized,
AbstractStorageDecl *Decl,
KeyPathPatternComponent Component) {
Optional<KeyPathPatternComponent> Component) {
auto prop = new (M) SILProperty(Serialized, Decl, Component);
M.properties.push_back(prop);
return prop;
Expand Down
6 changes: 3 additions & 3 deletions lib/SIL/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2648,10 +2648,10 @@ void SILProperty::print(SILPrintContext &Ctx) const {
if (auto sig = getDecl()->getInnermostDeclContext()
->getGenericSignatureOfContext()) {
sig->getCanonicalSignature()->print(OS, Options);
OS << ' ';
}
OS << '(';
SILPrinter(Ctx).printKeyPathPatternComponent(getComponent());
OS << " (";
if (auto component = getComponent())
SILPrinter(Ctx).printKeyPathPatternComponent(*component);
OS << ")\n";
}

Expand Down
32 changes: 16 additions & 16 deletions lib/SIL/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4814,7 +4814,6 @@ void SILProperty::verify(const SILModule &M) const {

auto *decl = getDecl();
auto *dc = decl->getInnermostDeclContext();
auto &component = getComponent();

// TODO: base type for global/static descriptors
auto sig = dc->getGenericSignatureOfContext();
Expand Down Expand Up @@ -4844,21 +4843,22 @@ void SILProperty::verify(const SILModule &M) const {
}
};

verifyKeyPathComponent(const_cast<SILModule&>(M),
require,
baseTy,
leafTy,
component,
{},
canSig,
subs,
/*property descriptor*/true,
hasIndices);

// verifyKeyPathComponent updates baseTy to be the projected type of the
// component, which should be the same as the type of the declared storage
require(baseTy == leafTy,
"component type of property descriptor should match type of storage");
if (auto &component = getComponent()) {
verifyKeyPathComponent(const_cast<SILModule&>(M),
require,
baseTy,
leafTy,
*component,
{},
canSig,
subs,
/*property descriptor*/true,
hasIndices);
// verifyKeyPathComponent updates baseTy to be the projected type of the
// component, which should be the same as the type of the declared storage
require(baseTy == leafTy,
"component type of property descriptor should match type of storage");
}
}

/// Verify that a vtable follows invariants.
Expand Down
Loading