Even when we don't know for sure if a bridged cast would succeed, we still want to lower it to produce a more efficient code that does not performs conformance checks at run-time.
This is useful when performing casts optimizations as a guaranteed optimization.
Swift SVN r27377
It was producing a checked_cast_add_br, which took a metatype inst as it's argument, which is simply wrong, as it is not an address type. SILVerifier was complaining about it. Now we produce an equivalent peephole, but with a correct instruction sequence, which makes SILVerifier happy.
Swift SVN r27375
Sometimes, during intermediate optimization steps, the operands of produced checked_cast_addr_br instructions do not actually have address types.
Don't try to optimize further in such cases. Let the optimizer clean-up those instructions.
Swift SVN r27373
I noticed under Instruments that the bridged casts opts produced a code that was leaking in some cases. Therefore I took the change to re-visit the code and to clean-up the logic for insertion of retains and releases.
Swift SVN r27371
When casts optimizations were lowering bridged casts from ObjC to Swift, they were producing internally an intermediate cast from an ObjC type into the expected ObjC type of a bridged cast, before converting this expected ObjC type into a Swift type. In many cases, this resulted in a useless intermediate cast where both types were the same and such a cast would be eliminated afterwards. E.g.
unconditional_checked_cast A to B // where A is ObjC type and B is a Swift type
was lowered into:
unconditional_checked_cast A to B. _ObjectiveCType // Often useless as A is equal to B. _ObjectiveCType already.
unconditional_checked_cast B._ObjectiveCType to B
This small inefficiency is fixed now. This problem was no observable from outside the optimization pass, it just required additional processing, therefore there is no test-case.
Swift SVN r27370
If a conformance to _BridgedToObjectiveC is statically known, generate a more efficient code by using the newly introduced library functions for bridging casts.
This covers the casts resulting from SIL optimizations.
Tests are included. I tried to cover most typical casts from ObjC types into Swift types and vice versa and to check that we always generate something more efficient than a checked_cast or unconditional_checked_cast. But probably even more tests should be written or generated by means of gyb files to make sure that nothing important is missing.
The plan is to make the bridged casts SIL optimization a guaranteed optimization. Once it is done, there is no need to lower the bridged casts in a special way inside Sema, because they all can be handled by the optimizer in a uniform way. This would apply to bridging of Error types too.
With this change, no run-time conformance checks are performed at run-time if conformances are statically known at compile-time.
As a result, the performance of rdar://19081345 is improved by about 15%. In the past, conformance checks in this test took 50% of its execution time, then after some improvements 15% and now it is 0%, as it should be.
Swift SVN r27102
swift::clearBlockBody() in Local.cpp was popping instructions rather
than erasing them, resulting in leaking any instructions removed via
this function (which is reached via removeDeadBlock(), called throughout
SimplifyCFG).
Also tweak a couple comments and remove an assert that cannot fire.
Swift SVN r27018
Add more checks and logic into emitSuccessfulIndirectUnconditionalCast and emitSuccessfulScalarUnconditionalCast, so that its clients in sil-combine can be simplified by avoiding looking into special cases.
Swift SVN r26885
threaded into IRGen; tests to follow when that's done.
I made a preliminary effort to make the inliner do the
right thing with try_apply, but otherwise tried to avoid
touching the optimizer any more than was required by the
removal of ApplyInstBase.
Swift SVN r26747
Use existing machinery of the generic specializer to produce generic specializations of closures referenced by partial_apply instructions. Thanks to the newly introduced ApplyInstBase class, the required changes in the generic specializer are very minimal.
rdar://19290942
Swift SVN r26582
We no longer need or use it since we can always refer to the same bit on
the applied function when deciding whether to inline during mandatory
inlining.
Resolves rdar://problem/19478366.
Swift SVN r26534
When creating a new apply, we were copying the transparent bit from the
old apply, as well as the referenced function from the old apply. We
should just use the transparent bit from that referenced function.
Swift SVN r26384
Transforms :
%1 = alloc_ref $B
checked_cast_br [exact] %1 : $B to $B, bb1, bb2
into
br bb1(%1 : $B)
In case when source and destination types do no match, replace by a branch to the failure bb of the checked_cast_br.
Swift SVN r26275
TerminatorInsts. Now you can walk over the successor list of a terminator
and actually modify the SILSuccessor directly, allowing better CFG
transformations. NFC.
Swift SVN r26140
This is mostly a re-factoring. It creates a new helper class CastOptimizer which contains all the logic for performing type casts optimizations. This logic is a copy of the current logic from sil-combine and simplify-cfg. Those passes will become clients of this new helper class.
Swift SVN r26122
This enables high-level SIL optimizations to speculatively specialize loops into
'fast native swift array' loops vs standard array loops.
This commits adds two semantic calls to Arrays.swift.
@semantics("array.props.isNative")
func _getArrayPropertyIsNative() -> Bool
@semantics("array.props.needsElementTypeCheck")
func _getArrayPropertyNeedsElementTypeCheck() -> Bool
checkSubscript and getElement get extra arguments to pass in the result of above
function calls. This also allow us to communicate that an array that was made
mutable is always native. The array subscript getter is changed to use the
result of these new calls:
public subscript(index: Int) -> Element {
get {
let isNative = _getArrayPropertyIsNative()
let needsTypeCheck = _getArrayPropertyNeedsElementTypeCheck()
_checkSubscript(index, isNative)
return _getElement(index, isNative, needsTypeCheck)
}
mutableAddressWithPinnedNativeOwner {
_makeMutableAndUniqueOrPinned()
// When an array was made mutable we know it is a backed by a native
// array buffer.
_checkSubscript(index, true)
return (_getElementAddress(index),
Builtin.tryPin(Builtin.castToNativeObject(_buffer.owner)))
}
}
High-level SIL optimizations will now see a loop like this:
func f(inout a : A[AClass]) {
for i in 0..a.count {
let b = a.props.isNative()
.. += _getElement(a, i, b)
}
}
After proving that ‘a’ does not escape other than to known safe functions, the
optimizer hoists the array property calls out of the loop in a loop versioned
on the array property.
func f(inout a : A[AClass]) {
let b2 = a.props.isNative()
if (!b2) {
for i in 0..a.count {
.. += _getElement(a, i, false)
}
} else {
for i in 0..a.count {
let b = a.props.isNative
.. += _getElement(a, i, b)
}
}
}
rdar://17955309
Swift SVN r25698
In every instance, we were just creating the StringConcatenationOptimizer and
then invoking optimize on it. This is a cleaner solution since the details of
how we perform the string concatenation are hidden in Local.cpp instead of being
in a header.
NFC.
Swift SVN r25341
This refactors some code out of AllocBoxToStack that computes the
lifetime of a value in the strictess sense, limiting the lifetime to
that value and not anything derived from that value (whether by casting,
projection, etc.).
In the short term this will be used to fix a very rarely hit
optimization in AllocBoxToStack.
Longer term I will replace the other similar code in AllocBoxToStack to
use this instead.
Swift SVN r25176
This utility attempts to delete dead closures with a set of
post-dominating releases using the infrastructure from
getFinalReleasesForValue.
It currently only will eliminate closures that only have retain, release
uses and a final post-dominating release set.
The reason why we need the final post-dominating release set is so that
we can release any captured variables at the points where we would have
deallocated the release. This is b/c captured variables are passed in at
+1 to partial apply.
Swift SVN r25050
Array.init does not have a self argument (it returns the newly created array
@owned). Passes using the ArraySemantic::getSelf() interface must handle it
specially.
rdar://19724033
Swift SVN r25013
Till now, -debug-values-propagate-liveness was only taken into account if the compiler was built with assertions. This led to some problems with bots, which were testing both assertions-enabled and assertions-disables versions of the compiler. It was agreed that we want this experimental flag to be always taken into account when it is provided on the command-line. The default value of this flag is "disabled".
rdar://19674999
Swift SVN r24977
We can not remove unused global initializer function calls. They might have
side-effects.
I measured no regressions at -O.
rdar://19464274
Swift SVN r24513
1. Eliminate unused variable warnings.
2. Change field names to match capitalization of the rest of the field names in the file.
3. Change method names to match rest of the file.
4. Change get,set method for a field to match the field type.
Swift SVN r24501
In order to not completely loose testcoverage while rdar://problem/18709125
is under investiagtion, add a special flag for enabling debug value
liveness.
Patch by Michael Gottesman!
<rdar://problem/19267059>
Swift SVN r24416