Skip to content

Commit bec4ebd

Browse files
authored
Merge pull request #82574 from xedin/solver-perf-behind-a-flag
[ConstraintSystem] Implement disjunction favoring algorithm behind a flag
2 parents 377c66b + 4591884 commit bec4ebd

File tree

78 files changed

+3392
-151
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+3392
-151
lines changed

include/swift/AST/KnownStdlibTypes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ KNOWN_STDLIB_TYPE_DECL(WritableKeyPath, NominalTypeDecl, 2)
7070
KNOWN_STDLIB_TYPE_DECL(ReferenceWritableKeyPath, NominalTypeDecl, 2)
7171

7272
KNOWN_STDLIB_TYPE_DECL(Optional, EnumDecl, 1)
73+
KNOWN_STDLIB_TYPE_DECL(_OptionalNilComparisonType, NominalTypeDecl, 0)
7374

7475
KNOWN_STDLIB_TYPE_DECL(OptionSet, NominalTypeDecl, 1)
7576

include/swift/AST/TypeCheckRequests.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5389,6 +5389,22 @@ class DefaultIsolationInSourceFileRequest
53895389
bool isCached() const { return true; }
53905390
};
53915391

5392+
class ModuleHasTypeCheckerPerformanceHacksEnabledRequest
5393+
: public SimpleRequest<ModuleHasTypeCheckerPerformanceHacksEnabledRequest,
5394+
bool(const ModuleDecl *),
5395+
RequestFlags::Cached> {
5396+
public:
5397+
using SimpleRequest::SimpleRequest;
5398+
5399+
private:
5400+
friend SimpleRequest;
5401+
5402+
bool evaluate(Evaluator &evaluator, const ModuleDecl *module) const;
5403+
5404+
public:
5405+
bool isCached() const { return true; }
5406+
};
5407+
53925408
#define SWIFT_TYPEID_ZONE TypeChecker
53935409
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
53945410
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,3 +640,7 @@ SWIFT_REQUEST(TypeChecker, SemanticAvailabilitySpecRequest,
640640
SWIFT_REQUEST(TypeChecker, DefaultIsolationInSourceFileRequest,
641641
std::optional<DefaultIsolation>(const SourceFile *),
642642
Cached, NoLocationInfo)
643+
644+
SWIFT_REQUEST(TypeChecker, ModuleHasTypeCheckerPerformanceHacksEnabledRequest,
645+
bool(const ModuleDecl *),
646+
Cached, NoLocationInfo)

include/swift/Basic/BlockListAction.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ BLOCKLIST_ACTION(ShouldUseLayoutStringValueWitnesses)
2626
BLOCKLIST_ACTION(ShouldDisableOwnershipVerification)
2727
BLOCKLIST_ACTION(SkipEmittingFineModuleTrace)
2828
BLOCKLIST_ACTION(SkipIndexingModule)
29+
BLOCKLIST_ACTION(ShouldUseTypeCheckerPerfHacks)
2930

3031
#undef BLOCKLIST_ACTION

include/swift/Basic/LangOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,8 @@ namespace swift {
986986
/// Enable experimental operator designated types feature.
987987
bool EnableOperatorDesignatedTypes = false;
988988

989-
/// Disable constraint system performance hacks.
990-
bool DisableConstraintSolverPerformanceHacks = false;
989+
/// Enable old constraint system performance hacks.
990+
bool EnableConstraintSolverPerformanceHacks = false;
991991

992992
/// See \ref FrontendOptions.PrintFullConvention
993993
bool PrintFullConvention = false;

include/swift/Option/FrontendOptions.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,8 @@ def solver_trail_threshold_EQ : Joined<["-"], "solver-trail-threshold=">,
862862
def solver_disable_splitter : Flag<["-"], "solver-disable-splitter">,
863863
HelpText<"Disable the component splitter phase of expression type checking">;
864864

865-
def disable_constraint_solver_performance_hacks : Flag<["-"], "disable-constraint-solver-performance-hacks">,
866-
HelpText<"Disable all the hacks in the constraint solver">;
865+
def enable_constraint_solver_performance_hacks : Flag<["-"], "enable-constraint-solver-performance-hacks">,
866+
HelpText<"Enable all the old hacks in the constraint solver">;
867867

868868
def enable_operator_designated_types :
869869
Flag<["-"], "enable-operator-designated-types">,

include/swift/Sema/CSBindings.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,12 @@ class BindingSet {
527527
void forEachLiteralRequirement(
528528
llvm::function_ref<void(KnownProtocolKind)> callback) const;
529529

530+
void forEachAdjacentVariable(
531+
llvm::function_ref<void(TypeVariableType *)> callback) const {
532+
for (auto *typeVar : AdjacentVars)
533+
callback(typeVar);
534+
}
535+
530536
/// Return a literal requirement that has the most impact on the binding
531537
/// score.
532538
LiteralBindingKind getLiteralForScore() const;

include/swift/Sema/ConstraintSystem.h

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,18 @@ class TypeVariableType::Implementation {
491491
/// literal (represented by `ArrayExpr` and `DictionaryExpr` in AST).
492492
bool isCollectionLiteralType() const;
493493

494+
/// Determine whether this type variable represents a literal such
495+
/// as an integer value, a floating-point value with and without a sign.
496+
bool isNumberLiteralType() const;
497+
498+
/// Determine whether this type variable represents a result type of a
499+
/// function call.
500+
bool isFunctionResult() const;
501+
502+
/// Determine whether this type variable represents a type of the ternary
503+
/// operator.
504+
bool isTernary() const;
505+
494506
/// Retrieve the representative of the equivalence class to which this
495507
/// type variable belongs.
496508
///
@@ -1952,6 +1964,9 @@ enum class ConstraintSystemFlags {
19521964

19531965
/// Disable macro expansions.
19541966
DisableMacroExpansions = 0x80,
1967+
1968+
/// Enable old type-checker performance hacks.
1969+
EnablePerformanceHacks = 0x100,
19551970
};
19561971

19571972
/// Options that affect the constraint system as a whole.
@@ -3591,11 +3606,10 @@ class ConstraintSystem {
35913606
return Options.contains(ConstraintSystemFlags::ForCodeCompletion);
35923607
}
35933608

3594-
/// Check whether type-checker performance hacks has been explicitly
3595-
/// disabled by a flag.
3609+
/// Check whether old type-checker performance hacks has been explicitly
3610+
/// enabled.
35963611
bool performanceHacksEnabled() const {
3597-
return !getASTContext()
3598-
.TypeCheckerOpts.DisableConstraintSolverPerformanceHacks;
3612+
return Options.contains(ConstraintSystemFlags::EnablePerformanceHacks);
35993613
}
36003614

36013615
/// Log and record the application of the fix. Return true iff any
@@ -5397,8 +5411,12 @@ class ConstraintSystem {
53975411

53985412
/// Pick a disjunction from the InactiveConstraints list.
53995413
///
5400-
/// \returns The selected disjunction.
5401-
Constraint *selectDisjunction();
5414+
/// \returns The selected disjunction and a set of it's favored choices.
5415+
std::optional<std::pair<Constraint *, llvm::TinyPtrVector<Constraint *>>>
5416+
selectDisjunction();
5417+
5418+
/// The old method that is only used when performance hacks are enabled.
5419+
Constraint *selectDisjunctionWithHacks();
54025420

54035421
/// Pick a conjunction from the InactiveConstraints list.
54045422
///
@@ -6098,6 +6116,12 @@ class DisjunctionChoice {
60986116
return false;
60996117
}
61006118

6119+
bool isDisfavored() const {
6120+
if (auto *decl = getOverloadChoiceDecl(Choice))
6121+
return decl->getAttrs().hasAttribute<DisfavoredOverloadAttr>();
6122+
return false;
6123+
}
6124+
61016125
bool isBeginningOfPartition() const { return IsBeginningOfPartition; }
61026126

61036127
// FIXME: All three of the accessors below are required to support
@@ -6332,7 +6356,8 @@ class DisjunctionChoiceProducer : public BindingProducer<DisjunctionChoice> {
63326356
public:
63336357
using Element = DisjunctionChoice;
63346358

6335-
DisjunctionChoiceProducer(ConstraintSystem &cs, Constraint *disjunction)
6359+
DisjunctionChoiceProducer(ConstraintSystem &cs, Constraint *disjunction,
6360+
llvm::TinyPtrVector<Constraint *> &favorites)
63366361
: BindingProducer(cs, disjunction->shouldRememberChoice()
63376362
? disjunction->getLocator()
63386363
: nullptr),
@@ -6342,6 +6367,11 @@ class DisjunctionChoiceProducer : public BindingProducer<DisjunctionChoice> {
63426367
assert(disjunction->getKind() == ConstraintKind::Disjunction);
63436368
assert(!disjunction->shouldRememberChoice() || disjunction->getLocator());
63446369

6370+
// Mark constraints as favored. This information
6371+
// is going to be used by partitioner.
6372+
for (auto *choice : favorites)
6373+
cs.favorConstraint(choice);
6374+
63456375
// Order and partition the disjunction choices.
63466376
partitionDisjunction(Ordering, PartitionBeginning);
63476377
}
@@ -6386,8 +6416,9 @@ class DisjunctionChoiceProducer : public BindingProducer<DisjunctionChoice> {
63866416
// Partition the choices in the disjunction into groups that we will
63876417
// iterate over in an order appropriate to attempt to stop before we
63886418
// have to visit all of the options.
6389-
void partitionDisjunction(SmallVectorImpl<unsigned> &Ordering,
6390-
SmallVectorImpl<unsigned> &PartitionBeginning);
6419+
void
6420+
partitionDisjunction(SmallVectorImpl<unsigned> &Ordering,
6421+
SmallVectorImpl<unsigned> &PartitionBeginning);
63916422

63926423
/// Partition the choices in the range \c first to \c last into groups and
63936424
/// order the groups in the best order to attempt based on the argument

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,8 +2009,8 @@ static bool ParseTypeCheckerArgs(TypeCheckerOptions &Opts, ArgList &Args,
20092009
SWIFT_ONONE_SUPPORT);
20102010
}
20112011

2012-
Opts.DisableConstraintSolverPerformanceHacks |=
2013-
Args.hasArg(OPT_disable_constraint_solver_performance_hacks);
2012+
Opts.EnableConstraintSolverPerformanceHacks |=
2013+
Args.hasArg(OPT_enable_constraint_solver_performance_hacks);
20142014

20152015
Opts.EnableOperatorDesignatedTypes |=
20162016
Args.hasArg(OPT_enable_operator_designated_types);

lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_swift_host_library(swiftSema STATIC
1414
CSStep.cpp
1515
CSTrail.cpp
1616
CSFix.cpp
17+
CSOptimizer.cpp
1718
CSDiagnostics.cpp
1819
CodeSynthesis.cpp
1920
CodeSynthesisDistributedActor.cpp

0 commit comments

Comments
 (0)