Commit Graph

882 Commits

Author SHA1 Message Date
Ben Cohen 502cd9a50b Sema: pre-reserve DenseMap capacity in ConstraintSystem::finalize (#88857)
`ConstraintSystem::finalize` copies several maps from the constraint
system into the fresh `Solution`: `overloadChoices`,
`argumentMatchingChoices`, `nodeTypes`, `keyPathComponentTypes`, etc.
All are populated by single-element inserts that grow the destination
DenseMap incrementally, producing a cascade of rehashes. Pre-size the
destination maps to their source's size before inserting. The loops are
otherwise unchanged.

Measured on a synthetic stress test (N-element `[UV(u: Float(bitPattern:
0xX), v: Float(bitPattern: 0xY)), ...]`, 2-run averages, baseline vs
patched):
                                                            
| N | baseline | patched | speedup |
  |-----:|---------:|--------:|--------:|                   
| 1000 | 1.06s | 0.72s | 1.47× |
| 2000 | 5.82s | 3.06s | 1.90× |
| 4000 | 22.89s | 15.41s | 1.49× |
Memory impact is negligible (same final map sizes; we just avoid
intermediate doublings). This is a pure CPU win from a
behavior-preserving change. This doesn't solve the O(N²) memory problem,
just makes smaller cases faster.
2026-05-06 15:57:04 -07:00
Pavel Yaskevich 4201e579be Merge pull request #88162 from CognitiveDisson/feat/scope-based-expression-type-checking-warnings
[Sema] Add scope-based analogues of -warn-long-expression-type-checking
2026-05-04 06:35:35 -07:00
CognitiveDisson 1b88e68605 Move CS.NumTrailSteps update into endScope to mirror scope counter 2026-04-30 13:43:45 +01:00
CognitiveDisson 575e69f90e [Sema] Add scope-based analogues of -warn-long-expression-type-checking 2026-04-23 10:46:03 +01:00
Pavel Yaskevich 974346026f [ConstraintSystem] NFC: Extract logic to detect whether function is generic only over thrown type 2026-03-31 13:34:54 -07:00
Hamish Knight 54c0870a25 [CS] Better handle rewriting implicit callAsFunction call splitting
The previous logic was relying on doing `coerceCallArguments` with the
full argument list instead of only the non-trailing args, and wasn't
handling the non-shorthand-init case. Update the logic to fix-up the
apply during the pre-walk, ensuring it gets applied consistently.

rdar://170076966
2026-03-21 13:45:27 +00:00
Nacho Soto 9c77f54e1e Trail replayed potential throw sites
Use the trailed recordPotentialThrowSite helper when replaying solution state so replayed potential throw sites are rolled back with solver scopes.

Co-authored-by: Codex <noreply@openai.com>
2026-03-02 19:47:19 -08:00
Slava Pestov 4e9b5da77a Sema: Move more code to CSDisjunction.cpp 2026-02-28 14:54:07 -05:00
Slava Pestov 7a717654c7 Sema: Inline collectDisjunctions() into its only caller 2026-02-28 14:54:07 -05:00
Slava Pestov a4e47070c3 Sema: Move some code to CSDisjunction.cpp
The argument label-based pruning is similar to the type-based pruning
that I added, and I'm going to try to consolidate the duplication
a bit.
2026-02-28 14:54:07 -05:00
Pavel Yaskevich 23fcfa0da7 [CSSolver] Exclude overloads that are generic only over an error type from generic overload optimization
Typed throws adoption could cause source compatibility regressions
because it makes some overloads generic when they used to be concrete
and didn't participate in this generic-only optimization that covers
cases where there are only two overloads.

Resolves: rdar://171016238
2026-02-26 12:00:50 -08:00
Slava Pestov 7cd06a5088 Sema: Split off TypeVariableType.h/.cpp 2026-02-05 09:19:01 -05:00
Slava Pestov 2e60d29973 Sema: Add -solver-{disable,enable}-performance-hacks flags 2026-02-03 16:34:10 -05:00
Pavel Yaskevich a33ec97216 [ConstraintSystem] Remove old performance hacks from the solver
The hacks have been disabled for a while now, so it's time to
completely remove them in favor of CSOptimizer.
2026-01-21 11:53:19 -08:00
Slava Pestov 9157d984e9 Sema: Convert solver arena usage into an always-on statistic 2025-12-19 17:26:19 -05:00
Slava Pestov 74fa1e7f5e Sema: BindingSet::Literals can just be a vector and not a map 2025-12-13 21:16:43 -05:00
Slava Pestov d8cf185a13 Sema: Fix source location bookkeeping for 'reasonable time' diagnostic
We already had bookkeeping to track which statement in a multi-statement
closure we were looking at, but this was only used for the 'reasonable time'
diagnostic in the case that we hit the expression timer, which was almost
never hit, and is now off by default. The scope, memory, and trial limits
couldn't use this information, so they would always diagnose the entire
target being type checked.

Move it up from ExpressionTimer to ConstraintSystem, so that we get the
right source location there too. Also, factor out some code duplication
in BuilderTransform to ensure we get the same benefit for result builders
applied to function bodies too.
2025-12-06 20:31:08 -05:00
Anthony Latsis bda6edb85c AST: Rename GenericContext::isGeneric to hasGenericParamList
`isGeneric` is a misleading name because this method checks for the
existence of a `GenericParamList`, which is not implied by genericity.
2025-11-11 15:55:16 +00:00
Hamish Knight bb877ce3c0 [CS] Remove ConstraintSystemPhase 2025-10-19 13:14:00 +01:00
Hamish Knight f1823786b6 [CS] Re-order subscript constraint generation
Add the application constraint before the member constraint, which
allows us to get rid of the special-cased delaying logic for dynamic
member subscripts. The diagnostic change here is due to the fact that
we no longer have a simplified type for the result in CSGen, which
would also be the case if we had a disjunction for the member.
2025-10-19 13:14:00 +01:00
Slava Pestov 8600cdcc2a Sema: Remove ConstraintSystemPhase::{Diagnostics,Finalization} 2025-10-10 16:06:42 -04:00
Hamish Knight 954b08cae5 [AST] Remove UnresolvedType
We have now removed all uses of this type.
2025-10-03 09:50:42 +01:00
Hamish Knight f9a4386922 [CS] Remove some uses of hasUnresolvedType in the solver
These should be unreachable now.
2025-10-03 09:50:42 +01:00
Hamish Knight 23560df5f4 [CS] Remove AllowUnresolvedTypeVariables
This has been unused for a while now.
2025-09-07 12:17:18 +01:00
Hamish Knight 5b8dfc70ab [CS] Emit fallback diagnostic if needed for IgnoreInvalidASTNode
If no other error has been emitted, make sure we emit a fallback
diagnostic rather than crashing in the ASTVerifier or SILGen.
2025-08-28 15:31:48 +01:00
Slava Pestov f7a552385c Sema: PreparedOverload => PreparedOverloadBuilder 2025-07-10 19:53:18 -04:00
Slava Pestov 751edee921 Sema: Rework PreparedOverload to store a list of changes 2025-07-09 15:00:10 -04:00
Slava Pestov 426c7a3fdf Sema: Collect constraints in PreparedOverload 2025-07-09 15:00:02 -04:00
Slava Pestov 543793564e Sema: Record opened type variables in PreparedOverload 2025-07-09 15:00:02 -04:00
Pavel Yaskevich 0e72686a2c [CSSolver] Attempt choices marked as disfavored at the end of partition
Since such choices are all but guaranteed to be worst than any
other non-disfavored choice, let's attempt them last to avoid
having to form a complete solution just to filter it out during
ranking.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich 0a5bd787d0 [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.

(cherry picked from commit 8d5cb112ef)
2025-06-27 23:43:09 -07:00
Pavel Yaskevich c04068db1c [ConstraintSystem] Initial implementation of disjunction optimizer
This brings back CSOptimizer from https://github.com/swiftlang/swift/pull/63585
2025-06-27 23:43:09 -07:00
Pavel Yaskevich 1b2ba7e059 [CSStep] Modify selectDisjunction to return favored choices directly
This is currently unused because current mechanism set favored choices
directly but it would be utilized by the disjunction optimizer.

(cherry picked from commit e404ed722a)
2025-06-27 23:43:09 -07:00
Allan Shortlidge 6b05efd087 Sema: Remove unused variable in CSSolver.cpp. 2025-06-25 22:23:47 -07:00
Pavel Yaskevich 286f975c29 Merge pull request #82320 from xedin/hide-solver-hacks-behind-a-flag
[ConstraintSystem] Guard all the performance hacks with a flag
2025-06-20 00:12:16 -07:00
Pavel Yaskevich fc573b6612 [ConstraintSystem] Guard all the performance hacks with a flag
Package the flag into `performanceHacksEnabled()` method on
`ConstraintSystem` and start using it to wrap all of the hacks
in constraint generator and the solver.
2025-06-17 13:53:05 -07:00
Pavel Yaskevich 833b6b19e9 [Frontend] Remove -disable-solver-shrink option
It's folded into a more general "disable performance hacks" one.
2025-06-17 13:52:28 -07:00
Hamish Knight 71d20b7635 [CS] NFC: Remove FreeTypeVariableBinding::UnresolvedType
This is now unused.
2025-06-12 12:11:19 +01:00
Hamish Knight f8ab391737 Introduce type sugar for InlineArray (#80087)
* [CS] Decline to handle InlineArray in shrink

Previously we would try the contextual type `(<int>, <element>)`,
which is wrong. Given we want to eliminate shrink, let's just bail.

* [Sema] Sink `ValueMatchVisitor` into `applyUnboundGenericArguments`

Make sure it's called for sugar code paths too. Also let's just always
run it since it should be a pretty cheap check.

* [Sema] Diagnose passing integer to non-integer type parameter

This was previously missed, though would have been diagnosed later
as a requirement failure.

* [Parse] Split up `canParseType` 

While here, address the FIXME in `canParseTypeSimpleOrComposition`
and only check to see if we can parse a type-simple, including
`each`, `some`, and `any` for better recovery.

* Introduce type sugar for InlineArray

Parse e.g `[3 x Int]` as type sugar for InlineArray. Gated behind
an experimental feature flag for now.
2025-03-23 15:31:37 -07:00
Slava Pestov 34e7e824e9 Sema: Fix non-determinism with ConstraintSystem::ConstraintRestrictions
We iterate over the DenseMap and simplify both types appearing
in the key. If multiple keys simplify to the same type, later
keys overwrite earlier keys, so the outcome depends on hash
table order.

Fix this by introducing an arbitrary tie break: we prefer the
highest-numbered restriction.

Fixes rdar://146780049.
2025-03-17 14:35:10 -04:00
Pavel Yaskevich 1bd0cd7b94 Merge pull request #79928 from xedin/support-debug-constraints-on-line-for-result-builders
[ResultBuilders] NFC: Support `-debug-constraints{-on-line=}` for dec…
2025-03-12 00:29:41 -07:00
eeckstein 47b449372e Merge pull request #79917 from eeckstein/rename-opened-archetype-type
AST: rename OpenArchetypeType -> ExistentialArchetypeType
2025-03-12 06:01:10 +01:00
Slava Pestov 9e721a35f2 Merge pull request #79914 from slavapestov/retry-removing-one-way-equal
Sema: Mostly remove one-way constraints
2025-03-11 21:24:57 -04:00
Pavel Yaskevich 6197aab467 [ResultBuilders] NFC: Support -debug-constraints{-on-line=} for declaration transforms 2025-03-11 15:13:58 -07:00
Erik Eckstein d225c47d25 AST: rename OpenArchetypeType -> ExistentialArchetypeType
NFC
2025-03-11 20:21:46 +01:00
Slava Pestov 6d8b55a9f6 Sema: Split up gatherConstraints() into gatherAllConstraints() and gatherNearbyConstraints()
The two GatherKinds no longer share any implementation, so there's
no point keeping the logic together. Doing this also allows removing
the acceptConstraintFn from gatherAllConstraints(), which further
simplifies depthFirstSearch().
2025-03-11 13:55:40 -04:00
Slava Pestov ada6f77655 Sema: Track solver arena size in statistics 2025-03-10 14:03:05 -04:00
Doug Gregor 6a4d1a41c4 Prohibit isolated conformances with Sendable(Metatype) constraints
Within the constraint system, introduce a new kind of conformance constraint,
a "nonisolated conforms-to" constraint, which can only be satisfied by
nonisolated conformances. Introduce this constraint instead of the normal
conforms-to constraint whenever the subject type is a type parameter that
has either a `Sendable` or `SendableMetatype` constraint, i.e., when the type
or its values can escape the current isolation domain.
2025-03-01 08:19:42 -08:00
Hamish Knight 29e9f429e3 [CS] NFC: Rename PackEnvironments -> PackElementExpansions
IMO "environment" is confusable for GenericEnvironment,
make it clearer it's referring to the parent
PackExpansionExpr.
2025-02-20 21:31:46 +00:00
Slava Pestov 521ea46d15 Sema: Don't record constraints containing UnboundGenericType from shrink()
Something changed here between the removal of shrink() and it's
re-introduction, and we now record constraints that contain
UnboundGenericType, which crashes in matchTypes().

As a narrow workaround, let's just ignore the contextual type if
contains an UnboundGenericType.

Fixes rdar://145092838.
2025-02-19 13:27:33 -05:00