Commit Graph

2592 Commits

Author SHA1 Message Date
Nate Chandler
6e2f09f225 [NFC] CopyPropagation: Add subpass bailouts.
Before each transformation is done, check whether to continue with the
next subpass.
2025-01-16 08:18:29 -08:00
Nate Chandler
8ace8b9ef9 [NFC] CopyPropagation: Extract transformation.
Separate the propagation of copies from the setup and cleanup.
Facilitates bailing after completing N subpasses.
2025-01-16 08:18:29 -08:00
Meghana Gupta
15733446ed Merge pull request #78581 from meg-gupta/outlinerfix
Outliner: Do not outline if the BridgedArg value is not available at the BridgedCall
2025-01-13 04:15:20 -08:00
Meghana Gupta
abc37ce218 Remove copy added by outliner for guaranteed bridged value
This copy may remain unoptimized, avoid outlining this case.
2025-01-11 08:15:24 -08:00
Meghana Gupta
c56573e424 Outliner: Do not outline if the BridgedArg value is not available at the BridgedCall
Avoid outlining this scenario instead of creating a copy to make the value available
2025-01-11 07:40:35 -08:00
Erik Eckstein
b80a08ee6c CodeMotion: check the ownership of arguments when sinking arguments
So far we only checked the ownership of incoming values.
But even if the incoming instruction has no ownership, the argument may have.
This can happen with enums which are constructed with a non-payload case:

   %1 = enum $Optional<C>, #Optional.none!enumelt
   br bb3(%1)
 bb1(%3 : @owned $Optional<C>):

Fixes an ownership verification error:
rdar://142506300
2025-01-09 19:56:25 +01:00
Erik Eckstein
db96d63e9d SimplifyCFG: correctly handle borrowed-from values when removing a redundant phi argument
Fixes a verifier crash
2025-01-09 19:56:25 +01:00
Allan Shortlidge
d0f63a0753 AST: Split Availability.h into multiple headers.
Put AvailabilityRange into its own header with very few dependencies so that it
can be included freely in other headers that need to use it as a complete type.

NFC.
2025-01-03 18:36:04 -08:00
Erik Eckstein
53d78abd01 Optimizer: enable SILCodeMotion in OSSA, but only for trivial values.
to-do: we should eventually remove code motion of retain and release instructions and implement them as semantic ARC optimizations in OSSA.
2025-01-02 11:01:39 +01:00
eeckstein
aa2cfd3ed1 Merge pull request #78361 from eeckstein/dead-obj-elimination
DeadObjectElimination: handle OSSA instructions when analyzing class destructors
2025-01-02 08:41:15 +01:00
eeckstein
1bdb51edc3 Merge pull request #78318 from eeckstein/fix-access-enforcement-opts
AccessEnforcementOpts: fix a SIL verifier error caused by a wrong bail-out condition
2025-01-02 08:31:28 +01:00
Erik Eckstein
41dd3dc453 DeadObjectElimination: handle OSSA instructions when analyzing class destructors
Handle typical patterns which are generated by SILGen.
2024-12-24 12:00:22 +01:00
Erik Eckstein
413c78e33a AccessEnforcementOpts: fix a SIL verifier error caused by a wrong bail-out condition
In case of a bail-out on failed ownership extension the optimization didn't remove old instructions from previously merged access pairs.
2024-12-20 15:51:37 +01:00
Nate Chandler
6d9ae19629 [DCE] Don't complete lifetimes of erased values.
DCE may enqueue a value for lifetime completion and later on erase the
instruction that defines that value.  When erasing an instruction, erase
each of its results from the collection of values to complete.

rdar://141560546
2024-12-19 15:16:32 -08:00
Nate Chandler
f75f12b681 [DCE] Add delete handler for phi erasure.
Set CallsChanged when appropriate and increment NumDeletedInsts.
2024-12-19 15:12:24 -08:00
Nate Chandler
00116edcfb [NFC] DCE: TermInsts increment NumDeletedInsts. 2024-12-19 15:12:24 -08:00
Nate Chandler
1851e712f8 [Gardening] DCE: Use bound variable in branch.
Rather than reusing the source of the dyn_cast.
2024-12-19 15:12:24 -08:00
Allan Shortlidge
17dfbf5053 AST: Adopt SemanticAvailableAttr for SpecializeAttr. 2024-12-19 08:40:00 -08:00
Meghana Gupta
aaaf5a4916 Merge pull request #78260 from meg-gupta/enableedgecase
Allow SimplifyCFG::simplifyArgument on borrowed values
2024-12-17 20:59:31 -08:00
Meghana Gupta
363731686e Allow SimplifyCFG::simplifyArgument on borrowed values 2024-12-17 15:03:11 -08:00
Meghana Gupta
a62112bd1c Merge pull request #78200 from meg-gupta/fixdce
Look through borrowed from instructions before calling lifetime completion in DCE
2024-12-15 21:06:05 -08:00
Meghana Gupta
50bde829b4 Look through borrowed from instructions before calling lifetime completion
Lifetime completion will insert end_borrows only on borrow introducers.

Look through "borrowed from" instructions before calling it.

rdar://141490551
2024-12-15 01:53:19 -08:00
Meghana Gupta
3701f01de4 Mark ownership fixup instructions as live 2024-12-15 01:53:17 -08:00
Meghana Gupta
01da59f370 Merge pull request #78176 from meg-gupta/fixstropt
Fix StringOptimization to handle load_borrow
2024-12-13 19:15:46 -08:00
Meghana Gupta
d351623df0 Fix StringOptimization to handle load_borrow
rdar://140229560
2024-12-13 13:16:22 -08:00
Erik Eckstein
9781e99544 DeadObjectElimination: handle begin_borrow/end_borrow when deleting dead arrays
This is part of fixing regressions when enabling OSSA modules:
rdar://140229560
2024-12-13 10:49:01 +01:00
Meghana Gupta
984f9f6cd2 Remove unreachable blocks after inlining 2024-12-10 17:01:11 -08:00
Meghana Gupta
f7d1c59df5 Merge pull request #78053 from meg-gupta/fixdce
Fix DCE for ownership forwarding instructions
2024-12-09 09:56:30 -08:00
Meghana Gupta
daa9b161d0 Fix DCE for ownership forwarding instructions
DCE deletes ownership forwarding instructions when it doesn’t have useful users.
It inserts destroy_value/end_borrow for its operands to compensate their lifetimes.

DCE also deletes branches when its successor blocks does not have useful instructions.
It deletes blocks and creates a jump to the nearest post dominating block.

When DCE needs to delete a forwarding instruction in a dead block, it cannot just create
lifetime ends of its operands at its position. Use LifetimeCompletion utility in such cases.

rdar://140428721
2024-12-09 03:22:57 -08:00
Meghana Gupta
adf1eae287 Bring in DominanceInfo and DeadEndBlocks to DCE 2024-12-09 03:00:18 -08:00
Meghana Gupta
23498595bf Remove incorrect assert in ConditionForwarding
ConditionForwarding is able to handle owned values and non-local guaranteed values.
Remove incorrect assertion about enum trivialiaty

Fixes rdar://140977875
2024-12-05 13:02:50 -08:00
Kuba Mracek
6f4ae28520 [ASTMangler] Pass ASTContext to all instantiations of ASTMangler 2024-12-02 15:01:04 -08:00
eeckstein
ca50c55eb5 Merge pull request #77806 from eeckstein/rle-of-array-elements
Optimizer: remove the ArrayElementPropagation optimization
2024-12-02 07:13:08 +01:00
Erik Eckstein
63f6a2f30d Optimizer: remove the ArrayElementPropagation optimization
Propagating array element values is done by load-simplification and redundant-load-elimination.
So ArrayElementPropagation is not needed anymore.

ArrayElementPropagation also replaced `Array.append(contentsOf:)` with individual `Array.append` calls.
This optimization is removed, because the benefit is questionably, anyway.
In most cases it resulted in a code size increase.
2024-11-28 10:35:40 +01:00
Erik Eckstein
ca09336154 ConditionForwarding: fix a wrong assert
Some terminator instructions can have type-dependent operands.
Therefore we need to use `getNumRealOperands` instead of `getNumOperands`.

Fixes a compiler crash.
2024-11-27 13:19:12 +01:00
Anton Korobeynikov
55d51b782d Closure specialization might create functions with lots of arguments. (#77629)
Increase inlining benefits for functions with more than 5 arguments and / or results.
We assume that each argument beyond these 5 would be passed on stack and therefore would incur a pair of load and store.
2024-11-20 16:31:47 -08:00
Andrew Trick
4612728581 [NFC] Add BeginApplyInst::getEndApplyUses() API.
A begin_apply token may be used by operands that do not end the coroutine:
mark_dependence.

We need an API that gives us only the coroutine-ending uses. This blocks
~Escapable accessors.

end_borrow is considered coroutine-ending even though it does not actually
terminate the coroutine.

We cannot simply ask isLifetimeEnding, because end_apply and abort_apply do not
end any lifetime.
2024-11-18 01:37:00 -08:00
eeckstein
972ac6ff28 Merge pull request #77610 from eeckstein/fix-array-element-propagation
ArrayElementValuePropagation: require that the initialization and element-get are in the same block for non-trivial values.
2024-11-15 07:53:11 +01:00
Meghana Gupta
434461d8f4 Fix DCE of load_borrow when it is derived from another borrow scope
Fixes rdar://138663452
2024-11-14 12:09:11 -08:00
Meghana Gupta
5486b7a289 Merge pull request #77571 from meg-gupta/fixsimplifycfgunreachable
Fix simplifySwitchEnumUnreachableBlocks for default cases in ossa
2024-11-14 07:44:19 -08:00
Erik Eckstein
16c77d97dc ArrayElementValuePropagation: require that the initialization and element-get are in the same block for non-trivial values.
In OSSA we only insert a copy_value of the element at the array initialization point.
This would result in an over-consume if the getElement is in a loop.
Therefore require that both semantic calls are in the same block.

Fixes an ownership verifier crash.
2024-11-14 13:30:06 +01:00
eeckstein
6d4db0e649 Merge pull request #77583 from eeckstein/fix-dce
DeadCodeElimination: don't remove end_lifetime instructions with address operands
2024-11-13 18:51:45 +01:00
Meghana Gupta
452ae3b994 Merge pull request #77568 from meg-gupta/dceupdate2
Insert end_borrow for dead phi operands only when required
2024-11-13 04:10:30 -08:00
Erik Eckstein
64698ca6bb DeadCodeElimination: don't remove end_lifetime instructions with address operands
DCE cannot reason about values in memory.

Fixes a memory lifetime verification error
rdar://139779406
2024-11-13 12:04:41 +01:00
Meghana Gupta
106dc8a7e0 Fix simplifySwitchEnumUnreachableBlocks for default cases in ossa
Unlike non-ossa, ossa's switch_enum accepts an argument for the default case
When all other cases are unreachable, replace the default block's phi with
the switch_enum's operand and transform the switch_enum to a branch.

Fixes rdar://139441002
2024-11-12 21:11:54 -08:00
Meghana Gupta
fd6d29d10b Insert end_borrow for dead phi operands only when required
DCE inserts end_borrow at phi operands when a guaranteed phi becomes dead.
This should be done for reborrows which end the lifetime of the incoming value.
The existing check was not accurate and ended up inserting end_borrow for forwarded values as well.

Fixes rdar://139283745
2024-11-12 14:05:23 -08:00
Erik Eckstein
51e3e5ed80 Optimizer: rename BorrowArgumentsUpdater -> GuaranteedPhiUpdater
NFC
2024-11-12 09:26:59 +01:00
Erik Eckstein
6b8c6a3c3b SIL: rename updateBorrowedFrom to updateBorrowArguments
NFC
2024-11-12 09:26:58 +01:00
Erik Eckstein
15c6abf66d SIL: avoid creating SIL functions for not used imported specialization attributes
This ended up in creating a lot of Array functions, even if a program didn't use Array at all.
Now, only add specialization attributes if a function is already there.
Otherwise remember the attributes and add them to a function once it is created.
2024-11-05 20:37:15 +01:00
Meghana Gupta
359f6d14bf DCE unconditional_checked_cast 2024-10-16 16:55:22 -07:00