Commit Graph

7628 Commits

Author SHA1 Message Date
Pavel Yaskevich
40a41c82d9 [CSBindings] Don't attempt to strip optional for closure result types
Let's not perform $T? -> $T for closure result types to avoid having
to re-discover solutions that differ only in location of optional
injection.

The pattern with such type variables is:

```
$T_body <conv/subtype> $T_result <conv/subtype> $T_contextual_result
```

When `$T_contextual_result` is `Optional<$U>`, the optional injection
can either happen from `$T_body` or from `$T_result` (if `return`
expression is non-optional), if we allow  both the solver would
find two solutions that differ only in location of optional
injection.
2024-12-17 11:36:42 -08:00
Pavel Yaskevich
56d6635e46 [CSOptimizer] Implement special prioritization rules for result builder contexts
Prioritize `build{Block, Expression, ...}` and any chained
members that are connected to individual builder elements
i.e. `ForEach(...) { ... }.padding(...)`, once `ForEach`
is resolved, `padding` should be prioritized because its
requirements can help prune the solution space before the
body is checked.
2024-12-17 11:36:42 -08:00
Pavel Yaskevich
bf8ae3bc1b [CSOptimizer] Allow only widening CGFloat->Double conversions while matching candidate arguments
Allow CGFloat -> Double widening conversions between
candidate argument types and parameter types. This would
make sure that Double is always preferred over CGFloat
when using literals and ranking supported disjunction
choices. Narrowing conversion (Double -> CGFloat) should
be delayed as much as possible.
2024-12-17 11:36:42 -08:00
Pavel Yaskevich
87cd5f8733 [CSOptimizer] Add support for chained members without arguments
If disjunction represents a member reference that has no arguments
applied, let's score that as `1` to indicate that it should be priorized.

This helps in situations like `a.b + 1` where resolving `a.b` member
chain helps to establish context for `+`.
2024-12-17 11:36:41 -08:00
Pavel Yaskevich
15c773b9d7 [CSOptimizer] Make a light-weight generic overload check if some requirements are unsatisfiable
If some of the requirements of a generic overload reference other
generic parameters, the optimizer won't be able to satisfy them
because it only has candidates for one (current) parameter. In
cases like that, let's fallback to a light-weight protocol conformance
check instead of skipping an overload choice altogether.
2024-12-17 11:36:41 -08:00
Pavel Yaskevich
c2a55886f0 [CSOptimizer] Fix selectDisjunction to use favored choices even if disjunction was not optimized
Some disjunctions e.g. explicit coercions, compositions of restrictions,
and implicit CGFloat initializers have favored choices set independently
from optimization algorithm, `selectDisjunction` should account for
such situations.
2024-12-17 11:36:41 -08:00
Pavel Yaskevich
ff8663ff16 [Tests] NFC: Update a couple of type-checker tests 2024-12-17 11:36:41 -08:00
Pavel Yaskevich
28396a6dce [Tests] NFC: Move simd related test-case from slow to fast 2024-12-17 11:36:41 -08:00
Pavel Yaskevich
3996b25fbd [CSOptimizer] Rank results of operators regardless of whether anything is known about parameters
When operators are chained it's possible that we don't know anything
about parameter(s) but result is known from the context, we should
use that information.
2024-12-17 11:36:40 -08:00
Pavel Yaskevich
66981364fe [Tests] NFC: Adjust a couple of improved tests 2024-12-17 11:36:40 -08:00
Pavel Yaskevich
670127abd6 [Tests] NFC: Add a test-case for rdar://133340307 which is now fast 2024-12-17 11:36:39 -08:00
Pavel Yaskevich
bc5f70a9a3 [CSOptimizer] Allow generic operator overloads without associated type parameters 2024-12-17 11:36:38 -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
Slava Pestov
8eb9e745a6 Merge pull request #78225 from slavapestov/optimized-shifts
Sema: Filter out protocol extension default implementations of operators
2024-12-17 07:44:10 -05:00
Slava Pestov
27812856dc Sema: Extend DisjunctionStep::shouldSkip() hack to cover ~ & | ^ << >> 2024-12-16 18:08:01 -05:00
omochimetaru
eb2d4b62b2 add regression test 2024-12-15 21:11:46 +09:00
Pavel Yaskevich
fb54682753 [CSSimplify] Don't match pack expansion patterns if shapes are not the same
This helps to avoid spurious failures pointing to involved pattern types
because they won't match exactly if shape types are not the same.
2024-12-12 20:55:02 -08:00
Pavel Yaskevich
70b07af38b Merge pull request #78035 from xedin/rdar-141012049
[CSSimplify] Avoid resolving extraneous (trailing) closures
2024-12-09 14:30:32 -08:00
Alexis Laferrière
224efd72c5 Merge pull request #77964 from xymus/optional-memcmp
SwiftShims: memcmp should accept optional pointers on Darwin
2024-12-09 13:35:23 -08:00
Pavel Yaskevich
7701f4e0dd [CSSimplify] Avoid resolving extraneous (trailing) closures
If a (trailing) closure is determined to be an extraneous argument
for one of the overload choices it needs to be marked as hole as
eagerly as possible and prevented from being resolved because
otherwise it's going to be disconnected from the rest of the
constraint system and resolution might not be able to find all of
the referenced variables. This could result either in crashes
or superfluous diagnostics.

Resolves: rdar://141012049
2024-12-06 16:41:49 -08:00
Slava Pestov
302b163edc AST: Optimize TypeAliasType representation 2024-12-06 17:39:55 -05:00
Egor Zhdan
7704534132 Merge pull request #77754 from swiftlang/egorzhdan/swiftinterface-no-cxx-flag
[cxx-interop] Do not emit C++ interop flag in textual interfaces
2024-12-06 14:30:21 +00:00
Egor Zhdan
7ae2bebfe8 [cxx-interop] Do not emit C++ interop flag in textual interfaces
This makes sure that the compiler does not emit `-enable-experimental-cxx-interop`/`-cxx-interoperability-mode` flags in `.swiftinterface` files. Those flags were breaking explicit module builds. The module can still be rebuilt from its textual interface if C++ interop was enabled in the current compilation.

rdar://140203932
2024-12-05 19:25:09 +00:00
nate-chandler
0d76250033 Merge pull request #77908 from nate-chandler/rdar139840307
[BarrierAccessScopes] Handle end_access instructions' barrierness introduced during run.
2024-12-04 15:29:02 -08:00
Alexis Laferrière
0c27a4c38c SwiftShims: memcmp should accept optional pointers on Darwin
Darwin defines memcmp with optional pointers. Update SwiftShims to
define it to the same type to avoid deserialization failures where we
get one over the other and the types don't match anymore.

rdar://140596571
2024-12-04 14:58:32 -08:00
Anthony Latsis
ec5bee8bc9 Merge pull request #77873 from AnthonyLatsis/day-10
test: Add regression test for #74545
2024-12-04 02:43:41 +00:00
Anthony Latsis
ea18b4b53c test: Add regression test for #74545 2024-12-03 20:59:20 +00:00
eeckstein
31cc6b8f54 Merge pull request #77901 from eeckstein/fix-test
tests: fix a wrong check line in `SILOptimizer/large_string_array.swift.gyb`
2024-12-03 02:36:05 +01:00
Pavel Yaskevich
8a0a083a45 Merge pull request #77797 from xedin/locatable-types
[AST/Sema]  Introduce a new type that has associated location in source
2024-12-02 16:33:43 -08:00
Nate Chandler
f79def4cee [BarrierAccessScopes] Handle found gen locality.
As the utility runs, new gens may become local: as access scopes are
determined to contain deinit barriers, their `end_access` instructions
become kills; if such an `end_access` occurs in the same block above an
initially-non-local gen, that gen is now local.

Previously, it was asserted that initially-non-local gens would not
encounter when visiting the block backwards from that gen.  Iteration
would also _stop_ at the discovered kill, if any.  As described above,
the assertion was incorrect.

Stopping at the discovered kill was also incorrect.  It's necessary to
continue walking the block after finding such a new kill because the
book-keeping the utility does for which access scopes contain barriers.
Concretely, there are two cases:
(1) It may contain another `end_access` and above it a deinit barrier
which must result in that second scope becoming a deinit barrier.
(2) Some of its predecessors may be in the region, all the access scopes
which are open at the begin of this block must be unioned into the set
of scopes open at each predecessors' end, and more such access scopes
may be discovered above the just-visited `end_access`.

Here, both the assertion failure and the early bailout are fixed by
walking from the indicated initially-non-local gen backwards over the
entire block, regardless of whether a kill was encountered.  If a kill
is encountered, it is asserted that the kill is an `end_access` to
account for the case described above.

rdar://139840307
2024-12-02 15:36:00 -08:00
Erik Eckstein
15ead8aa48 tests: fix a wrong check line in SILOptimizer/large_string_array.swift.gyb
Triggered by https://github.com/swiftlang/swift/pull/77806
rdar://140778726
2024-12-02 19:26:39 +01:00
Erik Eckstein
c6408387a7 tests: add a missing -sil-print-types in validation-test/SILOptimizer/rdar114699006.swift
rdar://140405972
2024-11-22 14:32:52 +01:00
Pavel Yaskevich
e3b1f341f0 [CSDiagnostics] Adjust conformance failure diagnostic to check for locatable types
If conformance failure is associated with a `LocatableType`, use
its location to anchor the failure.
2024-11-21 14:13:16 -08:00
Erik Eckstein
7cceaff5f3 SIL: don't print operand types in textual SIL
Type annotations for instruction operands are omitted, e.g.

```
  %3 = struct $S(%1, %2)
```

Operand types are redundant anyway and were only used for sanity checking in the SIL parser.

But: operand types _are_ printed if the definition of the operand value was not printed yet.
This happens:

* if the block with the definition appears after the block where the operand's instruction is located

* if a block or instruction is printed in isolation, e.g. in a debugger

The old behavior can be restored with `-Xllvm -sil-print-types`.
This option is added to many existing test files which check for operand types in their check-lines.
2024-11-21 18:49:52 +01:00
Pavel Yaskevich
9eaad6315c Merge pull request #77689 from JanBaig/issue-65500
[CSGen] Fix compiler crash when property wrapper applied to a param lacks wrappedValue initializer
2024-11-20 08:44:49 -08:00
Pavel Yaskevich
9665b0d4e3 Merge pull request #77592 from xedin/rdar-139675914
[CSSimplify] CGFloat-Double: Fix ambiguity when assigning CGFloat to …
2024-11-20 08:44:19 -08:00
JanBaig
8af3aef7c4 [CSGen] Fix compiler crash when property wrapper applied to a param lacks wrappedValue initializer 2024-11-18 19:23:28 -05:00
Hamish Knight
abec83f68e [test] Quote %original_path_env in swift-xcodegen.test
Ensure we can handle a PATH with spaces.
2024-11-16 22:28:13 +00:00
Dario Rexin
abfa450e3e Merge pull request #77597 from drexin/wip-139375022
[IRGen] Set minimum runtime availability version for CVW
2024-11-14 06:11:37 -08:00
Dario Rexin
c0c31c0613 [IRGen] Set minimum runtime availability version for CVW
rdar://139375022

CVW should not be generated when the deployment target does not have the necessary runtime functions.
2024-11-13 22:36:15 -08:00
Pavel Yaskevich
7c35c881a9 [CSSimplify] CGFloat-Double: Fix ambiguity when assigning CGFloat to double property/variable
Situations like:
```
  let _: Double = <<CGFloat>>
  <var/property of type Double> = <<CGFloat>>
```

Used to be supported due to an incorrect fix added in
diagnostic mode. Lower impact here means that right-hand
side of the assignment is allowed to maintain CGFloat
until the very end which minimizes the number of conversions
used and keeps literals as Double when possible.

Resolves: rdar://139675914
2024-11-13 13:45:56 -08:00
Meghana Gupta
184058e4ac Merge pull request #76588 from meg-gupta/lifetimeremovefeatureguardincompiler
Remove feature guard on lifetime dependence inference
2024-11-12 18:38:41 -08:00
Meghana Gupta
827caa9fce XFAIL typechecker test which leads to a cycle in the requirement machine
This PR introduces a `mapTypeIntoContext` in `InterfaceTypeRequest::evaluate` which introduces a cycle in this test.
2024-11-12 12:05:23 -08:00
Nate Cook
e12e968570 Add contains(_:) methods to (Closed)Range (#76891)
The _StringProcessing module provides a generic, collection-based
`contains` method that performs poorly for ranges and closed ranges.
This addresses the primary issue by providing concrete overloads
for Range and ClosedRange which match the expected performance for
these operations.

This change also fixes an issue with the existing range overlap tests.
The generated `(Closed)Range.overlap` tests are ignoring the "other"
range type when generating ranges for testing, so all overlap tests
are only being run against ranges of the same type. This fixes things
so that heterogeneous testing is included.
2024-11-12 11:47:24 -08:00
Henrik G. Olsson
0678829cf7 Add @PointerBounds macro (#76969)
Add @PointerBounds macro

@PointerBounds is a macro intended to be applied by ClangImporter when
importing functions with pointer parameters from C headers. By
leveraging C attributes we can get insight into bounds, esapability, and
(eventually) lifetimes of pointers, allowing us to map them to safe(r)
and more ergonomic types than UnsafePointer.

This initial macro implementation supports CountedBy and Sizedby, but
not yet EndedBy. It can generate function overloads with and without an
explicit count parameter, as well as with UnsafeBufferPointer or Span
(if marked nonescaping), and any of their combinations. It supports
nullable/optional pointers, and both mutable and immutable pointers.
It supports arbitrary count expressions. These are passed to the macro
as a string literal since any parameters referred to in the count
expression will not have been declared yet when parsing the macro.

It does not support indirect pointers or inout parameters. It supports
functions with return values, but returned pointers can not be bounds
checked yet.

Bounds checked pointers must be of type Unsafe[Mutable]Pointer[?]<T>
or Unsafe[Mutable]RawPointer[?]. Count expressions must conform to
the BinaryInteger protocol, and have an initializer with signature
"init(exactly: Int) -> T?" (or be of type Int).

rdar://137628612

---------

Co-authored-by: Doug Gregor <dgregor@apple.com>
2024-11-11 14:54:25 -08: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
Hamish Knight
43be5456b1 [xcodegen] Allow output to be missing in compile_commands.json
This may not always be present for CI builds.
2024-11-06 12:20:44 +00:00
Hamish Knight
a9efa842a9 Merge pull request #77406 from hamishknight/a-new-generation
Introduce swift-xcodegen
2024-11-06 11:58:14 +00:00