Commit Graph

2592 Commits

Author SHA1 Message Date
Meghana Gupta
badf0c1c07 [NFC] Simplify DenseMapInfo<SimpleValue>::isEqual in CSE
It is not correct to check if we support ownership rauwing of values in
the isEqual method. Move it to a later point in CSE
2021-01-04 13:26:56 -08:00
Michael Gottesman
0de00d1ce4 [sil-inst-opt] Improve performance of InstModCallbacks by eliminating indirect call along default callback path.
Specifically before this PR, if a caller did not customize a specific callback
of InstModCallbacks, we would store a static default std::function into
InstModCallbacks. This means that we always would have an indirect jump. That is
unfortunate since this code is often called in loops.

In this PR, I eliminate this problem by:

1. I made all of the actual callback std::function in InstModCallback private
   and gave them a "Func" postfix (e.x.: deleteInst -> deleteInstFunc).

2. I created public methods with the old callback names to actually call the
   callbacks. This ensured that as long as we are not escaping callbacks from
   InstModCallback, this PR would not result in the need for any source changes
   since we are changing a call of a std::function field to a call to a method.

3. I changed all of the places that were escaping inst mod's callbacks to take
   an InstModCallback. We shouldn't be doing that anyway.

4. I changed the default value of each callback in InstModCallbacks to be a
   nullptr and changed the public helper methods to check if a callback is
   null. If the callback is not null, it is called, otherwise the getter falls
   back to an inline default implementation of the operation.

All together this means that the cost of a plain InstModCallback is reduced and
one pays an indirect function cost price as one customizes it further which is
better scalability.

P.S. as a little extra thing, I added a madeChange field onto the
InstModCallback. Now that we have the helpers calling the callbacks, I can
easily insert instrumentation like this, allowing for users to pass in
InstModCallback and see if anything was RAUWed without needing to specify a
callback.
2021-01-04 12:51:55 -08:00
swift_jenkins
11993fd1cf Merge remote-tracking branch 'origin/main' into next 2020-12-23 10:53:57 -08:00
Meghana Gupta
42c031985c Enable CSE on OSSA 2020-12-22 23:20:06 -08:00
Slava Pestov
e675bee26c AST: Split off DependencyCollector.h from EvaluatorDependencies.h
Also remove some unnecessary #includes from DependencyCollector.h,
which necessitated adding #includes in various other files.
2020-12-23 00:00:25 -05:00
Meghana Gupta
d24d264469 Enable ArrayCountPropagation on OSSA 2020-12-21 21:28:48 -08:00
swift_jenkins
f9fd6c273f Merge remote-tracking branch 'origin/main' into next 2020-12-14 23:19:41 -08:00
Meghana Gupta
81107e4235 Improve handling of copy_value and destroy_value in (#35011)
MemoryBehaviorVisitor

- Also, compute use points for destroy_value
- Cleanup explicit checks for refcount instructions in RLE
2020-12-14 22:44:58 -08:00
swift_jenkins
ce1a0b54be Merge remote-tracking branch 'origin/main' into next 2020-12-13 01:53:07 -08:00
Erik Eckstein
92ab19052a DeadObjectElimination: don't remove a dead alloc_ref which has a store to a non-trivial property.
In non-OSSA we cannot reliably track the lifetime of non-trivial stored properties, in case the deallocation is inlined.
Removing such a dead alloc_ref might leak a property value (or crash the compiler).

rdar://70689545
2020-12-11 17:26:51 +01:00
swift_jenkins
1cdb072141 Merge remote-tracking branch 'origin/main' into next 2020-12-10 21:06:38 -08:00
John McCall
d874479290 Add builtins to initialize and destroy a default-actor member.
It would be more abstractly correct if this got DI support so
that we destroy the member if the constructor terminates
abnormally, but we can get to that later.
2020-12-10 19:18:53 -05:00
John McCall
b22407ef0c Add a builtin to convert a Task* to a Job*. 2020-12-10 17:06:14 -05:00
swift_jenkins
852745366e Merge remote-tracking branch 'origin/main' into next 2020-12-03 10:01:51 -08:00
Erik Eckstein
423169ce5c SILOptimizer: update alias analysis in TempRValueOpt and TempLValueOpt
When instructions are changed within a pass in a way that affects subsequent alias queries in the same pass run,
their alias analysis information must be invalidated.
Otherwise it can result in miscompiles and/or invalid SIL.

rdar://71924430
2020-12-03 13:53:57 +01:00
swift_jenkins
144cc9c1a4 Merge remote-tracking branch 'origin/main' into next 2020-12-01 21:41:10 -08:00
Meghana Gupta
0992e7731f Enable GenericSpecializer on OSSA by default (#34899) 2020-12-01 21:19:17 -08:00
swift_jenkins
8b28c896dd Merge remote-tracking branch 'origin/main' into next 2020-11-30 18:06:27 -08:00
Richard Wei
de2dbe57ed [AutoDiff] Bump-pointer allocate pullback structs in loops. (#34886)
In derivatives of loops, no longer allocate boxes for indirect case payloads. Instead, use a custom pullback context in the runtime which contains a bump-pointer allocator.

When a function contains a differentiated loop, the closure context is a `Builtin.NativeObject`, which contains a `swift::AutoDiffLinearMapContext` and a tail-allocated top-level linear map struct (which represents the linear map struct that was previously directly partial-applied into the pullback). In branching trace enums, the payloads of previously indirect cases will be allocated by `swift::AutoDiffLinearMapContext::allocate` and stored as a `Builtin.RawPointer`.
2020-11-30 15:49:38 -08:00
swift_jenkins
2dda7530ac Merge remote-tracking branch 'origin/main' into next 2020-11-30 07:36:55 -08:00
Michael Gottesman
b0676be437 [allocbox-to-stack] Fix an ossa bug in PromotedParamCloner.
For those who are unfamiliar, alloc-box-to-stack while generally not
interprocedural, will look one level into the callgraph to see if a
partial_apply that captures a box really needs to capture the box due to an
escape. If not, allocbox-to-stack clones the closure with the address inside the
box being passed instead of the box itself. This can then allow us to promote
the box from the heap to the stack.

What went wrong here is that in OSSA, this promoted param cloner drops
copy_value, destroy_value, and project_box on the given box. Both the copy_value
and destroy_value cases correctly looked through copy_values, but when porting,
the author forgot to handle project_box as well. This then caused the cloner to
assert since:

1. The project_box in the original function had a copy_value operand.

2. When we visited that copy_value, we saw it was for the box, so we dropped the
copy_value and did not add it to the cloner's Value -> op(Value) map.

3. Then when the cloner tried to create op(project_box), it tries to lookup the
value associated with the copy_value that is the project_box's operand... but we
don't have any such value due to (2). =><=.

The test change exercises this code path by adding a (project_box (copy_value))
to one of the allocbox to stack tests.
2020-11-29 23:53:06 -08:00
swift_jenkins
905b751548 Merge remote-tracking branch 'origin/main' into next 2020-11-20 04:26:27 -08:00
Andrew Trick
39db766257 Restrict CopyForwarding (and destroy hoisting) to OSSA SIL.
The premise of CopyForwarding was that memory locations have their own
ownership lifetime. We knew this was unmaintainable at the time, and
that was the original incentive for SIL opaque values, aided by
OSSA. In the meantime, we've been relying on SILGen producing
reasonable SIL patterns. Unfortunately, the CopyForwarding pass is
still with us while we make major changes to SIL ownership patterns
and agressively optimize ownership. That introduces risk.

Ultimately, the entire CopyForwarding pass should be redesigned for
OSSA-only and destroy hoisting should be a simple OSSA utility where
most of the work is done by AccessPath::collectUses.

But in the meantime, we should remove the source of risk by limiting
the CopyForwarding pass to OSSA. Any performance regressions will be
recovered as OSSA moves later in the pipeline. After that, opaque
values will improve even more over the current state by handling
generic SIL using the more powerful CopyPropagation pass.

Fixes rdar://71584600 (miscompile in CopyForwarding's release hoisting)

Here's an example of the kind of SIL the CopyForwarding does not
anticipate (although it only actually miscompiles in much more obscure
scenarios, which is why it's so dangerous):

bb0(%0 : $AnyObject):
  %alloc1 = alloc_stack $AnyObject
  store %0 to %objaddr : $*AnyObject
  %ref = load %objaddr : $*AnyObject
  %alloc2 = alloc_stack $ObjWrapper
  # The in-memory reference is destroyed before retaining the loaded ref.
  copy_addr [take] %alloc1 to [initialization] %alloc2 : $*ObjWrapper
  retain_value %ref : $AnyObject
2020-11-19 21:52:04 -08:00
swift_jenkins
43b2fe020b Merge remote-tracking branch 'origin/main' into next 2020-11-18 11:49:18 -08:00
Michael Gottesman
03646bea33 Merge pull request #34803 from gottesmm/pr-859299a3855ec8453c6129e100c7aa7c374d4fa7
[sil] Element shadowing of SILInstruction::getKind() by renaming MarkUninitializedInst::get{,MarkUninitialized}Kind().
2020-11-18 10:48:28 -08:00
swift_jenkins
dc1b4de4c2 Merge remote-tracking branch 'origin/main' into next 2020-11-17 23:58:39 -08:00
Michael Gottesman
611284fcc2 [sil] Element shadowing of SILInstruction::getKind() by renaming MarkUninitializedInst::get{,MarkUninitialized}Kind().
Interestingly this problem can only occur if one invokes
MarkUninitializedInst::getKind() directly. Once our instruction is just a
SILInstruction, we call the appropriate method so we didn't notice it.

I used Xcode's refactoring functionality to find all of the invocation
locations.
2020-11-17 21:25:57 -08:00
Michael Gottesman
a9ca793f1a [allocbox-to-stack] Eliminate temporary dominance issue and fix improper use of non-ossa generic method createDestroyValue.
NOTE: I also added a partial_apply [guaranteed] test.

Whats interesting about these is that we only ever perform allocbox_to_stack if
we know that we are going to eliminate the allocbox completely. So if we break
dominance among some uses of the alloc box or insert destroy_value when we are
in non-ossa... it doesn't matter since we will eliminate the box and these uses
before the pass is done running.

This will harmless on the surface is an instance of the compiler being in a
"fixed point of correctness". This occurance is when the compiler implementation
is incorrect but the incorrectness is being hidden in the final output. If the
output of the compiler changes or the code in question is changed, new bugs can
be introduced due to the lack of preserving of standard invariants like
dominance.

I also added an additional helper: SILBuilder::insertAfter(SILValue). This
builds on Erik's commit that gave us insert(SILInstruction *). I wanted this
functionality, but additionally I wanted to make it so that if I had an
argument, I got back the first instruction in the block. So it was natural to
extend this to values.
2020-11-17 20:41:56 -08:00
swift_jenkins
4b89f765a7 Merge remote-tracking branch 'origin/main' into next 2020-11-16 13:52:10 -08:00
Doug Gregor
cd8029a079 Merge pull request #34759 from DougGregor/concurrency-future-builtin
[Concurrency] Add Builtin.createAsyncTaskFuture and implement runDetached on it
2020-11-16 13:28:56 -08:00
swift_jenkins
b950541caf Merge remote-tracking branch 'origin/main' into next 2020-11-16 01:37:28 -08:00
Doug Gregor
069dfad638 [Concurrency] Add Builtin.createAsyncTaskFuture.
This new builtin allows the creation of a "future" task, which calls
down to swift_task_create_future to actually form the task.
2020-11-15 22:37:13 -08:00
Michael Gottesman
7718bd1fed [value-lifetime] Cleanup constructors. 2020-11-15 16:56:31 -08:00
swift_jenkins
65876c8601 Merge remote-tracking branch 'origin/main' into next 2020-11-11 16:30:57 -08:00
Meghana Gupta
515dc39eee Fix ownership SROA (#34691)
Stores to new allocations should have the same ownership qualifier as
the original store
2020-11-11 15:57:22 -08:00
swift_jenkins
d5be531a83 Merge remote-tracking branch 'origin/main' into next 2020-11-10 19:07:28 -08:00
Michael Gottesman
c026e95cce [ownership] Extract out SILOwnershipKind from ValueOwnershipKind into its own type and rename Invalid -> Any.
This makes it easier to understand conceptually why a ValueOwnershipKind with
Any ownership is invalid and also allowed me to explicitly document the lattice
that relates ownership constraints/value ownership kinds.
2020-11-10 14:29:11 -08:00
swift_jenkins
7e9cbf4c0d Merge remote-tracking branch 'origin/main' into next 2020-11-09 20:52:43 -08:00
Doug Gregor
d038989fd8 Merge branch 'main' into create-async-task-builtin 2020-11-09 11:26:33 -08:00
swift_jenkins
a54d817b95 Merge remote-tracking branch 'origin/main' into next 2020-11-09 09:07:45 -08:00
Andrew Trick
c2b13cdd51 Merge pull request #34635 from atrick/verify-critedge
Verify non-critical edges in OSSA
2020-11-09 08:59:08 -08:00
swift_jenkins
27e2ffef81 Merge remote-tracking branch 'origin/main' into next 2020-11-09 00:07:47 -08:00
Michael Gottesman
6f60a17cc7 Merge pull request #34633 from gottesmm/pr-200203a48964c3efb090a7f4f2389b115a88b607
[ownership] Change switch_enum to be a true forwarding instructions.
2020-11-08 23:54:50 -08:00
Andrew Trick
da0f1edc09 Fix CopyPropagation: remove broken critical edge handling. 2020-11-08 21:34:24 -08:00
swift_jenkins
b75f1b80ce Merge remote-tracking branch 'origin/main' into next 2020-11-08 21:33:38 -08:00
Andrew Trick
36fa93a3bf Merge pull request #34610 from atrick/eager-critedge
Fix EagerSpecializer to avoid critical edges.
2020-11-08 21:08:05 -08:00
Michael Gottesman
f36e8561f1 [ownership] Use a new ADT SwitchEnumBranch instead of SwitchEnumInstBase for generic operations on SwitchEnum{,Addr}Inst.
I have a need to have SwitchEnum{,Addr}Inst have different base classes
(TermInst, OwnershipForwardingTermInst). To do this I need to add a template to
SwitchEnumInstBase so I can switch that BaseTy. Sadly since we are using
SwitchEnumInstBase as an ADT type as well as an actual base type for
Instructions, this is impossible to do without introducing a template in a ton
of places.

Rather than doing that, I changed the code that was using SwitchEnumInstBase as
an ADT to instead use a proper ADT SwitchEnumBranch. I am happy to change the
name as possible see fit (maybe SwitchEnumTerm?).
2020-11-08 19:52:02 -08:00
swift_jenkins
7f5c6c924d Merge remote-tracking branch 'origin/main' into next 2020-11-08 19:00:40 -08:00
Michael Gottesman
642a993702 [ownership] Rename Operand::isConsumingUse() -> Operand::isLifetimeEnding().
This makes it clearer that isConsumingUse() is not an owned oriented API and
returns also for instructions that end the lifetime of guaranteed values like
end_borrow.
2020-11-08 13:23:17 -08:00
Doug Gregor
4c2c2f32e9 [Concurrency] Implement a builtin createAsyncTask() to create a new task.
`Builtin.createAsyncTask` takes flags, an optional parent task, and an
async/throwing function to execute, and passes it along to the
`swift_task_create_f` entry point to create a new (potentially child)
task, returning the new task and its initial context.
2020-11-07 23:05:04 -08:00