Commit Graph

682 Commits

Author SHA1 Message Date
Michael Gottesman
dfd40e4443 [cast-optimizer] Move the cast optimizer into its own .cpp file.
Local.cpp was ~3k lines of which 1.5k (i.e. 1/2) was the cast optimizer. This
commit extracts the cast optimizer into its own .cpp and .h file. It is large
enough to stand on its own and allows for Local.cpp to return to being a small
group of helper functions.

I am making some changes in this area due to the change in certain function
conventions caused by the +0-normal-arg work. I am just trying to leave the area
a little cleaner than before.
2018-01-13 18:26:22 -08:00
Andrew Trick
abedb192bc Rename canUnsafeCastValue to checkABIForUnsafeScalarCast and comment.
This is an ABI-dependent routine, so at least should have ABI in the name.  In
the future, the compiler may introduce new ABI layout rules for select types
based on the deployment target. Code like this needs to be reviewed at that time.

Clarify the comments.
2018-01-08 14:17:36 -08: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
3761ee3f1b Remove a transformation introduced in PR13652 which fails on the iOS
simulator in a way I can't repro.  Reimplement the important part of it
in a new way to achieve the same effect.
2018-01-01 07:45:54 -08:00
Chris Lattner
1e63108998 Speculative fix for a regression on the
"oss-swift_tools-RA_stdlib-RD_test-simulator" iOS simulator bot.
2017-12-31 20:13:58 -08:00
Chris Lattner
de289752fe Implement a few silcombine transformations for arrays (#13652)
* Implement a few silcombine transformations for arrays

 - Useless existential_ref <-> class conversions.
 - mark_dependence_inst depending on uninteresting instructions.
 - release(init_existential_ref(x)) -> release(x) when hasOneUse(x)
 - Update COWArrayOpt to handle the new forms generated by this.

these aren't massive performance wins, but do shrink the size of SIL when
dealing with arrays.

* Generalize testcase to work on linux and on mac when checking stdlib is enabled.
2017-12-30 22:30:37 -08:00
John McCall
b13f30ff30 Move a convenience API for changing a SILFunctionType into the AST. NFC. 2017-12-15 18:19:07 -05:00
Andrew Trick
32a8dbcfa8 Fix a dangling stack pointer in SILCombine.
The bug was introduced recently:
commit ac8a48b2
Date:   Fri Oct 6 12:34:14 2017 -0700

Fixes <rdar://35800315> (crash using freed OpenedArchetypesTracker).
2017-12-04 10:04:48 -08:00
Andrew Trick
708aadfada Cleanup SILCombine code in preparation for a bug fix.
This is not NFC because the isUseAfterFree helper and surrounding code
is rewritten. The previous code's intention is unclear, but it was at
best imprecise and unsafe w.r.t. future SIL changes.

Pattern matching code that becomes highly complicated should
be commented with motivating SIL examples.
2017-12-04 10:04:48 -08:00
Slava Pestov
1f79af7504 SIL: Use objc_method instruction for Objective-C protocol method calls
Fixes <rdar://problem/15933365>.
2017-11-29 16:26:43 -08:00
Arnold Schwaighofer
ea9907ae15 Revert "SIL: Use objc_method instruction for Objective-C protocol method calls" 2017-11-29 11:19:46 -08:00
Slava Pestov
1ee0970934 SIL: Use objc_method instruction for Objective-C protocol method calls
Fixes <rdar://problem/15933365>.
2017-11-29 01:22:05 -08:00
Slava Pestov
fb2a6d2025 SILOptimizer: Fix convert_function -> apply peephole for metatype conversions
Fixes <rdar://problem/35492775>.
2017-11-13 22:10:14 -08:00
Arnold Schwaighofer
3d349ae2f8 SILCombine: Fix @callee_guaranteed combine of try_apply
I missed another place where we were releasing the context.

SR-5441
rdar://33255593
2017-11-10 15:18:01 -08:00
Arnold Schwaighofer
1c67e1ebb3 Use emitDestroyValueOperation instead of createStrongRelease 2017-11-04 15:13:49 -07:00
Arnold Schwaighofer
b5fab6145f SILCombiner: Fix partial_apply optimization of @callee_guaranteed
closures

@callee_guaranteed closure contexts must not be released when replacing
a closure application.

SR-5441
rdar://33255593
2017-11-04 14:03:56 -07:00
Huon Wilson
0236db7be1 [SIL] Witness methods store the conformance from which they come. 2017-11-01 11:33:26 -07:00
swift-ci
03cef65d75 Merge pull request #12570 from lattner/nfc-headers 2017-10-25 10:03:32 -07:00
Joe Shajrawi
8fdca7c5a6 Relax SILCombiner's archtype tracker: better existential_metatype support 2017-10-23 16:27:44 -07:00
Chris Lattner
98f137ed3e Fix a comment header, trim #includes. NFC. 2017-10-22 16:32:18 -07:00
Andrew Trick
d369aa4070 Support @noescape SIL function types. (#12420)
Support for @noescape SILFunctionTypes.

These are the underlying SIL changes necessary to implement the new
closure capture ABI.

Note: This includes a change to function name mangling that
primarily affects reabstraction thunks.

The new ABI will allow stack allocation of non-escaping closures as a
simple optimization.

The new ABI, and the stack allocation optimization, also require
closure context to be @guaranteed. That will be implemented as the
next step.

Many SIL passes pattern match partial_apply sequences. These all
needed to be fixed to handle the convert_function that SILGen now
emits. The conversion is now needed whenever a function declaration,
which has an escaping type, is passed into a @NoEscape argument.

In addition to supporting new SIL patterns, some optimizations like
inlining and SIL combine are now stronger which could perturb some
benchmark results.

These underlying SIL changes should be merged now to avoid conflicting
with other work. Minor benchmark discrepancies can be investigated as part of
the stack-allocation work.

* Add a noescape attribute to SILFunctionType.

And set this attribute correctly when lowering formal function types to SILFunctionTypes based on @escaping.

This will allow stack allocation of closures, and unblock a related ABI change.

* Flip the polarity on @noescape on SILFunctionType and clarify that
we don't default it.

* Emit withoutActuallyEscaping using a convert_function instruction.

It might be better to use a specialized instruction here, but I'll leave that up to Andy.

Andy: And I'll leave that to Arnold who is implementing SIL support for guaranteed ownership of thick function types.

* Fix SILGen and SIL Parsing.

* Fix the LoadableByAddress pass.

* Fix ClosureSpecializer.

* Fix performance inliner constant propagation.

* Fix the PartialApplyCombiner.

* Adjust SILFunctionType for thunks.

* Add mangling for @noescape/@escaping.

* Fix test cases for @noescape attribute, mangling, convert_function, etc.

* Fix exclusivity test cases.

* Fix AccessEnforcement.

* Fix SILCombine of convert_function -> apply.

* Fix ObjC bridging thunks.

* Various MandatoryInlining fixes.

* Fix SILCombine optimizeApplyOfConvertFunction.

* Fix more test cases after merging (again).

* Fix ClosureSpecializer. Hande convert_function cloning.

Be conservative when combining convert_function. Most of our code doesn't know
how to deal with function type mismatches yet.

* Fix MandatoryInlining.

Be conservative with function conversion. The inliner does not yet know how to
cast arguments or convert between throwing forms.

* Fix PartialApplyCombiner.
2017-10-17 13:07:25 -07:00
Joe Shajrawi
39c7612cca Merge pull request #12411 from shajrawi/silcombine_bugfix
SILCombiner: fix an infinite loop corner-case
2017-10-12 17:05:28 -07:00
Joe Shajrawi
bdafb354bd SILCombiner: fix an infinite loop corner-case 2017-10-12 16:34:31 -07:00
Doug Gregor
cd3c63cbfd [AST] Stop including GenericSignature.h in other headers.
Except GenericEnvironment.h, because you can't meaningfully use a
GenericEnvironment without its signature. Lots less depends on
GenericSignature.h now. NFC
2017-10-12 14:23:46 -07:00
Joe Shajrawi
ac8a48b2c2 Fixes a bug in SILCombiner that caused a use-after-free 2017-10-10 13:40:14 -07:00
Erik Eckstein
a51d9b950b SILCombine: fix propagation of existential self into witness method call
In case of a mutating method call we replaced the self argument with the source of a copy_addr. This let the call modify the wrong self value.

rdar://problem/34753633
2017-10-02 16:04:21 -07:00
Roman Levenstein
d9d58bc6c5 [sil-combine] Fix a bug in the convert_function peephole
Bail if the return type of the converted function is different from the
return type of the apply instruction.

Fixes rdar://problem/34603304
2017-09-28 09:03:40 -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
Arnold Schwaighofer
8a85a9efd5 Use array copy runtime implementation instead of the array value witnesses
And add builtins for the added runtime functions (assign-take, assign-copy).

rdar://27412867
SR-3376
2017-09-12 12:43:26 -07:00
Slava Pestov
de0a7a066a SILCombiner: Remove another usage of ProtocolConformanceRef::getInherited()
This codepath consumed the result of getConformanceAndConcreteType(),
which already maps inherited conformances to exact conformances.
2017-09-01 00:46:18 -07:00
Slava Pestov
96ee1157f7 SILCombiner: Use ASTContext::getExistentialSignature()
This addresses the unsupported case outlined in the FIXME.

We have an init_existential_{addr,ref} instruction which
constructs an existential of type R from a value whose
concrete type conforms to R.

Later on, we have a witness_method invoking a method of P,
where R refines Q refines P.

In order to give the witness_method instruction the correct
conformance after substituting the concrete type, we would
call ProtocolConformanceRef::getInherited().

However, this only supported a single level of protocol
refinement, so we would just bail out in the unsupported
case. Instead, this patch builds a SubstitutionMap from the
generic signature <T : R>, and uses the SubstitutionMap to
perform the conformance lookup of T to P.
2017-09-01 00:46:18 -07:00
Slava Pestov
6607058f7b SILCombiner: Clean up getConformanceAndConcreteType()
Instead of using an out parameter and a tuple return value,
return everything in the tuple.
2017-09-01 00:46:18 -07:00
Roman Levenstein
87719df70c Support concrete type propagation into witness_method instructions only for directly inherited conformances for now
Indirectly inherited conformances cannot be properly handled yet due to some representation issues with init_existential_* instructions.

This fixes a compiler crash reported in rdar://34022255.
2017-08-30 17:00:08 -07:00
Erik Eckstein
020a283bbb SIL: Let the OpenedArchetypesTracker be constructed with a null SILFunction.
NFC
2017-08-23 09:15:00 -07:00
Roman Levenstein
f802fe4829 [sil-combine] Minor improvement to the peephole for removal of alloc_ref instructions that are only allocated and then deallocated 2017-08-01 15:23:23 -07:00
Michael Gottesman
3aadede925 [sil-combine] Make sure that (apply (partial_apply)) -> (apply') bails if we have an argument with a dependent type.
What is going on here is that currently this optimization if itneeds to perform
lfietime extension always creates an alloc_stack at the beginning/end of a
function. If the object whose lifetime is being extended has a dependent type,
then the alloc_stack will be created before the dependent type exists resulting
in the compiler crashing.

rdar://33595317
2017-07-28 17:20:54 -07:00
Michael Gottesman
fcafd6baad [gardening] Reflow some comments to make them easier to read. NFC. 2017-07-28 13:06:30 -07:00
Saleem Abdulrasool
8e0b43afd0 SILOptimizer: include used header
Include headers that are used in the file.
2017-07-24 13:41:23 -07:00
Michael Gottesman
5a2556eeca [sil-combine] When deleting a dead partial_apply, insert a destroy for all non-trivial captured values.
partial_apply is a confusing instruction since it:

1. Is printed with a function signature.
2. Takes in some arguments of the same type as the underlying types of the given function signatures.
3. Always takes in those arguments at +1 regardless of the convention printed on the partial apply.

Eventually we will split the partial apply representation so that the box is
represnted explicitly separately from the function signature, eliminating this
confusion.

The problem that we ran into here is that we were not treating @in values from
an alloc_stack or @in_guaranteed at all correctly. The reason why the tests did
not catch this is that a seperate sil_combine optimization that eliminates
trivially dead live ranges was eliminting the alloc_stack of the @in value in
our test. In contrast, the @in_guaranteed case was actually never tested at all.

I added tests for all of these conventions and in addition added a special mode
to SILCombine that stops the alloc_stack eliminating during testing.

rdar://32887993
2017-07-06 17:01:43 -07:00
Robert Widmann
3b202c18d8 Use 'hasAssociatedValues'
Use 'hasAssociatedValues' instead of computing and discarding the
interface type of an enum element decl.  This change has specifically not
been made in conditions that use the presence or absence of the
interface type, only conditions that depend on the presence or absence
of associated values in the enum element decl.
2017-05-22 09:54:47 -07:00
Roman Levenstein
f909858f53 Remove dead code that became obsolete after re-factoring 2017-05-10 08:04:28 -07:00
Roman Levenstein
45c2c4af0e Re-factoring: Get rid of useless arguments in "create*Apply" functions
Till now createApply, createTryApply, createPartialApply were taking some arguments like SubstCalleeType or ResultType. But these arguments are redundant and can be easily derived from other arguments of these functions. There is no need to put the burden of their computation on the clients of these APIs.

The removal of these redundant parameters simplifies the APIs and reduces the possibility of providing mismatched types by clients, which often happened in the past.
2017-05-10 08:03:37 -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
Joe Shajrawi
0d0cac357a retain_value_addr and release_value_addr SIL instructions: take as an input an address, load the value inside it and call retain_value and release_value respectively 2017-04-30 10:23:55 -07:00
Joe Shajrawi
d17258cac7 @in_constant calling convention - part of passing large loadable types by address 2017-04-30 10:13:02 -07:00
Erik Eckstein
d4cbc48e40 SILCombine: replace switch_value of Builtin.Int1 with a cond_br
cond_br is more common and can be handled by more optimizations
2017-04-27 09:06:55 -07:00
Roman Levenstein
48b1f16f0b Simplify code 2017-04-26 23:07:48 -07:00
Roman Levenstein
e144d0f40e [sil-combine] Do not form the SubstitutionList by hand. Use SubstitutionMap APIs for it. 2017-04-26 23:07:41 -07:00
Roman Levenstein
28d7f80c24 [sil-combine] Fix a subtle bug in getConformanceAndConcreteType related to handling of inherited conformances
This fixes a couple of tests and some of the compatibility suite projects.
rdar://problem/31815540, rdar://problem/31838976
2017-04-26 23:07:32 -07:00
Roman Levenstein
616ef49d57 [sil-combine] Handle more patterns in cond_br conditions
Add the following peepholes:
cond_br(x == 0), t_label, f_label -> cond_br x, f_label, t_label
cond_br (x != 1), t_label, f_label -> cond_br x, f_label, t_label
2017-04-20 15:27:49 -07:00