Commit Graph

2593 Commits

Author SHA1 Message Date
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
Meghana Gupta
4561658799 Avoid creating unoptimizable copies in CSE
CSE uses OSSA rauw which creates copies and copies that are created to optimize
across borrow scopes are unoptimizable. This PR avoids this situation for now.
2025-02-19 11:20:43 -08:00
Erik Eckstein
5ef3b96da2 SimplifyCFG: remove all borrowed-from uses from arguments when merging blocks
So far we only considered the forwarding use. But there may be other uses.

Fixes a compiler crash.
rdar://145091197
2025-02-19 13:25:48 +01:00
Erik Eckstein
f0b7bdb382 TempLValueOpt: avoid creating invalid apply argument aliasing.
An indirect argument (except `@inout_aliasable`) must not alias with another indirect argument.
Now, if we would replace tempAddr with destAddr in
```
  apply %f(%tempAddr, %destAddr) : (@in T) -> @out T
```
we would invalidate this rule.
This is even true if the called function does not read from destAddr.

Fixes a SIL verification error.
rdar://145090659
2025-02-19 10:33:18 +01:00
Meghana Gupta
cf22bced31 Merge pull request #79315 from meg-gupta/typevaluecse
Add CSE support for type_value instruction and add a few bounds check tests for InlineArray
2025-02-13 20:54:26 -08:00
Meghana Gupta
a31a603e49 Handle type_value instruction in CSE 2025-02-13 10:30:25 -08:00
Erik Eckstein
af5ac6d283 Re-instate the allocVector builtin
Although it's not used anymore we still have to support it to be able to read old Swift.interface files which still contain the builtin.

rdar://144781646
2025-02-13 17:55:27 +01:00
Erik Eckstein
6af5876f72 PerformanceInliner: add the @_semantics("optimize.sil.inline.aggressive") attribute to enable inlining into large functions.
This attribute overrides the limit of maximum number of basic blocks in the caller.
2025-02-13 07:40:24 +01:00
Erik Eckstein
3136218a64 PerformanceInliner: allow inlining of small functions even if the caller block limit is exceeded
This can fix performance problems in large functions.

rdar://141320229
2025-02-13 07:30:44 +01:00
Erik Eckstein
6407f9a0bd remove the allocVector builtin
It's not needed anymore, because the "FixedArray" experimental feature is replaced by inline-arrays.
2025-02-12 10:51:14 +01:00
eeckstein
aa0833b0b3 Merge pull request #79110 from eeckstein/verify_mark_dependence
SIL: Fix memory behavior of mark_dependence
2025-02-11 06:39:43 +01:00
Erik Eckstein
d918b316c9 TempRValueOptimization: don't optimize copies to mark_dependence base values.
We want to keep the original lifetime of the base. If we would eliminate the base alloc_stack, we risk to insert a destroy_addr too early.
2025-02-10 17:57:47 +01:00
Arnold Schwaighofer
7a251af60c AccessEnforcement: Fix analysis to include mayReleases as potentially
executing unknown code

This means we have to claw back some performance by recognizing harmless
releases.

Such as releases on types we known don't call a deinit with unknown
side-effects.

rdar://143497196
rdar://143141695
2025-02-07 15:10:13 -08:00
Anthony Latsis
a84dfc8387 [Gardening] Fix some set but not used variables 2025-01-30 21:34:38 +00:00
nate-chandler
8c63134570 Merge pull request #78682 from nate-chandler/rdar142636711_2
[TempRValueOpt] Invalidate insts when completing.
2025-01-27 12:53:57 -08:00
Meghana Gupta
01d4f11cf5 Fix condition forwarding for switch_enum with default in ossa
In ossa, switch_enum's destination accepts an argument. Fixup
condition forwarding to correctly forward the enum in this case.

Fixes rdar://143042093
2025-01-17 11:01:12 -08:00
Nate Chandler
8fb0fd2ba4 [TempRValueOpt] Invalidate insts when completing. 2025-01-16 08:18:30 -08:00
Nate Chandler
bf1e5dd308 [NFC] DestroyAddrHoisting: Add subpass bailouts. 2025-01-16 08:18:29 -08:00