-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
[ownership] Define a new instruction copy_unmanaged_value. #26839
Conversation
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.
There was a problem hiding this 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: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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) \ |
There was a problem hiding this comment.
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) \ |
There was a problem hiding this comment.
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, ...) \ |
There was a problem hiding this comment.
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).
@swift-ci test |
@lorentey I have a second PR that once this is in, just enables SILGen. So this is the meat. |
There was a problem hiding this 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!
@eeckstein I am going to merge this and do that in a follow on PR. |
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. = ).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, sounds good
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.