Commit Graph

170 Commits

Author SHA1 Message Date
Slava Pestov
37f2bc1e59 DI: Add non-working code for handling value_metatype when used on an address-only type 2020-10-23 22:25:58 -04:00
Slava Pestov
7f2b01ccb3 DI: Fix crash when value_metatype was used with a non-class 'self' type
In a constructor, SILGen lowers type(of: self) to a value_metatype
instruction, but 'self' might not have been initialized yet if
this is a convenience initializer, so we replace it with a use of
the 'self' metatype argument.

However getDynamicSelfMetadata() asserts if 'self' is a non-class
type. Since we know we're inside of a proper method here and not a
closure that captured 'self', we can use getSelfArgument() instead.

Fixes https://bugs.swift.org/browse/SR-12665 / rdar://problem/62481587.
2020-10-23 22:25:58 -04:00
Slava Pestov
360e406d3a SIL: Rename SILFunction::hasSelfMetadataParam()/getSelfMetadataArgument()
These are only for class types and are related to the usage of the
DynamicSelfType, so rename them to {has,get}DynamicSelfMetadata().
2020-10-23 21:35:11 -04:00
Slava Pestov
50963451b8 DI: Handle instructions that initialize multiple tuple elements
DI had trouble with this pattern:

  %s = struct_element_addr ...
  %t0 = tuple_element_addr %s, 0
  %t1 = tuple_element_addr %s, 1
  %f = function_ref ...
  apply %f(%t0, %t1)

This is because the NonLoadUses map only stored a single use of a
tuple element per instruction, preventing instructions such as
'apply' from being able to use multiple tuple elements.

In other cases where this comes up, such as with 'assign' and
'copy_addr', DI scalarizes the tuple operation by projecting
each component, however clearly this can't be easily done with
an 'apply'.

Instead, we can get DI out of the business of scalarization, at
least for instructions which are known to perform an unconditional
initialization.

We do this by changing the NonLoadUses map to store a vector of
DIMemoryUse IDs instead of a single value.
2020-10-10 23:00:03 -04:00
Holly Borla
1f426773ee [Property Wrappers] Lower assign_by_wrapper to re-assignment of the backing
property wrapper before all of self is initialized if the wrapper has already
been initialized.
2020-09-11 15:43:21 -07:00
Slava Pestov
233eeea6cb DI: Correctly handle conditional destroy of 'self' in root class
In a designated initializer of a non-root class, 'self' becomes
fully initialized after the 'super.init' call, at which point
escaping uses of 'self' become valid, and releases of 'self' are
lowered to a 'strong_release' instruction, which runs the
deinitializer.

In a root class, 'self' becomes fully initialized after all stored
properties have been initialized, at which point escaping uses of
'self' become valid.

However, DI would still lower a conditional destroy of 'self' by
destroying any initialized stored properties and freeing the object
with 'dealloc_partial_ref'. This is incorrect, because 'self' may
have escaped.

In the non-conditional destroy case, we correctly lowered the
release to a 'strong_release' if all stored properties are known
to be initialized.

Fix DI to handle the conditional destroy case by first checking if all
bits in the control variable are set, and releasing the instance with
'strong_release' if so. The 'dealloc_partial_ref' is only emitted
if not at least one stored property was not initialized.

This ensures that we don't deallocate an instance that may have
escaped.

Fixes <https://bugs.swift.org/browse/SR-13439>, <rdar://problem/67746791>,
<https://bugs.swift.org/browse/SR-13355>, <rdar://problem/67361228>.
2020-08-31 22:14:36 -04:00
Slava Pestov
df0a8b3746 DI: Track of initialization state of trivial fields in root class initializers
While we don't need to destroy these fields, we still need to know
when the class is _semantically_ fully initialized, since this will
determine if we're going to release the class, running the deinitializer,
or if we're going to deallocate the memory for the instance directly.
2020-08-31 22:08:47 -04:00
Slava Pestov
e6249576ea DI: Rename testControlVariable() to testControlVariableBit() 2020-08-31 18:40:21 -04:00
Anthony Latsis
9fd1aa5d59 [NFC] Pre- increment and decrement where possible 2020-06-01 15:39:29 +03:00
Michael Gottesman
f57a6cfd6d [silgen] Teach CleanupCloner how to handle OwnedValueWritebackCleanups correctly.
Previously we would just forward the cleanup and create a normal "destroy"
cleanup resulting in early deallocation and use after frees along error paths.

As part of this I also had to tweak how DI recognizes self init writebacks to
not use SILLocations. This is approach is more robust (since we aren't relying
on SourceLocs/have verifiers to make sure we don't violate SIL) and also avoids
issues from the write back store not necessarily have the same SILLocation.

<rdar://problem/59830255>
2020-05-19 16:15:33 -07:00
Ravi Kandhadai
1bedce022b Merge pull request #31554 from ravikandhadai/definite-init-failable-init-diag
[Definite Init] Improve diagnostics for delegating, failable initializers that do not initialize along all paths.
2020-05-06 18:19:50 -07:00
Ravi Kandhadai
4e5389dacb [Definite Initialization] Improve diagnostics for delegating, failable
initializers that do not initialize along all paths.

<rdar://problem/62562254>
2020-05-04 22:00:18 -07:00
Artem Chikin
d04ae47801 Allow initializing a wrapped property with a nonmutating setter
This is achieved in 3 steps:
1. CSApply detects assignments to property wrappers inside constructors, and produces an `inout` expr instead of a `load`, which it normally would because nonmutating setters take the `self` by-value. This is necessary becasue the assign_by_wrapper instruction expects an address type for its $1 operand.
2. SILGenLValue now emits the assign_by_wrapper pattern for such setters, ignoring the fact that they capture `self` by value. It also introduces an additional load instruction for the setter patrial_apply because the setter signature still expects a value and we now have an address (because of (1)).
3. DefiniteInitialization specifically ignores load instructions used to produce a `self` value for a setter referenced on assign_by_wrapper because it will be deleted by lowering anyway.

Resolves rdar://problem/60600911 and rdar://problem/52280477
2020-04-30 14:49:22 -07:00
Holly Borla
3aabdb5a81 [Property Wrappers] Only re-write assign_by_wrapper to assignment
if all fields have been initialized.
2020-04-24 11:36:47 -07:00
Anthony Latsis
d0ae3ee580 [AST] Replace FuncDecl::getName & EnumElementDecl::getName with ValueDecl::getBaseIdentifier 2020-03-29 00:35:51 +03:00
Slava Pestov
9ec80df97e SIL: Remove curried SILDeclRefs 2020-03-19 02:20:21 -04:00
Fred Riss
259d78a350 Adapt to llvm.org StringRef API change 2020-03-13 19:08:22 +01:00
Michael Gottesman
b4cf1afd19 [di] When emitting element addresses to destroy fields, use begin_access [deinit]. 2020-01-08 14:09:55 -08:00
Michael Gottesman
28ffcf9a7a [ownership] Eliminate the need for mark_uninitialized fixup.
This commit eliminates the need for mark uninitialized fixup by updating the
compiler so that we now emit:

```
%0 = alloc_box
%1 = mark_uninitialized %0
%2 = project_box %1
...
destroy_value %1
```

Instead of:

```
%0 = alloc_box
%1 = project_box %0
%2 = mark_uninitialized %1
...
destroy_value %0
```

Now that the first type of code is generated, I can change project_box to only
take guaranteed arguments. This will ensure that the OSSA ARC optimizer can
eliminate copies of boxes without needing to understand the usage of the
project_box.
2020-01-02 09:54:18 -08:00
Michael Gottesman
ba6763ac10 [di] Instead of accessing TheMemory.MemoryInst directly, use the helper getUninitializedValue().
This is going to let me change getUninitializedValue() to special case alloc_box
so that we return the project_box (the address we want to analyze) rather than
the mark_uninitialized (which is on the box).
2019-12-27 19:59:12 -08:00
Michael Gottesman
722d0bb028 [di] Add an accessor for NumElements and use that instead of accessing the field directly. 2019-12-27 19:49:34 -08:00
Michael Gottesman
2cf6d02a93 [di] Now that DI and PMO are split, debride some dead code.
Specifically isDefiniteInitFinished is always set to false and
TreatAddressToPointerAsInout is set to true when ever DI is run, so this patch
just constant propagates those values and then DCEs as appropriate.
2019-12-27 19:07:54 -08:00
Michael Gottesman
980b1f0561 [di] Hide some internal state and give some methods better names given current usage.
NFC.
2019-12-19 15:09:15 -08:00
Ravi Kandhadai
935686460c [SIL Optimization] Create a new utility InstructionDeleter to delete instructions
and eliminate dead code. This is meant to be a replacement for the utility:
recursivelyDeleteTriviallyDeadInstructions. The new utility performs more aggresive
dead-code elimination for ownership SIL.

This patch also migrates most non-force-delete uses of
recursivelyDeleteTriviallyDeadInstructions to the new utility.
and migrates one force-delete use of recursivelyDeleteTriviallyDeadInstructions
(in IRGenPrepare) to use the new utility.
2019-12-18 13:17:17 -08:00
Andrew Trick
bddc69c8a6 Organize SILOptimizer/Utils headers. Remove Local.h.
The XXOptUtils.h convention is already established and parallels
the SIL/XXUtils convention.

New:
- InstOptUtils.h
- CFGOptUtils.h
- BasicBlockOptUtils.h
- ValueLifetime.h

Removed:
- Local.h
- Two conflicting CFG.h files

This reorganization is helpful before I introduce more
utilities for block cloning similar to SinkAddressProjections.

Move the control flow utilies out of Local.h, which was an
unreadable, unprincipled mess. Rename it to InstOptUtils.h, and
confine it to small APIs for working with individual instructions.
These are the optimizer's additions to /SIL/InstUtils.h.

Rename CFG.h to CFGOptUtils.h and remove the one in /Analysis. Now
there is only SIL/CFG.h, resolving the naming conflict within the
swift project (this has always been a problem for source tools). Limit
this header to low-level APIs for working with branches and CFG edges.

Add BasicBlockOptUtils.h for block level transforms (it makes me sad
that I can't use BBOptUtils.h, but SIL already has
BasicBlockUtils.h). These are larger APIs for cloning or removing
whole blocks.
2019-10-02 11:34:54 -07:00
Erik Eckstein
0bac147361 DefiniteInitialization: fix a minor problem with memory lifetime
Dead loads for type-of-self must be removed, otherwise it's a violation of memory lifetime.
2019-08-13 09:29:05 +02:00
Erik Eckstein
32c0feb577 SIL: add a [dynamic_lifetime] flag to alloc_stack and alloc_box
This flag is set by DefinitInitialization if the lifetime of the stored value is controlled dynamically.
If the flag is set, it's not (easily) possibly to statically calculate the lifetime of the stored value.
2019-08-13 09:29:05 +02:00
Doug Gregor
c02ecf9859 [SE-0258] Rename to Property Wrappers 2019-05-29 22:17:50 -07:00
Arnold Schwaighofer
c187c8ac13 SIL: Replace uses of getReferencedFunction() by getReferencedFunctionOrNull() and getInitialReferencedFunction()
With the advent of dynamic_function_ref the actual callee of such a ref
my vary. Optimizations should not assume to know the content of a
function referenced by dynamic_function_ref. Introduce
getReferencedFunctionOrNull which will return null for such function
refs. And getInitialReferencedFunction to return the referenced
function.
Use as appropriate.

rdar://50959798
2019-05-26 08:58:14 -07:00
Andrew Trick
e400b66897 Cleanup replaceAllUsesAndErase, return an iterator, allow erase handlers.
This will make the forthcoming CanonicalizeInstruction interface more
clear.

This is generally the better approach to utilities that mutate the
instruction stream. It avoids the temptation to assume that only a
single instruction will be deleted or that only instructions before
the current iterator will be deleted. This often happens to work but
eventually fails in the presense of debug and end-of-scope
instructions.

A function returning an iterator has a more clear contract than one
accepting some iterator reference of unknown
providence. Unfortunately, it doesn't work at the lowest level of
utilities, such as recursivelyDeleteTriviallyDeadInstructions, where
we want to handle instruction batches.
2019-05-06 08:36:56 -07:00
Erik Eckstein
86fb74a34e DI: support assign_by_delegate instruction 2019-04-23 11:32:28 -07:00
Slava Pestov
8915f96e3e SIL: Replace SILType::isTrivial(SILModule) with isTrivial(SILFunction) 2019-03-12 01:16:04 -04:00
Azoy
5af2663c57 Textualize assign init kind
Rename [assign] to [reassign]

fix some tests

AssignOwnershipQualifier

formatting

moar formatting
2019-02-12 20:16:25 -06:00
Azoy
fcd6a8adf3 Lower Assign to store [assign] & Remove MandatoryOptUtils
update debug_type
2019-02-12 18:10:17 -06:00
Azoy
397b96c917 [SILOptimizer] Don't lower AssignInst in DI 2019-02-12 18:10:17 -06:00
Jordan Rose
425c190086 Restore initializing entry points for @objc convenience initializers (#21815)
This undoes some of Joe's work in 8665342 to add a guarantee: if an
@objc convenience initializer only calls other @objc initializers that
eventually call a designated initializer, it won't result in an extra
allocation. While Objective-C /allows/ returning a different object
from an initializer than the allocation you were given, doing so
doesn't play well with some very hairy implementation details of
compiled nib files (or NSCoding archives with cyclic references in
general).

This guarantee only applies to
(1) calling `self.init`
(2) where the delegated-to initializer is @objc
because convenience initializers must do dynamic dispatch when they
delegate, and Swift only stores allocating entry points for
initializers in a class's vtable. To dynamically find an initializing
entry point, ObjC dispatch must be used instead.

(It's worth noting that this patch does NOT check that the calling
initializer is a convenience initializer when deciding whether to use
ObjC dispatch for `self.init`. If we ever add peer delegation to
designated initializers, which is totally a valid feature, that should
use static dispatch and therefore should not go through objc_msgSend.)

This change doesn't /always/ result in fewer allocations; if the
delegated-to initializer ends up returning a different object after
all, the original allocation was wasted. Objective-C has the same
problem (one of the reasons why factory methods exist for things like
NSNumber and NSArray).

We do still get most of the benefits of Joe's original change. In
particular, vtables only ever contain allocating initializer entry
points, never the initializing ones, and never /both/ (which was a
thing that could happen with 'required' before).

rdar://problem/46823518
2019-01-14 13:06:50 -08:00
Slava Pestov
84ed245f2f DI: Fix transformation of value_metatype inside convenience init in Swift 5 mode
In Swift 5 mode, 'self' in a convenience init has a DynamicSelfType, so
the value_metatype instruction returns a DynamicSelfType metatype.

However, the metatype argument to the constructor is a plain class metatype
because it's an interface type, so we have to "open" it by bitcasting it
to a DynamicSelfType.

Fixes <https://bugs.swift.org/browse/SR-9430>, <rdar://problem/46982573>.
2019-01-11 15:53:37 -05:00
Michael Gottesman
c6f5995254 [definite-init] Convert always true bool return value to void.
Just a small cleanup.
2019-01-02 09:45:11 -08:00
Saleem Abdulrasool
cde0862468 Merge pull request #21537 from compnerd/optimize-incorrectly
SILOptimizer: fix a few instances of use-after-free
2019-01-02 08:28:44 -08:00
Michael Gottesman
9620bedf7a [di] Rename: DIMemoryUseCollector{Ownership,}.{cpp,h}
This was done early on during the split of predictable mem opts from DI. This
has been done for a long time, so eliminate the "Ownership" basename suffix.
2018-12-30 16:11:56 -08:00
Saleem Abdulrasool
bd93229428 SILOptimizer: fix a few instances of use-after-free
When building on Windows, the std::string being passed will on the
stack, where the destructor will be invoked before the return, nil'ing
out reference.  This causes incorrect behaviour when building the
diagnostic or FIXITs.  Explicitly create the StringRef to prevent the
std::string from being invalidated.
2018-12-23 16:24:04 -08:00
Slava Pestov
a5f9619062 DI: Lower AssignInst in a post-processing pass
Compiler passes that intermingle analysis with mutation of the CFG
are fraught with danger. The bug here was that a single AssignInst
could appear twice in DI's Uses list, once as a store use and once
as a load use.

When processing Uses, we would lower AssignInsts on the fly. We would
take care to erase the instruction pointer from the current Use, but
if a subsequent Use *also* referenced the now-deleted AssignInst, we
would crash.

Handle this in the standard way: instead of lowering assignments
right away, just build a list of assignments that we're planning on
lowering and process them all at the very end.

This has the added benefit of simplifying the code, because we no
longer have to work as hard to keep the state of the Uses list
consistent while lowering AssignInsts. The rest of DI should not
care that the lowering got postponed either, since it was already
expected to handle any ordering of elements in the Uses list, so
it could not assume that any particular AssignInst has been lowered.

Fixes <https://bugs.swift.org/browse/SR-9451>.
2018-12-10 00:03:08 -05:00
Slava Pestov
38263ca000 DI: Small cleanups
- Handle DIMemoryUse::Kind::IndirectIn just like Load
- Consolidate duplicated 'only touches trivial elements' check
2018-12-10 00:03:08 -05:00
Saleem Abdulrasool
15d3bf15db SILOptimizer: avoid use-after-free with the name
On Windows at least, the std::string associated with the name of the
property would be copy constructed before being passed to the diagnostic
engine.  The resultant DiagnosticArgument in the InFlightDiagnostic
would hold a StringRef to the copy-constructed std::string.  However,
because the arguments are inalloca'ed on Windows, the copy constructed
string would be destructed before the return of the argument.
Fortunately, this would result in the standard library overwriting the
string and the use-after-free would fail to print the argument.
Explicitly construct the StringRef before passing the name to the
diagnostic to avoid the use-after-free.
2018-11-26 13:04:06 -08:00
Andrew Trick
fe10da1b6e Don't create critical edges in definite-initialization. 2018-10-20 01:55:15 -07:00
Joe Groff
9c5432c0dd Make __consuming meaningful for code generation.
Previously, the `__consuming` decl modifier failed to get propagated to the value ownership of the
method's `self` parameter, causing it to effectively be a no-op. Fix this, and address some of the
downstream issues this exposes:

- `coerceCallArguments` in the type checker failing to handle the single `__owned` parameter case
- Various places in SILGen and optimizer passes that made inappropriate assertions that `self`
  was always passed guaranteed
2018-09-28 14:09:59 -07:00
Michael Gottesman
c62f31f5dc Inject llvm::SmallBitVector into namespace swift;
I also eliminated all llvm:: before SmallBitVector in the code base.
2018-09-21 09:49:25 -07:00
Joe Groff
5a55b4fc4f Small cleanups to type(of: self) handling in DI
Should be NFC
2018-09-13 15:33:15 -07:00
Joe Groff
b44ddf59d5 SILOptimizer: Turn type(of: self) from uninitialized self stack space into the self argument.
Before we changed convenience inits into allocating entry points, we allowed type(of: self) to be invoked on the uninitialized object, which was fine. This no longer Just Works when self doesn't even exist before `self.init` is called, but to maintain the old semantics and source compatibility, we can still just use the metatype value we were passed in.
2018-09-13 12:31:27 -07:00
John McCall
b80618fc80 Replace materializeForSet with the modify coroutine.
Most of this patch is just removing special cases for materializeForSet
or other fairly mechanical replacements.  Unfortunately, the rest is
still a fairly big change, and not one that can be easily split apart
because of the quite reasonable reliance on metaprogramming throughout
the compiler.  And, of course, there are a bunch of test updates that
have to be sync'ed with the actual change to code-generation.

This is SR-7134.
2018-08-27 03:24:43 -04:00