Skip to content

RequirementMachine: Implement some generic signature queries #38147

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 18 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
8da15f8
AST: Add an extension point to TypeMatcher to always mismatch generic…
slavapestov Jun 23, 2021
e4faf73
RequirementMachine: Fix another bug in checkForOverlap()
slavapestov Jun 28, 2021
d03ff29
RequirementMachine: Protocol atoms order before associated type atoms
slavapestov Jun 29, 2021
8107cea
RequirementMachine: Define operator<< overloads
slavapestov Jun 28, 2021
40e3409
RequirementMachine: Tiny comment fix
slavapestov Jun 28, 2021
ac9a285
RequirementMachine: Remove bogus pre-sorting pass
slavapestov Jun 30, 2021
3803767
RequirementMachine: Fix logic error in mergeAssociatedTypes()
slavapestov Jun 30, 2021
3418f47
RequirementMachine: Actually compute the inherited protocol transitiv…
slavapestov Jun 30, 2021
f28d9b3
RequirementMachine: Stub out the equivalence class map
slavapestov Jun 17, 2021
6fa7a3f
AST: Introduce GenericSignatureImpl::getRequirementMachine()
slavapestov Jun 28, 2021
7df09f1
RequirementMachine: Implement GenericSignature::requiresClass() query
slavapestov Jun 28, 2021
8053b91
RequirementMachine: Implement GenericSignature::requiresProtocol() query
slavapestov Jun 28, 2021
53d6814
RequirementMachine: Implement GenericSignature::getLayoutConstraint()…
slavapestov Jun 28, 2021
62e0ad0
RequirementMachine: Add a dump() method
slavapestov Jun 28, 2021
5d91b59
RequirementMachine: Implement GenericSignature::isConcreteType() query
slavapestov Jun 29, 2021
a37ad9f
RequirementMachine: Implement GenericSignature::areSameTypeParameters…
slavapestov Jun 29, 2021
05b3f79
RequirementMachine: Implement GenericSignature::getRequiredProtocols(…
slavapestov Jun 29, 2021
8ffe44f
RequirementMachine: Bump completion step limit again
slavapestov Jun 30, 2021
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: 4 additions & 0 deletions include/swift/AST/GenericSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace swift {
class GenericSignatureBuilder;
class ProtocolConformanceRef;
class ProtocolType;
class RequirementMachine;
class SubstitutionMap;
class GenericEnvironment;

Expand Down Expand Up @@ -303,6 +304,9 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
/// Retrieve the generic signature builder for the given generic signature.
GenericSignatureBuilder *getGenericSignatureBuilder() const;

/// Retrieve the requirement machine for the given generic signature.
RequirementMachine *getRequirementMachine() const;

/// Returns the generic environment that provides fresh contextual types
/// (archetypes) that correspond to the interface types in this generic
/// signature.
Expand Down
22 changes: 19 additions & 3 deletions include/swift/AST/RequirementMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@
#ifndef SWIFT_REQUIREMENTMACHINE_H
#define SWIFT_REQUIREMENTMACHINE_H

#include "swift/AST/GenericSignature.h"

namespace llvm {
class raw_ostream;
}

namespace swift {

class ASTContext;
class AssociatedTypeDecl;
class CanGenericSignature;
class CanType;
class GenericSignature;
class LayoutConstraint;
class ProtocolDecl;
class Requirement;
class Type;

/// Wraps a rewrite system with higher-level operations in terms of
/// generic signatures and interface types.
Expand All @@ -43,10 +49,20 @@ class RequirementMachine final {
void addGenericSignature(CanGenericSignature sig);

bool isComplete() const;
void markComplete();
void computeCompletion(CanGenericSignature sig);

public:
~RequirementMachine();

// Generic signature queries
bool requiresClass(Type depType) const;
LayoutConstraint getLayoutConstraint(Type depType) const;
bool requiresProtocol(Type depType, const ProtocolDecl *proto) const;
GenericSignature::RequiredProtocols getRequiredProtocols(Type depType) const;
bool isConcreteType(Type depType) const;
bool areSameTypeParameterInContext(Type depType1, Type depType2) const;

void dump(llvm::raw_ostream &out) const;
};

} // end namespace swift
Expand Down
16 changes: 15 additions & 1 deletion include/swift/AST/TypeMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,21 @@ class TypeMatcher {
TRIVIAL_CASE(ModuleType)
TRIVIAL_CASE(DynamicSelfType)
TRIVIAL_CASE(ArchetypeType)
TRIVIAL_CASE(GenericTypeParamType)
TRIVIAL_CASE(DependentMemberType)

bool visitGenericTypeParamType(CanGenericTypeParamType firstType,
Type secondType,
Type sugaredFirstType) {
/* If the types match, continue. */
if (!Matcher.asDerived().alwaysMismatchGenericParams() &&
firstType->isEqual(secondType))
return true;

/* Otherwise, let the derived class deal with the mismatch. */
return mismatch(firstType.getPointer(), secondType,
sugaredFirstType);
}

/// FIXME: Split this out into cases?
bool visitAnyFunctionType(CanAnyFunctionType firstFunc, Type secondType,
Type sugaredFirstType) {
Expand Down Expand Up @@ -300,6 +312,8 @@ class TypeMatcher {
#undef TRIVIAL_CASE
};

bool alwaysMismatchGenericParams() const { return false; }

ImplClass &asDerived() { return static_cast<ImplClass &>(*this); }

const ImplClass &asDerived() const {
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ namespace swift {

/// Maximum iteration count for requirement machine confluent completion
/// algorithm.
unsigned RequirementMachineStepLimit = 1000;
unsigned RequirementMachineStepLimit = 2000;

/// Maximum term length for requirement machine confluent completion
/// algorithm.
Expand Down
6 changes: 4 additions & 2 deletions include/swift/Basic/Statistics.def
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,12 @@ FRONTEND_STATISTIC(Sema, NumAccessorBodiesSynthesized)
/// amount of work the requirement machine does analyzing type signatures.
FRONTEND_STATISTIC(Sema, NumRequirementMachines)

/// Number of requirement machines constructed. Rough proxy for
/// amount of work the requirement machine does analyzing type signatures.
/// Number of new rules added by Knuth-Bendix completion procedure.
FRONTEND_STATISTIC(Sema, NumRequirementMachineCompletionSteps)

/// Number of new rules added by concrete term unification.
FRONTEND_STATISTIC(Sema, NumRequirementMachineUnifiedConcreteTerms)

/// Number of generic signature builders constructed. Rough proxy for
/// amount of work the GSB does analyzing type signatures.
FRONTEND_STATISTIC(Sema, NumGenericSignatureBuilders)
Expand Down
1 change: 1 addition & 0 deletions lib/AST/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ add_swift_host_library(swiftAST STATIC
ProtocolConformance.cpp
RawComment.cpp
RequirementEnvironment.cpp
RequirementMachine/EquivalenceClassMap.cpp
RequirementMachine/ProtocolGraph.cpp
RequirementMachine/RequirementMachine.cpp
RequirementMachine/RewriteSystem.cpp
Expand Down
Loading