Commit Graph

91 Commits

Author SHA1 Message Date
Daniil Kovalev
02fcd218d7 Address review comments 2025-10-15 05:14:15 +03: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
Arnold Schwaighofer
25a071efc8 Add experimental feature @inline(always)
The intent for `@inline(always)` is to act as an optimization control.
The user can rely on inlining to happen or the compiler will emit an error
message.

Because function values can be dynamic (closures, protocol/class lookup)
this guarantee can only be upheld for direct function references.

In cases where the optimizer can resolve dynamic function values the
attribute shall be respected.

rdar://148608854
2025-09-30 08:36:26 -07:00
Kavon Farvardin
61fe8a9b8e introduce @_manualOwnership performance attribute
This attribute forces programmers to acknowledge every
copy that is required to happen in the body of the
function. Only those copies that make sense according
to Swift's ownership rules should be "required".

The way this is implemented as of now is to flag each
non-explicit copy in a function, coming from SILGen, as
an error through PerformanceDiagnostics.
2025-09-17 13:51:57 -07: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
Slava Pestov
6c60f03f9e SIL: Split up Function.loweredFunctionType/InContext 2025-08-25 16:45:45 -04:00
Erik Eckstein
41a6b8e257 SwiftCompilerSources: move SIL-related Context APIs from Optimizer to the SIL module 2025-07-28 14:19:11 +02:00
Erik Eckstein
f70177ca13 SIL: fix typo in comment in Function.swift 2025-06-20 08:15:03 +02:00
Erik Eckstein
c19aa69940 SIL: implement Function.isSerialized and Function.isAnySerialized with serializedKind
No need for bridging functions
2025-06-20 08:15:02 +02:00
Erik Eckstein
9bd85c6723 SIL: add some Function APIs
* `isReferencedInModule`
* `shouldOptimize`
2025-06-20 08:15:01 +02:00
Valeriy Van
949c2bad67 Fix some typos in SwiftCompilerSources/Sources 2025-06-08 11:22:45 +03:00
Andrew Trick
b670a6d9cc Merge pull request #81191 from atrick/var-walker-recursion
LifetimeDependenceDiagnostics: bug fixes and output clarity
2025-04-30 15:36:32 -07:00
Andrew Trick
25e9cbf3f1 [NFC] SwiftCompilerSource: bridge Function.accessorKindName 2025-04-29 22:09:42 -07:00
Erik Eckstein
ffc17322f9 SIL: bridge Function.isDeinitBarrier back from Swift to C++ SIL
This API already existed in Swift, but not in the C++ SIL.
2025-04-29 20:30:21 +02:00
Erik Eckstein
5a67af507e Swift SIL: add Function.isSpecialization 2025-04-18 06:58:39 +02:00
Erik Eckstein
67556cfbf4 SIL: add some Function APIs
* `var wasDeserializedCanonical`
* `var genericSignature`
* `func mapTypeIntoContext`
* `var forwardingSubstitutionMap`
2025-03-26 08:45:07 +01:00
Erik Eckstein
380ea889bc SIL: fix self-argument APIs in Function
`var selfArgument` should return an optional, i.e. nil if the function doesn't have a self argument.
2025-03-26 07:30:10 +01:00
Erik Eckstein
f8e013a21c Fix a compiler warning in SIL/Function.swift 2024-12-20 09:10:17 +01:00
Andrew Trick
7a11d5a3f1 SwiftCompilerSources: bridge Function.sourceFileKind. 2024-12-18 16:56:45 -08:00
Andrew Trick
98da813f02 [NFC] SwiftCompilerSources: add isConvertPointerToPointerArgument 2024-12-14 22:46:54 -08:00
Erik Eckstein
709dfc2d21 MandatoryPerformanceOptimization: don't let not-inlinable functions to be inlined
Also refactor canInline.

Fixes a compiler crash.
rdar://137544788
2024-10-15 12:19:50 +02:00
Erik Eckstein
10782cf42b SwiftCompilerSources: introduce the AST module
As the optimizer uses more and more AST stuff, it's now time to create an "AST" module.
Initially it defines following AST datastructures:
* declarations: `Decl` + derived classes
* `Conformance`
* `SubstitutionMap`
* `Type` and `CanonicalType`

Some of those were already defined in the SIL module and are now moved to the AST module.
This change also cleans up a few things:
* proper definition of `NominalTypeDecl`-related APIs in `SIL.Type`
* rename `ProtocolConformance` to `Conformance`
* use `AST.Type`/`AST.CanonicalType` instead of `BridgedASTType` in SIL and the Optimizer
2024-10-02 07:10:29 +02:00
Erik Eckstein
c96b196ffa SwiftCompilerSources: bridge SILLinkage
Make SILLInkage available in SIL as `SIL.Linkage`.
Also, rename the misleading Function and GlobalVariable ABI `isAvailableExternally` to `isDefinedExternally`
2024-08-22 08:56:27 +02:00
Erik Eckstein
c61733f985 SwiftCompilerSources: refactor Function.mayBindDynamicSelf
Instead of bridging the whole function, just bridge `hasDynamicSelfMetadata` and do the other work in swift.
2024-06-25 17:59:23 +02:00
Ellie Shin
41ee82a589 [SwiftCompilerSources] Remove warning: add unknown default case for bridged function serializedkind. 2024-06-13 14:42:13 -07:00
Ellie Shin
8b00478bc6 [SwiftCompilerSources] Remove unnecessary default case from SerializedKind funcs 2024-06-11 23:59:43 -07:00
Meghana Gupta
470fa2f365 Remove resultDependsOn/resultDependsOnSelf 2024-06-05 11:36:16 -07:00
Ellie Shin
c3ded85c4f Update SILBridgedFunction APIs.
Add isAnySerialized() and canBeInlinedIntoCaller(SerializedKind).
2024-05-29 15:54:36 -07:00
Kshitij
74166a4ab6 [Autodiff] Moves bridging code accesses in closure-spec opt behind APIs
Addresses some other surfacial feedback as well.
2024-05-13 15:37:30 -07:00
Kshitij
ab751d57ab [Autodiff] Adds logic to generate specialized functions in the closure-spec pass 2024-05-13 11:16:42 -07:00
Kshitij
003c9082cb [Autodiff] Adds SwiftCompilerSources changes in preparation for the Autodiff closure-spec optimization pass 2024-05-02 09:14:05 -07:00
Andrew Trick
8b24382494 SwiftCompilerSources: cleanup and extend ArgumentConventions
Provide APIs needed by lifetime dependence diagnostics, namely LifetimeDependenceConvention.

Reorganize the APIs so it's easy to find related functionality which
API is responsible for which functionality.

Remove the originalFunctionConvention complexity. It is no longer
needed for lifetime dependence inference, and generally should be
avoided in SIL.

Add some placeholder FIXMEs because this not a good PR in which to
change existing functionality.
2024-02-12 09:57:14 -08:00
Erik Eckstein
31f3102540 swift SIL: fix Function.isGeneric
It didn't work for functions in generic contexts, which don't add a generic parameter themselves
2024-01-31 17:16:18 +01:00
Erik Eckstein
3b239b25f3 swift SIL: add Function thunk APIs
* `var thunkKind`
* `set(thunkKind:)`
2024-01-31 17:16:13 +01:00
Andrew Trick
9770ce41a2 Improve the ArgumentConvention API 2024-01-30 08:38:57 -08:00
Andrew Trick
2128c21106 Migrate SwiftCompilerSources to FunctionConvention.
Layers:
- FunctionConvention: AST FunctionType: results, parameters
- ArgumentConventions: SIL function arguments
- ApplyOperandConventions: applied operands

The meaning of an integer index is determined by the collection
type. All the mapping between the various indices (results,
parameters, SIL argument, applied arguments) is restricted to the
collection type that owns that mapping. Remove the concept of a
"caller argument index".
2024-01-03 12:24:50 -08:00
Meghana Gupta
3c56a7a88e Bridge hasResultDependsOnSelf() 2023-12-29 02:06:15 -08:00
Andrew Trick
c1f0f06907 [SIL] Add FunctionConvention.swift
Bridge information about the AST function type that does not require a
SIL function body.

Support querying the function type's ResultInfo.
2023-12-18 09:43:18 -08:00
eeckstein
10ff55b2b3 Merge pull request #70155 from eeckstein/refactoring
MemoryLifetimeVerifier: use CalleeCache instead of AliasAnalysis
2023-12-04 08:53:59 +01:00
Erik Eckstein
0897d8a720 MemoryLifetimeVerifier: use CalleeCache instead of AliasAnalysis
To verify if a function may read from an indirect argument, don't use AliasAnalysis.
Instead use the CalleeCache to get the list of callees of an apply instruction.
Then use a simple call-back into the swift Function to check if a callee has any relevant memory effect set.

This avoids a dependency from SIL to the Optimizer.
It fixes a linker error when building some unit tests in debug.
2023-12-01 19:20:18 +01:00
zoecarver
69498e2f2e [opt] Add three new perf annotations: @_noRuntime, @_noExistential, and @_noObjCBridging. 2023-12-01 09:13:24 -07:00
Erik Eckstein
9bbee4411b SwiftCompilerSources: consider indirect error results in Function.numArguments 2023-11-27 08:51:12 +01:00
Hamish Knight
ce23252a3f [Basic] Improve some bridging APIs
Improve APIs for BridgedStringRef,
BridgedOwnedString, and BridgedSourceLoc.
2023-10-30 23:49:53 +00:00
Andrew Trick
ca7253a25c Add the @_unsafeNonEscapableResult function
This will allow NonEscapable types to be returned from a function.
2023-10-17 12:44:31 -07:00
Erik Eckstein
2dbd6cc56b SwiftCompilerSources: rework bridging
Introduce two modes of bridging:
* inline mode: this is basically how it worked so far. Using full C++ interop which allows bridging functions to be inlined.
* pure mode: bridging functions are not inlined but compiled in a cpp file. This allows to reduce the C++ interop requirements to a minimum. No std/llvm/swift headers are imported.

This change requires a major refactoring of bridging sources. The implementation of bridging functions go to two separate files: SILBridgingImpl.h and OptimizerBridgingImpl.h.
Depending on the mode, those files are either included in the corresponding header files (inline mode), or included in the c++ file (pure mode).

The mode can be selected with the BRIDGING_MODE cmake variable. By default it is set to the inline mode (= existing behavior). The pure mode is only selected in certain configurations to work around C++ interop issues:
* In debug builds, to workaround a problem with LLDB's `po` command (rdar://115770255).
* On windows to workaround a build problem.
2023-10-09 09:52:52 +02:00
Erik Eckstein
ad594f2713 Swift SIL: add some APIs
* `AssignInst`
* `Function.isDestructor`
* `MarkUninitializedInst.kind`
* `Type.isMoveOnly`
* `RefElementAddrInst.isImmutable` and `RefElementAddrInst.set(isImmutable:)`
* `BeginBorrowInst.endBorrows`
* `Context.hadError` and `Context.silStage`
2023-09-19 15:10:30 +02:00
zoecarver
48513f3c5b [embedded] Introduce isGenericFunction and addAllNonGenericFunctions. 2023-09-06 10:48:17 -07:00
John McCall
65e2e8c2fe Fix some SILArgument infrastructure for pack results.
Getting this right convinces the memory verifier to not complain
about untouched empty pack result arguments, which should be
innocuous.
2023-06-30 02:08:57 -04:00
Erik Eckstein
ee1c52bc77 Swift SIL: add some APIs 2023-05-22 15:34:26 +02:00
Erik Eckstein
b9169064c6 Swift SIL: add some APIs
* `Options.assertConfiguration`
* `Argument.isIndirectResult`
* in `Function`: `selfArgument`, `isTransparent`, `performanceConstraints` and `inlineStrategy`
* `BuiltinInst.substitutionMap`
* `SubstitutionMap.replacementTypes`
* `Type.canBeClass`
2023-05-11 08:11:44 +02:00