Commit Graph

82 Commits

Author SHA1 Message Date
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
Aidan Hall
c7af4c584e Bridging: APIs for PackSpecialization pass 2025-10-26 13:44:34 +00:00
Erik Eckstein
898b8754bf SIL: add some Instruction/Builder APIs
* `var UncheckedValueCastInst.fromValue`
* `BeginApplyInst.isNonThrowing` and `BeginApplyInst.isNonAsync`
* `Builder.createUncheckedValueCast`
2025-10-06 09:47:40 +02:00
Anthony Latsis
3d898f71e5 SwiftCompilerSources/SIL: More robust IntegerLiteralInst construction
ac619010e3 backfired when building the
stdlib on rebranch. This time the problem is reversed: we should be
interpreting an integer as unsigned where we no longer do, here:
8f7af45115/SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyLoad.swift (L78).

Rather than treating integers as signed by default, make
`Builder.createIntegerLiteral` accept a generic `FixedWidthInteger`, and
manipulate the value based on the generic type argument's signedness.

This doesn't entirely define away unintentional sign extensions, but
makes this mistake of humouring the parameter type less likely.
2025-09-25 08:05:57 +01:00
Anthony Latsis
275deaf154 Merge pull request #84052 from swiftlang/jepa-main2
SwiftCompilerSources/SIL: Fix APInt assertion failure on rebranch
2025-09-24 17:39:04 +01:00
Anthony Latsis
ac619010e3 SwiftCompilerSources/SIL: Fix APInt assertion failure on rebranch
The assertion is hit through `TypeValueInst.simplify` when constructing
an integer literal instruction with a negative 64-bit `Swift.Int` and a
bit width of 32 (the target pointer bit width for arm64_32 watchOS).
This happens because we tell the `llvm::APInt` constructor to treat the
input integer as unsigned by default in `getAPInt`, and a negative
64-bit signed integer does not fit into 32 bits when interpreted as
unsigned.

Fix this by flipping the default signedness assumption for the Swift API
and introducing a convenience method for constructing a 1-bit integer
literal instruction, where the correct signedness assumption depends on
whether you want to use 1 or -1 for 'true'.

In the context of using an integer to construct an `llvm::APInt`, there
are 2 other cases where signedness matters that come to mind:
1. A non-decimal integer literal narrower than 64 bits, such as
   `0xABCD`, is used.
2. The desired bit width is >64, since `llvm::APInt` can either
   zero-extend or sign-extend the 64-bit integer it accepts.

Neither of these appear to be exercised in SwiftCompilerSources, and
if we ever do, the caller should be responsible for either (1)
appropriately extending the literal manually, e.g.
`Int(Int16(bitPattern: 0xABCD))`, or (2) passing along the appropriate
signedness.
2025-09-24 09:37:42 +01:00
Erik Eckstein
935e5ecd7a SIL: fix Builder.emitDestroy(of:)
Do nothing for values with address types.

Fixes a crash in ConstantCapturePropagation
rdar://160816390
2025-09-23 10:50:26 +02: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
3da2c1476b SIL: add Builder.createCondBranch 2025-08-28 08:04:50 +02: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
65c9828cb3 SwiftCompilerSources: move the Context protocols from the Optimizer to the SIL module
This allows to move many SIL APIs and utilities, which require a context, to the SIL module.

The SIL-part of SwiftPassInvocation is extracted into a base class SILContext which now lives in SIL.

Also: simplify the begin/end-pass functions of the SwiftPassInvocation.
2025-07-28 14:19:07 +02:00
Erik Eckstein
1343dc562d SILBridging: remove OptionalBridgedSILDebugVariable
This didn't work because the BridgedSILDebugVariable destructor was called even in the "none" case.

Fixes a compiler crash
rdar://154689481
2025-07-01 10:31:30 +02:00
Erik Eckstein
2259fe6972 SIL: add some instruction creation functions in Builder 2025-06-20 08:15:03 +02:00
Erik Eckstein
c482b09878 SIL: let Builder.createAllocStack specify a debugVariable 2025-06-20 08:15:02 +02:00
Doug Gregor
bc4cf1236b [SIL] Generalize CastingIsolatedConformances to CheckedCastInstOptions
We are going to need to add more flags to the various checked cast
instructions. Generalize the CastingIsolatedConformances bit in all of
these SIL instructions to an "options" struct that's easier to extend.

Precursor to rdar://152335805.
2025-06-04 17:12:28 -07:00
Erik Eckstein
a38db6439a SIL: add the vector_base_addr instruction
It derives the address of the first element of a vector, i.e. a `Builtin.FixedArray`, from the address of the vector itself.
Addresses of other vector elements can then be derived with `index_addr`.
2025-05-12 19:24:31 +02:00
Meghana Gupta
ba845de874 Fix newly introduced warnings in LifetimeDependenceScopeFixup 2025-05-05 11:22:16 -07:00
Erik Eckstein
93c41c649c Swift SIL: add Builder.createUncheckedOwnershipConversion 2025-05-02 18:07:57 +02:00
Meghana Gupta
35d62a4a36 Introduce end_cow_mutation_addr instruction 2025-04-30 13:39:45 -07:00
Doug Gregor
e0b52cd20e [SIL] Extend checked-cast instructions with "prohibit isolated conformances" flag
When performing a dynamic cast to an existential type that satisfies
(Metatype)Sendable, it is unsafe to allow isolated conformances of any
kind to satisfy protocol requirements for the existential. Identify
these cases and mark the corresponding cast instructions with a new flag,
`[prohibit_isolated_conformances]` that will be used to indicate to the
runtime that isolated conformances need to be rejected.
2025-03-26 22:31:47 -07:00
Andrew Trick
e7000e4668 SIL: Add mark_dependence_addr 2025-03-25 23:02:42 -07:00
Erik Eckstein
810064b7dc Cleanup and additions to AST and SIL Type/CanonicalType
* factor out common methods of AST Type/CanonicalType into a `TypeProperties` protocol.
* add more APIs to AST Type/CanoncialType.
* move `MetatypeRepresentation` from SIL.Type to AST.Type and implement it with a swift enum.
* let `Builder.createMetatype` get a CanonicalType as instance type, because the instance type must not be a lowered type.
2025-03-07 15:59:33 +01:00
Erik Eckstein
4595d5da22 SIL Builder: make createInitExistentialMetatype more usable by passing the conformances in an Array
Instead of a ConformanceArray
2025-03-07 15:59:33 +01:00
Erik Eckstein
db03a55c79 Swift SIL: add some Builder APIs
* `createCheckedCastAddrBranch`
* `createUnconditionalCheckedCastAddr`
* `createDebugValue`
* `createWitnessMethod`
* `createInitEnumDataAddr`
2025-03-07 15:59:33 +01:00
Erik Eckstein
e0b4f71af6 SIL: remove the alloc_vector instruction
It's not needed anymore, because the "FixedArray" experimental feature is replaced by inline-arrays.
2025-02-12 10:51:14 +01:00
Erik Eckstein
2d10da001a SIL: add some Builder-create functions 2024-12-21 08:28:20 +01:00
Erik Eckstein
bcdbc05260 SIL: make Builde.insertionPoint public 2024-12-10 16:28:11 +01:00
Andrew Trick
442739a176 [NFC] Add a Builder.insertionBlock API.
Some utilities, like Builder.insert(after:) pass a Builder object that
represents an insertion point. That insertion point is sometimes needed to
determine which instructions to create (endApply vs. abortApply). But there is
no way to recover the insertion point. We don't want to simply return an
instruction, because that could be interpreted in different ways. Instead we
provide insertion block, but only in those situations where it makes sense and can't
be used incorrectly.
2024-11-18 01:37:00 -08:00
Andrew Trick
3e26cf81d0 SwiftCompilerSources: bridge createEndApply and createAbortApply. 2024-11-18 01:37:00 -08:00
Erik Eckstein
6bf7cc8f2f Swift SIL: add flag parameters to Builder.createBeginBorrow 2024-10-11 09:41:36 +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
f3a9b08e7a SwiftCompilerSources: bridged ProtocolConformance
And use that in some Builder APIs
2024-09-25 19:31:40 +02:00
Kuba Mracek
c7a5569c4f [embedded] Add support for ManagedBuffer to Embedded Swift
This makes ManagedBuffer available and usable in Embedded Swift, by:

- Removing an internal consistency check from ManagedBuffer that relies on metatypes.
- Making the .create() API transparent (to hoist the metatype to the callee).
- Adding a AllocRefDynamicInst simplification to convert `alloc_ref_dynamic` to `alloc_ref`, which removes a metatype use.
- Adding tests for the above.
2024-09-23 13:32:50 -07:00
Kshitij
c288d95c1e [Autodiff] Separates out changes for using non-ossa instructions in the closure-spec optimization pass
The OSSA elimination pass has not yet been moved below all high level
function passes. Until that work has been completed the Autodiff
closure-spec optimization pass cannot solely support OSSA instructions.
2024-05-21 12:02:28 -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
Erik Eckstein
ac4bc89c9a SIL: add the borrowed-from instruction.
It declares from which enclosing values a guaranteed phi argument is borrowed from.
2024-04-10 13:38:10 +02:00
Nate Chandler
8ed11e1788 [SIL] Expose alloc_stack flag to Swift sources.
Code in SwiftCompilerSources should be able to create an `alloc_stack`
with the `var_decl` flag.
2024-03-11 21:15:58 -07:00
Andrew Trick
7dea051bf6 Review feedback; Builder.createEndAccess() 2024-03-05 17:08:13 -08:00
Meghana Gupta
14578db701 Bridge createEndAccess 2024-02-13 00:12:23 -08:00
Andrew Trick
79fe8bad30 SwiftCompilerSources: improve MarkDependenceInst. 2024-02-12 09:57:14 -08:00
Andrew Trick
da3f583492 Add mark_dependence [unresolved]
In preparation for inserting mark_dependence instructions for lifetime
dependencies early, immediately after SILGen. That will simplify the
implementation of borrowed arguments.

Marking them unresolved is needed to make OSSA verification
conservative until lifetime dependence diagnostics runs.
2024-02-08 22:53:16 -08:00
Erik Eckstein
a98448b89a swift Builder: add a few instruction creation functions 2024-01-31 17:16:12 +01:00
Erik Eckstein
bc99986cf9 SIL: add a dependency token operand to global_addr
Optionally, the dependency to the initialization of the global can be specified with a dependency token `depends_on <token>`.
This is usually a `builtin "once"` which calls the initializer for the global variable.
2024-01-10 09:33:58 +01:00
Andrew Trick
264cbaea42 Add mark_dependence [nonescaping] flag.
The dependent 'value' may be marked 'nonescaping', which guarantees that the
lifetime dependence is statically enforceable. In this case, the compiler
must be able to follow all values forwarded from the dependent 'value', and
recognize all final (non-forwarded, non-escaping) use points. This implies
that `findPointerEscape` is false. A diagnostic pass checks that the
incoming SIL to verify that these use points are all initially within the
'base' lifetime. Regular 'mark_dependence' semantics ensure that
optimizations cannot violate the lifetime dependence after diagnostics.
2024-01-04 14:47:35 -08:00
Erik Eckstein
e652f2c92e SIL: add the alloc_vector and vector instructions
* `alloc_vector`: allocates an uninitialized vector of elements on the stack or in a statically initialized global
* `vector`: creates an initialized vector in a statically initialized global
2023-12-09 18:49:55 +01:00
Erik Eckstein
0bd6034270 SwiftCompilerSources: add some Builder APIs
* `createAddressToPointer`
* `createLoadBorrow`
2023-12-09 18:48:50 +01:00
Erik Eckstein
bfeb2128cb remove default value for the keepUnique argument of Builder.createEndCOWMutation
This avoids bugs because the default value false is sometimes not the right choice.
NFC
2023-12-06 15:40:08 +01:00
Erik Eckstein
37c715c58c SwiftCompilerSources: add EndLifetimeInst 2023-11-27 09:21:33 +01:00
Erik Eckstein
6f6c0a5608 SwiftCompilerSources: add a few enum related APIs in Type and Builder
* `Type.getEnumCases`
* `Builder.createUncheckedTakeEnumDataAddr`
* `Builder.createSwitchEnumAddr`
2023-11-27 09:21:33 +01:00