Skip to content

Re-implement NominalTypeDecl::getStoredProperties() using request evaluator #26119

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 4 commits into from
Jul 17, 2019
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
1 change: 1 addition & 0 deletions include/swift/AST/ASTTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//===----------------------------------------------------------------------===//
SWIFT_TYPEID_NAMED(NominalTypeDecl *, NominalTypeDecl)
SWIFT_TYPEID_NAMED(VarDecl *, VarDecl)
SWIFT_TYPEID_NAMED(Decl *, Decl)
SWIFT_TYPEID(Type)
SWIFT_TYPEID(PropertyWrapperBackingPropertyInfo)
SWIFT_TYPEID(PropertyWrapperTypeInfo)
Expand Down
32 changes: 2 additions & 30 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3451,40 +3451,12 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
/// Retrieve information about this type as a property wrapper.
PropertyWrapperTypeInfo getPropertyWrapperTypeInfo() const;

private:
/// Predicate used to filter StoredPropertyRange.
struct ToStoredProperty {
ToStoredProperty() {}
Optional<VarDecl *> operator()(Decl *decl) const;
};

public:
/// A range for iterating the stored member variables of a structure.
using StoredPropertyRange = OptionalTransformRange<DeclRange,
ToStoredProperty>;

/// Return a collection of the stored member variables of this type.
StoredPropertyRange getStoredProperties() const;
ArrayRef<VarDecl *> getStoredProperties() const;

private:
/// Predicate used to filter StoredPropertyRange.
struct ToStoredPropertyOrMissingMemberPlaceholder {
Optional<Decl *> operator()(Decl *decl) const;
};

public:
/// A range for iterating the stored member variables of a structure.
using StoredPropertyOrMissingMemberPlaceholderRange
= OptionalTransformRange<DeclRange,
ToStoredPropertyOrMissingMemberPlaceholder>;

/// Return a collection of the stored member variables of this type, along
/// with placeholders for unimportable stored properties.
StoredPropertyOrMissingMemberPlaceholderRange
getStoredPropertiesAndMissingMemberPlaceholders() const {
return StoredPropertyOrMissingMemberPlaceholderRange(getMembers(),
ToStoredPropertyOrMissingMemberPlaceholder());
}
ArrayRef<Decl *> getStoredPropertiesAndMissingMemberPlaceholders() const;

// Implement isa/cast/dyncast/etc.
static bool classof(const Decl *D) {
Expand Down
46 changes: 46 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,52 @@ class TypeCheckFunctionBodyUntilRequest :
bool isCached() const { return true; }
};

/// Request to obtain a list of stored properties in a nominal type.
///
/// This will include backing storage for lazy properties and
/// property wrappers, synthesizing them if necessary.
class StoredPropertiesRequest :
public SimpleRequest<StoredPropertiesRequest,
ArrayRef<VarDecl *>(NominalTypeDecl *),
CacheKind::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
llvm::Expected<ArrayRef<VarDecl *>>
evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const;

public:
bool isCached() const { return true; }
};

/// Request to obtain a list of stored properties in a nominal type,
/// together with any missing members corresponding to stored
/// properties that could not be deserialized.
///
/// This will include backing storage for lazy properties and
/// property wrappers, synthesizing them if necessary.
class StoredPropertiesAndMissingMembersRequest :
public SimpleRequest<StoredPropertiesAndMissingMembersRequest,
ArrayRef<Decl *>(NominalTypeDecl *),
CacheKind::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
llvm::Expected<ArrayRef<Decl *>>
evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const;

public:
bool isCached() const { return true; }
};

// Allow AnyValue to compare two Type values, even though Type doesn't
// support ==.
template<>
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ SWIFT_TYPEID(IsSetterMutatingRequest)
SWIFT_TYPEID(OpaqueReadOwnershipRequest)
SWIFT_TYPEID(LazyStoragePropertyRequest)
SWIFT_TYPEID(TypeCheckFunctionBodyUntilRequest)
SWIFT_TYPEID(StoredPropertiesRequest)
SWIFT_TYPEID(StoredPropertiesAndMissingMembersRequest)
2 changes: 1 addition & 1 deletion include/swift/Basic/CTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ SWIFT_TYPEID_TEMPLATE1_NAMED(std::vector, Vector, typename T, T)

// LLVM ADT types.
SWIFT_TYPEID_TEMPLATE1_NAMED(llvm::TinyPtrVector, TinyPtrVector, typename T, T)

SWIFT_TYPEID_TEMPLATE1_NAMED(llvm::ArrayRef, ArrayRef, typename T, T)
14 changes: 14 additions & 0 deletions include/swift/Basic/SimpleDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ namespace swift {
}
out << "}";
}

template<typename T>
void simple_display(llvm::raw_ostream &out,
const llvm::ArrayRef<T> &array) {
out << "{";
bool first = true;
for (const T &value : array) {
if (first) first = false;
else out << ", ";

simple_display(out, value);
}
out << "}";
}
}

#endif // SWIFT_BASIC_SIMPLE_DISPLAY_H
3 changes: 0 additions & 3 deletions include/swift/Basic/Statistics.def
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,6 @@ FRONTEND_STATISTIC(AST, NumPrefixOperators)
/// Number of precedence groups in the AST context.
FRONTEND_STATISTIC(AST, NumPrecedenceGroups)

/// Number of precedence groups in the AST context.
FRONTEND_STATISTIC(AST, NumStoredPropertiesQueries)

/// Number of full function bodies parsed.
FRONTEND_STATISTIC(Parse, NumFunctionsParsed)

Expand Down
6 changes: 1 addition & 5 deletions include/swift/SIL/Projection.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,18 +317,14 @@ class Projection {
llvm_unreachable("Unhandled ProjectionKind in switch.");
}

/// WARNING: This is not a constant time operation because it requests all
/// BaseType's stored properties.
VarDecl *getVarDecl(SILType BaseType) const {
assert(isValid());
assert((getKind() == ProjectionKind::Struct ||
getKind() == ProjectionKind::Class));
assert(BaseType.getNominalOrBoundGenericNominal() &&
"This should only be called with a nominal type");
auto *NDecl = BaseType.getNominalOrBoundGenericNominal();
auto Iter = NDecl->getStoredProperties().begin();
std::advance(Iter, getIndex());
return *Iter;
return NDecl->getStoredProperties()[getIndex()];
}

EnumElementDecl *getEnumElementDecl(SILType BaseType) const {
Expand Down
9 changes: 4 additions & 5 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -4824,11 +4824,10 @@ class StructInst final : public InstructionBaseWithTrailingOperands<

StructDecl *S = getStructDecl();

NominalTypeDecl::StoredPropertyRange Range = S->getStoredProperties();
unsigned Index = 0;
for (auto I = Range.begin(), E = Range.end(); I != E; ++I, ++Index)
if (V == *I)
return &getAllOperands()[Index];
auto Props = S->getStoredProperties();
for (unsigned I = 0, E = Props.size(); I < E; ++I)
if (V == Props[I])
return &getAllOperands()[I];

// Did not find a matching VarDecl, return nullptr.
return nullptr;
Expand Down
7 changes: 2 additions & 5 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1172,16 +1172,13 @@ FuncDecl *ASTContext::getArrayReserveCapacityDecl() const {

StructDecl *IntDecl = IntType->getDecl();
auto StoredProperties = IntDecl->getStoredProperties();
auto FieldIter = StoredProperties.begin();
if (FieldIter == StoredProperties.end())
if (StoredProperties.size() != 1)
return nullptr;
VarDecl *field = *FieldIter;
VarDecl *field = StoredProperties[0];
if (field->hasClangNode())
return nullptr;
if (!field->getInterfaceType()->is<BuiltinIntegerType>())
return nullptr;
if (std::next(FieldIter) != StoredProperties.end())
return nullptr;

if (!FnDecl->getResultInterfaceType()->isVoid())
return nullptr;
Expand Down
66 changes: 21 additions & 45 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "swift/Basic/Range.h"
#include "swift/Basic/StringExtras.h"
#include "swift/Basic/Statistic.h"
#include "swift/Basic/TypeID.h"
#include "swift/Demangling/ManglingMacros.h"

#include "clang/Basic/CharInfo.h"
Expand Down Expand Up @@ -1029,31 +1030,6 @@ ImportDecl::findBestImportKind(ArrayRef<ValueDecl *> Decls) {
return FirstKind;
}

Optional<VarDecl *>
NominalTypeDecl::ToStoredProperty::operator()(Decl *decl) const {
if (auto var = dyn_cast<VarDecl>(decl)) {
if (!var->isStatic() && var->hasStorage())
return var;
}

return None;
}

Optional<Decl *>
NominalTypeDecl::ToStoredPropertyOrMissingMemberPlaceholder
::operator()(Decl *decl) const {
if (auto var = dyn_cast<VarDecl>(decl)) {
if (!var->isStatic() && var->hasStorage())
return var;
}
if (auto missing = dyn_cast<MissingMemberDecl>(decl)) {
if (missing->getNumberOfFieldOffsetVectorEntries() > 0)
return missing;
}

return None;
}

void NominalTypeDecl::setConformanceLoader(LazyMemberLoader *lazyLoader,
uint64_t contextData) {
assert(!Bits.NominalTypeDecl.HasLazyConformances &&
Expand Down Expand Up @@ -3460,22 +3436,23 @@ void NominalTypeDecl::addExtension(ExtensionDecl *extension) {
addedExtension(extension);
}

auto NominalTypeDecl::getStoredProperties() const
-> StoredPropertyRange {
// This should be called at most once per SIL instruction that accesses a
// VarDecl.
//
// FIXME: Once VarDecl itself caches its field index, it should be called at
// most once per finalized VarDecl.
if (getASTContext().Stats)
getASTContext().Stats->getFrontendCounters().NumStoredPropertiesQueries++;

// Clang-imported classes never have stored properties.
if (hasClangNode() && isa<ClassDecl>(this))
return StoredPropertyRange(DeclRange(nullptr, nullptr),
ToStoredProperty());
ArrayRef<VarDecl *> NominalTypeDecl::getStoredProperties() const {
auto &ctx = getASTContext();
auto mutableThis = const_cast<NominalTypeDecl *>(this);
return evaluateOrDefault(
ctx.evaluator,
StoredPropertiesRequest{mutableThis},
{});
}

return StoredPropertyRange(getMembers(), ToStoredProperty());
ArrayRef<Decl *>
NominalTypeDecl::getStoredPropertiesAndMissingMemberPlaceholders() const {
auto &ctx = getASTContext();
auto mutableThis = const_cast<NominalTypeDecl *>(this);
return evaluateOrDefault(
ctx.evaluator,
StoredPropertiesAndMissingMembersRequest{mutableThis},
{});
}

bool NominalTypeDecl::isOptionalDecl() const {
Expand Down Expand Up @@ -5435,14 +5412,13 @@ bool VarDecl::isSelfParameter() const {
/// a declared property that is either `lazy` or has an attached
/// property wrapper.
static bool isBackingStorageForDeclaredProperty(const VarDecl *var) {
if (var->getOriginalWrappedProperty())
if (var->isLazyStorageProperty())
return true;

auto name = var->getName();
if (name.empty())
return false;
if (var->getOriginalWrappedProperty())
return true;

return name.str().startswith("$__lazy_storage_$_");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhhh, thank you!

return false;
}

/// Whether the given variable is a delcared property that has separate backing storage.
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticsCommon.h"
#include "swift/AST/Module.h"
#include "swift/AST/PropertyWrappers.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/TypeLoc.h"
#include "swift/AST/TypeRepr.h"
#include "swift/AST/Types.h"
Expand Down
4 changes: 2 additions & 2 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ namespace {
auto properties = getType()->getStoredProperties();

// uint32_t NumFields;
B.addInt32(std::distance(properties.begin(), properties.end()));
B.addInt32(properties.size());

// uint32_t FieldOffsetVectorOffset;
B.addInt32(FieldVectorOffset / IGM.getPointerSize());
Expand Down Expand Up @@ -1640,7 +1640,7 @@ namespace {
B.addInt32(numImmediateMembers);

// uint32_t NumFields;
B.addInt32(std::distance(properties.begin(), properties.end()));
B.addInt32(properties.size());

// uint32_t FieldOffsetVectorOffset;
B.addInt32(getFieldVectorOffset() / IGM.getPointerSize());
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
B.addInt16(fieldRecordSize);

auto properties = NTD->getStoredProperties();
B.addInt32(std::distance(properties.begin(), properties.end()));
B.addInt32(properties.size());
for (auto property : properties)
addFieldDecl(property, property->getInterfaceType(),
NTD->getGenericSignature());
Expand Down
6 changes: 2 additions & 4 deletions lib/IRGen/GenStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,14 +944,12 @@ const TypeInfo *TypeConverter::convertStructType(TypeBase *key, CanType type,

} else if (isa<clang::EnumDecl>(clangDecl)) {
// Fall back to Swift lowering for the enum's representation as a struct.
assert(std::distance(D->getStoredProperties().begin(),
D->getStoredProperties().end()) == 1 &&
assert(D->getStoredProperties().size() == 1 &&
"Struct representation of a Clang enum should wrap one value");
} else if (clangDecl->hasAttr<clang::SwiftNewtypeAttr>()) {
// Fall back to Swift lowering for the underlying type's
// representation as a struct member.
assert(std::distance(D->getStoredProperties().begin(),
D->getStoredProperties().end()) == 1 &&
assert(D->getStoredProperties().size() == 1 &&
"Struct representation of a swift_newtype should wrap one value");
} else {
llvm_unreachable("Swift struct represents unexpected imported type");
Expand Down
5 changes: 2 additions & 3 deletions lib/IRGen/GenType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2304,9 +2304,8 @@ SILType irgen::getSingletonAggregateFieldType(IRGenModule &IGM, SILType t,
// If there's only one stored property, we have the layout of its field.
auto allFields = structDecl->getStoredProperties();

auto field = allFields.begin();
if (!allFields.empty() && std::next(field) == allFields.end()) {
auto fieldTy = t.getFieldType(*field, IGM.getSILModule());
if (allFields.size() == 1) {
auto fieldTy = t.getFieldType(allFields[0], IGM.getSILModule());
if (!IGM.isTypeABIAccessible(fieldTy))
return SILType();
return fieldTy;
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/MemAccessUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const ValueDecl *AccessedStorage::getDecl() const {

case Class: {
auto *decl = getObject()->getType().getNominalOrBoundGenericNominal();
return *std::next(decl->getStoredProperties().begin(), getPropertyIndex());
return decl->getStoredProperties()[getPropertyIndex()];
}
case Argument:
return getArgument()->getDecl();
Expand Down
Loading