Commit Graph

13 Commits

Author SHA1 Message Date
Erik Eckstein
56521f0c3d Optimizer: fix handling of dependent existential archetypes in alloc_stack and apply simplification
When replacing an opened existential type with the concrete type, we didn't consider that the existential archetype can also be a "dependent" type of the root archetype.
For now, just bail in this case. In future we can support dependent archetypes as well.

Fixes a compiler crash.
rdar://158594365
2025-08-26 18:01:10 +02:00
Valeriy Van
761bee9d09 Fix typo in func replaceOpenedArchetypeInSubstituations -> replaceOpenedArchetypeInSubstitutions 2025-06-08 11:30:04 +03:00
Erik Eckstein
1a5ea19a85 SimplifyApply: don't do the raw-enum comparison optimization for custom RawRepresentable enums
Because we don't know what the custom `rawValue` can do.

Fixes a miscompile
rdar://152143111
2025-05-29 08:12:17 +02:00
Erik Eckstein
cac594fb86 Optimizer: peephole optimization for raw-value enum comparsions
Optimize (the very inefficient) RawRepresentable comparison function call to a simple compare of enum tags.
For example,
```
  enum E: String {
    case  a, b, c
  }
```
is compared by getting the raw values of both operands and doing a string compare.
This peephole optimizations replaces the call to such a comparison function with a direct compare of the enum tags, which boils down to a single integer comparison instruction.

rdar://151788987
2025-05-27 12:11:03 +02:00
Erik Eckstein
04ede105d6 Simplification: remove redundant array casts
Remove casts between arrays of the same type.

```
  %1 = function_ref @_arrayConditionalCast : (@guaranteed Array<Int>) -> @owned Optional<Array<Int>>
  %2 = apply %1(%0) : (@guaranteed Array<Int>) -> @owned Optional<Array<Int>>
```
->
```
  %1 = copy_value %0
  %2 = enum $Optional<Array<Int>>, #Optional.some!enumelt, %1
```

This can be a result of specialization, e.g.

```
func foo<T>(from: [T]) -> [Int]? {
  return from as? [Int]
}

func bar(a: [Int]) -> [Int]? {
  return foo(from: a)
}
```

rdar://139632757
2025-03-15 08:32:58 +01:00
Erik Eckstein
d52f7d1619 AST/SIL: Refactor and simplify AST.Type, AST.CanonicalType and SIL.Type
* let `SIL.Type` conform to `TypeProperties` to share the implementation of common type properties between the AST types and `SIL.Type`
* call references to an `AST.Type` `rawType` (instead of just `type`)
* remove unneeded stuff
* add comments
2025-03-14 09:40:22 +01:00
Erik Eckstein
71de3d90e0 Optimizer: replace existential archetypes with concrete types in apply instructions
If an apply uses an existential archetype (`@opened("...")`) and the concrete type is known, replace the existential archetype with the concrete type
  1. in the apply's substitution map
  2. in the arguments, e.g. by inserting address casts
For example:
```
  %5 = apply %1<@opend("...")>(%2) : <τ_0_0> (τ_0_0) -> ()
```
->
```
  %4 = unchecked_addr_cast %2 to $*ConcreteType
  %5 = apply %1<ConcreteType>(%4) : <τ_0_0> (τ_0_0) -> ()
```
2025-03-07 15:59:34 +01:00
Erik Eckstein
3f95ce9645 Optimizer: fix spelling of the Simplifiable protocol
Rename `Simplifyable` -> `Simplifiable`

NFC
2025-02-12 09:01:11 +01:00
Erik Eckstein
cd7c533d42 Optimizer: add SingleValueInstruction.replace(with:) and use it throughout the optimizer
It replaces all uses and then erases the instruction.
2024-12-19 20:53:45 +01:00
Kuba Mracek
b642d771be [embedded] Compile-time (literal) KeyPaths for Embedded Swift
Enable KeyPath/AnyKeyPath/PartialKeyPath/WritableKeyPath in Embedded Swift, but
for compile-time use only:

- Add keypath optimizations into the mandatory optimizations pipeline
- Allow keypath optimizations to look through begin_borrow, to make them work
  even in OSSA.
- If a use of a KeyPath doesn't optimize away, diagnose in PerformanceDiagnostics
- Make UnsafePointer.pointer(to:) transparent to allow the keypath optimization
  to happen in the callers of UnsafePointer.pointer(to:).
2024-03-20 15:35:46 -07:00
Erik Eckstein
8b9f2a73d0 SimplifyApply: optimize thick calls where the callee is a thin_to_thick_function
```
   %2 = thin_to_thick_function %1
   %3 = apply %2(...) : @callee_guaranteed
 ->
   %2 = thin_to_thick_function %1
   %3 = apply %1(...): @convention(thin)
```
2023-10-27 10:47:07 +02:00
Erik Eckstein
82734b6ac2 Swift Optimizer: simplification for apply, try_apply, begin_apply and partial_apply
* move the apply of partial_apply transformation from simplify-apply to simplify-partial_apply
* delete dead partial_apply instructions
* devirtualize apply, try_apply and begin_apply
2023-05-11 08:11:44 +02:00
Erik Eckstein
f1c6ed681d Swift Optimizer: add Onone simplification of apply instructions 2023-02-09 06:50:05 +01:00