Commit Graph

682 Commits

Author SHA1 Message Date
Raj Barik
ff5a8009fa Refactor SILCombiner to build ConcreteExistentialInfo using an Uniform interface that first uses Data-Flow analysis and then uses ProtocolConformanceAnalysis 2018-11-27 11:54:35 -08:00
Erik Eckstein
c7593fb97e SILCombine: fix an assert in the optimization which promotes a concrete type of an existential
rdar://problem/45915705
2018-11-08 16:57:24 -08:00
Andrew Trick
f19d91d15a Fix SILCombiner::propagateConcreteTypeOfInitExistential.
A SIL argument index was being passed as an AST function type
parameter index. Don't do this.

I also cleaned up the peephole to avoid processing indirect results as
parameter indices, but that part is NFC.

Fixes rdar://45415719 Assertion failed: (Index < Length && "Invalid index!")
2018-10-23 18:44:16 -07:00
Erik Eckstein
54399f97d3 SILCombine: Optimize a sequence of conversion of an builtin integer to and from BridgeObject
This sequence appears in the new UTF8 String implementation.
2018-10-16 08:20:06 -07:00
Raj Barik
3c5b13dd9e Propagate concrete types for all arguments of an apply instruction 2018-09-12 17:57:08 -07:00
Raj Barik
9a507641fe Propagate concrete types for all arguments of an apply instruction 2018-09-12 17:45:19 -07:00
Robert Widmann
014fd952ef [NFC] Silence a bunch of Wunused-variable diagnostics 2018-08-24 15:16:40 -07:00
Erik Eckstein
99a9ed5535 SIL: remove the pinning instructions: strong_pin, strong_unpin, is_unique_or_pinned
They are not used anymore after removing the pinning addressors.
2018-08-23 12:47:56 -07:00
Raj Barik
d9a051ecdc Concrete type propagation using ProtocolConformanceAnalysis 2018-08-22 11:08:29 -07:00
Raj Barik
971772d0a9 Merge pull request #18109 from rajbarik/raj-refactor
Extend SILCombiner code to handle existential self concrete type propagation using ProtocolConformanceAnalysis
2018-08-13 09:56:56 -07:00
Michael Gottesman
f35a2a3cf8 [sil-opt] Only notify the pass manager of newly added functions in SILOptFunctionBuilder.
To do so this commit does a few different things:

1. I changed SILOptFunctionBuilder to notify the pass manager's logging
functionality when new functions are added to the module and to notify analyses
as well. NOTE: This on purpose does not put the new function on the pass manager
worklist since we do not want to by mistake introduce a large amount of
re-optimizations. Such a thing should be explicit.

2. I eliminated SILModuleTransform::notifyAddFunction. This just performed the
operations from 1. Now that SILOptFunctionBuilder performs this operation for
us, it is not needed.

3. I changed SILFunctionTransform::notifyAddFunction to just add the function to
the passmanager worklist. It does not need to notify the pass manager's logging
or analyses that a new function was added to the module since
SILOptFunctionBuilder now performs that operation. Given its reduced
functionality, I changed the name to addFunctionToPassManagerWorklist(...). The
name is a little long/verbose, but this is a feature since one should think
before getting the pass manager to rerun transforms on a function. Also, giving
it a longer name calls out the operation in the code visually, giving this
operation more prominance when reading code. NOTE: I did the rename using
Xcode's refactoring functionality!

rdar://42301529
2018-08-06 18:27:24 -07:00
Michael Gottesman
b72304415d [passmanager] Change the optimizer to use SILOptFunctionBuilder.
I am going to add the code in a bit that does the notifications. I tried to pass
down the builder instead of the pass manager. I also tried not to change the
formatting.

rdar://42301529
2018-08-05 21:21:55 -07:00
Raj Barik
484925c26f Extend SILCombiner code to handle existential self concrete type propagation using ProtocolConformanceAnalysis 2018-08-01 14:25:52 -07:00
Bob Wilson
8e330ee344 NFC: Fix indentation around the newly renamed LLVM_DEBUG macro.
Jordan used a sed command to rename DEBUG to LLVM_DEBUG. That caused some
lines to wrap and messed up indentiation for multi-line arguments.
2018-07-21 00:56:18 -07:00
Jordan Rose
cefb0b62ba Replace old DEBUG macro with new LLVM_DEBUG
...using a sed command provided by Vedant:

$ find . -name \*.cpp -print -exec sed -i "" -E "s/ DEBUG\(/ LLVM_DEBUG(/g" {} \;
2018-07-20 14:37:26 -07:00
Ben Cohen
2b04e9f105 Suppress a number of warnings in no-assert builds (#17721)
* Supress a number of warnings about things used only in asserts

* Re-use a couple of variables instead of supressing the warning
2018-07-04 07:15:14 -07:00
Michael Gottesman
9e70b855e4 [cmake] Use a helper macro to simplify adding sources to the SILOptimizer library.
All this does is automate the creation of the ${DIRNAME}_SOURCES variables that we already create and allows for the author to avoid having to prefix with the directory name, i.e.:

set(FOOBAR_SOURCES
  FooBar/Source.cpp
  PARENT_SCOPE)

=>

silopt_register_sources(
  Source.cpp)

Much easier and cleaner to read. I put the code that implements this in the
CMakeLists.txt file just for the SILOptimizer.
2018-06-27 17:48:59 -07:00
Slava Pestov
13e1c83410 SILCombiner: Remove bogus dump() call (#17553) 2018-06-27 01:14:10 -07:00
Andrew Trick
9d4b4c755c Rewrite SILCombiner::propagateConcreteTypeOfInitExistential. (#17315)
Fixes <rdar://40555427> [SR-7773]:
SILCombiner::propagateConcreteTypeOfInitExistential fails to full propagate type
substitutions.

Fixes <rdar://problem/40923849>
SILCombiner::propagateConcreteTypeOfInitExistential crashes on protocol
compositions.

This rewrite fixes several fundamental bugs in the SILCombiner optimization that
propagates concrete types. In particular, the pass needs to handle:

- Arguments of callee Self type in non-self position.
- Indirect and direct return values of Self type.
- Types that indirectly depend on Self within callee function signature.
- Protocol composition existentials.
- All of the above need to work for protocol extensions as well as witness methods.
- For protocol extensions, conformance lookup should be based on the existential's conformance list.

Additionally, the optimization should not depend on a SILFunction's DeclContext,
which is not serialized. (In fact, we should prevent SIL passes from using
DeclContext). Furthermore, the code needs to be expressed in a way that one can
reason about correctness and invariants.

The root cause of these bugs is that SIL passes are written based on untested
assumptions of Swift type system. A SIL pass needs to handle all verifiable SIL
input because passes need to be composable. Bail-out logic can be added to
simplify the design; however, _the bail-out logic itself cannot make any
assumptions about the language or type system_ that aren't clearly and
explicitly enforced in the SIL verifier. This is a common mistake and major
source of bugs.

I created as many unit tests as I reasonably could to prevent this code from
regressing. Creating enough unit tests to cover all corner cases that were
broken in the original code would be intractable. But the code has been
simplified such that many corner cases disappear.

This opens up some oportunity for generalizing the optimization and eliminating
special cases. However, I want this PR to be limited to fixing correctness
issues only. In the long term, it would be preferable to replace this
optimization entirely with a much more powerful general type propagation pass.
2018-06-26 19:33:31 -07:00
Dante Broggi
43f79dfba8 fix a code example found looking through commits 2018-06-08 06:54:57 -04:00
Erik Eckstein
bb5fe18300 Add/fix comments as a follow-up to the string constants optimization changes. 2018-06-07 16:22:04 -07:00
Erik Eckstein
52a27d10fa SILCombine: propagate the extra-bits of StringObjects if they are read right after creation.
For example:
    %0 = string_literal "abc"
    %1 = integer_literal 0x8000000000000000
    %2 = builtin "stringObjectOr_Int64" (%0, %1)
    %3 = integer_literal 0x4000000000000000
    %4 = builtin "and_Int64" (%2, %3)

In this case we know that %4 is 0.
2018-06-07 13:44:03 -07:00
Erik Eckstein
71b6c82230 SILCombine: optimize loading bytes from a string literal 2018-06-07 13:43:34 -07:00
Arnold Schwaighofer
65e9593216 SILCombine: Can't replace a alloc_ref_dynamic with an alloc_ref if the metatype is generic
rdar://40853265
SR-7896
2018-06-06 12:04:32 -07:00
Raj Barik
c277c90be6 Reorganizing the code to find init_existential; Move them to Existential.cpp/h in order for other passes such as ExistentialSpecializer to use it apart from SILCombiner 2018-06-04 11:07:12 -07:00
Erik Eckstein
a2b0e8f96b SILCombine: make the value_to_bridge_object peephole less conservative.
We can always eliminate ARC operations on the result of a value_to_bridge_object instruction.
There is no need to restrict the operand to a specific SIL pattern.
2018-05-29 11:01:03 -07:00
Slava Pestov
bd6281c558 AST: Change SubstitutionMap conformance lookup callbacks to take ProtocolDecl and not ProtocolType 2018-05-19 01:09:17 -07:00
Doug Gregor
ef020c74aa Eliminate all vestiges of Substitution and SubstitutionList.
Introduced during the bring-up of the generics system in July, 2012,
Substitution (and SubstitutionList) has been completely superseded by
SubstitutionMap. R.I.P.
2018-05-11 21:43:40 -07:00
Doug Gregor
911ed60a98 Eliminate dead code making use of SubstitutionList. 2018-05-11 17:37:27 -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
Slava Pestov
e85b68e151 SILOptimizer: Remove unused variable 2018-05-09 22:49:36 -04:00
Erik Eckstein
df6a3af5a5 SILCombine: remove a problematic cast peephole optimization.
This optimization inserted bit casts based on structural properties of types.
It caused a miscompile in case of imported C structs. Also, by inspection, I found that checks for resilient types are missing.
As this optimization does not have any noticeable impact on the benchmarks it's better to remove it at all, together with the complexity for checking the types.

rdar://problem/40074362
2018-05-09 13:28:20 -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
Doug Gregor
408aaa5332 [SIL] Use SubstitutionMap in BuiltinInst. 2018-05-03 08:48:55 -07:00
Doug Gregor
d2cf60c465 Revert "[SIL] Replace more SubstitutionLists with SubstitutionMap" 2018-05-03 08:35:20 -07:00
Doug Gregor
ed1983d9d0 [SIL] Use SubstitutionMap in BuiltinInst. 2018-05-03 00:05:21 -07:00
Raj Barik
612d771b9e Handle global_addr for concrete type propagation 2018-04-30 14:38:13 -07:00
Davide Italiano
b4d563802b [SILInstruction] Introduce isDebugInstruction().
This is a property of an instruction and should be a member
function of `SILInstruction` and not a free function in
`DebugUtils`. Discussed with Adrian.
2018-04-11 10:14:21 -07:00
Andrew Trick
9703d56e03 [exclusivity] Remove dead access markers after optimization.
Generalized to handle scope markers which will become common with future
ownership and lifetime dependence markers.
2018-03-11 23:13:30 -07:00
Andrew Trick
e7b63e53b8 Fix closure inlining after witness devirtualization.
Certain patterns of directly applied partial_apply's were not being
inlined. This can happen when a closure is defined in the
implementation of a generic witness method. I noticed this issue while
debugging SR-6254: "Fix (or explain) strange ways to make code >3
times faster/slower".

After the witness method is devirtualization and specialized, the
closure application looks like this:

  %pa = partial_apply [callee_guaranteed] %fn() : $@convention(thin) (@in Int) -> @out Int
  %cvt = convert_escape_to_noescape %pa
  apply %cvt(...)

SILCombine already removes the partial_apply and convert_escape_to_noescape generating:

  %thick = thin_to_thick %fn
  apply %thick(...)

However, surprisingly, neither the inliner nor SILCombine can handle this.

I cleaned up the code in SILCombine's apply visitor that handles
thin_to_thick and generalized it to handle this and other patterns.

I also added a restriction to this code so that it doesn't break SIL
ownership in the future, as I discussed with Arnold.
2018-03-08 13:36:59 -08:00
Erik Eckstein
1c20806d44 Revert "Fix closure inlining after witness devirtualization."
This reverts commit 8990ed4f78.
2018-03-07 16:48:05 -08:00
Andrew Trick
8990ed4f78 Fix closure inlining after witness devirtualization.
Certain patterns of directly applied partial_apply's were not being
inlined. This can happen when a closure is defined in the
implementation of a generic witness method. I noticed this issue while
debugging SR-6254: "Fix (or explain) strange ways to make code >3
times faster/slower".

After the witness method is devirtualization and specialized, the
closure application looks like this:

  %pa = partial_apply [callee_guaranteed] %fn() : $@convention(thin) (@in Int) -> @out Int
  %cvt = convert_escape_to_noescape %pa
  apply %cvt(...)

SILCombine already removes the partial_apply and convert_escape_to_noescape generating:

  %thick = thin_to_thick %fn
  apply %thick(...)

However, surprisingly, neither the inliner nor SILCombine can handle this.

I cleaned up the code in SILCombine's apply visitor that handles
thin_to_thick and generalized it to handle this and other patterns.

I also added a restriction to this code so that it doesn't break SIL
ownership in the future, as I discussed with Arnold.
2018-03-07 11:25:19 -08:00
Arnold Schwaighofer
390ba419fc Add an effects(releasenone) function effects attribute
A ``@effects(releasenone)`` function might read/write global state but does not
perform a release.
2018-03-05 07:03:54 -08:00
Adrian Prantl
9b6a9946ec Be explicit about whether a DebugInfo-carying SILInstruction has debug info.
This patch both makes debug variable information it optional on
alloc_stack and alloc_box instructions, and forced variable
information on debug_value and debug_value_addr instructions. The
change of the interface uncovered a plethora of bugs in SILGen,
SILTransform, and IRGen's LoadableByAddress pass.

Most importantly this fixes the previously commented part of the
DebugInfo/local-vars.swift.gyb testcase.

rdar://problem/37720555
2018-02-21 10:50:19 -08:00
Arnold Schwaighofer
a9fa8c9c2b Merge pull request #14514 from aschwaighofer/wip_closure_capture_abi_part2
WIP: Closure ABI - Make @noescape Swift closures trivial
2018-02-14 05:19:25 -08:00
Dante Broggi
943033ef32 Fix typo in assert: Excted to Expected (#14619) 2018-02-13 18:21:14 -08:00
Arnold Schwaighofer
a05f0398a1 SILCombiner: Fixes for convert_escape_to_noescape
- also combine thin_to_thick_function and convert_escape_to_noescape
2018-02-13 04:19:59 -08:00
Joe Shajrawi
3675019184 [SILCombiner] Remove strong retains and strong releases of tagged strings. A retain/release of a string literal is a no-op. 2018-02-13 11:45:01 +02:00
Andrew Trick
113bebb035 Centralize logic for access marker and exclusivity verification.
Create helpers in InstructionUtils.h wherever we need a guarantee that the diagnostics cover the same patterns as the verifier. Eventually this will be called from both SILVerifier and the diagnostic pass:
- findAccessedAddressBase
- isPossibleFormalAccessBase
- isPartialApplyOfReabstractionThunk
- findClosureForAppliedArg
- visitAccessedAddress

Add partial_apply verification assert.

This applies the normal "find a closure" logic inside the "find all partial_apply uses" verification. Making the verifier round-trip ensures that we don't have holes in exclusivity enforcement related to this logic.
2018-02-05 18:43:30 -08:00
Erik Eckstein
4167fa00d5 SILCombiner: fix a non-determinism in a peephole optimization for load instructions. 2018-02-01 12:15:46 -08:00