Skip to content

Commit 1791108

Browse files
Merge pull request #67722 from nate-chandler/opaque-values/1/20230803/unowned-copies
[SIL] Represent copies to @sil_unowned values.
2 parents 3336083 + ded4d32 commit 1791108

28 files changed

+295
-17
lines changed

docs/SIL.rst

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5285,7 +5285,8 @@ positive. Otherwise, traps.
52855285

52865286
This operation must be atomic with respect to the final ``strong_release`` on
52875287
the operand (source) heap object. It need not be atomic with respect to
5288-
``store_unowned`` or ``load_unowned`` operations on the same address.
5288+
``store_unowned``/``unowned_copy_value`` or
5289+
``load_unowned``/``strong_copy_unowned_value`` operations on the same address.
52895290

52905291
store_unowned
52915292
`````````````
@@ -5305,8 +5306,30 @@ The storage must be initialized iff ``[init]`` is not specified.
53055306

53065307
This operation must be atomic with respect to the final ``strong_release`` on
53075308
the operand (source) heap object. It need not be atomic with respect to
5308-
``store_unowned`` or ``load_unowned`` operations on the same address.
5309+
``store_unowned``/``unowned_copy_value`` or
5310+
``load_unowned``/``strong_copy_unowned_value`` operations on the same address.
53095311

5312+
unowned_copy_value
5313+
``````````````````
5314+
::
5315+
5316+
sil-instruction ::= 'unowned_copy_value' sil-operand
5317+
5318+
%1 = unowned_copy_value %0 : $T
5319+
// %1 will be an @owned value of type $@sil_unowned T.
5320+
// $T must be a reference type
5321+
// $@sil_unowned T must be address-only
5322+
5323+
Only valid in opaque values mode. Lowered by AddressLowering to store_unowned.
5324+
5325+
Increments the unowned reference count of the object at ``%0``.
5326+
5327+
Wraps the operand in an instance of ``@sil_unowned``.
5328+
5329+
This operation must be atomic with respect to the final ``strong_release`` on
5330+
the operand (source) heap object. It need not be atomic with respect to
5331+
``store_unowned``/``unowned_copy_value`` or
5332+
``load_unowned``/``strong_copy_unowned_value`` operations on the same address.
53105333

53115334
fix_lifetime
53125335
````````````

include/swift/SIL/SILBuilder.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,17 @@ class SILBuilder {
10611061
getSILDebugLocation(Loc), ArgumentsSpecification, getModule()));
10621062
}
10631063

1064+
UnownedCopyValueInst *createUnownedCopyValue(SILLocation Loc,
1065+
SILValue operand) {
1066+
assert(!getFunction().getModule().useLoweredAddresses());
1067+
auto type = operand->getType()
1068+
.getReferenceStorageType(getFunction().getASTContext(),
1069+
ReferenceOwnership::Unowned)
1070+
.getObjectType();
1071+
return insert(new (getModule()) UnownedCopyValueInst(
1072+
getSILDebugLocation(Loc), operand, type));
1073+
}
1074+
10641075
WeakCopyValueInst *createWeakCopyValue(SILLocation Loc, SILValue operand) {
10651076
assert(!getFunction().getModule().useLoweredAddresses());
10661077
auto type = operand->getType()

include/swift/SIL/SILCloner.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,15 @@ SILCloner<ImplClass>::visitDebugStepInst(DebugStepInst *Inst) {
14071407
recordClonedInstruction(Inst, getBuilder().createDebugStep(Inst->getLoc()));
14081408
}
14091409

1410+
template <typename ImplClass>
1411+
void SILCloner<ImplClass>::visitUnownedCopyValueInst(
1412+
UnownedCopyValueInst *Inst) {
1413+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1414+
recordClonedInstruction(
1415+
Inst, getBuilder().createUnownedCopyValue(
1416+
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand())));
1417+
}
1418+
14101419
template <typename ImplClass>
14111420
void SILCloner<ImplClass>::visitWeakCopyValueInst(WeakCopyValueInst *Inst) {
14121421
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));

include/swift/SIL/SILInstruction.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8109,6 +8109,18 @@ class WeakCopyValueInst
81098109
}
81108110
};
81118111

8112+
class UnownedCopyValueInst
8113+
: public UnaryInstructionBase<SILInstructionKind::UnownedCopyValueInst,
8114+
SingleValueInstruction> {
8115+
friend class SILBuilder;
8116+
UnownedCopyValueInst(SILDebugLocation DebugLoc, SILValue operand,
8117+
SILType type)
8118+
: UnaryInstructionBase(DebugLoc, operand, type) {
8119+
assert(type.getReferenceStorageOwnership() == ReferenceOwnership::Unowned);
8120+
assert(type.getReferenceStorageReferentType() == operand->getType());
8121+
}
8122+
};
8123+
81128124
#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
81138125
class StrongCopy##Name##ValueInst \
81148126
: public UnaryInstructionBase< \

include/swift/SIL/SILNodes.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
453453
// alone by OSSA optimizations.
454454
SINGLE_VALUE_INST(ExplicitCopyValueInst, explicit_copy_value,
455455
SingleValueInstruction, None, DoesNotRelease)
456+
// Produce a @sil_unowned value from the specified value of reference type.
457+
SINGLE_VALUE_INST(UnownedCopyValueInst, unowned_copy_value,
458+
SingleValueInstruction, MayHaveSideEffects, DoesNotRelease)
456459
SINGLE_VALUE_INST(WeakCopyValueInst, weak_copy_value,
457460
SingleValueInstruction, MayHaveSideEffects, DoesNotRelease)
458461
#define REF_STORAGE(Name, name, ...) \

lib/IRGen/IRGenSIL.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,7 @@ class IRGenSILFunction :
15041504
void visitHasSymbolInst(HasSymbolInst *i);
15051505

15061506
void visitWeakCopyValueInst(swift::WeakCopyValueInst *i);
1507+
void visitUnownedCopyValueInst(swift::UnownedCopyValueInst *i);
15071508
#define LOADABLE_REF_STORAGE_HELPER(Name) \
15081509
void visitRefTo##Name##Inst(RefTo##Name##Inst *i); \
15091510
void visit##Name##ToRefInst(Name##ToRefInst *i);
@@ -5361,6 +5362,12 @@ void IRGenSILFunction::visitWeakCopyValueInst(swift::WeakCopyValueInst *i) {
53615362
llvm::report_fatal_error("weak_copy_value not lowered by AddressLowering!?");
53625363
}
53635364

5365+
void IRGenSILFunction::visitUnownedCopyValueInst(
5366+
swift::UnownedCopyValueInst *i) {
5367+
llvm::report_fatal_error(
5368+
"unowned_copy_value not lowered by AddressLowering!?");
5369+
}
5370+
53645371
#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, name, ...) \
53655372
void IRGenSILFunction::visitLoad##Name##Inst(swift::Load##Name##Inst *i) { \
53665373
Address source = getLoweredAddress(i->getOperand()); \

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ OPERAND_OWNERSHIP(InstantaneousUse, ClassMethod)
220220
OPERAND_OWNERSHIP(InstantaneousUse, SuperMethod)
221221
OPERAND_OWNERSHIP(InstantaneousUse, ClassifyBridgeObject)
222222
OPERAND_OWNERSHIP(InstantaneousUse, SetDeallocating)
223+
OPERAND_OWNERSHIP(InstantaneousUse, UnownedCopyValue)
223224
OPERAND_OWNERSHIP(InstantaneousUse, WeakCopyValue)
224225
#define REF_STORAGE(Name, ...) \
225226
OPERAND_OWNERSHIP(InstantaneousUse, StrongCopy##Name##Value)

lib/SIL/IR/SILPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,10 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
21412141
*this << getIDAndType(I->getOperand());
21422142
}
21432143

2144+
void visitUnownedCopyValueInst(UnownedCopyValueInst *I) {
2145+
*this << getIDAndType(I->getOperand());
2146+
}
2147+
21442148
void visitWeakCopyValueInst(WeakCopyValueInst *I) {
21452149
*this << getIDAndType(I->getOperand());
21462150
}

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class ValueOwnershipKindClassifier
4949
return OwnershipKind::OWNERSHIP; \
5050
}
5151

52+
CONSTANT_OWNERSHIP_INST(Owned, UnownedCopyValue)
5253
CONSTANT_OWNERSHIP_INST(Owned, WeakCopyValue)
5354
#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
5455
CONSTANT_OWNERSHIP_INST(Owned, StrongCopy##Name##Value) \

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,6 +3646,7 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
36463646
REFCOUNTING_INSTRUCTION(RetainValue)
36473647
REFCOUNTING_INSTRUCTION(ReleaseValueAddr)
36483648
REFCOUNTING_INSTRUCTION(RetainValueAddr)
3649+
UNARY_INSTRUCTION(UnownedCopyValue)
36493650
UNARY_INSTRUCTION(WeakCopyValue)
36503651
#define UNCHECKED_REF_STORAGE(Name, ...) \
36513652
UNARY_INSTRUCTION(StrongCopy##Name##Value)

0 commit comments

Comments
 (0)