Commit Graph

827 Commits

Author SHA1 Message Date
Pavel Yaskevich
8d5cb112ef [ConstraintSystem] Narrowly disable tryOptimizeGenericDisjunction when some of the arguments are number literals
Don't attempt this optimization if call has number literals.
This is intended to narrowly fix situations like:

```swift
func test<T: FloatingPoint>(_: T) { ... }
func test<T: Numeric>(_: T) { ... }

test(42)
```

The call should use `<T: Numeric>` overload even though the
`<T: FloatingPoint>` is a more specialized version because
selecting `<T: Numeric>` doesn't introduce non-default literal
types.
2024-12-17 11:36:40 -08:00
Pavel Yaskevich
672ae3d252 [CSOptimizer] Initial implementation of disjunction choice favoring algorithm
This algorithm attempts to ensure that the solver always picks a disjunction
it knows the most about given the previously deduced type information.

For example in chains of operators like: `let _: (Double) -> Void = { 1 * 2 + $0 - 5 }`

The solver is going to start from `2 + $0` because `$0` is known to be `Double` and
then proceed to `1 * ...` and only after that to `... - 5`.

The algorithm is pretty simple:

- Collect "candidate" types for each argument
  - If argument is bound then the set going to be represented by just one type
  - Otherwise:
    - Collect all the possible bindings
    - Add default literal type (if any)

- Collect "candidate" types for result

- For each disjunction in the current scope:
  - Compute a favoring score for each viable* overload choice:
    - Compute score for each parameter:
      - Match parameter flags to argument flags
      - Match parameter types to a set of candidate argument types
        - If it's an exact match
          - Concrete type: score = 1.0
          - Literal default: score = 0.3
        - Highest scored candidate type wins.
      - If none of the candidates match and they are all non-literal
        remove overload choice from consideration.

    - Average the score by dividing it by the number of parameters
      to avoid disfavoring disjunctions with fewer arguments.

    - Match result type to a set of candidates; add 1 to the score
      if one of the candidate types matches exactly.

  - The best choice score becomes a disjunction score

- Compute disjunction scores for all of the disjunctions in scope.

- Pick disjunction with the best overall score and favor choices with
  the best local candidate scores (if some candidates have equal scores).

- Viable overloads include:
  - non-disfavored
  - non-disabled
  - available
  - non-generic (with current exception to SIMD)
2024-12-17 11:36:38 -08:00
Pavel Yaskevich
b5f08a4009 [ConstraintSystem] Add skeleton of constraint optimizer 2024-12-17 11:36:38 -08:00
Pavel Yaskevich
757ca24e8a [ConstraintSystem] Remove shrink 2024-12-17 11:36:37 -08:00
Slava Pestov
25252cd683 Merge pull request #77728 from slavapestov/cg-cleanup
Sema: Small ConstraintGraph cleanup
2024-11-20 19:43:42 -05:00
Slava Pestov
7fef225c9f Sema: Allow turning off expression timer with -solver-expression-time-threshold=0 2024-11-20 13:46:19 -05:00
Slava Pestov
c8415aeac4 Sema: Record the trail step count in solver statistics
Also introduce two new frontend flags:

The -solver-scope-threshold flag sets the maximum number of scopes, which was
previously hardcoded to 1 million.

The -solver-trail-threshold flag sets the maximum number of trail steps,
which defaults to 64 million.
2024-11-20 13:46:19 -05:00
Slava Pestov
6ff8f55634 Sema: Fix handling of Solution::typeBindings in replaySolution() 2024-11-20 13:35:21 -05:00
Slava Pestov
f702e46751 Merge pull request #77475 from slavapestov/fix-rdar139237781
Sema: Fix handling of appliedPropertyWrappers in ConstraintSystem::replaySolution()
2024-11-08 15:15:27 -05:00
Slava Pestov
d67e89d89c Sema: Fix handling of appliedPropertyWrappers in ConstraintSystem::replaySolution()
When we replay a solution, we must record changes in the trail, so fix the
logic to do that. This fixes the first assertion failure with this test case.

The test case also exposed a second issue. We synthesize a CustomAttr in
applySolutionToClosurePropertyWrappers() with a type returned by simplifyType().
Eventually, CustomAttrNominalRequest::evaluate() looks at this type, and passes
it to directReferencesForType(). Unfortunately, this entry point does not
understand type aliases whose underlying type is a type parameter.
However, directReferencesForType() is the wrong thing to use here, and we
can just call getAnyNominal() instead.

Fixes rdar://139237781.
2024-11-08 10:46:07 -05:00
Pavel Yaskevich
392dac41f1 [CSSolver] DynamicMemberLookup: Ignore disabled choices while filtering disjunctions
`filterDisjunction` should ignore the choices that are already
disabled while attempting to optimize disjunctions related to
dynamic member lookup.

Resolves: rdar://139314763
2024-11-07 18:31:52 -08:00
Doug Gregor
720429daf4 [Constraint solver] Disable fallback diagnostic when error emitted into a transaction
When we are using diagnostic transactions to disable immediate emission
of diagnostics, `DiagnosticEngine::hadAnyError()` no longer accurately
reports whether an error occurred. Thread the DiagnosticTransaction
into the ConstraintSystem so we can also check whether it contains an
error before emitting the fallback diagnostic.

Fixes rdar://128272346.
2024-11-06 17:07:13 -08:00
Slava Pestov
38fe0696a4 Sema: Factor out clearScore() and replayScore() 2024-10-09 11:48:07 -04:00
Slava Pestov
c8db9f55e6 Sema: Remove last heap allocation of Scope 2024-10-09 11:48:07 -04:00
Slava Pestov
0b85ce6f00 Sema: Remove SolverScope::numFixes 2024-10-08 16:57:49 -04:00
Slava Pestov
5fdc1a81b8 Sema: Record retired constraints in the trail 2024-10-08 16:39:34 -04:00
Slava Pestov
90175400d2 Sema: Record generated constraints in the trail 2024-10-08 16:17:28 -04:00
Slava Pestov
1e2d4fbc59 Sema: Record score increases in the trail 2024-10-08 16:16:31 -04:00
Slava Pestov
877c60e3d0 Sema: Rename applySolution() to replaySolution() 2024-10-08 16:16:01 -04:00
Slava Pestov
4a82d384b7 Sema: Record synthesized conformances in the trail 2024-10-07 16:50:49 -04:00
Slava Pestov
12eb7cec26 Sema: Record implicit callAsFunction() roots in the trail 2024-10-07 16:50:49 -04:00
Slava Pestov
72a60728fc Sema: Record argument lists in the trail 2024-10-07 16:50:49 -04:00
Slava Pestov
f2412f318e Sema: Record key path expressions in the trail 2024-10-07 16:50:48 -04:00
Slava Pestov
881a0100b2 Sema: Record implicit value conversions in the trail 2024-10-07 16:50:48 -04:00
Slava Pestov
ac17292dfe Sema: Record preconcurrency closures in the trail 2024-10-07 16:50:48 -04:00
Slava Pestov
b961a7ec51 Sema: Record isolated parameters in the trail 2024-10-07 16:50:48 -04:00
Slava Pestov
1d177d0187 Sema: Record expression patterns in the trail 2024-10-07 16:50:48 -04:00
Slava Pestov
500acd122a Sema: Record potential throw sites in the trail 2024-10-07 16:50:47 -04:00
Slava Pestov
39d0eab275 Sema: Record case label items in the trail 2024-10-07 16:50:47 -04:00
Slava Pestov
8c8a385a4a Sema: Record SyntacticElementTargetKeys in the trail 2024-10-07 16:50:47 -04:00
Slava Pestov
516277f0fd Sema: Record contextual types in the trail 2024-10-07 16:50:47 -04:00
Slava Pestov
666361adf2 Sema: Record implied results in the trail 2024-10-07 16:50:47 -04:00
Slava Pestov
1d18cd07cf Sema: Record closure types in the trail 2024-10-07 16:50:47 -04:00
Slava Pestov
43a4ac9216 Sema: Record resolved overloads in the trail 2024-10-07 16:50:46 -04:00
Slava Pestov
9201a37519 Sema: Strenghten invariants in SolverTrail::Change::undo() 2024-10-07 16:50:46 -04:00
Slava Pestov
6862955244 Sema: Record applied property wrappers in the trail 2024-10-07 16:50:46 -04:00
Slava Pestov
c7edc3494e Sema: Record result builder transforms in the trail 2024-10-07 16:50:45 -04:00
Slava Pestov
64293ece51 Sema: Push reconciliation down into applySolution() to strengthen invariants
Now, we assert if you try to record the same change twice in any
other code path.
2024-10-07 16:50:45 -04:00
Slava Pestov
9115a46736 Sema: Record favored constraints in the trail 2024-10-07 16:50:45 -04:00
Slava Pestov
b2adf51d78 Sema: Record disabled constraints in the trail 2024-10-07 16:50:45 -04:00
Slava Pestov
411c590bc8 Sema: Record key path component types in the trail 2024-10-07 16:50:44 -04:00
Slava Pestov
7ea975015f Sema: Record ASTNode types in the trail 2024-10-07 16:50:44 -04:00
Slava Pestov
3dc16a9f5b Sema: Record defaulted constraints in the trail 2024-10-01 18:10:20 -04:00
Slava Pestov
fcd6bc04fd Sema: Remove Solution::PackElementGenericEnvironments 2024-10-01 18:10:20 -04:00
Slava Pestov
0518b762d1 Sema: Record pack environments in the trail 2024-10-01 18:10:20 -04:00
Slava Pestov
f51bf3cf9d Sema: Record pack expansion environments in the trail 2024-10-01 18:10:20 -04:00
Slava Pestov
9afb1a960b Sema: Record opened pack expansion types in the trail 2024-10-01 18:10:20 -04:00
Slava Pestov
7d3350d0f2 Sema: Record opened existential types in the trail 2024-10-01 18:10:19 -04:00
Slava Pestov
f963f36a16 Sema: Record opened types in the trail 2024-10-01 18:10:19 -04:00
Slava Pestov
a9f392278f Sema: Record argument matching choices in the trail 2024-10-01 18:10:18 -04:00