Skip to content

Commit c77d086

Browse files
committed
[ConstraintSystem] Record all Double <-> CGFloat conversions performed along a current solver path
This information makes it easier to compute a score for the next implicit value conversion solver would have to perform.
1 parent e6bdb60 commit c77d086

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,10 @@ class Solution {
11771177
/// The locators of \c Defaultable constraints whose defaults were used.
11781178
llvm::SmallPtrSet<ConstraintLocator *, 2> DefaultedConstraints;
11791179

1180+
/// Implicit value conversions applied for a given locator.
1181+
SmallVector<std::pair<ConstraintLocator *, ConversionRestrictionKind>, 2>
1182+
ImplicitValueConversions;
1183+
11801184
/// The node -> type mappings introduced by this solution.
11811185
llvm::DenseMap<ASTNode, Type> nodeTypes;
11821186

@@ -2210,6 +2214,11 @@ class ConstraintSystem {
22102214
std::vector<std::pair<ConstraintLocator*, TrailingClosureMatching>>
22112215
trailingClosureMatchingChoices;
22122216

2217+
/// The set of implicit value conversions performed by the solver on
2218+
/// a current path to reach a solution.
2219+
SmallVector<std::pair<ConstraintLocator *, ConversionRestrictionKind>, 2>
2220+
ImplicitValueConversions;
2221+
22132222
/// The worklist of "active" constraints that should be revisited
22142223
/// due to a change.
22152224
ConstraintList ActiveConstraints;
@@ -2738,6 +2747,9 @@ class ConstraintSystem {
27382747
/// The length of \c caseLabelItems.
27392748
unsigned numCaseLabelItems;
27402749

2750+
/// The length of \c ImplicitValueConversions.
2751+
unsigned numImplicitValueConversions;
2752+
27412753
/// The previous score.
27422754
Score PreviousScore;
27432755

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5083,14 +5083,10 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
50835083
}
50845084
}
50855085

5086-
auto isDoubleType = [&](NominalType *NTD) {
5087-
return NTD->getDecl() == getASTContext().getDoubleDecl();
5088-
};
5089-
50905086
if (kind >= ConstraintKind::Subtype &&
50915087
nominal1->getDecl() != nominal2->getDecl() &&
50925088
((nominal1->isCGFloatType() || nominal2->isCGFloatType()) &&
5093-
(isDoubleType(nominal1) || isDoubleType(nominal2)))) {
5089+
(nominal1->isDoubleType() || nominal2->isDoubleType()))) {
50945090
SmallVector<LocatorPathElt, 4> path;
50955091
auto anchor = locator.getLocatorParts(path);
50965092

@@ -10531,7 +10527,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
1053110527
auto defaultImpact =
1053210528
restriction == ConversionRestrictionKind::CGFloatToDouble ? 1 : 2;
1053310529

10534-
auto numConversions = CurrentScore.Data[SK_ImplicitValueConversion];
10530+
auto numConversions = ImplicitValueConversions.size();
1053510531
increaseScore(SK_ImplicitValueConversion,
1053610532
defaultImpact * (numConversions + 1));
1053710533

@@ -10572,6 +10568,9 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
1057210568
addConstraint(ConstraintKind::ApplicableFunction,
1057310569
FunctionType::get({FunctionType::Param(type1)}, type2),
1057410570
memberTy, applicationLoc);
10571+
10572+
ImplicitValueConversions.push_back(
10573+
{getConstraintLocator(locator), restriction});
1057510574
return SolutionKind::Solved;
1057610575
}
1057710576
}

lib/Sema/CSSolver.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ Solution ConstraintSystem::finalize() {
193193
solution.appliedPropertyWrappers.insert(appliedWrapper);
194194
}
195195

196+
// Remember implicit value conversions.
197+
for (const auto &valueConversion : ImplicitValueConversions) {
198+
solution.ImplicitValueConversions.push_back(valueConversion);
199+
}
200+
196201
return solution;
197202
}
198203

@@ -282,6 +287,10 @@ void ConstraintSystem::applySolution(const Solution &solution) {
282287
appliedPropertyWrappers.insert(appliedWrapper);
283288
}
284289

290+
for (auto &valueConversion : solution.ImplicitValueConversions) {
291+
ImplicitValueConversions.push_back(valueConversion);
292+
}
293+
285294
// Register any fixes produced along this path.
286295
Fixes.append(solution.Fixes.begin(), solution.Fixes.end());
287296
}
@@ -488,6 +497,7 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
488497
numContextualTypes = cs.contextualTypes.size();
489498
numSolutionApplicationTargets = cs.solutionApplicationTargets.size();
490499
numCaseLabelItems = cs.caseLabelItems.size();
500+
numImplicitValueConversions = cs.ImplicitValueConversions.size();
491501

492502
PreviousScore = cs.CurrentScore;
493503

@@ -578,6 +588,9 @@ ConstraintSystem::SolverScope::~SolverScope() {
578588
// Remove any case label item infos.
579589
truncate(cs.caseLabelItems, numCaseLabelItems);
580590

591+
// Remove any implicit value conversions.
592+
truncate(cs.ImplicitValueConversions, numImplicitValueConversions);
593+
581594
// Reset the previous score.
582595
cs.CurrentScore = PreviousScore;
583596

0 commit comments

Comments
 (0)