Commit Graph

685 Commits

Author SHA1 Message Date
Erik Eckstein 4a8444ad42 add Builtin.gepProjection
It's like Builtin.gep, but marks the resulting `index_addr` as a projection, meaning the result can only reach the single element at the given index and cannot be used for general pointer arithmetic by chaining `index_addr` instructions.
2026-06-09 16:17:53 +02:00
Meghana Gupta 2d53a89dcb Merge pull request #89760 from meg-gupta/deref
Introduce Builtin.dereferenceable
2026-06-09 09:08:54 +05:30
Meghana Gupta e56942b533 Introduce Builtin.dereferenceable
This PR introduces a new Swift builtin `Builtin.dereferenceable` that
allows the compiler to provide dereferenceable memory hints to LLVM for
optimization purposes.

Resolves rdar://174255636
2026-06-08 12:06:23 +05:30
Anthony Latsis 09416428df Merge pull request #89274 from swiftlang/jepa-main5
AST: Adjust to renamed `llvm::Intrinsic::IITDescriptor` fields
2026-05-22 20:42:52 +01:00
Konrad `ktoso` Malawski ab31d297a6 [Concurrency] Remove builtin cancelAsyncTask dead code (#89342)
AFAIR we were using the "enter task cancel cleanup" in async let to
cancel tasks, however this moved to enterAsyncLetCleanup. There is no
uses of the cancel cleanup anymore.

The Task.cancel was using the builtin, but the UnsafeCurrentTask.cancel
was using the normal rutime func, which the builtin would just end up
calling anyway.

I cna't see a reason to keep the builtin path, so let's remove this dead
code.
2026-05-21 23:01:26 -07:00
Anthony Latsis e5cf5c8bb1 AST: Adjust to renamed llvm::Intrinsic::IITDescriptor fields
Paired with https://github.com/swiftlang/llvm-project/pull/13016.
2026-05-20 01:44:10 +01:00
Konrad `ktoso` Malawski 689d85ffaa [Concurrency] New non-copyable Continuation type (#88182)
**Explanation**: 
Implementation of `Continuation` as proposed in upcoming SE proposal:
https://github.com/swiftlang/swift-evolution/pull/3246

This is a ~Copyable Continuation with much stronger safety guarantees
and no runtime overhead. Refer to proposal for details.

Initial work by @fabianfett.

**Scope**: Adds a new continuation type.

**Risk**: Low, adds new type, allows `~Copyable` into existing builtin
-- I believe this change should be safe (?)

**Testing**: Added lots of tests covering problems this aims to prevent.

**Issues**: 
resolves rdar://174826360
Somewhat relevant to rdar://139975911

Co-authored-by: Fabian Fett <fabianfett@apple.com>
2026-05-13 19:30:57 +09:00
Kavon Farvardin 21b2ebff12 Sema: infer inverses in opaque return types
Opaque return types are special type declarations that have it
own nested generic signature. Thus, given this:
```
  protocol P<A> { associatedtype A: ~Copyable }
  func f<T: ~Copyable>() -> some P<T> {}
```
The generic signature for f is <T where T Escapable>, and
for the opaque return type, its nested signature ends up as
```
  <X where X: P, X.A == T>
```
With SE-503, we will now also expand a default for the suppressed
primary associated type, so the signature after expansion becomes
```
  <X where X: P, X.A == T, X.A: Copyable>
```
It would be smarter to effectively have this rule
```
  X.A == T, T: ~Copyable
  ----------------------
     X.A: ~Copyable
```
where we infer the inverse on X.A to cancel-out the
expanded default X.A: Copyable. We already do this for
two in-scope type parameters, and it would be better if
we did it if one side was out-of-scope, but that would
be source-breaking to do in general.

In the case of opaque return types, the fact that
it has a nested generic signature seems more an
artifact of the implementation. There also is little
risk of source break, as the only kinds of same-type
requirements that can appear are from parameterized
protocol type.

The experimental suppressed associated types prior to
SE-503 wouldn't be broken by this change, as they do
not infer defaults that need suppression, and we only
filter-out requirements from defaults expansion, rather
than explicitly-written ones.

rdar://175500824
2026-05-04 14:49:34 -07:00
Kavon Farvardin 6651cb6389 NFC: refactor the applyInverses bool
There's a need for more control over how default requirements
for conformance to Copyable/Escapable are expanded, and
subsequently how inverses are applied or inferred to cancel-out
those defaults.

The pattern of `/*applyInverses*/BOOL` is insufficient, so this
is a refactoring to grow that into a proper type that carries
an option that can be used in some future scenario about inferring
inverses for opaque return types.
2026-04-29 16:56:00 -07:00
Andrew Trick 9230fbecdf [Builtin] Add Builtin.borrowAt for borrowing in-memory values
This eliminates the need for unsafe addressors. It will replace unsafeAddress in
the UnsafePointer subscript and the Span subscript.

This is needed to support Span<~E>.

rdar://175382153 (Add Builtin.borrowAt: allows borrowing in-memory values)
2026-04-23 10:47:43 -07:00
Anton Korobeynikov f771a6e2ea Synthesize builtin generic parameter names when there are more than 6 generic parameters (#87201)
First 6 got pre-defined names.

Fixes #87091
2026-04-09 19:04:40 -07:00
Konrad `ktoso` Malawski 07e7a4e17b [Distributed] Try to fail more gracefully if Actor type does not exist (#88117)
Might help with rdar://173338459 to find underlying reason rather than
crash
2026-03-26 04:56:24 -07:00
John McCall 0b6a4e21ff Add properly paired builtins for pushing and popping task-local values
I can't quite enable modeling their stack effects in this patch because
SILGen generates improperly-nested allocations; that'll be fixed in a
follow-up.
2026-03-20 00:21:40 -04:00
Slava Pestov ca72d2d276 Sema: Remove unused TypeOperation cases 2026-03-17 13:41:19 -04:00
Erik Eckstein 0ae20ed07b SILGen: add Builtin.markDependence
It directly translates to the `mark_dependence` SIL instruction
2026-02-25 21:47:30 +01:00
Konrad `ktoso` Malawski 68c8641321 Task Cancellation Shields (#85637)
This is a follow up from the "async" `deinit` work, which will allow us
to guarantee cleanup code to run in deinitializers, even if they need to
call asynchronous code, and even if they may be run in a task that was
cancelled: by "shielding" it from cancellation.

This is incomplete, the child handling needs some more love.

SE proposal: https://github.com/swiftlang/swift-evolution/pull/3037/
2026-02-16 09:35:27 +09:00
Joe Groff 2f02c7cda3 SIL: Introduce instructions for forming and dereferencing Builtin.Borrow.
Since after address lowering, `Borrow` can remain loadable with a known-
layout address-only referent, we need instructions that handle three
forms:

- borrow and referent are both loadable values
- borrow is a value, but referent is address-only
- borrow and referent are both address-only
2026-01-23 08:02:01 -08:00
Joe Groff bc166d5a8c Add a Builtin.Borrow type.
This will represent the layout of a borrow of a value.
2026-01-23 07:46:50 -08:00
Erik Eckstein e3e8c24201 SIL: add the infinite_loop_true_condition builtin
The value of the builtin is true, but the compiler doesn't know.
This builtin is used to break infinite loops.
2026-01-22 17:41:23 +01:00
Anton Korobeynikov 2ccfb5c946 [AutoDiff] Add throwing versions of differential operators (#86393) 2026-01-15 11:52:44 -08:00
Michael Gottesman a302d4e627 Merge pull request #85224 from gottesmm/pr-6cb303a9f5f15489fa44c26d00c70155a6d7cc97
[concurrency] Create builtins for invoking specific concurrency runtime functions.
2025-11-06 20:33:16 -08:00
Anton Korobeynikov 89a7663f1d [AutoDiff] Initial support for differentiation of throwing functions (#82653)
This adds initial support for differentiation of functions that may produce `Error` result. 

Essentially we wrap the pullback into `Optional` and emit a diamond-shape control flow pattern depending on whether the pullback value is available or not. VJP emission was modified to accommodate for this. In addition to this, some additional tricks are required as `try_apply` result is not available in the instruction parent block, it is available in normal successor basic block.

As a result we can now:
- differentiate an active `try_apply` result (that would be produced from `do ... try .. catch` constructions)
- `try_apply` when error result is unreachable (usually `try!` and similar source code constructs)
- Support (some) throwing functions with builtin differentiation operators. stdlib change will follow. Though we cannot support typed throws here (yet)
- Correctly propagate error types during currying around differentiable functions as well as type-checking for `@derivative(of:)` attribute, so we can register custom derivatives for functions producing error result
- Added custom derivative for `Optional.??` operator (note that support here is not yet complete as we cannot differentiate through autoclosures, so `x ?? y` works only if `y` is not active, e.g. a constant value).

Some fixes here and there
2025-11-06 13:12:43 -08:00
Michael Gottesman 97b0e2ebae [concurrency] Create builtins for invoking specific concurrency runtime functions.
Specifically:

1. swift_task_addCancellationHandler
2. swift_task_removeCancellationHandler
3. swift_task_addPriorityEscalationHandler
4. swift_task_removePriorityEscalationHandler
5. swift_task_localValuePush
6. swift_task_localValuePop

rdar://109850951
2025-11-05 11:03:44 -08:00
Michael Gottesman 5cb2409ba6 [sil] In SILGenBuiltin a constexpr StringLiteral to reference names of Builtins instead of using raw c strings.
Before this patch we referred to builtin names in SILGenBuiltin using raw c
strings. This can lead to potential spelling mistakes yielding bugs. Rather than
doing this, I stole a technique that we use in other parts of the compiler:
constexpr StringLiteral generation using CPP macros. Specifically, I defined in
Builtins.h a new namespace called BuiltinNames and inside of BuiltinNames I used
CPP macros to define a StringLiteral for each Builtin. Thus one can get the
appropriate name for a Builtin by writing:

```
BuiltinNames::Sizeof
```

instead of writing "Sizeof". I also cleaned up the code a little by adding for
functions that take identifiers an additional overload that takes a StringRef
and converts the StringRef to an identifier internally. This just eliminates
unnecessary code from call sites by moving them into the callee.
2025-11-05 11:02:44 -08:00
John McCall a7d7970e29 Turn finishAsyncLet into a builtin.
This is necessary because we need to model its stack-allocation
behavior, although I'm not yet doing that in this patch because
StackNesting first needs to be taught to not try to move the
deallocation.

I'm not convinced that `async let` *should* be doing a stack allocation,
but it undoubtedly *is* doing a stack allocation, and until we have an
alternative to that, we will need to model it properly.
2025-11-03 16:33:40 -08:00
John McCall cd67912a50 Remove the deprecated and unused endAsyncLet builtin. 2025-11-03 13:45:18 -08:00
John McCall 1d95fe14de Remove the deprecated and unused startAsyncLet builtin 2025-11-03 13:44:58 -08:00
Joe Groff cf5f7c8637 Add a BuiltinGenericType base class.
Builtin.FixedArray was introduced as the first generic builtin type, with
special case handling in all the various recursive visitors. Introduce
a base class, and move the handling to that base class, so it is easier
to introduce other generic builtins in the future.
2025-10-28 12:48:18 -07:00
Michael Gottesman 2fa3908e94 [concurrency] Add a new type Builtin.ImplicitActor.
This is currently not wired up to anything. I am going to wire it up in
subsequent commits.

The reason why we are introducing this new Builtin type is to represent that we
are going to start stealing bits from the protocol witness table pointer of the
Optional<any Actor> that this type is bitwise compatible with. The type will
ensure that this value is only used in places where we know that it will be
properly masked out giving us certainty that this value will not be used in any
manner without it first being bit cleared and transformed back to Optional<any
Actor>.
2025-10-16 10:51:13 -07:00
Hamish Knight 73710e3eef [AST] Introduce Decl::addAttribute
Introduce a convenience entrypoint that also calls `attachToDecl` on
the attribute, and migrate all existing uses of `getAttrs().add` onto
it.
2025-10-16 11:21:54 +01:00
Andrew Trick df42301b80 Allow Builtin.Load -> ~Escapable 2025-10-05 20:17:55 -07:00
Andrew Trick c494124d11 Allow Builtin.Take -> ~Escapable 2025-10-05 20:17:47 -07:00
swift-ci 94f8b0d0e3 Merge remote-tracking branch 'origin/main' into rebranch 2025-08-25 17:03:36 -07:00
Michael Gottesman 05536d7183 [ast] Convert swift::getBuiltinType to use a covered switch for BuiltinTypeKind.
This is an NFC change. The idea is that this will make it easier to add new
BuiltinTypeKinds. I missed this code when adding Builtin.ImplicitIsolationActor.
By adding a covered switch, we can make sure this code is updated in the
future.
2025-08-21 12:36:01 -07:00
swift-ci ece9e9c497 Merge remote-tracking branch 'origin/main' into rebranch 2025-07-10 00:38:50 -07:00
Slava Pestov aa87afd2cb AST: Fix printing of BuiltinFixedArrayType 2025-07-09 22:16:14 -04:00
Anthony Latsis 5b5f50ebdc IntrinsicInfo: Adjust call to llvm::Intrinsic::getAttributes after LLVM change
See https://github.com/llvm/llvm-project/pull/135642
2025-06-25 14:54:15 +01:00
Anthony Latsis b8b60f159a AST: Adjust for renamed/removed llvm::Intrinsic::IITDescriptor cases
See https://github.com/llvm/llvm-project/pull/141492
2025-06-13 21:57:36 +01:00
swift-ci 2e97515e4d Merge remote-tracking branch 'origin/main' into rebranch 2025-06-12 09:17:50 -07:00
Stephen Canon 9259c3eec4 Add new interleave and deinterleave builtins (#81689)
Ideally we'd be able to use the llvm interleave2 and deinterleave2
intrinsics instead of adding these, but deinterleave currently isn't
available from Swift, and even if you hack that in, the codegen from
LLVM is worse than what shufflevector produces for both x86 and arm. So
in the medium-term we'll use these builtins, and hope to remove them in
favor of [de]interleave2 at some future point.
2025-06-12 12:01:53 -04:00
swift-ci f56bccdc2c Merge remote-tracking branch 'origin/main' into rebranch 2025-05-20 21:37:55 -07:00
Erik Eckstein 9052652651 add the prepareInitialization builtin.
It is like `zeroInitializer`, but does not actually initialize the memory.
It only indicates to mandatory passes that the memory is going to be initialized.
2025-05-20 20:46:33 +02:00
swift-ci 8ec33f9344 Merge remote-tracking branch 'origin/main' into rebranch 2025-05-20 10:39:13 -07:00
Stephen Canon 3aa7b592b6 Implement Builtin.select binding llvm select instruction (#81598)
Not used (yet), but needed to implement SIMD.replacing(with:where:) idiomatically, and probably useful otherwise.
2025-05-20 13:30:59 -04:00
Anthony Latsis 81d194abaf AST: Conservatively handle new llvm::Intrinsic::IITDescriptor::IITDescriptorKind enum cases
Per 5a1e16f6de26c21cdfae1de05bd075d57029a3e1 (llvm-project).
2025-04-24 01:52:46 +01:00
Anthony Latsis 661d087b28 AST: Refactor use of obsoleted llvm::Intrinsic::lookupLLVMIntrinsicByName
Per f0297ae552e1e5aacafc1ed43968041994dc8a6e (llvm-project).
2025-04-24 01:52:46 +01:00
Karoy Lorentey 0cb4b1401e [AST] Generalize is_same_metatype builtin to take unconstrained metatypes
The builtin’s current is signature is:

```
(Any.Type, Any.Type) -> Bool
```

This needs to be changed to this:

```
(any (~Copyable & ~Escapable).Type, any (~Copyable & ~Escapable).Type) -> Bool
```

This requires a bit of support work in AST synthesis.

rdar://145707064

Co-authored-by: Alejandro Alonso <alejandro_alonso@apple.com>
2025-03-24 12:16:11 -07:00
Konrad `ktoso` Malawski fda7f539fb Reapply "Task names" (#79562) (#79600) 2025-03-08 10:58:49 +09:00
Konrad `ktoso` Malawski 09003d6f11 Revert "Merge pull request #77609 from ktoso/wip-task-names" (#79562)
This reverts commit 4ab5d2604f.
2025-02-23 22:59:21 -08:00
Konrad `ktoso` Malawski 4ab5d2604f Merge pull request #77609 from ktoso/wip-task-names
[Concurrency] Task names
2025-02-21 22:28:33 +09:00