Commit Graph

100 Commits

Author SHA1 Message Date
Aidan Hall 51dec85971 Simplify tuple_pack_element_addr with scalar_pack_index
If a tuple_pack_element_addr uses a scalar_pack_index rather than a
dynamic_pack_index, we can replace it with tuple_element_addr, since the
specific tuple element accessed is statically known.
2026-05-21 18:00:40 +01:00
elsa 83d4291709 CSE Optimizer Pass rewrite (#88248)
Resolves rdar://173862129
2026-05-15 19:11:10 +01:00
Erik Eckstein 7ef83e336d AST: add ClassDecl.selfAndSuperClasses which returns a sequence to conveniently iterate over the super classes 2026-04-03 07:49:34 +02:00
Erik Eckstein 66a86e2a4e AST: bridge some APIs for class and protocol decls 2026-04-03 07:49:34 +02:00
Erik Eckstein df8d87ee92 AST: add ProtocolDecl.isEligibleForFastCasting
This is true if this protocol is marked for fast casting, i.e. fast conformance lookup via the vtable of a conforming class.
Currently this is done with `@_semantics("fast_cast")`. However, in future we want to add a "real" attribute for this.
2026-04-03 07:49:34 +02:00
Erik Eckstein bd1af9283f Optimizer: fix the enum tag comparison optimization
This optimization replaces (the very inefficient) RawRepresentable comparison to a simple compare of enum tags.
However if the raw type is a custom type we don't know how the comparison is implemented.
A custom raw type can implement the case comparison in a way that comparing different cases will return `true`.
Therefore only do the optimization for known stdlib raw value types.

Fixes a mis-compile
https://github.com/swiftlang/swift/issues/87906
rdar://172746003
2026-03-24 08:54:09 +01:00
Erik Eckstein ed94c5faab Swift AST: add var NominalTypeDecl.selfInterfaceType 2026-03-10 13:22:58 +01:00
Daniil Kovalev 1f77138afe [AutoDiff] Closure specialization: specialize branch tracing enums (#85757)
This patch contains part of the changes intended to resolve #68944.

1. Closure info gathering logic.
2. Branch tracing enum specialization logic.
3. Specialization of branch tracing enum basic block arguments in VJP.
4. Specialization of branch tracing enum payload basic block arguments
in pullback.

Note that mangling-related logic is implemented in C++ since at this
moment we have no Swift bridged for that.

Here is a simplified example of how branch tracing enum (BTE)
specialization looks like.

Before specialization:

```
enum $_AD__xxx {
  case bb0(((Float) -> Float))
}

func vjp(...) {
  // ...
  %foo      = function_ref $foo         : (Float, Float) -> Float
  %pa1      = partial_apply %foo(%arg1) : (Float) -> Float
  %payload1 = tuple (%pa1)              : ((Float) -> Float)
  %bte      = enum $_AD__xxx.bb0!enumelt, %payload1
  // ...
}

func pullback(%bte, ...) {
  // ...
  %payload2 = unchecked_enum_data %bte, $_AD__xxx.bb0!enumelt : ((Float) -> Float)
  %pa2      = tuple_extract %payload2, 0                      : (Float) -> Float
  %res      = apply %pa2(%arg2)                               : Float
  // ...
}
```

After specialization:

```
enum $_AD__xxx_spec_bb0_0 {
  case bb0(((Float)))
}

func vjp(...) {
  // ...
  %captured1 = tuple (%arg1)      : (Float)
  %payload1  = tuple (%captured1) : ((Float))
  %bte_spec  = enum $_AD__xxx_spec_bb0_0.bb0!enumelt, %payload1
  // ...
}

func pullback_spec(%bte_spec, ...) {
  // ...
  %payload2  = unchecked_enum_data %bte, $_AD__xxx_spec_bb0_0.bb0!enumelt : ((Float))
  %captured2 = tuple_extract %payload2, 0                                 : (Float)
  %arg1      = tuple_extract %captured2, 0                                : Float
  %foo       = function_ref $foo                                          : (Float, Float) -> Float
  %res       = apply %foo(%arg2, %arg1)                                   : Float
  // ...
}
```
2025-12-21 00:33:50 +00:00
eeckstein f092709b72 Merge pull request #85883 from aeu/ast_suppress_compiler_warning
[AST] suppress compiler warning
2025-12-15 15:44:34 +00:00
Alfonso Urdaneta 527ce4ef4a suppress AST compiler warning 2025-12-06 16:26:33 +00:00
Erik Eckstein 7eedc18965 Swift AST: add ConstructorDecl.isInheritable
and make `AbstractFunctionDecl.isOverridden` final
2025-12-05 11:34:54 +01:00
Daniil Kovalev b73676ee68 Bridging: Implement several optional- and enum-related bridges (#85756)
In #85757, part of the changes resolving #68944 is submitted. Most
bridges required for #85757 were previously implemented in #84648. After
#82653 got merged, we have demand for several new bridges in order to
properly support optimizing derivatives of throwing functions via
AutoDiff Closure Specialization pass.

This patch implements:

- **AST:**

   * `var optionalObjectType: Type` property of `Type` struct
   
   * `var optionalType: Type` property of `Type` struct

- **SIL:**

  * `let name: StringRef` property of `EnumCase` struct

* `func createOptionalSome(operand: Value, type: Type) -> EnumInst`
method of `Builder`

* `func createOptionalNone(type: Type) -> EnumInst` method of `Builder`
2025-12-04 08:26:44 +00:00
Arnold Schwaighofer 36a3c6e611 Merge pull request #85644 from aschwaighofer/wip_embedded_exits_with_eriks_changes_v1
[embedded] Fix associated type conformances for specialized witness tables
2025-12-01 16:40:31 -08:00
eeckstein 9304ce951c Merge pull request #85707 from eeckstein/embedded-witness-method-specialization
embedded: change the function representation of directly called witness methods
2025-12-01 09:36:45 +01:00
Erik Eckstein 4920ace11e AST: add enum FunctionTypeRepresentation and TypeProperties.functionTypeRepresentation 2025-11-26 16:23:46 +01:00
Erik Eckstein a371aecb95 AST: add var ClassDecl.isForeign 2025-11-21 16:33:06 +01:00
Erik Eckstein 1486d009b0 AST: add var ProtocolDecl.isMarkerProtocol 2025-11-20 10:56:05 -08:00
Daniil Kovalev a172134162 Address review comments 2025-11-18 01:46:49 +03:00
Daniil Kovalev c12819f881 Address review comments 2025-11-17 14:13:09 +03:00
Daniil Kovalev c1f4bcfd98 Merge branch 'main' into users/kovdan01/ast-bridges-for-autodiff-closure-spec 2025-11-17 10:22:24 +03:00
Slava Pestov 819738c83e AST: Rename mapTypeIntoContext() => mapTypeIntoEnvironment(), mapTypeOutOfContext() => mapTypeOutOfEnvironment() 2025-11-12 14:48:19 -05:00
Daniil Kovalev f0bf57a269 Resolve merge conflicts & address review comments 2025-11-10 19:18:25 +03:00
Daniil Kovalev 72431dbd2d Merge branch 'main' into users/kovdan01/ast-bridges-for-autodiff-closure-spec 2025-11-10 18:46:04 +03:00
Aidan Hall c7af4c584e Bridging: APIs for PackSpecialization pass 2025-10-26 13:44:34 +00:00
Daniil Kovalev 7c90f03032 Merge branch 'main' into users/kovdan01/ast-bridges-for-autodiff-closure-spec 2025-10-15 13:17:36 +03:00
Daniil Kovalev 02fcd218d7 Address review comments 2025-10-15 05:14:15 +03:00
Andrew Trick 0d496b5404 SwiftCompilerSources: bridge Type.unsafePointerElementType 2025-10-07 10:44:42 -07:00
Daniil Kovalev b3d31c7d51 Bridging: Implement bridges required for ongoing AutoDiff changes
In #83926, part of the changes resolving #68944 is submitted. The AutoDiff
closure specialization optimizer pass relies on several bridges not
implemented yet.

This patch introduces these missing bridges. See the list of the changes below.

**AST:**

* Define `.asValueDecl` on each BridgedXXXDecl type that's also a ValueDecl.

* Define `.asNominalTypeDecl` on each BridgedXXXDecl type that's also a NominalTypeDecl.

* Define `.asGenericContext` on each BridgedXXXDecl type that's also a GenericContext.

* `class BridgedSourceFile`:
  - Make nullable
  - `func addTopLevelDecl(_ decl: BridgedDecl)`

* `class BridgedFileUnit`:
  - `func castToSourceFile() -> BridgedNullableSourceFile`

* `class BridgedDecl`:
  - `func getDeclContext() -> BridgedDeclContext`

* `class BridgedParamDecl`:
  - `func cloneWithoutType() -> BridgedParamDecl`
  - `func setInterfaceType(_ type : BridgedASTType)`

* Define `BridgedDecl.setImplicit()` instead of `BridgedParamDecl.setImplicit()`

* `class BridgedGenericContext`:
  - `setGenericSignature(_ genSig: BridgedGenericSignature)`

* Change return type of `BridgedEnumDecl.createParsed(/*...*/)` from `BridgedNominalTypeDecl` to `BridgedEnumDecl`

* `class BridgedValueDecl`:
  - `func setAccess(_ accessLevel: swift.AccessLevel)`

* `class BridgedNominalTypeDecl`:
  - `func addMember(_ member: BridgedDecl)`

* `class BridgedGenericTypeParamDecl`:
  - `func createImplicit(declContext: BridgedDeclContext, name: swift.Identifier, depth: UInt, index: UInt, paramKind: swift.GenericTypeParamKind)`

* `class ValueDecl`:
  - `var baseIdentifier: swift.Identifier`

* `class NominalTypeDecl`:
  - `var declaredInterfaceType: Type`

* `class EnumElementDecl`:
  - `var parameterList: BridgedParameterList`
  - `var nameStr: StringRef`

* `struct GenericSignature`:
  - `var canonicalSignature: CanGenericSignature`

* `struct CanGenericSignature`:
  - `var isEmpty: Bool`
  - `var genericSignature: GenericSignature`

* `struct Type`:
  - `func mapTypeOutOfContext() -> Type`
  - `func getReducedType(sig: GenericSignature) -> CanonicalType`
  - `func GenericTypeParam_getName() -> swift.Identifier`
  - `func GenericTypeParam_getDepth() -> UInt`
  - `func GenericTypeParam_getIndex() -> UInt`
  - `func GenericTypeParam_getParamKind() -> swift.GenericTypeParamKind`

* `struct CanonicalType`:
  - `func SILFunctionType_getSubstGenericSignature() -> CanGenericSignature`
  - `func loweredType(in function: SIL.Function) -> SIL.Type`

**SIL:**

* `class Argument`:
  - `func replaceAllUsesWith(newArg: Argument)`

* `class BasicBlock`:
  - `func insertPhiArgument(atPosition: Int, type: Type, ownership: Ownership, _ context: some MutatingContext) -> Argument`

* `struct Builder`:
  - `func createTuple(elements: [Value]) -> TupleInst`

* `protocol Context`:
  - `func getTupleType(elements: [AST.Type]) -> AST.Type`
  - `func getTupleTypeWithLabels(elements: [AST.Type], labels: [swift.Identifier]) -> AST.Type`

* `class Function`:
  - `var sourceFile: BridgedNullableSourceFile`
  - `func mapTypeIntoContext(_ type: Type) -> Type`

* `class PartialApplyInst`:
  - `var substitutionMap: SubstitutionMap`

* `class SwitchEnumInst`:
  - `var numCases: Int`
  - `public func getSuccessorForDefault() -> BasicBlock?`

* `Type`:
  - `var category: ValueCategory`
  - `func getEnumCasePayload(caseIdx: Int, function: Function) -> Type`
  - `func mapTypeOutOfContext() -> Type`
  - `static func getPrimitiveType(canType: CanonicalType, silValueCategory: ValueCategory) -> Type`

* `struct EnumCase`:
  - `let enumElementDecl: EnumElementDecl`

* `struct TupleElementArray`:
  - `func label(at index: Int) -> swift.Identifier`

* Define `enum ValueCategory` with `address` and `object` elements
2025-10-06 17:56:38 +03:00
Erik Eckstein 65d69fe965 SIL/AST: add some APIs
* `GenericSignature.isEmpty`
* `Builder.emitDestroy`
* `Function.abi`
* `KeyPathInst.substitutionMap`
* `KeyPathInst.hasPattern`
2025-09-04 08:15:44 +02:00
Erik Eckstein 0a8c60290f AST: add Type.interfaceTypeOfArchetype and some related APIs 2025-08-26 16:38:19 +02:00
Erik Eckstein 0a953b60ca SIL/AST: add var InjectEnumAddrInst.element and var EnumElementDecl.hasAssociatedValues 2025-08-03 11:06:15 +02:00
Anthony Latsis 6eb5d7d857 Bridging: Bridge swift::SourceLoc directly 2025-07-15 21:33:06 +01:00
eeckstein 1d3895610e Merge pull request #82349 from eeckstein/alloc-box-to-stack
Optimizer: re-implement and improve the AllocBoxToStack pass
2025-06-21 07:28:18 +02:00
Erik Eckstein 1f304e5609 SIL: improve APIs for Box types
* move `isBox` from `SIL.Type` to `TypeProperties` to make it also available for AST types
* add `BoxFieldsArray.isMutable(fieldIndex:)`
2025-06-20 08:14:59 +02:00
Erik Eckstein d025e9f7a5 SIL: add var Argument.decl: ValueDecl? 2025-06-20 08:14:58 +02:00
Anthony Latsis 722bc0f086 ASTBridging: Bridge swift::DiagID directly 2025-06-19 12:29:27 +01:00
Valeriy Van 949c2bad67 Fix some typos in SwiftCompilerSources/Sources 2025-06-08 11:22:45 +03:00
Pavel Yaskevich 202907d0f6 [CompilerSources] Register using declaration 2025-05-30 15:52:18 -07:00
Erik Eckstein 28985f4d41 Swift AST: add var EnumDecl.hasRawType 2025-05-29 08:12:17 +02:00
Erik Eckstein dbd0af063c AST: add some BuiltinFixedArray and IntegerType APIs 2025-05-15 21:29:02 +02:00
Erik Eckstein 5dc71aa0a5 AST/SIL: support source location in diagnostics for de-serialized debug info
Diagnostics only work with `SourceLoc` which is basically a pointer into a buffer of the loaded source file.
But when debug info is de-serialized, the SIL `Location` consists of a filename+line+column.
To "convert" this to a `SourceLoc`, the file must be loaded.
This change adds `DiagnosticEngine.getLocationFromExternalSource` for this purpose.
Also, the new protocol `ProvidingSourceLocation` - to which `SourceLoc` and `Location` conform - help to generalize the helper struct `Diagnostic` and make this "conversion" happen automatically.
2025-05-14 11:43:47 +02:00
eeckstein 66e07f04ac Merge pull request #81441 from eeckstein/vector_base_addr
SIL: introduce the `vector_base_addr` instruction and use it in `InlineArray`
2025-05-13 06:52:42 +02:00
Max Desiatov 5dd244c345 NFC: Fix comment typos in Type.swift (#81442)
`performas a global` -> `performs a global`
`canoncial` -> `canonical`
2025-05-12 11:26:24 -07:00
Erik Eckstein ff2341e83a Swift AST/SIL: add Type APIs for Builtin.FixedArray
* `Type.isBuiltinFixedArray`
* `Type.builtinFixedArrayElementType`
2025-05-12 19:24:31 +02:00
Erik Eckstein aacb4d458f Swift AST: add Type.hasDynamicSelf 2025-05-08 19:21:08 +02:00
Meghana Gupta 5395721a20 Bridge getSwiftMutableSpanDecl() and isBuiltinType() 2025-04-30 13:40:12 -07:00
Slava Pestov b911b80725 AST: Remove unused and incorrect BridgedASTType::subst() 2025-04-28 13:47:53 -04:00
Erik Eckstein fd17b7e9f1 Swift AST: add GenericSignature.mapTypeIntoContext 2025-04-18 06:58:38 +02:00
Erik Eckstein 9aa61e127a Swift AST: add some Type APIs 2025-04-18 06:58:38 +02:00
Erik Eckstein 2020897459 Swift AST: add some Decl APIs
* in `Decl`: `var parent: Decl?`
* in `ProtocolDecl`: `var requiresClass: Bool`
2025-04-18 06:58:38 +02:00