Commit Graph

682 Commits

Author SHA1 Message Date
Meghana Gupta
e5e61036e3 Update SILCombine now that OSSA RAUW always inserts borrow before the value to be replaced 2023-03-23 11:45:55 -07:00
Andrew Trick
119e712c32 Merge pull request #64534 from atrick/liveblocks-bitfield
Cleanup PrunedLiveBlocks
2023-03-22 16:33:08 -07:00
swift-ci
42c69cce99 Merge pull request #64533 from meg-gupta/fixsilcombineptai
Fix SILCombine of pointer_to_address instruction
2023-03-22 15:00:42 -07:00
Meghana Gupta
3812dad9b9 Fix SILCombine of pointer_to_address instruction
This change ensures the insertion point of the unchecked_addr_cast is after
any copies/borrows that OSSA rauw may create.

Before this fix we could end up creating invalid SIL where operands do not
dominate its instruction.
2023-03-22 12:42:09 -07:00
Andrew Trick
15796e3ff9 PrunedLiveness: add a SILFunction argument
So that liveness can migrate to using a SILBitfield.
2023-03-22 01:36:48 -07:00
Erik Eckstein
a092ecb5c2 remove SILBridgingUtils.h 2023-03-21 15:33:09 +01:00
eeckstein
60378e148f Merge pull request #64233 from eeckstein/optimize-convert-function
SILCombine: handle `convert_escape_to_noescape` in the apply-of-convert-function optimization
2023-03-13 12:46:40 +01:00
Meghana Gupta
89161f9d0f Disable SILCombine::visitInjectEnumAddrInst for empty tuple types. 2023-03-09 15:15:20 -08:00
Erik Eckstein
9deb9423ac SILCombine: handle convert_escape_to_noescape in the apply-of-convert-function optimization.
This fixes a bad optimization deficiency for dictionary subscript lookups with default values: there shouldn't be a closure context allocated.

rdar://106423763
2023-03-09 14:38:14 +01:00
Erik Eckstein
dd95922006 SILCombine: fix a problem in visitDifferentiableFunctionExtractInst
fixes a crash when trying to optimize ABI incompatible function types
2023-03-09 14:38:14 +01:00
John McCall
d25a8aec8b Add explicit lowering for value packs and pack expansions.
- SILPackType carries whether the elements are stored directly
  in the pack, which we're not currently using in the lowering,
  but it's probably something we'll want in the final ABI.
  Having this also makes it clear that we're doing the right
  thing with substitution and element lowering.  I also toyed
  with making this a scalar type, which made it necessary in
  various places, although eventually I pulled back to the
  design where we always use packs as addresses.

- Pack boundaries are a core ABI concept, so the lowering has
  to wrap parameter pack expansions up as packs.  There are huge
  unimplemented holes here where the abstraction pattern will
  need to tell us how many elements to gather into the pack,
  but a naive approach is good enough to get things off the
  ground.

- Pack conventions are related to the existing parameter and
  result conventions, but they're different on enough grounds
  that they deserve to be separated.
2023-01-29 03:29:06 -05:00
Erik Eckstein
3645becada PassManager: infrastructure to disable or enable a specific instruction simplification
* for testing: add the option `-simplify-instruction=<instruction-name>` to only run simplification passes for that instruction type
* on the swift side, add `Options.enableSimplification`
2023-01-16 19:00:09 +01:00
Erik Eckstein
cc68bd98c9 Swift Optimizer: rework pass context types and instruction passes
* split the `PassContext` into multiple protocols and structs: `Context`, `MutatingContext`, `FunctionPassContext` and `SimplifyContext`
* change how instruction passes work: implement the `simplify` function in conformance to `SILCombineSimplifyable`
* add a mechanism to add a callback for inserted instructions
2023-01-16 15:11:34 +01:00
Erik Eckstein
7c54be27ad SILCombine: a better fix for optimizing keypath-offset-of
Instead of bailing out, use the lowered type.

rdar://103834325
2023-01-05 08:07:50 +01:00
Erik Eckstein
a081e04f8c SILCombine: fix a crash when optimizing keypath-offset-of
Need to check if the keypath's root type is a legal SIL type.

rdar://103834325
2023-01-03 17:31:38 +01:00
Nate Chandler
8d8577e5b0 [SIL] Removed Indirect_In_Constant convention.
It is no different from @in.

Continue parse @in_constant in textual and serialized SIL, but just as
an alias for @in.
2022-12-09 21:54:00 -08:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Anton Korobeynikov
a5e83817b2 Reapply "Implement several peephole optimizations to unblock further optimizations of autodiff code" with correctness fix (#62012)
* Implement several peephole optimizations to unblock further optimizations of autodiff code

1. Simplify differentiable_function_extract of differentiable_function.
Before:
%x = differentiable_function(%orig, %jvp, %vjp)
%y = differentiable_function_extract [original] %x
After:
%y = %orig

2. Push conversion instructions inside of differentiable_function.
This unblocks inlining and specialization.
Before:
%x = differentiable_function(%orig, %jvp, %vjp)
%y = convert_escape_to_noescape %x
After:
%orig' = convert_escape_to_noescape %orig
%jvp' = convert_escape_to_noescape %jvp
%vjp' = convert_escape_to_noescape %vjp
%y = differentiable_function(%orig', %jvp', %vjp')

3. Another peephole is needed for reordering function conversion instructions to enable full inlining:
(convert_escape_to_noescape (convert_function (thin_to_thick_function x)))
=>
(convert_escape_to_noescape (thin_to_thick_function (convert_function x)))

Co-authored-by: Dan Zheng <danielzheng@google.com>
2022-11-16 23:21:27 -08:00
Meghana Gupta
5e20112e0c Revert "Implement several peephole optimizations to unblock further optimizations of autodiff code (#60520)"
This reverts commit 2f5492f572.
2022-10-25 13:35:14 -07:00
Erik Eckstein
ed54253d29 SIL Optimizer: remove legacy C++ passes
They were used as a backup during the transition to Swift passes. Now they are not needed anymore.
2022-10-20 18:31:06 +02:00
Nate Chandler
03253dbf48 [CanonicalizeOSSALifetime] Extend Onone lifetimes.
To improve the debugging experience of values whose lifetimes are
canonicalized without compromising the semantics expressed in the source
language, when canonicalizing OSSA lifetimes at Onone, lengthen
lifetimes as much as possible without incurring copies that would be
eliminated at O.

rdar://99618502
2022-10-08 16:08:35 -07:00
Nate Chandler
4fc42a63a3 [CanonicalizeOSSALifetime] Renamed file.
Matched to the name of the utility.
2022-10-05 17:07:05 -07:00
Allan Shortlidge
c7ce7b75e0 Merge pull request #42514 from jsoref/spelling-siloptimizer
Spelling siloptimizer
2022-10-03 22:50:19 -07:00
Josh Soref
730b16c569 Spelling siloptimizer
* access
* accessed
* accesses
* accessor
* acquiring
* across
* activated
* additive
* address
* addresses'
* aggregated
* analysis
* and
* appropriately
* archetype
* argument
* associated
* availability
* barriers
* because
* been
* beginning
* belongs
* beneficial
* blocks
* borrow
* builtin
* cannot
* canonical
* canonicalize
* clazz
* cleanup
* coalesceable
* coalesced
* comparisons
* completely
* component
* computed
* concrete
* conjunction
* conservatively
* constituent
* construct
* consuming
* containing
* covered
* creates
* critical
* dataflow
* declaration
* defined
* defining
* definition
* deinitialization
* deliberately
* dependencies
* dependent
* deserialized
* destroy
* deterministic
* deterministically
* devirtualizes
* diagnostic
* diagnostics
* differentiation
* disable
* discipline
* dominate
* dominates
* don't
* element
* eliminate
* eliminating
* elimination
* embedded
* encounter
* epilogue
* epsilon
* escape
* escaping
* essential
* evaluating
* evaluation
* evaluator
* executing
* existential
* existentials
* explicit
* expression
* extended
* extension
* extract
* for
* from
* function
* generic
* guarantee
* guaranteed
* happened
* heuristic
* however
* identifiable
* immediately
* implementation
* improper
* include
* infinite
* initialize
* initialized
* initializer
* inside
* instruction
* interference
* interferes
* interleaved
* internal
* intersection
* intractable
* intrinsic
* invalidates
* irreducible
* irrelevant
* language
* lifetime
* literal
* looks
* materialize
* meaning
* mergeable
* might
* mimics
* modification
* modifies
* multiple
* mutating
* necessarily
* necessary
* needsmultiplecopies
* nonetheless
* nothing
* occurred
* occurs
* optimization
* optimizing
* original
* outside
* overflow
* overlapping
* overridden
* owned
* ownership
* parallel
* parameter
* paths
* patterns
* pipeline
* plottable
* possible
* potentially
* practically
* preamble
* precede
* preceding
* predecessor
* preferable
* preparation
* probably
* projection
* properties
* property
* protocol
* reabstraction
* reachable
* recognized
* recursive
* recursively
* redundant
* reentrancy
* referenced
* registry
* reinitialization
* reload
* represent
* requires
* response
* responsible
* retrieving
* returned
* returning
* returns
* rewriting
* rewritten
* sample
* scenarios
* scope
* should
* sideeffects
* similar
* simplify
* simplifycfg
* somewhat
* spaghetti
* specialization
* specializations
* specialized
* specially
* statistically
* substitute
* substitution
* succeeds
* successful
* successfully
* successor
* superfluous
* surprisingly
* suspension
* swift
* targeted
* that
* that our
* the
* therefore
* this
* those
* threshold
* through
* transform
* transformation
* truncated
* ultimate
* unchecked
* uninitialized
* unlikely
* unmanaged
* unoptimized key
* updataflow
* usefulness
* utilities
* villain
* whenever
* writes

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2022-10-03 18:31:33 -04:00
Andrew Trick
5f7f6e464f Rename SILBuilder::createUncheckedBitCast
to SILBuilder::createUncheckedForwardingCast

It would be disastrous to confuse this utility with a bit cast. A bit
cast always produces an Unowned value which must immediately be copied
to be used. This utility always forwards ownership. It cannot be used
to truncate values.

Also, be careful not to convert "reinterpret cast"
(e.g. Builtin.reinterpretCast) into a "value cast" since ownership
will be incorrect and the reinterpreted types might not have
equivalent layout.
2022-10-03 10:09:07 -07:00
Nate Chandler
129dadbe70 [CopyPropagation] Removed poison mode.
The Onone strategy will be to shorten lifetimes as little as possible
while eliminating as many copies as are eliminated at -O.
2022-09-28 17:13:24 -07:00
Anton Korobeynikov
2f5492f572 Implement several peephole optimizations to unblock further optimizations of autodiff code (#60520)
* Implement several peephole optimizations to unblock further optimizations of autodiff code

1. Simplify differentiable_function_extract of differentiable_function.
Before:
%x = differentiable_function(%orig, %jvp, %vjp)
%y = differentiable_function_extract [original] %x
After:
%y = %orig

2. Push conversion instructions inside of differentiable_function.
This unblocks inlining and specialization.
Before:
%x = differentiable_function(%orig, %jvp, %vjp)
%y = convert_escape_to_noescape %x
After:
%orig' = convert_escape_to_noescape %orig
%jvp' = convert_escape_to_noescape %jvp
%vjp' = convert_escape_to_noescape %vjp
%y = differentiable_function(%orig', %jvp', %vjp')

3. Another peephole is needed for reordering function conversion instructions to enable full inlining:
(convert_escape_to_noescape (convert_function (thin_to_thick_function x)))
=>
(convert_escape_to_noescape (thin_to_thick_function (convert_function x)))

* Remove broken disabled folding transform:
  - It is disabled
  - It is broken
  - It is certainly not something mandatory

Co-authored-by: Dan Zheng <danielzheng@google.com>
2022-09-26 09:44:04 -07:00
Nate Chandler
84d0e2557d Changed todo label. 2022-09-23 17:37:11 -07:00
Erik Eckstein
97b2354be6 SIL: add needsStackProtection flags for address_to_pointer and index_addr instructions.
Also add new "unprotected" variants of the `addressof` builtins:
* `Builtin.unprotectedAddressOf`
* `Builtin.unprotectedAddressOfBorrow`
2022-09-08 08:42:22 +02:00
Slava Pestov
9d96ed940f AST: Rename 'canonical wrt. generic signature' to 'reduced'
We had two notions of canonical types, one is the structural property
where it doesn't contain sugared types, the other one where it does
not contain reducible type parameters with respect to a generic
signature.

Rename the second one to a 'reduced type'.
2022-08-09 12:46:31 -04:00
Erik Eckstein
e8e61a9565 SIL: create cond_fail 1 instead of builtin "int_trap" for unconditional failures
`int_trap` doesn't provide a failure message, which makes crash reports hard to understand.
This is mostly the case for optimized casts which fail.

rdar://97681511
2022-07-28 16:05:07 +02:00
Joe Groff
1aa0fb8126 Merge pull request #60252 from jckarter/silgen-no-sizeof-value-argument
SILGen: Drop value argument when lowering Builtin.sizeof/alignof/strideof
2022-07-27 13:27:46 -07:00
Joe Groff
24158b2aab SILGen: Drop value argument when lowering Builtin.sizeof/alignof/strideof.
The metatype value isn't needed to do the memory layout computation, and
IRGen only looks at the substitutions passed to the generic signature, so
we can reduce SIL code size, and avoid leaving behind useless metadata
accesses, if we don't emit it.
2022-07-27 09:03:23 -07:00
Michael Gottesman
1e6187c4f4 [sil] Update all usages of old API SILValue::getOwnershipKind() in favor of new ValueBase::getOwnershipKind().
Andy some time ago already created the new API but didn't go through and update
the old occurences. I did that in this PR and then deprecated the old API. The
tree is clean, so I could just remove it, but I decided to be nicer to
downstream people by deprecating it first.
2022-07-26 11:46:23 -07:00
Egor Zhdan
0e2d438c5b [cxx-interop][SwiftCompilerSources] Use llvm::StringRef instead of BridgedStringRef
rdar://83361000
2022-07-21 16:32:16 +01:00
Erik Eckstein
777b42180d SILOptimizer: use the new set data structures in various optimizations 2022-07-13 14:27:50 +02:00
Erik Eckstein
9f70c292a4 SILCombine: remove the old ObjC bridging peephole.
It's replaced by the new ObjCBridgingOptimization optimization pass
2022-06-08 22:51:57 +02:00
Slava Pestov
ac74b845e5 Merge pull request #58513 from AnthonyLatsis/se-309-sil-opt
SE-0309: SILOptimizer fixes & reenable executable tests
2022-05-20 16:38:05 -07:00
Joe Groff
7c73466a64 SILCombine: Handle result conversions for apply (convert_function) peephole
If a `convert_function` instruction operates on a function with indirect
results, or changes the type of direct results, then we can transform
an application of the converted function into an application of the
original function followed by bitwise conversions of the results, just
like we have done for arguments. Now that closures are emitted at their
context abstraction level, they are more likely to be emitted with
indirect results, so the inability to simplify function conversions
in this case would lead to missed inlining opportunities we used to
take.
2022-05-06 12:49:21 -07:00
Anthony Latsis
fb1aa8b6a5 SILCombiner: createApplyWithConcreteType should not proceed if the 'apply' involves associated types
This optimization rewrites only the 'self' argument, and does not know how to
substitute types in the users of the given apply instruction in case the
underlying protocol method returns a `Self`-dependent type. With SE-0309 in
motion, the bail-out logic must be generalized to `Self`-rooted type parameters.
2022-04-29 02:42:29 +03:00
Allan Shortlidge
f0360a2984 Fix trivial warnings emitted during a full build. 2022-04-05 17:16:19 -07:00
Erik Eckstein
6a020f8f15 Stabilize and simplify SIL linkage and serialization
The main point of this change is to make sure that a shared function always has a body: both, in the optimizer pipeline and in the swiftmodule file.
This is important because the compiler always needs to emit code for a shared function. Shared functions cannot be referenced from outside the module.
In several corner cases we missed to maintain this invariant which resulted in unresolved-symbol linker errors.

As side-effect of this change we can drop the shared_external SIL linkage and the IsSerializable flag, which simplifies the serialization and linkage concept.
2022-03-09 15:28:05 +01:00
Arnold Schwaighofer
30d3950ade Merge pull request #41439 from aschwaighofer/optimize_contiguousarraystorage_anyobject
Also optimize calls to getContiguousArrayStorageType<SomeClass> to ContiguousArrayStorage<AnyObject> under the right deployment setting
2022-03-07 16:35:57 -08:00
Arnold Schwaighofer
0c9a54a789 Also optimize calls to getContiguousArrayStorageType<SomeClass> to ContiguousArrayStorage<AnyObject> under the right deployment setting 2022-02-23 07:19:19 -08:00
Rintaro Ishizaki
7486cd1c21 [SwiftCompiler] Move common bridging facilities to 'Basic'
A preparation for AST/DiagnosticEngine bridging
2022-02-20 22:06:39 -08:00
Arnold Schwaighofer
cc8a86b2f3 Optimize array.getContiguousArrayStorageType calls 2022-02-16 13:21:12 -08:00
Erik Eckstein
8fe36eedd8 SILPassManager: improve bisecting for debugging the optimizer
* Add the possibility to bisect the individual transforms of SILCombine and SimplifyCFG.
   To do so, the `-sil-opt-pass-count` option now accepts the format `<n>.<m>`, where `m` is the sub-pass number.
   The sub-pass number limits the number of individual transforms in SILCombine or SimplifyCFG.

* Add an option `-sil-print-last` to print the SIL of the currently optimized function before and after the last pass, which is specified with `-sil-opt-pass-count`.
2022-02-09 13:25:30 +01:00
Erik Eckstein
0083b1b3e3 SILCombine: some refactoring
NFC
2022-02-09 13:25:30 +01:00
Erik Eckstein
79e9b9a088 SIL optimizer: add the ability to disable swift instruction passes with the -sil-disable-pass option. 2022-02-01 18:30:05 +01:00
Erik Eckstein
383c52aa35 SIL: rename dealloc_ref [stack] -> dealloc_stack_ref
Introduce a new instruction `dealloc_stack_ref ` and remove the `stack` flag from `dealloc_ref`.

The `dealloc_ref [stack]` was confusing, because all it does is to mark the deallocation of the stack space for a stack promoted object.
2022-01-07 16:20:27 +01:00