Commit Graph

2610 Commits

Author SHA1 Message Date
Slava Pestov
ee440f3c91 AST: Remove MakeAbstractConformanceForGenericType
While the intent behind this functor was noble, it has grown in complexity
considerably over the years, and it seems to be nothing but a source of
crashes in practice. I don't want to deal with it anymore, so I've decided
to just subsume all usages with LookUpConformanceInModule instead.
2025-07-15 16:34:11 -04:00
Meghana Gupta
b8fcd72f4d Disable retain and release sinking when rc root values are ~Copyable 2025-07-10 13:16:50 -07:00
nate-chandler
4c1dbd74c7 Merge pull request #82784 from nate-chandler/rdar153693915
[Mem2Reg] Don't promote proj(unchecked_addr_cast).
2025-07-04 02:02:16 -07:00
Nate Chandler
41877ae7b7 [Mem2Reg] Don't promote proj(unchecked_addr_cast).
In OSSA, the result of an `unchecked_bitwise_cast` must immediately be
copied or `unchecked_bitwise_cast`'d again.  In particular, it is not
permitted to borrow it.  For example, the result can't be borrowed for
the purpose of performinig additional projections (`struct_extract`,
`tuple_extract`) on the borrowed value.  Consequently, we cannot promote
an address if such a promotion would result in such a pattern.  That
means we can't promote an address `%addr` which is used like
`struct_element_addr(unchecked_addr_cast(%addr))` or
`tuple_element_addr(unchecked_addr_cast(%addr))`.  We can still promote
`unchecked_addr_cast(unchecked_addr_cast(%addr))`.

In ownership-lowered SIL, this doesn't apply and we can still promote
address with such projections.

rdar://153693915
2025-07-03 14:52:23 -07:00
nate-chandler
d2f4e84889 Merge pull request #82696 from nate-chandler/rdar154652254
[CSE] Fix combine of type_value.
2025-07-02 15:42:46 -07:00
Nate Chandler
429de09f87 [CSE] Fix combine of type_value.
The instruction has no operands but has a param type.  Hash and compare
the type and the type and the param type.

rdar://154652254
2025-07-01 14:26:21 -07:00
Erik Eckstein
606f7693d4 MandatoryPerformanceOptimizations: don't de-virtualize a generic class method call to specialized method
This results in wrong argument/return calling conventions.
First, the method call must be specialized. Only then the call can be de-virtualized.
Usually, it's done in this order anyway, because the `class_method` instruction is located before the `apply`.
But when inlining functions, the order (in the worklist) can be the other way round.

Fixes a compiler crash.
rdar://154631438
2025-07-01 09:44:47 +02:00
nate-chandler
991eb45432 Merge pull request #82557 from nate-chandler/rdar154407327
[DestroyAddrHoisting] Don't fold into read access.
2025-06-27 15:36:22 -07:00
Nate Chandler
2e2dee7a41 [DestroyAddrHoisting] Don't fold into read access.
Narrowly fix a long-standing bug where destroy_addrs would be hoisted
into read access scopes, leaving the scope as a read despite the fact
that it modifies memory.  This is a problem for utilities which expect
access scopes to provide correct summaries.  Do this by refusing to fold
into access scopes which are marked `[read]`.

In a follow-up, we can reenable this folding, promoting each access
scope to a modify.  Doing so requires checking that there are no access
scopes which overlap with any of the access scopes which would be
promoted to modify.

rdar://154407327
2025-06-26 17:40:03 -07:00
Erik Eckstein
6714a72256 Optimizer: re-implement and improve the AllocBoxToStack pass
This pass replaces `alloc_box` with `alloc_stack` if the box is not escaping.
The original implementation had some limitations. It could not handle cases of local functions which are called multiple times or even recursively, e.g.

```
public func foo() -> Int {
  var i = 1

  func localFunction() { i += 1 }

  localFunction()
  localFunction()
  return i
}

```

The new implementation (done in Swift) fixes this problem with a new algorithm.
It's not only more powerful, but also simpler: the new pass has less than half lines of code than the old pass.

The pass is invoked in the mandatory pipeline and later in the optimizer pipeline.
The new implementation provides a module-pass for the mandatory pipeline (whereas the "regular" pass is a function pass).
This is required because the mandatory pass needs to remove originals of specialized closures, which cannot be done from a function-pass.
In the old implementation this was done with a hack by adding a semantic attribute and deleting the function later in the pipeline.

I still kept the sources of the old pass for being able to bootstrap the compiler without a host compiler.

rdar://142756547
2025-06-20 08:15:04 +02: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
eeckstein
a33ff9879a Merge pull request #81969 from eeckstein/temp-lvalue-elimination
Optimizer: improve TempLValueOpt
2025-06-06 15:30:29 +02:00
Erik Eckstein
2b9b2d243c Optimizer: improve TempLValueOpt
* re-implement the pass in swift
* support alloc_stack liveranges which span over multiple basic blocks
* support `load`-`store` pairs, copying from the alloc_stack (in addition to `copy_addr`)

Those improvements help to reduce temporary stack allocations, especially for InlineArrays.

rdar://151606382
2025-06-05 06:45:18 +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
Nate Chandler
c41fd47f1f [DestroyAddrHoisting] Skip init_enum_data_addrs.
A destroy of an `init_enum_data_addr` is not equivalent to a destroy of
the whole enum's address.  Treat such destroys just like destroys of
`struct_element_addr`s are treated: by bailing out.

rdar://152431332
2025-06-03 15:32:42 -07:00
nate-chandler
dd75d2ed82 Merge pull request #81854 from nate-chandler/rdar152195094
[DestroyAddrHoisting] Don't destructure NE aggs.
2025-05-30 11:32:12 -07:00
Nate Chandler
f7ca26ce93 [NFC] SIL: This utility takes a func not a module.
In preparation to use the function in the implementation.
2025-05-29 15:36:15 -07:00
Arnold Schwaighofer
7ac551636b Merge pull request #81714 from aschwaighofer/se0460
SE-0460: Introduce @specialized attribute
2025-05-28 12:03:48 -07:00
Erik Eckstein
ed8922bc8e PerformanceInliner: always inline synthesized enum comparisons if one of the operands is a constant enum
If there is a "constant" enum argument to a synthesized enum comparison, we can always inline it, because most of it will be constant folded anyway.
This ensures the compiler is not creating terrible code for very simple enum comparisons, like
```
   if someEnum == .someCase {
      ...
   }
```

rdar://85677499
2025-05-27 12:11:03 +02:00
Arnold Schwaighofer
13ff5abdb8 Introduce @specialized attribute
Implements SE-0460 -- the non-underscored version of @specialized.

It allows to specify "internal" (not abi affecting) specializations.

rdar://150033316
2025-05-23 13:12:47 -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
Erik Eckstein
3cbe94d7d1 inliner: tune the heuristic for constructors to be inlined into global initializer functions.
Inlining constructors into global initializers increase the changes that the global can be initialized statically.
2025-05-20 20:46:33 +02: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
Erik Eckstein
c6b1e3e854 TempRValueElimination: re-implement the pass in swift
Beside cleaning up the source code, the motivation for the translation into Swift is to make it easier to improve the pass for some InlineArray specific optimizations (though I'm not sure, yet if we really need those).
Also, the new implementation doesn't contain the optimize-store-into-temp optimization anymore, because this is covered by redundant load elimination.
2025-05-06 13:08:09 +02:00
Kuba (Brecka) Mracek
3f61189657 Merge pull request #80914 from kubamracek/inliner-knobs
[SILOptimizer] Expose SIL inliner heuristics/constants as -Xllvm configurable knobs
2025-04-30 09:37:39 -07:00
Slava Pestov
3b83ac30a1 SILOptimizer: Clean up existential archetype remapping in CSE 2025-04-28 13:47:53 -04:00
nate-chandler
c2ed51ec63 Merge pull request #81059 from nate-chandler/rdar149896608
Revert "[DCE] Verify liveness of completed lifetimes."
2025-04-24 11:31:33 -07:00
Nate Chandler
fdd896e021 Revert "[DCE] Verify liveness of completed lifetimes."
This reverts commit 3ec9b269f5.

rdar://149896608
2025-04-23 16:59:26 -07:00
Evan Wilde
bca1378fdb SILOptimizer: Disable invalid passes in C++-only compiler
The SimplifyCFG and LoopRotate passes result in verification failures
when built in a compiler that is not built with Swift sources enabled.

Fixes: rdar://146357242
2025-04-21 12:46:01 -07:00
Kuba Mracek
9878da9046 [SILOptimizer] Expose SIL inliner heuristics/constants as -Xllvm configurable knobs 2025-04-18 10:03:26 -07:00
nate-chandler
9cc1947527 Merge pull request #80766 from nate-chandler/rdar149007151
[DCE] Don't delete instructions which consume escaping values.
2025-04-12 08:31:12 -07:00
Nate Chandler
3ec9b269f5 [DCE] Verify liveness of completed lifetimes. 2025-04-11 14:58:34 -07:00
Nate Chandler
b405c8b23c [DCE] Keep insts which consume escaping values.
When DCE deletes instructions as dead, if the instruction ends one of
its operands lifetimes, it must insert a compensating lifetime end.
When the def block of the value and the parent block of the instruction
are different, it uses lifetime completion.  Lifetime completion relies
on complete liveness, which doesn't and can't exist for values with
pointer escapes.  The result is ending lifetimes too early.

Avoid this scenario by marking such instructions live.

In the fullness of time, it may be possible to track the deleted
instruction's "location" even in the face of deletions of adjacent
instructions and parent blocks and to insert the lifetime end at that
location.

rdar://149007151
2025-04-11 14:58:33 -07:00
Erik Eckstein
6e17385325 add a semantic attribute "optimize.sil.inline.constant.arguments"
This forces inlining the annotated function if its arguments are constant.
2025-04-01 18:12:45 +02: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
d9dd93560d Support mark_dependence_addr in SIL passes. 2025-03-25 23:02:45 -07:00
Pavel Yaskevich
41c88f864a [SILOptimizer] Turn "is self-recursive" check into analysis
The body of a function has to be re-analyzed for every call
site of the function, which is very expensive and if the
body is not changed would produce the same result.

This takes about ~10% from swift-syntax overall build time
in release configuration.
2025-03-24 00:25:13 -07:00
Amritpan Kaur
39b48bb218 [SIL] Handle KeyPathComponentKind::Method in helpers. 2025-03-19 10:54:09 -07:00
Erik Eckstein
2ce9ddeb46 ConditionForwarding: don't violate ownership
Instructions in a block, which is moved, must not use any (non-trivial) value because we don't do liveness analysis.
When moving a block, there is no guarantee that the operand value is still alive at the new location.

Fixes an ownership violation error
rdar://146630743
2025-03-10 12:48:14 +01:00
Meghana Gupta
5851118f3a Merge pull request #79775 from meg-gupta/fixtemplvalue
Fix TempLValueOpt for init_enum_data_addr
2025-03-05 04:07:10 -08:00
Meghana Gupta
3addadc503 Merge pull request #79766 from meg-gupta/fixmem2reg
Fix mem2reg for load_borrows with reborrows
2025-03-04 18:29:17 -08:00
Meghana Gupta
8438e165fa Fix TempLValueOpt for init_enum_data_addr
TempLValueOpt eliminates copies from a temporary to destination and
supports hoisting projections of the destination.

An enum is fully initialized with the pair init_enum_data_addr and
inject_enum_addr. Calling destroy_addr only before inject_enum_addr
can cause a runtime crash.

This optimization can eliminate copy_addr and hoist init_enum_data_addr
such that enum is not fully initialized before it's use.

Disable this case for now.
Fixes rdar://145941433

To correctly do this optimization we have to find the corresponding inject_enum_addr
and hoist it as well or ensure the source is not used until fully initialized by inject_enum_addr.
2025-03-04 16:23:09 -08:00
Meghana Gupta
4c46faad26 Fix mem2reg for load_borrows with reborrows
StackAllocationPromoter::pruneAllocStackUsage substitutes loads/stores of alloc_stack
with values. For some reason isLoadFromStack was bailing out for load_borrows with
reborrows leaving them to be fixed up by fixBranchesAndUses which uses live in value
from predecessors for substitution which is obviosly incorrect the block containing
the load_borrow has a store before it.

Fixes rdar://145834542
2025-03-04 10:18:08 -08:00
Susana Monteiro
593f320460 Merge pull request #79325 from swiftlang/susmonteiro/copy-constructor-default-args
[cxx-interop] Prevent usage in Swift of C++ copy constructor with default args
2025-03-04 15:40:22 +00:00
Gábor Horváth
c91e295234 Merge pull request #79576 from swiftlang/gaborh/fix-consume-calling-conv
[cxx-interop] Fix calling convention for rvalue reference params
2025-03-04 02:31:49 -08:00
susmonteiro
bc6573ec32 [cxx-interop] Prevent usage in Swift of C++ copy constructor with default args 2025-03-03 13:22:11 +00:00
Gabor Horvath
00fa738209 [cxx-interop] Fix calling convention for rvalue reference params
In C++, we always expected to invoke the dtor for moved-from objects.
This is not the case for swift. Fortunately, @inCxx calling convention
is already expressing that the caller supposed to destroy the object.
This fixes the missing dtor calls when calling C++ functions taking
rvalue references. Fixes #77894.

rdar://140786022
2025-03-03 11:47:12 +00:00
Meghana Gupta
6908e9b776 Annotate some Span and InlineArray's methods with semantics 2025-02-28 09:52:00 -08:00
eeckstein
06042c1cba Merge pull request #79483 from eeckstein/fix-temp-lvalue-opt
TempLValueOpt: avoid creating invalid apply argument aliasing.
2025-02-20 06:55:33 +01:00
eeckstein
ee360e0854 Merge pull request #79484 from eeckstein/fix-simplify-cfg
SimplifyCFG: remove all borrowed-from uses from arguments when merging blocks
2025-02-20 06:55:09 +01:00