Commit Graph

6583 Commits

Author SHA1 Message Date
nate-chandler
f5e8fc2c37 Merge pull request #64448 from nate-chandler/eliminate-more-moves
[OwnershipUtils] Classify moves from limited-use values as redundant.
2023-03-20 11:31:51 -07:00
Michael Gottesman
ecdd9bc605 [gardening] Small cleanup of SILDebugInfoExpression. 2023-03-20 11:00:37 -07:00
John McCall
2e4ad65889 [NFC] Change forEachFunctionParam to only ignore the final orig parameter
and not also drop a subst parameter.

This turned out to be more convenient for certain clients (e.g. SILGenPoly)
than requiring the full subst param list to be passed in.  These clients
want to process the subst param list separately, and dropping self early
can be convenient for that.  The only fundamental reason we need this
flag is for working with the orig type, so just use it for that; clients
that need to use this feature can reasonably be expected to cooperate.
2023-03-20 13:36:40 -04:00
John McCall
2172c0ae52 [NFC] Provide a more generator-like interface for traversing orig+subst function parameters
This is necessary because the use patterns in SILGenPoly require
walking two orig+subst sequences in parallel, which poses problems
for a callback-centric design like the one I addded before.  An
inversion of control is necessary; this is basically a manual
coroutine.  But frankly it's a nicer interface than the callback
design, too; I switched the implementation of forEachFunctionParam
to use the generator just to avoid code duplication, but I might
try to remove it and switch all the clients over to the generator.
The main problems with the callback design are that (1) I wasn't
sure which values clients would want, and the result is that there
are a *lot* of parameters and (2) (relatedly) the types of those
parameters have to be written out explicitly, and some of them
are quite large.  The generator code is just much nicer.  Maybe
I can still give the generator a little unparameterized callback
to keep lexical loops simple.

I'll need to do this same thing for tuples.  One at a time.
2023-03-20 13:36:40 -04:00
John McCall
79cbb2eedf [NFC] Add a convenience function to drill through SIL pack types 2023-03-20 13:36:40 -04:00
Michael Gottesman
ee451f0862 [move-only] If we have a noncopyable type on an alloc_stack/alloc_box/debug_value, set the [moveable_value_debuginfo] flag.
This still does not have the complete behavior that we want since we are not yet
inserting the debug_value undef to invalidate after performing moves.

NOTE: In printed SIL, I decided to make the flag implicit if we have a
noncopyable type. This is just to reduce the bloat in SIL. If one needs this
property for a copyable type though, it is mandatory.
2023-03-19 16:05:51 -07:00
Michael Gottesman
8d12f893c2 [sil] Add a moveable_value_debuginfo field to AllocBoxInst.
Just getting parsing/serialization to work. I haven't wired it up to anything.
2023-03-19 15:50:29 -07:00
Michael Gottesman
1a76f8f271 [sil] Move alloc_box flags to SharedUInt8. 2023-03-19 15:39:01 -07:00
Michael Gottesman
4a309575d7 [sil] Rename [moved] flag on debug_value/alloc_stack to moveable_value_debuginfo.
This is in preparation for wiring up debug info support for noncopyable
values. Originally this flag name made sense since it was set when we performed
consume operator checking. Now I am going to use it for noncopyable types as
well. I think the new name uses_moveable_value_debuginfo actually describes what
the flag is supposed to do, tell IRGen that the value may be moved since it
needs to use moveable value debug info emission.
2023-03-19 15:38:42 -07:00
John McCall
999dbecf25 Merge pull request #64464 from rjmccall/pack-argument-reabstraction
A myriad of fixes, including the reabstraction of pack argument values
2023-03-18 04:22:32 -04:00
John McCall
3342d67161 Fix the creation of substituted abstraction patterns for expansions 2023-03-17 22:07:10 -04:00
Nate Chandler
6f5114ef68 [OwnershipUtils] Classify more moves as redundant.
Moves from limited use values are redundant.  When a move separates a
non-escaping lifetime from an escaping lifetime, it is still redundant
if the original lifetime couldn't be optimized because it's already as
small as possible.
2023-03-16 18:22:58 -07:00
nate-chandler
1f3623d570 Merge pull request #64339 from nate-chandler/copy-propagation/redundant-move-elim
[CopyPropagation] Eliminate redundant moves.
2023-03-16 17:02:25 -07:00
John McCall
72cc8dcb1e [NFC] Rewrite the subst-type extracttor to be a CanTypeVisitor 2023-03-16 15:56:46 -04:00
John McCall
f3e7daf478 [NFC] Remove the now-dead PackExpanded accessors from AbstractionPattern 2023-03-16 01:38:46 -04:00
John McCall
4499e3d055 [NFC] Introduce new APIs for traversing orig/subst parameters in parallel 2023-03-16 01:38:46 -04:00
John McCall
9ab4dc494c [NFC] Add better APIs for parallel destructuring of orig+subst types
As I've been iterating on this work, I've been gradually mulling these
over, and I think this is the way to go for now.  These should make it
a lot less cumbersome to write these kinds of traversals correctly.

The intent is to the sunset the existing expanded-components stuff
after I do a similar pass for function parameters.
2023-03-16 00:19:30 -04:00
Kavon Farvardin
a8a44ebb17 Merge pull request #64106 from kavon/enable-moveonly-by-default
Enable moveonly / noncopyable types by default
2023-03-15 09:59:29 -07:00
Nate Chandler
ceb941d1d1 [NFC] Extracted isRedundantMoveValue.
Extracted the predicate from inline in RedundantMoveValueElimination
into the new function which can now be called from elsewhere
(CopyPropagation).
2023-03-15 07:38:59 -07:00
Erik Eckstein
318c30895f Swift Optimizer: eliminate ARC operations on types which are marked as immortal
A type (mostly classes) can be attributed with `@_semantics("arc.immortal")`.
ARC operations on values of such types are eliminated.

This is useful for the bridged SIL objects in the swift compiler sources.
2023-03-14 21:07:03 +01:00
Erik Eckstein
3fca8cd06e Swift SIL: speed up Operand.value
Instead of doing the type casts and/or conformance lookup on the swift side, do it on the C++ side.
It makes a significant performance difference because `Operand.value` is a time critical function
2023-03-14 21:07:03 +01:00
Kavon Farvardin
5ad38daa08 [bug fix] add missing lowering of explicit_copy_addr 2023-03-13 22:39:31 -07:00
John McCall
1ead872468 Merge pull request #64293 from rjmccall/more-variadic-fixes
Fix some bugs with variadic tuple lowering and value-op outlining
2023-03-11 03:21:50 -05:00
nate-chandler
20e262b35c Merge pull request #64190 from nate-chandler/redundant-move-elim
[SILOptimizer] Add RedundantMoveValueElimination.
2023-03-10 20:41:45 -08:00
John McCall
ce9013a3f6 Fix IRGen to not use outlining for types with any kind of local archetype.
Previously it was testing for opened existentials specifically.

We should really teach outlining to handle local archetypes properly.
We'd have to build a generic signature for the lowered type, and that
probably means also adding requirements that are relevant to value
operations, but it would mean outlining would benefit all types, and
it would let us avoid bundling in unnecessary information from the
enclosing generic environment.

A minor side-effect of this is that we no longer bind names to
opened element type values.  The names were things like \tau_1_0,
which is not very useful, especially in LLVM IR where \tau is
printed with two UTF-8 escapes.
2023-03-10 19:00:28 -05:00
Nate Chandler
af1ac900f9 [SIL] Add hasPointerEscape(SILValue).
There is a preexisting function with this name that takes a
BorrowedValue.  The new function calls that preexisting function if a
BorrowedValue can be constructed from the SILValue.  Otherwise, it looks
for direct uses of the value which qualify as "pointer escapes".
2023-03-10 10:49:53 -08:00
John McCall
239777aacb Fix parameter binding for tuples containing pack expansions
More missing infrastructure.  In this case, it's really *existing*
missing infrastructure, though; we should have been imploding tuples
this way all along, given that we're doing it in the first place.

I don't like that we're doing all these extra tuple copies.  I'm not
sure yet if they're just coming out of SILGen and eliminated immediately
after in practice; maybe so.  Still, it should be obvious that they're
unnecessary.
2023-03-09 02:28:29 -05:00
John McCall
b0dd825f53 Prevent the normal tuple instructions from being used with pack expansions 2023-03-09 02:17:35 -05:00
John McCall
a15a2e86fe Add a helper function for checking if a tuple AP contains expansions 2023-03-09 02:16:09 -05:00
Anton Korobeynikov
869fc1792e [AutoDiff] Ensure autodiff code does not ignore getSingleTerminatorOperands return value (#64200)
Also extend activity analysis to handle `try_apply` normal result properly. Add testcase from #63728
2023-03-08 10:10:22 -08:00
Erik Eckstein
1c844ed29f SIL: remove dangerous SILBasicBlock APIs
`isSuccessorBlock` and `isPredecessorBlock` are dangerous because they can easily lead to quadratic behavior.
Fortunately they are not used anywhere (except in one place for verification, which I rewrote).
2023-03-08 12:20:52 +01:00
John McCall
81d9e6865a Add a couple convenience APIs for working with abstraction patterns 2023-03-07 03:15:31 -05:00
John McCall
157be3420c Implement the callee side of returning a tuple containing a pack expansion.
This required quite a bit of infrastructure for emitting this kind of
tuple expression, although I'm not going to claim they really work yet;
in particular, I know the RValue constructor is going to try to explode
them, which it really shouldn't.

It also doesn't include the caller side of returns, for which I'll need
to teach ResultPlan to do the new abstraction-pattern walk.  But that's
next.
2023-03-06 04:26:18 -05:00
Michael Gottesman
00d54ebaa8 [reference-binding] Add mark_unresolved_reference_binding to signal from SILGen to the pass to check.
Just the SIL part of this.
2023-03-03 17:14:41 -08:00
Andrew Trick
c588c657f5 SILVerifier - option to verify with or without linear lifetime check
Add a separate 'verifyOwnership()' entry point so it's possible
to check OSSA lifetimes at various points.

Move SILGenCleanup into a SILGen pass pipeline.

After SILGen, verify incomplete OSSA.

After SILGenCleanup, verify ownership.
2023-03-01 21:41:46 -08:00
Andrew Trick
103a6fefb8 LinearLifetimeChecker - make DeadEndBlocks optional 2023-03-01 21:41:46 -08:00
Andrew Trick
ccd08ca4b4 Add a completeOSSALifetime utility
Add local lifetime-ending operations to any owned or borrowed value.

This puts a single value into valid OSSA form so that linear lifetime
checking will pass.

Also adds UnreachableLifetimeCompletion which fixes OSSA after
converting a code path to unreachable (e.g. DiagnoseUnreachable and MandatoryInlining).
2023-03-01 21:04:09 -08:00
Andrew Trick
3b737ca52f Add InteriorLiveness computeBoundary wrapper 2023-03-01 18:29:24 -08:00
swift-ci
4e3d60826b Merge pull request #63921 from meg-gupta/movevalueossaonly
Fix SILInliner to create move_value in OSSA only
2023-02-26 14:13:08 -08:00
Meghana Gupta
e5eeb261d1 Fix SILInliner to create move_value in OSSA only 2023-02-26 08:47:15 -08:00
John McCall
d103bf7f35 Merge pull request #63883 from rjmccall/silgen-variadic-generics
Progress towards SILGen for variadic generic argument lowering
2023-02-25 01:14:36 -05:00
nate-chandler
2d52058284 Merge pull request #63876 from nate-chandler/lexical-phis
[SIL] Phi with incoming lexical value is lexical.
2023-02-24 15:28:00 -08:00
John McCall
bd53f5d5df Allow AbstractionPattern to optionally carry substitutions
This allows the AP to answer questions like how many
elements/parameters in the substituted type correspond to
this pack expansion.

I've threaded this through a lot of cases for foreign patterns
that probably won't ever need it.
2023-02-24 15:02:09 -05:00
Nate Chandler
7ea4523ee6 [SIL] Phi with incoming lexical value is lexical.
Added SILPhiArgument::isLexical.  It's true for phis which have an
incoming value that's lexical.  Dispatch to this member from
ValueBase::isLexical.
2023-02-23 16:26:44 -08:00
Nate Chandler
5d4b9154bc [SIL] Marked visitTransitiveIncomingPhis const. 2023-02-23 16:03:41 -08:00
Erik Eckstein
4c5b0b18a1 EscapeUtils: add test cases and an assert for addresses escaping to to a closure
It's essential that a non-escaping closure will be treated as having a relevant type for escape analysis.
Because if it's capturing an (inout) address it's like it's storing a pointer to the captured variable in its context.

Adds test cases for rdar://105676825
2023-02-23 17:40:15 +01:00
swift-ci
4ab5024b3c Merge pull request #63791 from meg-gupta/csebug
Fix CSE `isEqual` and `HashVisitor` for escaped values
2023-02-23 03:54:38 -08:00
Meghana Gupta
619b4cd539 CSE: Don't look through ownership instructions for escaped values
CSE relies on OSSA RAUW for lifetime extension when replacing a redundant instruction.
OSSA RAUW however does not handle lifetime extension for escaped base values.
Values escape from ownership via pointer escape, bitwise escape, forwarding unowned operations
and have none or unowned ownership. For all such values do not look through ownership instructions
while determining equality. It is possible to CSE such values with equivalent operands, because the
operand use guarantees lifetime of the base operand.
2023-02-22 21:11:03 -08:00
nate-chandler
77feef5125 Merge pull request #63766 from nate-chandler/lexical_lifetimes/per-function
Add attr to enable lexical lifetime per function.
2023-02-22 12:33:45 -08:00
Joe Groff
cf81c2015a Factor out helper to find the original PartialApplyInst deallocated by a DestroyValueInst in OSSA, if any 2023-02-20 18:23:20 -08:00