Commit Graph

254 Commits

Author SHA1 Message Date
Arnold Schwaighofer
36d5408125 SIL: Add an [escaped] attribute to convert_escape_to_noescape instruction
To mark when a user of it is known to escape the value. This happens
with materializeForSet arguments which are captured and used in the
write-back. This means we need to keep the context alive until after
the write-back.

Follow-up patches to fully replace the PostponedCleanup hack in SILGen
by a mandatory SIL transformation pass to guarantee the proper lifetime
will use this flag to be more conservative when extending the lifetime.

The problem:

%pa = partial_apply %f(%some_context)
%cvt = convert_escape_to_noescape [not_guaranteed] [escaped] %pa
%ptr = %materialize_for_set(..., %cvt)
...  write_back
... // <-- %pa needs to be alive until after write_back
2018-04-13 12:40:10 -07:00
Arnold Schwaighofer
8f0976d5aa Really fix the logic error in fixupConvertEscapeToNoEscapeLifetime 2018-03-30 12:55:41 -07:00
Arnold Schwaighofer
f40ac5d6d9 Rewrite DI to always guaranteed the lifetime of the convert_escape_to_noescape [not_guaranteed] operand
Either through a peephole or we keep the closure operand alive to the end of the function

rdar://38124009
2018-03-30 09:27:53 -07:00
Arnold Schwaighofer
a9786978b2 Ensure lifetimes of optional convert_escape_to_noescape closures
rdar://38124009
2018-03-30 06:20:13 -07:00
Slava Pestov
34fd4ae512 AST: Use DeclBaseName::Kind::Constructor
Fixes <rdar://problem/35852727>, <https://bugs.swift.org/browse/SR-1660>,
<https://bugs.swift.org/browse/SR-6557>.
2018-03-16 00:25:56 -07:00
Sho Ikeda
422136e1a2 [gardening][enum class] Replace unsigned char with uint8_t for consistency
Before the changes:

- `git grep -E "enum class .+ : uint8_t \{" | wc -l`: 90
- `git grep -E "enum class .+ : unsigned char \{" | wc -l`: 26
2018-03-12 13:57:36 +09:00
Andrew Trick
4734920222 Don't rerun diagnostic passes on deserialized SIL. 2018-02-09 09:55:47 -08:00
Mark Lacey
b4b66bc8e8 Replace getAnyOptionalObjectType with getOptionalObjectType. 2018-02-05 23:59:00 -08:00
Joe Groff
6e9a428543 DefiniteInitialization: Storing back to the 'self' box in a class init is OK.
SILGen does this now to maintain ownership balance when a class initializer delegation gets interrupted, such as by an error propagation through one of the arguments to the delegatee. Fixes rdar://problem/37007554 .
2018-02-03 10:01:15 -08:00
Davide Italiano
3d9eb3d7e5 [LifetimeChecker] Update the debug scope when changing insertion point.
<rdar://problem/36679700>
2018-01-22 14:45:08 -08:00
Adrian Prantl
c74ae5f375 Remove the redundant SILLocation::getCompilerGenerated interface (NFC) 2018-01-17 11:09:23 -08:00
Davide Italiano
1a153d410a [DefiniteInit] Correctly set the debug scope when creating a store.
Fixes https://bugs.swift.org/browse/SR-6745

<rdar://problem/36474392>
2018-01-14 14:43:41 -08:00
John McCall
52bb547a7e Merge pull request #13866 from rjmccall/accessor-decl
Split AccessorDecl out from FuncDecl.  NFC.
2018-01-12 17:02:35 -05:00
John McCall
7f0f8830cd Split AccessorDecl out from FuncDecl. NFC.
This has three principal advantages:

- It gives some additional type-safety when working
  with known accessors.

- It makes it significantly easier to test whether a declaration
  is an accessor and encourages the use of a common idiom.

- It saves a small amount of memory in both FuncDecl and its
  serialized form.
2018-01-12 14:20:27 -05:00
adrian-prantl
ecf9ee749d Merge pull request #13818 from vedantk/empty-locs
[DebugInfo] Add a free helper to get empty locations, NFC
2018-01-11 18:14:37 -08:00
Davide Italiano
e8e90df9b4 [DefiniteInitialization] Preserve DebugInfo correctly.
The scope was set incorrectly. Fixes SR-6722.

<rdar://problem/36414649>
2018-01-10 13:41:37 -08:00
Vedant Kumar
d60ff05265 Rename getEmptyLocation to getCompilerGeneratedLocation 2018-01-09 12:38:45 -08:00
Vedant Kumar
e33f3b663d [DebugInfo] Add a free helper to get empty locations, NFC 2018-01-08 17:59:16 -08:00
Jordan Rose
8f8f00012a Merge pull request #12834 from jrose-apple/restrict-cross-module-struct-initializers-2
Implementation of SE-0189 "Restrict cross-module struct initializers to be delegating"

rdar://problem/34777878
2017-11-30 13:32:45 -08:00
John McCall
aff457381c Change ApplyInstBase to not try to handle trailing objects itself. NFC.
The goal is to make it more composable to add trailing-objects fields in
a subclass.

While I was doing this, I noticed that the apply instructions provided
redundant getNumArguments() and getNumCallArguments() accessors, so I
went ahead and unified them.
2017-11-13 04:03:21 -05:00
Jordan Rose
85e84a85ef DI: Handle cross-module initializers for empty structs in pre-Swift-5
These also have to delegate to another initializer even though there
are no stored properties to initialize.
2017-11-10 14:10:14 -08:00
Jordan Rose
7ff98f3ee8 DI: Suggest "self.init()" for C structs in Swift 5 mode too
...being careful to only do it once per initializer. Additionally,
/don't/ offer the suggestion if there was already a conditional
assignment to 'self', because that would wipe it out and the user
should think harder.
2017-11-10 11:15:33 -08:00
Jordan Rose
1f12e72071 DI: Suggest "self.init()" for extension initializers of C structs
...unless the struct contains a field that cannot be zero-initialized,
such as a non-nullable pointer.

This suggestion is only made for C structs because 'init()' may not be
the right choice for other structs.
2017-11-09 14:28:38 -08:00
Jordan Rose
1598a21e43 DI: Warn on non-delegating cross-module struct initializers
...as detected by initializing an individual field without having
initialized the whole object (via `self = value`).

This only applies in pre-Swift-5 mode because the next commit will
treat all cross-module struct initializers as delegating in Swift 5.
2017-11-09 11:24:28 -08:00
Slava Pestov
16f9438b82 DI: Consistently use 'Use' instead of 'InstInfo' to name DIMemoryUse values 2017-10-20 16:10:34 -07:00
Slava Pestov
ff698f218f DI: Rip out the old 'self consumed' analysis now that it's dead 2017-10-20 16:10:33 -07:00
Slava Pestov
c69686f102 DI: Use new 'self initialized' analysis in conditional destroy lowering
This changes code generation a bit, because now the conditional
state bitmap uses a bit to track if the 'self' box was stored,
not if the 'self' value was consumed. In some cases, this
eliminates an extra bit, in other places it introduces an
extra bit, but it really doesn't matter because LLVM will
optimize this bit manipulation easily.
2017-10-20 16:10:33 -07:00
Slava Pestov
3d5a16a1ca DI: Use new 'self initialized' analysis in LifetimeChecker::isInitializedAtUse() 2017-10-20 16:10:33 -07:00
Slava Pestov
9d80f60607 DI: Use new 'self initialized' analysis in LifetimeChecker::handleSelfInitUse() 2017-10-20 16:10:33 -07:00
Slava Pestov
4c9d736ea0 DI: Use new 'self initialized' analysis in LifetimeChecker::handleStoreUse() 2017-10-20 16:10:32 -07:00
Slava Pestov
18c29b0cb4 DI: New analysis to replace the 'self consumed' analysis
In a throwing or failable initializer for a class, the typical pattern
is that an apply or try_apply consumes the self value, and returns
success or failure. On success, a new self value is produced.
On failure, there is no new self value. In both cases, the original
self value no longer exists.

We used to model this by attempting to look at the apply or try_apply
instruction, and figure out from subsequent control flow which
successor block was the success case and which was the error case.

The error blocks were marked as such, and a dataflow analysis was used
to compute whether 'self' had been consumed in each block reachable
from the entry block.

This analysis was used to prevent invalid use of 'self' in catch
blocks when the initializer delegation was wrapped in do/catch;
more importantly, it was also used to know when to release 'self'
on exit from the initializer.

For example, when we 'throw e' here, 'self' was already consumed
and does not need to be released -- doing so would cause a crash:

do {
  try self.init(...)
} catch let e {
  // do some other cleanup
  throw e
}

On the other hand, here we do have to release 'self', otherwise we
will exit leaking memory:

do {
  try someOtherThing()
  self.init(...)
} catch let e {
  // do some other cleanup
  throw e
}

The problem with the old analysis is that it was too brittle and did
not recognize certain patterns generated by SILGen. For example, it
did not correctly detect the failure block of a delegation to a
foreign throwing initializer, because those are not modeled as a
try_apply; instead, they return an Optional value.

For similar reasons, we did not correctly failure blocks emitted
after calls to initializers which are both throwing and failable.

The new analysis is simpler and more robust. The idea is that in the
success block, SILGen emits a store of the new 'self' value into
the self box. So all we need to do is seed the dataflow analysis with
the set of blocks where the 'self' box is stored to, excluding the
initial entry block.

The new analysis is called 'self initialized' rather than 'self
consumed'. In blocks dominated by the self.init() delegation,
the result is the logical not of the old analysis:

- If the old analysis said self was consumed, the new one says self
 is not initialized.

- If the old analysis said self was not consumed, the new analysis
  says that self *is* initialized.

- If the old analysis returned a partial result, the new analysis
  will also; it means the block in question can be reached from
  blocks where the 'self' box is both initialized and not.

Note that any blocks that precede the self.init() delegation now
report self as uninitialized, because they are not dominated by
a store into the box. So any clients of the old analysis must first
check if self is "live", meaning we're past the point of the
self.init() call. Only if self is live do we then go on to check
the 'self initialized' analysis.
2017-10-20 16:10:32 -07:00
Slava Pestov
d6bb97a57f DI: Factor out LifetimeChecker::emitSelfConsumedDiagnostic() 2017-10-14 23:53:03 -07:00
Slava Pestov
3e9a30be53 DI: Only call getSelfConsumedAtInst() inside initializers 2017-10-14 23:52:09 -07:00
Slava Pestov
421797cb6e DI: Fold handleSuperInitUse() into handleSelfInitUse() 2017-10-13 23:51:15 -07:00
Slava Pestov
94d4b5aa23 DI: Small cleanup now that SelfInit is only used for classes and class-bound protocols 2017-10-13 23:51:15 -07:00
Slava Pestov
b5eeae7446 DI: All enum initializers should be delegating
Again, since there's no distinction between an enum initializer that
delegates to 'self.init' from one that assigns to 'self', we can remove
the special handling of enum initializers in the 'root self' case.

Now, 'root self' is only used for designated initializers in classes
with no superclass, and struct initializers that perform memberwise
initialization of stored properties.

This regresses some diagnostics, because the logic for delegating
init diagnostics is missing some heuristics present in the root self
case. I will fix this in a subsequent patch.
2017-10-13 23:51:15 -07:00
Slava Pestov
0c16aedb60 DI: All protocol extension initializers should be delegating
Previously protocol extension initializers which called 'self.init' were
considered 'delegating', and ones that assign to 'self' were considered
'root'.

Both have the same SIL lowering so the distinction is not useful, and
removing it simplifies some code.
2017-10-13 23:51:14 -07:00
Slava Pestov
5ffbd281c5 DI: Fix diagnostic for return from address-only initializer without calling self.init 2017-10-13 23:51:14 -07:00
Slava Pestov
e51556345d DI: Consolidate and clean up some diagnostics 2017-10-13 23:51:14 -07:00
Erik Eckstein
ab934d0500 DefiniteInitialization: fixed crash for wrong super call with inout instance variable
Fixes rdar://problem/22960985
2017-10-10 09:24:46 -07:00
Slava Pestov
7bf3b90b62 SIL: Split off objc_method / objc_super_method from class_method / super_method
This replaces the '[volatile]' flag. Now, class_method and
super_method are only used for vtable dispatch.

The witness_method instruction is still overloaded for use
with both ObjC protocol requirements and Swift protocol
requirements; the next step is to make it only mean the
latter, also using objc_method for ObjC protocol calls.
2017-10-03 22:13:31 -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
Michael Gottesman
4c087097f3 [di] Update definite_initialization for ownership.
rdar://31521023
2017-09-07 15:23:22 -07:00
Michael Gottesman
9e720c6308 [di] Extract out a helper routine. NFC intended. 2017-07-12 16:50:15 -07:00
Michael Gottesman
f85da6860a [sil] Delete SILInstruction::removeFromParent().
This is a very easily misused API since it allows for users to leak instructions
if they are not careful. This commit removes this API and replaces the small
number of uses of this API with higher level APIs that accomplish the same task
without using removeFromParent(). There were no API users that specifically
required removeFromParent.

An example of one way we were using removeFromParent is to move a SILInstruction
to the front of a block. That does not require exposing an API like
removeFromParent()... we can just create a higher level API like the one added
in this commit: SILInstruction::moveFront(SILBasicBlock *).

rdar://31276565
2017-06-07 16:52:10 -07:00
Alex Hoppen
8946015d5a [SIL] Preparations for removal of getName on ValueDecl
With the introduction of special decl names, `Identifier getName()` on
`ValueDecl` will be removed and pushed down to nominal declarations
whose name is guaranteed not to be special. Prepare for this by calling
to `DeclBaseName getBaseName()` instead where appropriate.
2017-05-28 19:13:24 -07:00
practicalswift
492f5cd35a [gardening] Remove redundant repetition of type names (DRY): RepeatedTypeName foo = dyn_cast<RepeatedTypeName>(bar)
Replace `NameOfType foo = dyn_cast<NameOfType>(bar)` with DRY version `auto foo = dyn_cast<NameOfType>(bar)`.

The DRY auto version is by far the dominant form already used in the repo, so this PR merely brings the exceptional cases (redundant repetition form) in line with the dominant form (auto form).

See the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es11-use-auto-to-avoid-redundant-repetition-of-type-names) for a general discussion on why to use `auto` to avoid redundant repetition of type names.
2017-05-05 09:45:53 +02:00
practicalswift
d4abd21f00 [gardening] Remove duplicate #include 2017-05-04 15:23:11 +02:00
Saleem Abdulrasool
8741e4cd20 Merge pull request #8971 from compnerd/siloptimizer-iwyu
SILOptimizer: IWYU CommandLine.h (NFC)
2017-04-26 20:08:38 -07:00
Michael Gottesman
237a00bc07 [upstream-update] Include a header directly rather than relying on transitive includes.
This causes breakage with some changes coming from upstream.
2017-04-26 16:50:13 -07:00