Commit Graph

2441 Commits

Author SHA1 Message Date
Andrew Trick
47a4ad267d Merge pull request #35531 from atrick/ossa-enforcementopts
Enable AccessEnforcementOpts with OSSA
2021-01-21 11:27:12 -08:00
Andrew Trick
454a4452bd Enable AccessEnforcementOpts with OSSA 2021-01-20 19:08:36 -08:00
swift-ci
74761e3ffe Merge remote-tracking branch 'origin/main' into rebranch 2021-01-19 18:32:17 -08:00
Slava Pestov
c456748551 Frontend: Remove a couple of obsolete staging flags
-enable-subst-sil-function-types-for-function-values
-enable-large-loadable-types

These defaulted to on, and there were no corresponding flags for
turning them off, so the flags had no effect.
2021-01-19 17:47:53 -05:00
swift-ci
21cf3993d8 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-15 02:12:24 -08:00
eeckstein
d121d7d55f Merge pull request #35428 from eeckstein/fix-blocklist-api
SIL: move all the block-list modifying APIs to SILFunction.
2021-01-15 10:56:10 +01:00
swift-ci
1cf1ef2ab5 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-14 09:52:27 -08:00
Erik Eckstein
b7351780f7 SIL: move all the block-list modifying APIs to SILFunction.
... and remove SILFunction::getBlocks().

It's just a cleanup, NFC.
2021-01-14 17:35:31 +01:00
Andrew Trick
a116aa6528 Fix printSILMultipleValueInstructionResult. 2021-01-13 23:44:14 -08:00
swift-ci
ad05509ac4 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-13 20:32:34 -08:00
Michael Gottesman
ffa55937c4 Merge pull request #35297 from gottesmm/ossa-sil-combine-2
[sil-combine] Fix for Ownership round 2
2021-01-13 20:22:05 -08:00
swift-ci
3e32e5337f Merge remote-tracking branch 'origin/main' into rebranch 2021-01-13 17:53:04 -08:00
swift-ci
5a92dfbaf5 Merge pull request #35401 from atrick/move-canacceptunowned 2021-01-13 17:35:42 -08:00
Andrew Trick
7c3865c663 Move canAcceptUnownedValue to SILValue.h.
Now all the logic for mapping OperandOwnership to operand constraints
is defined in one place and fits on a single page.
2021-01-13 10:50:51 -08:00
Michael Gottesman
90f3846bb8 [sil] Define ValueBase::getNextInstruction().
Sometimes when you are working with SILValues, you need a "next" insertion
point. This creates a problem with the SILValue API since even though one can
get an instruction or an insertion point for a value, to find the appropriate
next instruction one needs to pierce through the API and see if one has a
SILArgument or SILInstruction breaking the whole point of abstraction. The
specific problem here is that a SILArgument's "next instruction" is the first
element of the block (that is ValueBase::getDefiningInsertionPoint()) and
SILInstruction's "next instruction" is
std::next(ValueBase::getDefiningInsertionPoint()). This new
API (ValueBase::getNextInstruction()) handles this case for the compiler writer
and eliminates unnecessary code contortions.

I also did a little cleanup where I moved a doxygen comment from a near by a
const_casting trampoline method to the method that the trampoline called (see
getDefiningInsertionPoint()).
2021-01-13 10:43:41 -08:00
swift-ci
5e2c7dba68 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-13 09:12:39 -08:00
Slava Pestov
1996e7bd8b Merge pull request #35390 from slavapestov/sil-unowned-reference-lowering
SIL: Fix type lowering of unowned reference to class-bound generic parameter
2021-01-13 12:00:01 -05:00
Nate Chandler
20db2c0981 Merge branch 'main' into rebranch
Conflicts:
	include/swift/Basic/AnyValue.h
2021-01-12 16:30:02 -08:00
Slava Pestov
8f38532018 SIL: Fix type lowering of unowned reference to class-bound generic parameter
Unowned references lower as address-only or loadable, depending on
whether the underlying class type is known to be native Swift or
not.

In the case where we were lowering an interface type, we would
unconditionally erase the type to AnyObject, producing an incorrect
lowering when the generic signature constrained the type parameter
to a native Swift class.

Fixes <rdar://problem/73083179>.
2021-01-12 18:58:01 -05:00
Richard Wei
f79391b684 [AutoDiff] Remove 'differentiableFunction(from:)' and 'linearFunction(from:)'.
Remove unused APIs `differentiableFunction(from:)` and `linearFunction(from:)`. They were never official APIs, are not included in the [initial proposal](https://github.com/rxwei/swift-evolution/blob/autodiff/proposals/0000-differentiable-programming.md#make-a-function-differentiable-using-derivative), and are unused by existing supported client libraries (SwiftFusion and S4TF). Most importantly, they block crucial optimizations on linear map closures (#34935) and would need nontrivial work in SILGen to support.
2021-01-12 11:58:48 -08:00
swift_jenkins
797e165962 Merge remote-tracking branch 'origin/main' into next 2021-01-07 09:31:18 -08:00
Richard Wei
5e5f48471b Merge pull request #35259 from rxwei/autodiff-mangling
[AutoDiff] Mangle derivative functions and linear maps
2021-01-07 08:46:42 -08:00
Richard Wei
ffe6064101 Mangle derivative functions and linear maps.
- `Mangle::ASTMangler::mangleAutoDiffDerivativeFunction()` and `Mangle::ASTMangler::mangleAutoDiffLinearMap()` accept original function declarations and return a mangled name for a derivative function or linear map. This is called during SILGen and TBDGen.
- `Mangle::DifferentiationMangler` handles differentiation function mangling in the differentiation transform. This part is necessary because we need to perform demangling on the original function and remangle it as part of a differentiation function mangling tree in order to get the correct substitutions in the mangled derivative generic signature.

A mangled differentiation function name includes:
- The original function.
- The differentiation function kind.
- The parameter indices for differentiation.
- The result indices for differentiation.
- The derivative generic signature.
2021-01-07 02:21:10 -08:00
swift_jenkins
0f61b1d8d4 Merge remote-tracking branch 'origin/main' into next 2021-01-06 09:03:32 -08:00
Andrew Trick
f22d0855df Add a CanonicalOSSALifetime utility.
Canonicalizing OSSA provably minimizes the number of retains and
releases within the boundaries of that lifetime. This eliminates the
need for ad-hoc optimization of OSSA copies.

This initial implementation only canonicalizes owned values, but
canonicalizing guaranteed values is a simple extension.

This was originally part of the CopyPropagation prototype years
ago. Now OSSA is specified completely enough that it can be turned
into a simple utility instead.

CanonicalOSSALifetime uses PrunedLiveness to find the extended live
range and identify the consumes on the boundary. All other consumes
need their own copy. No other copies are needed.

By running this after other transformations that affect OSSA
lifetimes, we can avoid the need to run pattern-matching optimization
to SemanticARC to recover from suboptimal patterns, which is not
robust, maintainable, or efficient.
2021-01-05 20:38:30 -08:00
swift_jenkins
189718aaef Merge remote-tracking branch 'origin/main' into next 2021-01-05 09:01:24 -08:00
Michael Gottesman
0787072b34 Merge pull request #35246 from gottesmm/ossa-sil-combine-final
[sil-combine] Update sil combine for Ownership [part 1]
2021-01-04 21:13:56 -08:00
Michael Gottesman
f6c7c0d0bc [ownership] bridge_object_to_word is a bitwise escape not an instantaneous use. 2021-01-04 15:44:58 -08:00
Dan Zheng
126f1ac6fb [AutoDiff] Disable differentiable_function_extract explicit type as… (#35239)
`differentiability_function_extract` instruction has an optional explicit
extractee type. This is currently used by TypeSubstCloner and the
LoadableByAddress transform to rewrite `differentiability_function_extract`
instructions while preserving `@differentiable` function type invariants.

There is an assertion that `differentiability_function_extract` instructions do
not have explicit extractee types outside of canonical/lowered SIL. However,
this does not handle the SIL deserialization case above: when a function
containing a `differentiable_function_extract` instruction with an explicit type
is deserialized into a raw SIL module (which happens when optimizations are
enabled).

Removing the assertion unblocks this encountered use case.

A more robust longer-term solution may be to change SIL `@differentiable`
function types to explicitly store component original/JVP/VJP function types.

Also fix `differentiable_function_extract` extractee type serialization.

Resolves SR-14004.
2021-01-04 18:40:11 -05:00
Michael Gottesman
f67c52ee1a [ownership] ref_to_* and *_to_ref produce values with OwnershipKind::Unowned so should be treated as PointerEscapes.
These instructions model a conversion in between ownership kinds without the result actually being owned by anything. As a result:

1. From an operand perspective, the instruction is treated like a pointer escape.
2. From a value perspective, the instruction returns a value with
   OwnershipKind::Unowned (to force the value to be copied before it can be used
   in an owned or guaranteed way) and

Example:

```
sil @example : $@convention(thin) (@owned Klass) -> @owned @sil_unowned Klass {
bb0(%0 : @owned $Klass):
  // Note that the ref_to_unowned does not consume %0 but instead converts %0
  // from a "strong" value to a "safe unowned" value. A "safe unowned" value is
  // a value that corresponds to an 'unowned' value at the Swift level that use
  // unowned reference counting. At the SIL level these values can be recognized
  // by their types having the type attribute @sil_unowned. We have not
  // incremented the unowned ref count of %1 so we must treat %1 as unowned.
  %1 = ref_to_unowned %0 : $Klass
  // Then before we can use %2 in any way as a "safe unowned" value we need to
  // bump its unowned ref count by making a copy of the value. %2 will be a
  // "safe unowned" value with OwnershipKind::Owned ensuring that we decrement
  // the unowned ref count and do not leak said ref count.
  %2 = copy_value %1 : $@sil_unowned $Klass
  // Then since the original ref_to_unowned did not consume %0, we need to
  // destroy it here.
  destroy_value %0 : $Klass
  // And then return out OwnershipKind::Owned @sil_unowned Klass.
  return %2 : $@sil_unowned $Klass
}
```
2021-01-04 15:37:57 -08:00
Andrew Trick
ab42f753f7 Remove OperandOwnership::NestedBorrow.
Now that OperandOwnership determines the operand constraints, it
doesn't make sense to distinguish between Borrow and NestedBorrow at
this level. We want these uses to automatically convert between the
nested/non-nested state as the operand's ownership changes. The use
does not need to impose any constraint on the ownership of the
incoming value.

For algorithms that need to distinguish nested borrows, it's still
trivial to do so.
2021-01-01 19:22:19 -08:00
Andrew Trick
f4e0eae15f OperandOwnership Remove unnecessary special case for callee operand. 2021-01-01 19:22:19 -08:00
Andrew Trick
07f5735e53 Give SILToken Guaranteed ownership.
The begin_apply token represents the borrow scope of all owned and guaranteed
call arguments. Although SILToken is (currently) trivially typed, it must
have guaranteed ownership so end_apply and abort_apply will be recognized
as lifetime-ending uses.
2021-01-01 19:22:19 -08:00
Andrew Trick
56f05da8b7 Fix OperandOwnership for coroutines.
Anything with a borrow scope in the caller needs to be considered a
Borrow of its operand. The end_apply needs to be considered the
lifetime-ending use of the borrow.
2021-01-01 19:22:19 -08:00
Andrew Trick
cdcd0bf341 Fix OperandOwnership for convert_escape_to_noescape.
This is only used for OSSA verification currently, but will be a
correctness issue when OSSA canonicalization is enabled.
2021-01-01 19:22:19 -08:00
Andrew Trick
ce2a7cfe0e Clearly discriminate OperandOwnership::NonUse and TrivialUse.
A NonUse operand does not use the value itself, so it ignores
ownership and does not require liveness. This is for operands that
represent dependence on a type but are not actually passed the value
of that type (e.g. they may refer an open_existential). This could be
used for other dependence-only operands in the future.

A TrivialUse operand has undefined ownership semantics aside from
requiring liveness. Therefore it is only legal to pass the use a value
with ownership None (a trivial value). Contrast this with things like
InstantaneousUse or BitwiseEscape, which just don't care about
ownership (i.e. they have no ownership semantics.

All of the explicitly listed operations in this category require
trivially typed operands. So the meaning is obvious to anyone
adding SIL operations and updating OperandOwnership.cpp, without
needing to decifer the value ownership kinds.
2021-01-01 19:22:10 -08:00
swift_jenkins
11993fd1cf Merge remote-tracking branch 'origin/main' into next 2020-12-23 10:53:57 -08:00
Meghana Gupta
b99533aced Merge pull request #34895 from meg-gupta/cseossa
Enable CSE on OSSA
2020-12-23 09:58:46 -08:00
Meghana Gupta
42c031985c Enable CSE on OSSA 2020-12-22 23:20:06 -08:00
swift_jenkins
5bfdc8352a Merge remote-tracking branch 'origin/main' into next 2020-12-18 22:13:35 -08:00
Michael Gottesman
368f8acc4b [sil] Eliminate a confusing optional method result by changing type dependent operands to have OwnershipKind::None instead of returning Optional::None from Operand::getOperandOwnership().
This eliminates when talking about this API an ambiguity in between
Optional::None (the C++ ADT) and OwnershipKind::None (the ownership kind).
2020-12-18 19:33:27 -08:00
swift_jenkins
467bc7d50b Merge remote-tracking branch 'origin/main' into next 2020-12-18 00:32:23 -08:00
swift_jenkins
2146699758 Merge remote-tracking branch 'origin/main' into next 2020-12-17 23:55:39 -08:00
Andrew Trick
f777e0a70e Convert OperandOwnership from an enum class to a struct enum. 2020-12-17 21:24:41 -08:00
Andrew Trick
84768bcdb3 OSSA: Add requirements on Unowned uses.
Clarify which uses are allowed to take Unowned values. Add enforcement
to ensure that Unowned values are not passed to other uses.

Operations that can take unowned are:

- copy_value
- apply/return @unowned argument
- aggregates (struct, tuple, destructure, phi)
- forwarding operations that are arbitrary type casts

Unowned values are currently borrowed within ObjC deinitializers
materialized by the Swift compiler. This will be banned as soon as
SILGen is fixed.
2020-12-17 21:08:56 -08:00
swift_jenkins
4abe2943fc Merge remote-tracking branch 'origin/main' into next 2020-12-16 11:44:28 -08:00
Andrew Trick
b1dba2554e Introduce OperandOwnership to classify OSSA uses.
Migrating to this classification was made easy by the recent rewrite
of the OSSA constraint model. It's also consistent with
instruction-level abstractions for working with different kinds of
OperandOwnership that are being designed.

This classification vastly simplifies OSSA passes that rewrite OSSA
live ranges, making it straightforward to reason about completeness
and correctness. It will allow a simple utility to canonicalize OSSA
live ranges on-the-fly.

This avoids the need for OSSA-based utilities and passes to hard-code
SIL opcodes. This will allow several of those unmaintainable pieces of
code to be replaced with a trivial OperandOwnership check.

It's extremely important for SIL maintainers to see a list of all SIL
opcodes associated with a simple OSSA classification and set of
well-specified rules for each opcode class, without needing to guess
or reverse-engineer the meaning from the implementation. This
classification does that while eliminating a pile of unreadable
macros.

This classification system is the model that CopyPropagation was
initially designed to use. Now, rather than relying on a separate
pass, a simple, lightweight utility will canonicalize OSSA
live ranges.

The major problem with writing optimizations based on OperandOwnership
is that some operations don't follow structural OSSA requirements,
such as project_box and unchecked_ownership_conversion. Those are
classified as PointerEscape which prevents the compiler from reasoning
about, or rewriting the OSSA live range.

Functional Changes:

As a side effect, this corrects many operand constraints that should
in fact require trivial operand values.
2020-12-16 01:58:53 -08:00
swift_jenkins
7f84c7b706 Merge remote-tracking branch 'origin/main' into next 2020-12-15 22:15:55 -08:00
John McCall
d68d406dae Merge pull request #35094 from rjmccall/actor-dynamic-layout
Do dynamic layout of generic/resilient default actors properly
2020-12-16 01:05:00 -05:00
John McCall
bad16fd105 Do dynamic layout of generic/resilient default actors properly.
Since these types have an implicit stored property, this requires
adding an abstraction over fields to IRGen, at least throughout
the class code.  In some ways I think this significantly improves
the code, especially in how we approach missing members.

Fixes rdar://72202671.
2020-12-15 20:10:46 -05:00