Commit Graph

129 Commits

Author SHA1 Message Date
Michael Gottesman
748bf88bbe [ownership] Convert SILOptimizer/cast_folding.swift to work with ownership and fix all issues exposed. 2019-05-21 09:07:13 -07:00
Slava Pestov
8915f96e3e SIL: Replace SILType::isTrivial(SILModule) with isTrivial(SILFunction) 2019-03-12 01:16:04 -04:00
Michael Gottesman
c2e8b9e195 [sil] Teach the SILCloner how to clone from ossa -> non-ossa.
At a high level, we perform the OME lowering as we transform.
2019-01-29 13:19:03 -08:00
Arnold Schwaighofer
f664b16010 SIL: Add an on stack version of partial_apply
It does not take ownership of its non-trivial arguments, is a trivial
function type and therefore must not be destroyed. The compiler must
make sure to extend the lifetime of non-trivial arguments beyond the
last use of the closure.

  %objc = copy_value %0 : $AnObject
  %closure = partial_apply [stack] [callee_guaranteed] %16(%obj) : $@convention(thin) (@guaranteed AnObject) -> ()
  %closure2 = mark_dependence %closure : $@noescape @callee_guaranteed () -> () on %obj : $AnObject
  %user = function_ref @useClosure : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
  apply %user(%closure2) : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
  dealloc_stack %closure : $() ->()
  destroy_value %obj : $AnObject // noescape closure does not take ownership

SR-904
rdar://35590578
2019-01-15 11:20:33 -08:00
Michael Gottesman
3551c43d97 [sil] Add SILBuilder helpers for updating passes to handle both ossa and non-ossa code.
Specifically we add a groups of APIs for destructure operations. The destructure
helpers are a family of functions built around
emitDestructureValueOperation. These in ossa produce destructures and pass the
results off to the caller in some manner that hides the internal destructure
instruction. In non-ossa, the appropriate projections are created and passed off
to the caller.
2019-01-15 09:01:34 -08:00
Erik Eckstein
5a91d13d78 SIL: make verification for loadable types more accurate regarding resilience.
Allow creating instructions with types which are only loadable in resilient functions (but not in all functions).
2019-01-04 11:21:25 -08:00
Michael Gottesman
9dc8bbb866 [ownership] Eliminate -assume-parsing-unqualified-ownership-sil now that it is a no-op. 2018-12-19 12:54:13 -08:00
Michael Gottesman
23378cc16f [sil] Rename QualifiedOwnership => Ownership.
Done using Xcode's refactoring engine.
2018-12-16 15:21:52 -08:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Michael Gottesman
fed6145922 [sil] Change all single value instructions with forwarding ownership to have static ownership.
Previously we would always calculate these instructions ownership dynamically
when asked and rely on the ownership verifier to catch if we made any
mistakes. Instead with this commit we move to a more static model where the
ownership that these instructions can take are frozen on construction. This is a
more static model that simplifies the ownership model.

I also eliminated a few asserts that are enforced in other places that caused
problems when parsing since we may not have a Function while Parsing (it was
generally asserts if a type was trivial).
2018-11-11 15:23:36 -08:00
Michael Gottesman
e4607a7191 [closure-lifetime-fixup] When emitting a load_borrow, make sure to emit the end_borrow for it as well.
In this commit I added a more convenient API for doing this sort of operation.
Specifically: SILBuilder::emitScopedBorrowOperation. This performs either a
load_borrow or begin_borrow, then calls a user provided closure, and finally
inserts the end_borrow after the scope has closed.

rdar://43398898
2018-10-26 11:11:01 -07:00
John McCall
512e55683e Make it easy to create a SILBasicBlock immediately before a target block.
Also, make "after" requests explicit in the API.
2018-08-18 12:36:36 -04:00
David Zarzycki
476d869e55 [SIL] NFC: Adopt reference storage type meta-programming macros 2018-06-30 06:44:33 -04:00
swift-ci
05845fdde7 Merge pull request #17567 from gottesmm/pr-f41d0ac398c9ff713799f99abdfdad846fb967ab 2018-06-27 13:34:47 -07:00
Michael Gottesman
97367d3dd4 [ownership] Do not lower copy_unowned_value to strong_retain_unowned.
The major important thing here is that by using copy_unowned_value we can
guarantee that the non-ownership SIL ARC optimizer will treat the release
associated with the strong_retain_unowned as on a distinc rc-identity from its
argument. As an example of this problem consider the following SILGen like
output:

----
%1 = copy_value %0 : $Builtin.NativeObject
%2 = ref_to_unowned %1
%3 = copy_unowned_value %2
destroy_value %1
...
destroy_value %3
----

In this case, we are converting a strong reference to an unowned value and then
lifetime extending the value past the original value. After eliminating
ownership this lowers to:

----
strong_retain %0 : $Builtin.NativeObject
%1 = ref_to_unowned %0
strong_retain_unowned %1
strong_release %0
...
strong_release %0
----

From an RC identity perspective, we have now blurred the lines in between %3 and
%1 in the previous example. This can then result in the following miscompile:

----
%1 = ref_to_unowned %0
strong_retain_unowned %1
...
strong_release %0
----

In this case, it is possible that we created a lifetime gap that will then cause
strong_retain_unowned to assert. By not lowering copy_unowned_value throughout
the SIL pipeline, we instead get this after lowering:

----
strong_retain %0 : $Builtin.NativeObject
%1 = ref_to_unowned %0
%2 = copy_unowned_value %1
strong_release %0
...
strong_release %2
----

And we do not miscompile since we preserved the high level rc identity
pairing.

There shouldn't be any performance impact since we do not really optimize
strong_retain_unowned at the SIL level. I went through all of the places that
strong_retain_unowned was referenced and added appropriate handling for
copy_unowned_value.

rdar://41328987

**NOTE** I am going to remove strong_retain_unowned in a forthcoming commit. I
just want something more minimal for cherry-picking purposes.
2018-06-27 13:02:58 -07:00
Joe Groff
55706bfd44 SILGen: Pseudogeneric partial applications do not produce pseudogeneric results.
partial_apply always fully applies the generic environment, so the result is not generic at all. Fixes rdar://problem/41474371 | SR-8107.
2018-06-27 09:25:01 -07:00
Andrew Trick
df94dffab7 [NFC] Add SILBuilderContext.
In an upcoming bug fix, I want to pass SILBuilderContext to a
utility. I could continue reusing SILBuilder, even though the utility
must set its own insertion point and debug location. However, this is
terrible practice that I don't want to perpetuate.

The lifetime of a SILBuilder should correspond to a single insertion
point and debug location. That's the only sane way to preserve debug
information in SIL passes.

There are various pieces of contextual state that we've been adding to
the SILBuilder. Those have made it impossible to use SILBuilder
correctly. I'm pulling the context out, so clients can begin using
better practices. In the future, we can make SILBuilderContext
polymorphic, so passes can extend it easily with arbitrary
callbacks. We can also make it self-contained so we don't need to pass
around pointers to an InsertedInst list anymore.
2018-06-06 14:45:45 -07:00
Doug Gregor
4b5abbddbc [SIL] Teach *ApplyInst to traffic in SubstitutionMap.
Push SubstitutionMaps through most of SILGen and the SIL optimizers
that involve the various *ApplyInsts.
2018-05-11 13:18:06 -07:00
David Zarzycki
8c0c55539f [SIL] NFC: Rename misleading getSwiftRValueType() to getASTType()
Reference storage types are not RValues. Also, use more SILType helper
methods to avoid line wrap.
2018-05-04 08:14:38 -04:00
Joe Shajrawi
a539c8f11d SILType: rename isLoadableOrLowered to isLoadableOrOpaque 2018-03-27 13:43:57 -07:00
Joe Shajrawi
4fca6b482c Add isLoadableOrLowered to SILType for use in the Builder 2018-03-27 11:16:27 -07:00
Joe Shajrawi
b26023b433 Add asserts to SIL Instruction Builder functions that should only take loadable types. 2018-03-26 15:33:57 -07:00
Vedant Kumar
f9372fdb62 [DebugInfo] Improve stepping behavior for switch case stmts
Assign the location of a switch statement's subject expression to all of
its case statements.

This improves the debugger's stepping behavior in switch statements.
Stepping into a switch now goes directly to the first matching case
(possibly one with a `where` clause that may or may not match). It's
still possible to set breakpoints within `where` clauses.

rdar://35628672
2018-03-12 15:21:46 -07:00
Chris Lattner
415cd50ba2 Reduce array abstraction on apple platforms dealing with literals (#13665)
* Reduce array abstraction on apple platforms dealing with literals

Part of the ongoing quest to reduce swift array literal abstraction
penalties: make the SIL optimizer able to eliminate bridging overhead
 when dealing with array literals.

Introduce a new classify_bridge_object SIL instruction to handle the
logic of extracting platform specific bits from a Builtin.BridgeObject
value that indicate whether it contains a ObjC tagged pointer object,
or a normal ObjC object. This allows the SIL optimizer to eliminate
these, which allows constant folding a ton of code. On the example
added to test/SILOptimizer/static_arrays.swift, this results in 4x
less SIL code, and also leads to a lot more commonality between linux
and apple platform codegen when passing an array literal.

This also introduces a couple of SIL combines for patterns that occur
in the array literal passing case.
2018-01-02 15:23:48 -08:00
Chris Lattner
0c6e0c25a1 Two NFC changes:
- Add a DominanceInfo::dominates helper functions that takes two instructions,
  as analogues to the properlyDominates ones.
- Further generalize some SILBuilder code to be more defensive in the face
  of incorrect or partially constructed SIL instructions.

These are cleanups, NFC.
2017-12-19 20:18:24 -08:00
Slava Pestov
19daae40d0 SIL: Small fixes for emitShallowDestructure{Value,Address}Operation() 2017-11-19 23:58:48 -05:00
Michael Gottesman
8f5a4859f2 [sil] Add some more utilities for working with destructure operations.
Just slicing off some code from my pred-memopts patch since Slava needs this
functionality for some other work he is doing.
2017-11-19 17:24:33 -08:00
John McCall
5c33d2106a Add simple accessor/generator coroutine support to SILFunctionType. 2017-11-07 01:50:12 -05:00
Roman Levenstein
59f7bfe502 Provide a comment clarifying why there could be an infinite loop and how it is prevented 2017-09-25 10:44:10 -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
Roman Levenstein
7f4c4f7112 Fix the hang in the SILBuilder::addOpenedArchetypeOperands
The problem is that within SimplifyCFG we call this for an instruction within unreachable code. And within an unreachable block it can happen that defs do not dominate uses (because there is no dominance defined).

The fix is to prevent the infinite loop in addOpenedArchetypeOperands by marking visited instructions.

Fixes  rdar://problem/34602036
2017-09-24 16:52:34 -07:00
Erik Eckstein
1ab582e121 SIL: A new representation of static initializers for global variables.
Static initializers are now represented by a list of literal and aggregate instructions in a SILGlobalVariable.
For details see SIL.rst.

This representation is cleaner than what we did so far (point to the initializer function and do some pattern matching).

One implication of that change is that now (a subset of) instructions not necessarily have a parent function.
Regarding the generated code it's a NFC.
Also the swift module format didn't change because so far we don't serializer global variables.
2017-08-23 09:15:01 -07:00
Erik Eckstein
c6a466ca40 SIL: allow some literal and aggregate instructions to be constructed without a function context
just refactoring, NFC
2017-08-23 09:15:00 -07:00
Michael Gottesman
90289277e8 [sil] Add assert to SILBuilder::createValueMetatype to ensure that its type is a lowered type of our expected formal instanceType.
It makes it easier to track down this type of bug since the assert occurs in
SILBuilder right where the invalid ValueMetatype is created.

rdar://problem/31880847
2017-07-12 17:32:00 -07:00
Michael Gottesman
5fa67e0adb [sil] Change two SILInstruction::removeFromParent => SILInstruction::eraseFromParent.
removeFromParent should only be used when one is moving a SILInstruction from
one block to another or in preparation for deleting an instruction. In this
case, we are removing these two SILInstructions from their parent block and
leaking the instruction. The right thing to do is to use eraseFromParent.

<rdar://problem/31276565>
2017-06-06 10:12:37 -07:00
Andrew Trick
3b781c95d9 [Exclusivity] Minor fix for a theoretical bug in SILBuilder emitDestroy.
Given:
 %box = alloc_box
 %var = project_box %box, 0
 ...
 %access = begin_access [unknown] [read] %var
 ...<other>...
 copy_addr %access to ...
 end_access %access
 destroy_addr %box

Only fold
  begin_access [read] + copy_addr + end_access + destroy_addr
into
  begin_access [deinit] + copy_addr [take] + end_access

if the code in <other> has no side effects.

Fixes <rdar://problem/31920646> [Exclusivity] Fix the SILBuilder emitDestroy
peephole to generate correct access markers.
2017-05-12 15:33:19 -07:00
Andrew Trick
8187aae1b8 [Exclusivity] Handle copy_addr+destroy_addr folding with end_access markers. 2017-04-28 21:33:09 -07:00
practicalswift
ff827e0455 [gardening] Fix recently introduced typos 2017-04-25 21:03:44 +02:00
Roman Levenstein
202de40f05 [sil-opened-archetype-tracker] Improve tracking of archetypes in SILBuilder
Fixes rdar://problem/31749245
2017-04-24 08:50:55 -07:00
Roman Levenstein
a60e037c48 Revert "[sil-opened-archetype-tracker] Improve tracking of archetypes in SILBuilder" 2017-04-22 20:41:31 -07:00
Roman Levenstein
33c8bde859 [sil-opened-archetype-tracker] Improve tracking of archetypes in SILBuilder
Fixes rdar://problem/31749245
2017-04-22 10:03:37 -07:00
Slava Pestov
162b2d252e AST: Include gardening to minimize dependencies on Expr.h
A lot of files transitively include Expr.h, because it was
included from SILInstruction.h, SILLocation.h and SILDeclRef.h.

However in reality most of these files don't do anything
with Exprs, especially not anything in IRGen or the SILOptimizer.

Now we're down to 171 files in the frontend which depend on
Expr.h, which is still a lot but much better than before.
2017-03-12 22:26:56 -07:00
Mikio Takeuchi
488d531846 Enhance -assume-single-threaded option (SR-3945) 2017-02-27 12:17:53 +09:00
Slava Pestov
3519e0cd25 AST: Introduce new SubstitutionList type to replace ArrayRef<Substitution>
SubstitutionList is going to be a more compact representation of
a SubstitutionMap, suitable for inline allocation inside another
object.

For now, it's just a typedef for ArrayRef<Substitution>.
2017-02-06 21:36:33 -08:00
Andrew Trick
1abeddcc5d [SILType] SILFunctionConventions API.
Separate formal lowered types from SIL types.
The SIL type of an argument will depend on the SIL module's conventions.
The module conventions are determined by the SIL stage and LangOpts.

Almost NFC, but specialized manglings are broken incidentally as a result of
fixes to the way passes handle book-keeping of aruments. The mangler is fixed in
the subsequent commit.

Otherwise, NFC is intended, but quite possible do to rewriting the logic in many
places.
2017-01-26 15:35:48 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Joe Groff
b6823b930b SIL: Change SILType::subst to be SubstitutionMap-based.
This simplifies the SILType substitution APIs and brings them in line with Doug and Slava's refactorings to improve AST-level type substitution. NFC intended.
2016-12-14 14:33:32 -08:00
Michael Gottesman
96837babda Merge pull request #5920 from gottesmm/vacation_gardening
Vacation gardening
2016-11-25 09:17:21 -06:00
Michael Gottesman
a998d98924 [gardening] SILBasicBlock::splitBasicBlock() => *::split().
The BasicBlock suffix is redundant.
2016-11-25 01:14:43 -06:00
Michael Gottesman
bf6920650c [gardening] Drop BB from all argument related code in SILBasicBlock.
Before this commit all code relating to handling arguments in SILBasicBlock had
somewhere in the name BB. This is redundant given that the class's name is
already SILBasicBlock. This commit drops those names.

Some examples:

getBBArg() => getArgument()
BBArgList => ArgumentList
bbarg_begin() => args_begin()
2016-11-25 01:14:36 -06:00