Commit Graph

214 Commits

Author SHA1 Message Date
Nate Chandler
1158b58097 [AddressLowering] Addr'd some unconditional_casts.
All checked casts are emitted as unconditional_checked_cast
instructions.  More than just the instructions which produce or consume
an opaque value must be rewritten as unconditional_checked_cast_addr
instructions.  In particular, all those instructions for which
canIRGenUseScalarCheckedCastInstructions returns false must be rewritten
as unconditional_checked_cast_addr instructions.

Note the instructions to rewrite like that while visiting values and
then rewrite them near the end of rewriting.
2022-11-09 17:43:22 -08:00
Nate Chandler
ed193d69fd [AddressLowering] Rewrite checked_casts as seen.
Instead of waiting until after rewriting everything else, rewrite them
as the terminator results they produce are encountered.  This enables
forming projections in the correct locations.
2022-11-08 17:41:28 -08:00
Nate Chandler
93e5701bba [AddressLowering] Rewrite uses of rewritten def.
During def rewriting, the def itself can be changed, for example to be a
"dummy" load.  In such cases, uses of the new def need to be rewritten,
not uses of the original def.
2022-11-08 12:22:09 -08:00
Nate Chandler
06cf796dbb [AddressLowering] Handle try_apply results.
When a block argument is a terminator result from a try_apply, use the
ApplyRewriter to convert the try_apply.

In the case where the result is stored into an enum, with this change,
the init_enum_data_addr instruction is created prior to the try_apply
which is necessary in order for it to be passed as an argument to the
try_apply.
2022-11-07 16:49:50 -08:00
Nate Chandler
a8dc31150c [AddressLowering] Handle multidefault switch_enum.
If a switch_enum instruction (1) exhaustively handles all cases, there
is no default case or block corresponding to it.  If (2) it handles all
cases but one, the default case corresponds to the unique unhandled
case. Otherwise, (3) the default case corresponds to all the unhandled
cases.

The first two scenarios were already handled by address lowering.

Here, handling is added for case (3).  It is similar to what is already
done for rewriting cases except that no unchecked_take_enum_data_addr
must be created and that the argument is always address-only (being the
same type as the operand of the switch_enum which is only being
rewritten because it was address-only).
2022-11-07 16:49:50 -08:00
Nate Chandler
5eb8fa4c88 [NFC] getElement(i) not getElements()[i]. 2022-11-07 16:49:49 -08:00
Nate Chandler
81d37bea64 [AddressLowering] Fix dead argument filtering.
The filterDeadArgs function takes a list of dead argument
indices--ordered from least to greatest--a list of original arguments,
and produces a list of arguments excluding the arguments at those dead
indices.

It does that by iterating from 0 to size(originalArguments) - 1, adding
the original argument at that index to the list of new arguments, so
long as the index that of a dead argument.  To avoid doing lookups into
a set, this relies on the dead arguments being ordered ascending.  There
is an interator into the dead argument list that is incremented only
when the current index is dead.

When that iterator is at the end, dereferencing it just gives the size
of the array of dead arguments.  So in the case where the first argument
is dead but no other arguments are, and there _are_ other arguments, the
first argument would be skipped, and the second argument's index would
be found to be equal to the dereferenced iterator (1).

Previously, there was no check that the iterator was not at the end.
The result was failing to add the second argument to the new list.  And
tripping an assertion failure.

Here, it is checked that the iterator is not at the end.
2022-11-07 16:49:49 -08:00
Nate Chandler
20400f4e56 [AddressLowering] Reserve capacity, don't resize.
Resizing a SmallVector of SILValues fills it with invalid SILValues.
THe intent is to reserve space for forthcoming values.
2022-11-07 16:49:49 -08:00
Nate Chandler
3a0f71fc0d [AddressLowering] Rewrite newly created uses.
When rewriting uses, it is possible for new uses of a value to be
created, as when a  debug_value instruction is created when a store
instruction is deleted.  Ensure that all uses are rewritten by adding
all uses to the worklist of uses after rewriting each use.
2022-11-07 16:49:49 -08:00
Nate Chandler
dec657f8a1 [AddressLowering] Set the right yield operand.
Rather than always setting the 0th operand of a yield instruction, set
the operand at the index of the original operand.
2022-11-04 10:47:09 -07:00
Nate Chandler
57352d75d4 [AddressLowering] Handle select_enum. 2022-11-04 10:47:09 -07:00
Nate Chandler
ce43690d8e [AddressLowering] Handle existential_metatype.
The handling is the same as for value_metatype.
2022-11-04 10:47:09 -07:00
Nate Chandler
7dd0a609a2 [AddressLowering] Handle unchecked to loadable.
When casting via unchecked_bitwise_cast, if the destination type is
loadable, don't mark the value it produces rewritten--that value is not
one that AddressLowering is tracking.  Instead, replace its copy_value
uses with load [copy] uses of the address the rewritten instruction
produces.
2022-11-04 10:47:09 -07:00
Nate Chandler
0aca521a19 [ApplySite] Renamed insertAfterApplication.
Now that it can be called on partial_apply instructions,
insertAfterFullEvaluation does not name what the function does.  One
could imagine a function which inserted after the applies of
(non-escaping) partial_applies.
2022-11-03 17:15:07 -07:00
Nate Chandler
646cf23442 [AddressLowering] Handle fix_lifetime. 2022-11-03 17:15:07 -07:00
Nate Chandler
639f318dda [AddressLowering] Cache ops before iteration.
Before iterating over an instruction's uses and deleting each, cache the
list of uses.  Otherwise, the for loop stops after the first instruction
when it's deleted (and has its NextUse field cleared).
2022-11-03 17:15:07 -07:00
Nate Chandler
ae7c69b1af [AddressLowering] Lower address-of variants.
Now that generic values have been lowered to addresses, create the
address_to_pointer instructions.
2022-11-02 14:44:45 -07:00
Nate Chandler
e357fb45c5 [OpaqueValues] Handle Builtin.copy.
Specify the operand ownership of the Builtin differently depending on
whether lowered addresses are used.  Handle rewriting the value version
of the builtin as the address version of the builtin in AddressLowering.
2022-11-02 13:48:47 -07:00
Nate Chandler
24c4c617d2 [AddressLowering] Rewrite partial_apply uses.
Use the CallArgRewriter to rewrite uses by partial_apply insts just as
uses by apply insts, etc., are rewritten.
2022-11-01 13:02:32 -07:00
Nate Chandler
5d1c99c0d6 [AddressLowering] CallArgRewriter takes ApplySite.
Rather than being able to handle only to FullApplySites, it now handles
PartialApplyInsts as well.
2022-11-01 13:02:32 -07:00
nate-chandler
b7793511a1 Merge pull request #61747 from nate-chandler/opaque-values/1/20221014
[AddressLowering] Storage root inherits lexical.
2022-10-27 16:35:15 -07:00
Nate Chandler
c766956440 [AddressLowering] Storage root inherits lexical.
When a `begin_borrow [lexical]` is lowered, the lifetime that it
describes can't be shortened (or eliminated) when lowering.  In some
cases, though, there will not be an alloc_stack corresponding directly
to the value being borrowed.

In these cases, mark the whole aggregate lexical.
2022-10-27 11:33:51 -07:00
Nate Chandler
ed623d7b64 [NFC] Shortened SIL [init] flag.
Instead of writing out [initalization] for some instructions, use [init]
everywhere.
2022-10-27 10:38:54 -07:00
Nate Chandler
a995c56d79 [Gardening] Removed unused variable. 2022-10-26 11:01:30 -07:00
Nate Chandler
5ce0148c2a [Gardening] Tweaked comment. 2022-10-26 07:57:55 -07:00
Andrew Trick
ca503b54b7 Redesign PrunedLiveness APIs, introducing live ranges
First restore the basic PrunedLiveness abstraction to its original
intention. Move code outside of the basic abstraction that polutes the
abstraction and is fundamentally wrong from the perspective of the
liveness abstraction.

Most clients need to reason about live ranges, including the def
points, not just liveness based on use points. Add a PrunedLiveRange
layer of types that understand where the live range is
defined. Knowing where the live range is defined (the kill set) helps
reliably check that arbitrary points are within the boundary. This
way, the client doesn't need to be manage this on its own. We can also
support holes in the live range for non-SSA liveness. This makes it
safe and correct for the way liveness is now being used. This layer
safety handles:

- multiple defs
- instructions that are both uses and defs
- dead values
- unreachable code
- self-loops

So it's no longer the client's responsibility to check these things!

Add SSAPrunedLiveness and MultiDefPrunedLiveness to safely handle each
situation.

Split code that I can't figure out into
DiagnosticPrunedLiveness. Hopefully it will be deleted soon.
2022-10-04 13:27:44 -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
Meghana Gupta
05452dad86 Fix store_borrow generation in AddressLowering 2022-08-16 15:08:22 -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
Meghana Gupta
7cf67e7eb4 Fix computation of dominance frontier (#59278) 2022-06-07 10:52:29 -07:00
Meghana Gupta
fb0be6ad97 Run AddressLowering after DI and RawSILInstLowering 2022-05-31 20:49:29 -07:00
Meghana Gupta
25186fffbd Use AddressMaterialization::materializeAddress instead of directly using storageAddres 2022-05-31 20:49:29 -07:00
Meghana Gupta
f0e52dbc56 AddressLowering: Handle opaque use in ValueMetatypeInst 2022-05-31 20:49:28 -07:00
Meghana Gupta
8a1ce59601 AddressLowering: Don't generate an end_borrow for store_borrow
store_borrow returns the address to be borrowed and not a token
Don't generate an end_borrow until this is fixed first
2022-05-31 20:49:21 -07:00
Meghana Gupta
dcce7fcf63 AddressLowering: Handle coroutines (#58821) 2022-05-13 19:53:29 -07:00
Meghana Gupta
60d0ac6b4e [SIL Opaque Value] Support checked_cast_br in Address Lowering with loadable source type and opaque target type (#58441) 2022-05-05 11:55:00 -07:00
Meghana Gupta
90a114ea26 AddressLowering: Support checked_cast_br (#42487) 2022-04-22 12:42:34 -07:00
Meghana Gupta
d110b6d82d Move DefRewriter::setStorageAddress above 2022-04-12 12:31:36 -07:00
Meghana Gupta
dcf0803c8c Address Lowering: Add support for unconditional_checked_cast 2022-04-12 12:31:31 -07:00
Meghana Gupta
d50005b4d6 Add support for open_existential_box_value in Address Lowering 2022-04-07 11:52:04 -07:00
Meghana Gupta
16c277f8fb Add new api replaceAllTypeDependentUsesWith 2022-04-07 09:19:42 -07:00
Meghana Gupta
425d457708 [SIL Opaque values] Add support for unchecked_bitwise_cast in AddressLowering 2022-03-28 12:10:09 -07:00
Andrew Trick
1a6d7c1ba5 Merge pull request #41947 from atrick/opv-silgen
[nfc-5.7] Initial round of SILGen fixes and tests for opaque value
2022-03-23 12:01:26 -07:00
Nate Chandler
97cfeb1162 Only define ValueStorageMap::dump in DEBUG. 2022-03-22 20:40:26 -07:00
Andrew Trick
1625a44ded [SIL-opaque] [NFC] clang-format AddressLowering.cpp 2022-03-22 17:04:13 -07:00
Andrew Trick
3ec96fa6f7 [SIL-opaque] Add an assert for open_existential_value.
Add comments.

Add a basic dominance sanity check.
2022-03-22 17:04:13 -07:00
Andrew Trick
a133549e00 [SIL-opaque] Add @in_guaranteed function argument support.
Temporarily map storage to a fake load_borrow.
2022-03-22 17:04:13 -07:00
Andrew Trick
4e3f0dfecc [SIL-opaque] use generated SILLocations
Anywhere that code is not obviously inserted immediately adjacent to
the origin instruction.
2022-03-22 17:04:13 -07:00
Andrew Trick
2fd4de411e [SIL-opaque] Removed [Unconditional]CheckedCastValue 2022-03-22 17:04:13 -07:00
Andrew Trick
6f3a0c3df0 [SIL-opaque] rename initializeComposingUse 2022-03-09 22:27:11 -08:00