Skip to content

[Type checker] Requestify the formation of an abstract generic signature #26802

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
Show all changes
20 commits
Select commit Hold shift + click to select a range
a23a435
[Type checker] Requestify the formation of an abstract generic signature
DougGregor Aug 22, 2019
4dfd071
Switch getOverrideGenericSignature() to the abstract generic signatur…
DougGregor Aug 22, 2019
9f35ce5
[SIL Optimizer] Use the abstract generic signature request.
DougGregor Aug 23, 2019
845ce11
[SIL Optimizer] Switch partial specialization to a request.
DougGregor Aug 23, 2019
25fc798
[Existential specializer] Switch to abstract generic signature request.
DougGregor Aug 23, 2019
4685f9d
[SILGen] Use abstract generic signature request for reabstraction thunks
DougGregor Aug 23, 2019
1167070
Switch configureGenericDesignatedInitOverride to be request-based
DougGregor Aug 23, 2019
64ccffc
Switch substGenericFunctionType to the abstract generic signature req…
DougGregor Aug 23, 2019
0f80384
[AST] Switch builtin creation over to the abstract generic signature …
DougGregor Aug 23, 2019
41f232f
[AST Demangler] Switch over to the abstract generic signature request.
DougGregor Aug 23, 2019
f277280
[Clang importer] Swift over to the abstract generic signature request.
DougGregor Aug 23, 2019
d1b945a
Switch opaque result type construction over to the abstract generic s…
DougGregor Aug 23, 2019
cfba13d
Switch other opaque result type construction over to the abstract gen…
DougGregor Aug 23, 2019
30d871b
Switch requirement environment over to the abstract generic signature…
DougGregor Aug 23, 2019
0e2bb0f
Prune includes of GenericSignatureBuilder.h.
DougGregor Aug 23, 2019
70c73ec
Don't create a generic signature builder when only adding parameters.
DougGregor Aug 23, 2019
8ad76a1
Canonicalize requests for the abstract generic signature.
DougGregor Aug 24, 2019
c8ac000
Record specialized signature in (SIL)SpecializeAttr.
DougGregor Aug 24, 2019
8c2707c
Fix printing of (SIL)SpecializeAttr.
DougGregor Aug 26, 2019
7883dc2
[SIL Printer] Make SILSpecializeAttr printing defensive
DougGregor Aug 26, 2019
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
3 changes: 3 additions & 0 deletions include/swift/AST/ASTTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ SWIFT_TYPEID_NAMED(Optional<PropertyWrapperMutability>, PropertyWrapperMutabilit
SWIFT_TYPEID_NAMED(CustomAttr *, CustomAttr)
SWIFT_TYPEID_NAMED(TypeAliasDecl *, TypeAliasDecl)
SWIFT_TYPEID(AncestryFlags)
SWIFT_TYPEID_NAMED(GenericSignature *, GenericSignature)
SWIFT_TYPEID_NAMED(GenericTypeParamType *, GenericTypeParamType)
SWIFT_TYPEID(Requirement)
3 changes: 3 additions & 0 deletions include/swift/AST/ASTTypeIDs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
namespace swift {

class CustomAttr;
class GenericSignature;
class GenericTypeParamType;
class NominalTypeDecl;
struct PropertyWrapperBackingPropertyInfo;
struct PropertyWrapperTypeInfo;
enum class CtorInitializerKind;
struct PropertyWrapperMutability;
class Requirement;
class Type;
class VarDecl;
class TypeAliasDecl;
Expand Down
38 changes: 13 additions & 25 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,9 @@ class DeclAttribute : public AttributeBase {
ownership : NumReferenceOwnershipBits
);

SWIFT_INLINE_BITFIELD_FULL(SpecializeAttr, DeclAttribute, 1+1+32,
SWIFT_INLINE_BITFIELD(SpecializeAttr, DeclAttribute, 1+1,
exported : 1,
kind : 1,
: NumPadBits,
numRequirements : 32
kind : 1
);

SWIFT_INLINE_BITFIELD(SynthesizedProtocolAttr, DeclAttribute,
Expand Down Expand Up @@ -1236,39 +1234,29 @@ class SpecializeAttr : public DeclAttribute {

private:
TrailingWhereClause *trailingWhereClause;

Requirement *getRequirementsData() {
return reinterpret_cast<Requirement *>(this+1);
}
GenericSignature *specializedSignature;

SpecializeAttr(SourceLoc atLoc, SourceRange Range,
TrailingWhereClause *clause, bool exported,
SpecializationKind kind);

SpecializeAttr(SourceLoc atLoc, SourceRange Range,
ArrayRef<Requirement> requirements,
bool exported,
SpecializationKind kind);
SpecializationKind kind,
GenericSignature *specializedSignature);

public:
static SpecializeAttr *create(ASTContext &Ctx, SourceLoc atLoc,
SourceRange Range, TrailingWhereClause *clause,
bool exported, SpecializationKind kind);

static SpecializeAttr *create(ASTContext &Ctx, SourceLoc atLoc,
SourceRange Range,
ArrayRef<Requirement> requirement,
bool exported, SpecializationKind kind);
bool exported, SpecializationKind kind,
GenericSignature *specializedSignature
= nullptr);

TrailingWhereClause *getTrailingWhereClause() const;

ArrayRef<Requirement> getRequirements() const;

MutableArrayRef<Requirement> getRequirements() {
return { getRequirementsData(), Bits.SpecializeAttr.numRequirements };
GenericSignature *getSpecializedSgnature() const {
return specializedSignature;
}

void setRequirements(ASTContext &Ctx, ArrayRef<Requirement> requirements);
void setSpecializedSignature(GenericSignature *newSig) {
specializedSignature = newSig;
}

bool isExported() const {
return Bits.SpecializeAttr.exported;
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/GenericSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ CanGenericSignature::CanGenericSignature(GenericSignature *Signature)
assert(!Signature || Signature->isCanonical());
}

void simple_display(raw_ostream &out, const GenericSignature *sig);

} // end namespace swift

#endif // SWIFT_AST_GENERIC_SIGNATURE_H
5 changes: 5 additions & 0 deletions include/swift/AST/LayoutConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "swift/Basic/SourceLoc.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/StringRef.h"
#include "swift/AST/PrintOptions.h"

Expand Down Expand Up @@ -290,6 +291,10 @@ class LayoutConstraint {
/// Return the layout constraint as a string, for use in diagnostics only.
std::string getString(const PrintOptions &PO = PrintOptions()) const;

friend llvm::hash_code hash_value(const LayoutConstraint &layout) {
return hash_value(layout.getPointer());
}

bool operator==(LayoutConstraint rhs) const {
if (isNull() && rhs.isNull())
return true;
Expand Down
49 changes: 49 additions & 0 deletions include/swift/AST/Requirement.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define SWIFT_AST_REQUIREMENT_H

#include "swift/AST/Type.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/Support/ErrorHandling.h"

Expand Down Expand Up @@ -119,12 +120,60 @@ class Requirement {
return SecondLayout;
}

/// Whether this requirement is in its canonical form.
bool isCanonical() const;

/// Get the canonical form of this requirement.
Requirement getCanonical() const;

void dump() const;
void dump(raw_ostream &out) const;
void print(raw_ostream &os, const PrintOptions &opts) const;
void print(ASTPrinter &printer, const PrintOptions &opts) const;

friend llvm::hash_code hash_value(const Requirement &requirement) {
using llvm::hash_value;

llvm::hash_code first =
hash_value(requirement.FirstTypeAndKind.getOpaqueValue());
llvm::hash_code second;
switch (requirement.getKind()) {
case RequirementKind::Conformance:
case RequirementKind::Superclass:
case RequirementKind::SameType:
second = hash_value(requirement.getSecondType());
break;

case RequirementKind::Layout:
second = hash_value(requirement.getLayoutConstraint());
break;
}

return llvm::hash_combine(first, second);
}

friend bool operator==(const Requirement &lhs, const Requirement &rhs) {
if (lhs.FirstTypeAndKind.getOpaqueValue()
!= rhs.FirstTypeAndKind.getOpaqueValue())
return false;

switch (lhs.getKind()) {
case RequirementKind::Conformance:
case RequirementKind::Superclass:
case RequirementKind::SameType:
return lhs.getSecondType().getPointer() ==
rhs.getSecondType().getPointer();

case RequirementKind::Layout:
return lhs.getLayoutConstraint() == rhs.getLayoutConstraint();
}
}
};

inline void simple_display(llvm::raw_ostream &out, const Requirement &req) {
req.print(out, PrintOptions());
}

} // end namespace swift

#endif
7 changes: 7 additions & 0 deletions include/swift/AST/SimpleRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,11 @@ class SimpleRequest<Derived, Output(Inputs...), Caching> {
};
}

namespace llvm {
template <typename T, unsigned N>
llvm::hash_code hash_value(const SmallVector<T, N> &vec) {
return hash_combine_range(vec.begin(), vec.end());
}
}

#endif // SWIFT_BASIC_SIMPLEREQUEST_H
6 changes: 6 additions & 0 deletions include/swift/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/STLExtras.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/ArrayRefView.h"
Expand Down Expand Up @@ -323,6 +324,11 @@ class Type {
/// Return the name of the type as a string, for use in diagnostics only.
std::string getString(const PrintOptions &PO = PrintOptions()) const;

friend llvm::hash_code hash_value(Type type) {
using llvm::hash_value;
return hash_value(type.getPointer());
}

/// Return the name of the type, adding parens in cases where
/// appending or prepending text to the result would cause that text
/// to be appended to only a portion of the returned type. For
Expand Down
33 changes: 32 additions & 1 deletion include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
namespace swift {

class AbstractStorageDecl;
class AccessorDecl;
enum class AccessorKind;
class GenericParamList;
struct PropertyWrapperBackingPropertyInfo;
struct PropertyWrapperMutability;
Expand All @@ -38,8 +40,8 @@ class SpecializeAttr;
class TypeAliasDecl;
struct TypeLoc;
class ValueDecl;
class AbstractStorageDecl;
enum class OpaqueReadOwnership: uint8_t;
class StorageImplInfo;

/// Display a nominal type or extension thereof.
void simple_display(
Expand Down Expand Up @@ -1073,6 +1075,35 @@ class ClassAncestryFlagsRequest :

void simple_display(llvm::raw_ostream &out, AncestryFlags value);

class AbstractGenericSignatureRequest :
public SimpleRequest<AbstractGenericSignatureRequest,
GenericSignature *(GenericSignature *,
SmallVector<GenericTypeParamType *, 2>,
SmallVector<Requirement, 2>),
CacheKind::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
llvm::Expected<GenericSignature *>
evaluate(Evaluator &evaluator,
GenericSignature *baseSignature,
SmallVector<GenericTypeParamType *, 2> addedParameters,
SmallVector<Requirement, 2> addedRequirements) const;

public:
// Separate caching.
bool isCached() const;

/// Abstract generic signature requests never have source-location info.
SourceLoc getNearestLoc() const {
return SourceLoc();
}
};

// Allow AnyValue to compare two Type values, even though Type doesn't
// support ==.
template<>
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ SWIFT_TYPEID(SynthesizeAccessorRequest)
SWIFT_TYPEID(EmittedMembersRequest)
SWIFT_TYPEID(IsImplicitlyUnwrappedOptionalRequest)
SWIFT_TYPEID(ClassAncestryFlagsRequest)
SWIFT_TYPEID(AbstractGenericSignatureRequest)
14 changes: 14 additions & 0 deletions include/swift/Basic/SimpleDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ namespace swift {
}
out << "}";
}

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

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

#endif // SWIFT_BASIC_SIMPLE_DISPLAY_H
20 changes: 9 additions & 11 deletions include/swift/SIL/SILFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ class SILSpecializeAttr final {
};

static SILSpecializeAttr *create(SILModule &M,
ArrayRef<Requirement> requirements,
GenericSignature *specializedSignature,
bool exported, SpecializationKind kind);

ArrayRef<Requirement> getRequirements() const;

bool isExported() const {
return exported;
}
Expand All @@ -91,24 +89,24 @@ class SILSpecializeAttr final {
return kind;
}

GenericSignature *getSpecializedSignature() const {
return specializedSignature;
}

SILFunction *getFunction() const {
return F;
}

void print(llvm::raw_ostream &OS) const;

private:
unsigned numRequirements;
SpecializationKind kind;
bool exported;
SILFunction *F;
GenericSignature *specializedSignature;
SILFunction *F = nullptr;

SILSpecializeAttr(ArrayRef<Requirement> requirements, bool exported,
SpecializationKind kind);

Requirement *getRequirementsData() {
return reinterpret_cast<Requirement *>(this+1);
}
SILSpecializeAttr(bool exported, SpecializationKind kind,
GenericSignature *specializedSignature);
};

/// SILFunction - A function body that has been lowered to SIL. This consists of
Expand Down
5 changes: 2 additions & 3 deletions include/swift/SILOptimizer/Utils/Generics.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ class ReabstractionInfo {
OptRemark::Emitter *ORE = nullptr);

/// Constructs the ReabstractionInfo for generic function \p Callee with
/// additional requirements. Requirements may contain new layout,
/// conformances or same concrete type requirements.
ReabstractionInfo(SILFunction *Callee, ArrayRef<Requirement> Requirements);
/// a specialization signature.
ReabstractionInfo(SILFunction *Callee, GenericSignature *SpecializedSig);

IsSerialized_t isSerialized() const {
return Serialized;
Expand Down
5 changes: 3 additions & 2 deletions include/swift/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t SWIFTMODULE_VERSION_MINOR = 513; // Added copy_unmanaged_value.
const uint16_t SWIFTMODULE_VERSION_MINOR = 514; // specialize attr

using DeclIDField = BCFixed<31>;

Expand Down Expand Up @@ -1642,7 +1642,8 @@ namespace decls_block {
using SpecializeDeclAttrLayout = BCRecordLayout<
Specialize_DECL_ATTR,
BCFixed<1>, // exported flag
BCFixed<1> // specialization kind
BCFixed<1>, // specialization kind
GenericSignatureIDField // specialized signature
>;

#define SIMPLE_DECL_ATTR(X, CLASS, ...) \
Expand Down
Loading