Skip to content

[DNM] remaining work for SIL COW representation #31730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

eeckstein
Copy link
Contributor

The second part of #31728

Still work in progress. Not to be merged yet.

@eeckstein
Copy link
Contributor Author

@dan-zheng Can you please help me fixing a small issue with AutoDiff?
Following tests are failing with this PR:

AutoDiff/stdlib/array.swift
AutoDiff/SILOptimizer/differentiation_activity_analysis.swift

The reason is that the SIL code for Array literals looks a bit different. After storing all elements, a new runtime function is called. For details see: af196a1

I think it's a simple fix to handle this runtime function, but I'm not really understanding the code.

@dan-zheng
Copy link
Contributor

dan-zheng commented May 12, 2020

@dan-zheng Can you please help me fixing a small issue with AutoDiff?

I can help take a look today, thanks for the context!

I wonder what is the urgency of this PR? I can try to share an update before EOD today.

Edit: I took a look and think I understand what AutoDiff changes are necessary to support the new array.finalize_intrinsic. I can try to share a fix this weekend, if that's okay!

@eeckstein
Copy link
Contributor Author

Thanks, it's not urgent

@eeckstein eeckstein force-pushed the cow-support branch 2 times, most recently from 63f564e to d26a144 Compare May 19, 2020 12:18
@eeckstein
Copy link
Contributor Author

@dan-zheng I rebased this PR.

@eeckstein
Copy link
Contributor Author

@dan-zheng Any progress on this?
I'd like to land my changes this or next week.
If you don't have time, is it okay for you if I just temporarily disable the 2 AutoDiff tests?

@dan-zheng
Copy link
Contributor

I will try to finish fixing the AutoDiff tests by tomorrow and share an update. Sorry for the delay!

(It would be ideal to keep the AutoDiff tests passing because users depend on the tested behavior.)

@eeckstein
Copy link
Contributor Author

ok, thanks! BTW, I just rebased the PR.

@eeckstein
Copy link
Contributor Author

@dan-zheng Is there any update?

@dan-zheng
Copy link
Contributor

dan-zheng commented Jun 1, 2020

Yes, the AutoDiff fixes have been done in eeckstein#3 for a few days. Sorry for not clarifying that explicitly, feel free to merge or cherry-pick it.

dan-zheng and others added 3 commits June 2, 2020 11:04
Update differentiation to handle `array.finalize_intrinsic` applications.

`VJPEmitter::visitApplyInst` does standard cloning for these applications.

`PullbackEmitter::visitApplyInst` treats the intrinsic like an identity
function, accumulating result's adjoint into argument's adjoint.

This fixes array literal initialization differentiation.
For COW support in SIL it's required to "finalize" array literals.
_finalizeUninitializedArray is a compiler known stdlib function which is called after all elements of an array literal are stored.
This runtime function marks the array literal as finished.

  %uninitialized_result_tuple = apply %_allocateUninitializedArray(%count)
  %mutable_array = tuple_extract %uninitialized_result_tuple, 0
  %elem_base_address = tuple_extract %uninitialized_result_tuple, 1
  ...
  store %elem_0 to %elem_addr_0
  store %elem_1 to %elem_addr_1
  ...
  %final_array = apply %_finalizeUninitializedArray(%mutable_array)

In this commit _finalizeUninitializedArray is still a no-op because the COW support is not used in the Array implementation yet.
…e Array types.

Use the new builtins for COW representation in Array, ContiguousArray and ArraySlice.
The basic idea is to strictly separate code which mutates an array buffer from code which reads from an array.
The concept is explained in more detail in docs/SIL.rst, section "Copy-on-Write Representation".

The main change is to use beginCOWMutation() instead of isUniquelyReferenced() and insert endCOWMutation() at the end of all mutating functions. Also, reading from the array buffer must be done differently, depending on if the buffer is in a mutable or immutable state.

All the required invariants are enforced by runtime checks - but only in an assert-build of the library: a bit in the buffer flags indicates if the buffer is mutable or not.

Along with the library changes, also two optimizations needed to be updated: COWArrayOpt and ObjectOutliner.
@eeckstein
Copy link
Contributor Author

@dan-zheng Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants