Skip to content

[ownership] Define a new instruction copy_unmanaged_value. #26839

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

Conversation

gottesmm
Copy link
Contributor

This provides a singular instruction for convert an unmanaged value to a ref,
then strong_retain it. I expanded the definition of UNCHECKED_REF_STORAGE to
include these copy like instructions. This instruction is valid in all SIL.

The reason why I am adding this instruction is that currently when we emit an
access to an unowned (unsafe) ivar, we use an unmanaged_to_ref and a strong
retain. This can look to the optimizer like a strong retain that can potentially
be optimized. By combining the two together into a new instruction, we can avoid
this potential problem since the pattern matching will break.

This provides a singular instruction for convert an unmanaged value to a ref,
then strong_retain it. I expanded the definition of UNCHECKED_REF_STORAGE to
include these copy like instructions. This instruction is valid in all SIL.

The reason why I am adding this instruction is that currently when we emit an
access to an unowned (unsafe) ivar, we use an unmanaged_to_ref and a strong
retain. This can look to the optimizer like a strong retain that can potentially
be optimized. By combining the two together into a new instruction, we can avoid
this potential problem since the pattern matching will break.
@gottesmm gottesmm requested a review from eeckstein August 26, 2019 04:29
Copy link
Contributor Author

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some notes on some small cleanups I did.

case SILInstructionKind::Name##RetainInst: \
case SILInstructionKind::Name##ReleaseInst: \
case SILInstructionKind::StrongRetain##Name##Inst: \
case SILInstructionKind::Copy##Name##ValueInst:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: I centralized this Copy##Name##ValueInst into COMMON_ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE above.

@@ -157,7 +160,6 @@ static bool isRLEInertInstruction(SILInstruction *Inst) {
case SILInstructionKind::IsEscapingClosureInst:
case SILInstructionKind::IsUniqueInst:
case SILInstructionKind::FixLifetimeInst:
case SILInstructionKind::CopyUnownedValueInst:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just moved this slightly further up.

@@ -154,6 +154,9 @@ class MemoryBehaviorVisitor
}
REFCOUNTINC_MEMBEHAVIOR_INST(StrongRetainInst)
REFCOUNTINC_MEMBEHAVIOR_INST(RetainValueInst)
#define UNCHECKED_REF_STORAGE(Name, ...) \
REFCOUNTINC_MEMBEHAVIOR_INST(Name##RetainValueInst) \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this b/c these are manual retain, release instructions and should be treated as ref counting membehavior insts.

@@ -2083,40 +2083,41 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
"Operand of " #name "_to_ref does not have the " \
"operand's type as its referent type"); \
}
#define ALWAYS_LOADABLE_CHECKED_REF_STORAGE_HELPER(Name, name, closure) \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why clang-format reformatted this. I can undo this hunk if you want.

case SILInstructionKind::Name##RetainInst: \
case SILInstructionKind::Name##ReleaseInst: \
case SILInstructionKind::StrongRetain##Name##Inst:
#define UNCHECKED_REF_STORAGE(Name, ...) \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the Unmanaged*ValueInst under UNCHECKED_REF_STORAGE (since Unmanaged is an unchecked ref storage).

@gottesmm
Copy link
Contributor Author

@swift-ci test

@gottesmm
Copy link
Contributor Author

@lorentey I have a second PR that once this is in, just enables SILGen. So this is the meat.

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add the instruction to SIL.rst?
Otherwise lgtm!

@gottesmm
Copy link
Contributor Author

@eeckstein I am going to merge this and do that in a follow on PR.

@gottesmm gottesmm merged commit 89c0182 into swiftlang:master Aug 26, 2019
@gottesmm gottesmm deleted the pr-686b39968d2ee9da42602cc4395184ca7faee36d branch August 26, 2019 16:21
gottesmm added a commit to gottesmm/swift that referenced this pull request Aug 26, 2019
This commit adds docs for copy_unmanaged_value to SIL.rst as requested by
@eeckstein in swiftlang#26839. I noticed while doing the PR that copy_unowned_value was
also not documented, so I added docs for it as well as a bonus. = ).
Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, sounds good

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