Commit Graph

53 Commits

Author SHA1 Message Date
JanBaig
52895fefc3 [SIL] Remove AssignByWrapper references from SIL passes 2025-08-22 23:20:53 -04:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Joe Groff
90e1ecb864 Consistently mark borrowing and consuming parameters for move-only checking when captured.
When a `borrowing` or `consuming` parameter is captured by a closure,
we emit references to the binding within the closure as if it is non-implicitly
copyable, but we didn't mark the bindings inside the closure for move-only
checking to ensure the uses were correct, so improper consumes would go
undiagnosed and lead to assertion failures, compiler crashes, and/or
miscompiles. Fixes rdar://127382105
2024-05-01 13:41:28 -07:00
Nate Chandler
6100a1b495 Allow partial consumption of self in deinit.
Basic support for partial consumption in deinit without full support for
all future uses of drop_deinit.
2024-02-28 15:57:09 -08:00
Michael Gottesman
37d60a08bb [move-only] Rename mark_must_check -> mark_unresolved_non_copyable_value.
I was originally hoping to reuse mark_must_check for multiple types of checkers.
In practice, this is not what happened... so giving it a name specifically to do
with non copyable types makes more sense and makes the code clearer.

Just a pure rename.
2023-08-30 22:29:30 -07:00
Holly Borla
df69020eca [DefiniteInitialization] Lower AssignOrInit instructions to either call the
initializer or the setter closure with the given argument.
2023-06-06 18:59:13 -07:00
Michael Gottesman
59c8cff917 [borrowing] Add support for borrowing/consuming copyable types to be a noimplicitcopy type.
rdar://108383660
2023-06-06 18:12:29 -04:00
Andrew Trick
058c03ec23 Fix an unused variable warning in AccessEnforcementSelection 2023-01-13 08:55:16 -08:00
Andrew Trick
d593623ffb Do not insert dynamic exclusivity checks in closure cycles
Closure cycles were originally enforced "conservatively". Real code
does, however, use recursive local functions. And it's surprisingly
easy to create false exclusivity violations in those cases.

This is safe as long as we can rely on SILGen to keep captured
variables in boxes if the capture escapes in any closure that may call
other closures that capture the same variable.

Fixes https://github.com/apple/swift/issues/61404
Dynamic exclusivity checking bug with nested functions. #61404

Fixes rdar://102056143 (assertion failure due to exclusivity checker -
AccessEnforcementSelection should handle recursive local captures)
2022-12-18 12:32:25 -08:00
Michael Gottesman
467b742a5c [move-only] Update the non-address move checker part of the optimizer to handle mark_must_check on addresses. 2022-09-09 13:38:18 -07:00
Andrew Trick
1c07c50cf8 Fix a minor (1 per process) leak in AccessEnforcementSelection
This fixes a commit from a few days ago where I meant to call
unique_ptr::reset() instead of unique_ptr::release() and
apparently didn't notice the compiler warning.
2022-03-22 11:23:11 -07:00
Andrew Trick
ddf0965d3f Rewrite ClosureScopeAnalysis for generality.
Handle recursive non-escaping local functions.

Previously, it was thought that recursion would force a closure to be
escaping. This is not necessarilly true.

Update AccessEnforcementSelection to conservatively handle closure cycles.

Fixes rdar://88726092 (Compiler hangs when building)
2022-03-18 02:28:53 -07:00
Nate Chandler
ddb93df800 [AES] See through borrows. 2022-01-13 13:33:38 -08:00
Erik Eckstein
fe10f98cf0 SIL: rename the SILBitfield.h header file to BasicBlockBits.h
NFC
2021-02-12 11:15:55 +01:00
Erik Eckstein
f48191966c SILOptimizer: use BasicBlockSet instead of SmallPtrSet in various transformations.
It reduces compile time.
2021-01-27 10:31:17 +01:00
Doug Gregor
c02ecf9859 [SE-0258] Rename to Property Wrappers 2019-05-29 22:17:50 -07:00
Andrew Trick
4e00cad71e Add a comment in ClosureScopeAnalysis.
As a follow-up to reviewing the dynamically replaceable
implementation, document the place where this analysis will likely
crash in the future once we start optimizing non-escaping closure
captures.
2019-05-20 17:12:13 -07:00
Erik Eckstein
24e28caf3d Fix an assert in AccessEnformentSelections to support assign_by_delegate 2019-04-23 11:32:28 -07:00
Arnold Schwaighofer
13498c46bd Fix exclusivity accesss enforcement for partial_apply [stack] 2019-01-15 11:32:10 -08:00
Michael Gottesman
3cd1b7bedc [sil] Extract out ApplySite/FullApplySite into their own header.
I believe that these were in SILInstruction for historic reasons. This is a
separate API on top of SILInstruction so it makes sense to pull it out into its
own header.
2018-09-25 13:32:59 -07:00
Andrew Trick
d21f4021bb Enforce exclusivity dynamically in more situations.
A directly applied noescape closure that captures a local requires
dynamic enforcement if that local escaped prior to the direct
application. Previously this was only statically enforced, which could
miss conflicts.

It's unlikely that an actual conflict could occur, but can happen in
cases like this:

func noescapeConflict() {
  var localVal = 0
  let nestedModify = { localVal = 3 }
  _ = {
    takeInoutAndPerform(&localVal, closure: nestedModify)
  }()
}

Code isn't normally written like this, but the general pattern of
passing an escaping closure as an argument to a noescape closure may
arise when using withoutActuallyEscaping. Even in that case, it's
highly unlikely that a real conflict would exist.

For this case to be handled correcly, we must also never allow a
closure passed to withoutActuallyEscaping to be SILGen's as a
non-escaping closure. Doing so would result in a non-escaping closure
to be passed as an argument to the withoutActuallyEscaping trailing
closure, which is also non-escaping. That directly violates the
Non-Escaping Recursion Restriction, but there would be no way to
diagnose the violation, creating a hole in the exclusivity model.
2018-08-17 18:21:20 -07:00
Bob Wilson
8e330ee344 NFC: Fix indentation around the newly renamed LLVM_DEBUG macro.
Jordan used a sed command to rename DEBUG to LLVM_DEBUG. That caused some
lines to wrap and messed up indentiation for multi-line arguments.
2018-07-21 00:56:18 -07:00
Jordan Rose
cefb0b62ba Replace old DEBUG macro with new LLVM_DEBUG
...using a sed command provided by Vedant:

$ find . -name \*.cpp -print -exec sed -i "" -E "s/ DEBUG\(/ LLVM_DEBUG(/g" {} \;
2018-07-20 14:37:26 -07:00
Ben Cohen
2b04e9f105 Suppress a number of warnings in no-assert builds (#17721)
* Supress a number of warnings about things used only in asserts

* Re-use a couple of variables instead of supressing the warning
2018-07-04 07:15:14 -07:00
Andrew Trick
d96306fbad Don't run AccessEnforcementSelection on deserialized SIL and add verification.
This is a module pass, so was processing a combination of raw and deserialized
canonical SIL. The analysis makes structural assumptions that only hold prior to
mandatory inlining.

Add more verification of the structural properties.

Teach the pass to skip over canonical SIL, which only works because closures
must be serialized/deserialized in the same module as their parent.

Fixes <rdar://38042781> [Repl] crash while running SILOptimizer
2018-03-02 15:49:01 -08:00
Andrew Trick
5921c3fb11 Reenable stripping of access markers at -O.
Until other bugs are fixed, this is still required in the short term after
mandatory inlining of debug libraries into an optimized executable.

Also, enable rerunning access marker elimination on deserialized functions,
because theoretically, the same problem could occur if we emit an external
function without inlining it.
2018-02-26 16:38:56 -08:00
Arnold Schwaighofer
a9fa8c9c2b Merge pull request #14514 from aschwaighofer/wip_closure_capture_abi_part2
WIP: Closure ABI - Make @noescape Swift closures trivial
2018-02-14 05:19:25 -08:00
Arnold Schwaighofer
1d27a040b3 Merge pull request #14526 from aschwaighofer/indirect_out_accessenforcementselection
AccessEnforcementSelection: An @out parameter can be used
2018-02-13 06:03:52 -08:00
Arnold Schwaighofer
ba521d80b3 AccessEnforcementSelect: Handle escape to noescape conversions 2018-02-13 04:19:59 -08:00
Arnold Schwaighofer
5387bd1732 AccessEnforcementSelection: An @out parameter can be used 2018-02-10 09:29:20 -08:00
Andrew Trick
0dfce6c0f6 Don't rerun mandatory lowering passes on deserialized SIL. 2018-02-09 09:55:47 -08:00
Andrew Trick
7ceb43fda9 Add support to AccessEnforcementSelection for borrowed SILValues.
Previously, this asserted on unexpected situations that the
optimizer couldn't handle.

It makes more sense now to handle these cases conservatively since we can't
catch them in early testing.

Fixes <rdar://problem/35402799> [4.1] Assertion failed: (user->getResults().empty())
2017-11-13 17:05:20 -08:00
Andrew Trick
d369aa4070 Support @noescape SIL function types. (#12420)
Support for @noescape SILFunctionTypes.

These are the underlying SIL changes necessary to implement the new
closure capture ABI.

Note: This includes a change to function name mangling that
primarily affects reabstraction thunks.

The new ABI will allow stack allocation of non-escaping closures as a
simple optimization.

The new ABI, and the stack allocation optimization, also require
closure context to be @guaranteed. That will be implemented as the
next step.

Many SIL passes pattern match partial_apply sequences. These all
needed to be fixed to handle the convert_function that SILGen now
emits. The conversion is now needed whenever a function declaration,
which has an escaping type, is passed into a @NoEscape argument.

In addition to supporting new SIL patterns, some optimizations like
inlining and SIL combine are now stronger which could perturb some
benchmark results.

These underlying SIL changes should be merged now to avoid conflicting
with other work. Minor benchmark discrepancies can be investigated as part of
the stack-allocation work.

* Add a noescape attribute to SILFunctionType.

And set this attribute correctly when lowering formal function types to SILFunctionTypes based on @escaping.

This will allow stack allocation of closures, and unblock a related ABI change.

* Flip the polarity on @noescape on SILFunctionType and clarify that
we don't default it.

* Emit withoutActuallyEscaping using a convert_function instruction.

It might be better to use a specialized instruction here, but I'll leave that up to Andy.

Andy: And I'll leave that to Arnold who is implementing SIL support for guaranteed ownership of thick function types.

* Fix SILGen and SIL Parsing.

* Fix the LoadableByAddress pass.

* Fix ClosureSpecializer.

* Fix performance inliner constant propagation.

* Fix the PartialApplyCombiner.

* Adjust SILFunctionType for thunks.

* Add mangling for @noescape/@escaping.

* Fix test cases for @noescape attribute, mangling, convert_function, etc.

* Fix exclusivity test cases.

* Fix AccessEnforcement.

* Fix SILCombine of convert_function -> apply.

* Fix ObjC bridging thunks.

* Various MandatoryInlining fixes.

* Fix SILCombine optimizeApplyOfConvertFunction.

* Fix more test cases after merging (again).

* Fix ClosureSpecializer. Hande convert_function cloning.

Be conservative when combining convert_function. Most of our code doesn't know
how to deal with function type mismatches yet.

* Fix MandatoryInlining.

Be conservative with function conversion. The inliner does not yet know how to
cast arguments or convert between throwing forms.

* Fix PartialApplyCombiner.
2017-10-17 13:07:25 -07:00
John McCall
ab3f77baf2 Make SILInstruction no longer a subclass of ValueBase and
introduce a common superclass, SILNode.

This is in preparation for allowing instructions to have multiple
results.  It is also a somewhat more elegant representation for
instructions that have zero results.  Instructions that are known
to have exactly one result inherit from a class, SingleValueInstruction,
that subclasses both ValueBase and SILInstruction.  Some care must be
taken when working with SILNode pointers and testing for equality;
please see the comment on SILNode for more information.

A number of SIL passes needed to be updated in order to handle this
new distinction between SIL values and SIL instructions.

Note that the SIL parser is now stricter about not trying to assign
a result value from an instruction (like 'return' or 'strong_retain')
that does not produce any.
2017-09-25 02:06:26 -04:00
Slava Pestov
5973a2722c SILOptimizer: Fix some unused variable warnings 2017-09-01 02:04:22 -07:00
Andrew Trick
ac56f91f47 Add a little tiny comment to AccessEnforcementSelection. 2017-07-05 14:10:05 -07:00
Andrew Trick
c2c55eea12 AccessSummaryAnalysis: handle @convention(block) in nested nonescape closures.
This analysis has a whitelist to ensure that we aren't missing any SIL
patterns and failing to enforce some cases.

There is a special case involving nested non-escaping closures being passed as a
block argument.  Whitelist this very special case even though we don't enforce
it because the corresponding diagnostics pass also doesn't enforce it.
2017-06-21 19:39:12 -07:00
Andrew Trick
4575525786 NFC: Cleanup ClosureScope/AccessEnforcementSelection/Tests.
Per Devin and John's review.
2017-06-20 14:57:56 -07:00
Andrew Trick
fcfabfd5d2 Implement and enable dynamic exclusivity checks for noescape closures.
This uses the new ClosureScopeAnalysis to process parent functions before the
closures they reference. The analysis now tracks captured variables in addition
to immediately accessed variables. If the variable escapes before it's capture
is applied, then that closure uses dynamic enforcement for the variable.

<rdar://problem/32061282> [Exclusivity] Enforcement selection for noescape closure captures.
2017-06-20 00:01:52 -07:00
Andrew Trick
50409b3b1a Make AccessEnforcementSelection a module pass.
Parent scopes need to be analyzed before enforcement in noescape closures can be
selected.
2017-06-16 19:09:24 -07:00
Andrew Trick
c76269ccd9 Comment typos. 2017-05-10 16:37:59 -07:00
Andrew Trick
dd31c40995 [Exclusivity] Disable dynamic enforcement in noescape closures. 2017-05-08 17:27:26 -07:00
Devin Coughlin
de9c646804 [Exclusivity] Use dynamic enforcement on boxes whose projections escape
In AccessEnforcementSelection, treat passing a projection from a box to
a partial_apply as an escape to force using dynamic enforcement on the box.

This will now correctly use dynamic enforcement on variables that are taken
as inout and also captured by storage address in a closure:

  var x = ...

  x.mutatingMethod { ... use x ...}

but does pessimize some of our existing enforcement into dynamic since
access enforcement selection.

Ideally we would distinguish between escaping via an nonescaping closures
(which can only conflict with accesses that are in progress) and
escaping via escaping closures (which can conflict for any reachable code
after the escape)
2017-05-02 14:54:12 -07:00
Andrew Trick
2336a87866 [Exclusivity] Enable access markers for the entire -Onone pipeline.
Dynamic markers are still conditional on the command line option.
2017-04-28 21:33:09 -07:00
Andrew Trick
f469fb73db [Exclusivity] Allow AccessEnforcementSelection to run before DI. 2017-04-28 21:33:09 -07:00
Andrew Trick
b872ae80c1 [Exclusivity] AccessMarkerEnforcementSelection support for unpaired markers. 2017-04-26 09:02:34 -07:00
Andrew Trick
39cd8126a6 [Exclusivity] Reenable AccessEnforcementSelection assert. 2017-04-25 15:58:11 -07:00
Andrew Trick
e461f4809c [Exclusivity] Temporarily disable an assert in AccessEnforcementSelector.
This pass doesn't do anything useful downstream yet, so it's safe to temporarily
disable the assert.

Reenable after fixing:
<rdar://problem/31797132> [Exclusivity] lldb asserts in AccessEnforcementSelector
2017-04-24 14:46:41 -07:00
practicalswift
7eb7d5b109 [gardening] Fix 100 typos. 2017-04-18 17:01:42 +02:00
Andrew Trick
3fe16ac03c [Exclusivity] Fix AccessEnforcementSelection to handle unreachable blocks. 2017-04-16 22:46:04 -07:00