Skip to content

Commit 89c0182

Browse files
authored
Merge pull request #26839 from gottesmm/pr-686b39968d2ee9da42602cc4395184ca7faee36d
[ownership] Define a new instruction copy_unmanaged_value.
2 parents 031dead + 5fc1d1d commit 89c0182

31 files changed

+223
-107
lines changed

include/swift/AST/ReferenceStorage.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
/// UNCHECKED_REF_STORAGE
5656
/// Name##RetainValueInst
5757
/// Name##ReleaseValueInst
58+
/// Copy##Name##ValueInst
5859
/// LOADABLE_REF_STORAGE
5960
/// Ref*ToNameInst
6061
/// Name*ToRefInst
@@ -98,6 +99,10 @@
9899
/// storage type, and SOMETIMES_LOADABLE_CHECKED_REF_STORAGE needs *two*
99100
/// TypeInfos to be created. One for the loadable scenario, and one for the
100101
/// address-only scenario.
102+
///
103+
/// TODO: We should change Copy##Name##ValueInst to be defined on
104+
/// LOADABLE_REF_STORAGE. It just will require us to go through, refactor, and
105+
/// fix up this code.
101106

102107

103108
#ifndef REF_STORAGE

include/swift/SIL/SILBuilder.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -896,17 +896,25 @@ class SILBuilder {
896896
return insert(new (getModule()) \
897897
Store##Name##Inst(getSILDebugLocation(Loc), value, dest, isInit)); \
898898
}
899-
#define LOADABLE_REF_STORAGE_HELPER(Name) \
900-
Name##ToRefInst *create##Name##ToRef(SILLocation Loc, SILValue op, \
901-
SILType ty) { \
902-
return insert(new (getModule()) \
903-
Name##ToRefInst(getSILDebugLocation(Loc), op, ty)); \
904-
} \
905-
RefTo##Name##Inst *createRefTo##Name(SILLocation Loc, SILValue op, \
906-
SILType ty) { \
907-
return insert(new (getModule()) \
908-
RefTo##Name##Inst(getSILDebugLocation(Loc), op, ty)); \
899+
#define LOADABLE_REF_STORAGE_HELPER(Name) \
900+
Name##ToRefInst *create##Name##ToRef(SILLocation Loc, SILValue op, \
901+
SILType ty) { \
902+
return insert(new (getModule()) \
903+
Name##ToRefInst(getSILDebugLocation(Loc), op, ty)); \
904+
} \
905+
RefTo##Name##Inst *createRefTo##Name(SILLocation Loc, SILValue op, \
906+
SILType ty) { \
907+
return insert(new (getModule()) \
908+
RefTo##Name##Inst(getSILDebugLocation(Loc), op, ty)); \
909+
} \
910+
Copy##Name##ValueInst *createCopy##Name##Value(SILLocation Loc, \
911+
SILValue operand) { \
912+
auto type = getFunction().getLoweredType( \
913+
operand->getType().getASTType().getReferenceStorageReferent()); \
914+
return insert(new (getModule()) Copy##Name##ValueInst( \
915+
getSILDebugLocation(Loc), operand, type)); \
909916
}
917+
910918
#define ALWAYS_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
911919
LOADABLE_REF_STORAGE_HELPER(Name) \
912920
StrongRetain##Name##Inst *createStrongRetain##Name(SILLocation Loc, \
@@ -925,13 +933,6 @@ class SILBuilder {
925933
Atomicity atomicity) { \
926934
return insert(new (getModule()) \
927935
Name##ReleaseInst(getSILDebugLocation(Loc), Operand, atomicity)); \
928-
} \
929-
Copy##Name##ValueInst *createCopy##Name##Value(SILLocation Loc, \
930-
SILValue operand) { \
931-
auto type = getFunction().getLoweredType( \
932-
operand->getType().getASTType().getReferenceStorageReferent()); \
933-
return insert(new (getModule()) \
934-
Copy##Name##ValueInst(getSILDebugLocation(Loc), operand, type)); \
935936
}
936937
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
937938
NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, "...") \

include/swift/SIL/SILCloner.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,14 @@ SILCloner<ImplClass>::visitDebugValueAddrInst(DebugValueAddrInst *Inst) {
13081308
Inst, getBuilder().create##Name##ToRef(getOpLocation(Inst->getLoc()), \
13091309
getOpValue(Inst->getOperand()), \
13101310
getOpType(Inst->getType()))); \
1311+
} \
1312+
template <typename ImplClass> \
1313+
void SILCloner<ImplClass>::visitCopy##Name##ValueInst( \
1314+
Copy##Name##ValueInst *Inst) { \
1315+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope())); \
1316+
recordClonedInstruction(Inst, getBuilder().createCopy##Name##Value( \
1317+
getOpLocation(Inst->getLoc()), \
1318+
getOpValue(Inst->getOperand()))); \
13111319
}
13121320
#define ALWAYS_LOADABLE_CHECKED_REF_STORAGE(Name, name, ...) \
13131321
LOADABLE_REF_STORAGE_HELPER(Name, name) \
@@ -1336,14 +1344,6 @@ SILCloner<ImplClass>::visitDebugValueAddrInst(DebugValueAddrInst *Inst) {
13361344
getOpLocation(Inst->getLoc()), \
13371345
getOpValue(Inst->getOperand()), \
13381346
Inst->getAtomicity())); \
1339-
} \
1340-
template <typename ImplClass> \
1341-
void SILCloner<ImplClass>::visitCopy##Name##ValueInst( \
1342-
Copy##Name##ValueInst *Inst) { \
1343-
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope())); \
1344-
recordClonedInstruction(Inst, getBuilder().createCopy##Name##Value( \
1345-
getOpLocation(Inst->getLoc()), \
1346-
getOpValue(Inst->getOperand()))); \
13471347
}
13481348
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, name, ...) \
13491349
NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, name, "...") \

include/swift/SIL/SILInstruction.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6468,15 +6468,25 @@ class CopyValueInst
64686468
: UnaryInstructionBase(DebugLoc, operand, operand->getType()) {}
64696469
};
64706470

6471-
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
6472-
class Copy##Name##ValueInst \
6473-
: public UnaryInstructionBase<SILInstructionKind::Copy##Name##ValueInst, \
6474-
SingleValueInstruction> { \
6475-
friend class SILBuilder; \
6476-
Copy##Name##ValueInst(SILDebugLocation DebugLoc, SILValue operand, \
6477-
SILType type) \
6478-
: UnaryInstructionBase(DebugLoc, operand, type) {} \
6479-
};
6471+
#define UNCHECKED_REF_STORAGE(Name, ...) \
6472+
class Copy##Name##ValueInst \
6473+
: public UnaryInstructionBase<SILInstructionKind::Copy##Name##ValueInst, \
6474+
SingleValueInstruction> { \
6475+
friend class SILBuilder; \
6476+
Copy##Name##ValueInst(SILDebugLocation DebugLoc, SILValue operand, \
6477+
SILType type) \
6478+
: UnaryInstructionBase(DebugLoc, operand, type) {} \
6479+
};
6480+
6481+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
6482+
class Copy##Name##ValueInst \
6483+
: public UnaryInstructionBase<SILInstructionKind::Copy##Name##ValueInst, \
6484+
SingleValueInstruction> { \
6485+
friend class SILBuilder; \
6486+
Copy##Name##ValueInst(SILDebugLocation DebugLoc, SILValue operand, \
6487+
SILType type) \
6488+
: UnaryInstructionBase(DebugLoc, operand, type) {} \
6489+
};
64806490
#include "swift/AST/ReferenceStorage.def"
64816491

64826492
class DestroyValueInst

include/swift/SIL/SILNodes.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,9 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
562562
SingleValueInstruction, MayHaveSideEffects, DoesNotRelease)
563563
SINGLE_VALUE_INST(CopyValueInst, copy_value,
564564
SingleValueInstruction, MayHaveSideEffects, DoesNotRelease)
565+
#define UNCHECKED_REF_STORAGE(Name, name, ...) \
566+
SINGLE_VALUE_INST(Copy##Name##ValueInst, copy_##name##_value, \
567+
SingleValueInstruction, MayHaveSideEffects, DoesNotRelease)
565568
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, name, ...) \
566569
SINGLE_VALUE_INST(Copy##Name##ValueInst, copy_##name##_value, \
567570
SingleValueInstruction, MayHaveSideEffects, DoesNotRelease)

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5252
/// describe what change you made. The content of this comment isn't important;
5353
/// it just ensures a conflict if two people change the module format.
5454
/// Don't worry about adhering to the 80-column limit for this line.
55-
const uint16_t SWIFTMODULE_VERSION_MINOR = 512; // extended types may be left as unbound generic types
55+
const uint16_t SWIFTMODULE_VERSION_MINOR = 513; // Added copy_unmanaged_value.
5656

5757
using DeclIDField = BCFixed<31>;
5858

lib/IRGen/IRGenSIL.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,19 +1123,18 @@ class IRGenSILFunction :
11231123

11241124
void visitKeyPathInst(KeyPathInst *I);
11251125

1126-
1127-
#define LOADABLE_REF_STORAGE_HELPER(Name) \
1128-
void visitRefTo##Name##Inst(RefTo##Name##Inst *i); \
1129-
void visit##Name##ToRefInst(Name##ToRefInst *i);
1126+
#define LOADABLE_REF_STORAGE_HELPER(Name) \
1127+
void visitRefTo##Name##Inst(RefTo##Name##Inst *i); \
1128+
void visit##Name##ToRefInst(Name##ToRefInst *i); \
1129+
void visitCopy##Name##ValueInst(Copy##Name##ValueInst *i);
11301130
#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
11311131
void visitLoad##Name##Inst(Load##Name##Inst *i); \
11321132
void visitStore##Name##Inst(Store##Name##Inst *i);
1133-
#define ALWAYS_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
1134-
LOADABLE_REF_STORAGE_HELPER(Name) \
1135-
void visitStrongRetain##Name##Inst(StrongRetain##Name##Inst *i); \
1136-
void visit##Name##RetainInst(Name##RetainInst *i); \
1137-
void visit##Name##ReleaseInst(Name##ReleaseInst *i); \
1138-
void visitCopy##Name##ValueInst(Copy##Name##ValueInst *i);
1133+
#define ALWAYS_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
1134+
LOADABLE_REF_STORAGE_HELPER(Name) \
1135+
void visitStrongRetain##Name##Inst(StrongRetain##Name##Inst *i); \
1136+
void visit##Name##RetainInst(Name##RetainInst *i); \
1137+
void visit##Name##ReleaseInst(Name##ReleaseInst *i);
11391138
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
11401139
NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, "...") \
11411140
ALWAYS_LOADABLE_CHECKED_REF_STORAGE(Name, "...")
@@ -3878,6 +3877,31 @@ static const ReferenceTypeInfo &getReferentTypeInfo(IRGenFunction &IGF,
38783877
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, name, ...) \
38793878
NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, name, "...") \
38803879
ALWAYS_LOADABLE_CHECKED_REF_STORAGE(Name, name, "...")
3880+
#define UNCHECKED_REF_STORAGE(Name, name, ...) \
3881+
void IRGenSILFunction::visitCopy##Name##ValueInst( \
3882+
swift::Copy##Name##ValueInst *i) { \
3883+
Explosion in = getLoweredExplosion(i->getOperand()); \
3884+
auto silTy = i->getOperand()->getType(); \
3885+
auto ty = cast<Name##StorageType>(silTy.getASTType()); \
3886+
auto isOptional = bool(ty.getReferentType()->getOptionalObjectType()); \
3887+
auto &ti = getReferentTypeInfo(*this, silTy); \
3888+
/* Since we are unchecked, we just use strong retain here. We do not \
3889+
* perform any checks */ \
3890+
ti.strongRetain(*this, in, irgen::Atomicity::Atomic); \
3891+
/* Semantically we are just passing through the input parameter but as a \
3892+
*/ \
3893+
/* strong reference... at LLVM IR level these type differences don't */ \
3894+
/* matter. So just set the lowered explosion appropriately. */ \
3895+
Explosion output = getLoweredExplosion(i->getOperand()); \
3896+
if (isOptional) { \
3897+
auto values = output.claimAll(); \
3898+
output.reset(); \
3899+
for (auto value : values) { \
3900+
output.add(Builder.CreatePtrToInt(value, IGM.IntPtrTy)); \
3901+
} \
3902+
} \
3903+
setLoweredExplosion(i, output); \
3904+
}
38813905
#include "swift/AST/ReferenceStorage.def"
38823906
#undef COMMON_CHECKED_REF_STORAGE
38833907

lib/ParseSIL/ParseSIL.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,6 +2948,7 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
29482948
REFCOUNTING_INSTRUCTION(RetainValue)
29492949
REFCOUNTING_INSTRUCTION(ReleaseValueAddr)
29502950
REFCOUNTING_INSTRUCTION(RetainValueAddr)
2951+
#define UNCHECKED_REF_STORAGE(Name, ...) UNARY_INSTRUCTION(Copy##Name##Value)
29512952
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
29522953
REFCOUNTING_INSTRUCTION(StrongRetain##Name) \
29532954
REFCOUNTING_INSTRUCTION(Name##Retain) \

lib/SIL/InstructionUtils.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,15 @@ bool swift::onlyAffectsRefCount(SILInstruction *user) {
324324
case SILInstructionKind::StrongReleaseInst:
325325
case SILInstructionKind::StrongRetainInst:
326326
case SILInstructionKind::UnmanagedAutoreleaseValueInst:
327-
case SILInstructionKind::UnmanagedReleaseValueInst:
328-
case SILInstructionKind::UnmanagedRetainValueInst:
329-
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
330-
case SILInstructionKind::Name##RetainInst: \
331-
case SILInstructionKind::Name##ReleaseInst: \
332-
case SILInstructionKind::StrongRetain##Name##Inst:
327+
#define UNCHECKED_REF_STORAGE(Name, ...) \
328+
case SILInstructionKind::Name##RetainValueInst: \
329+
case SILInstructionKind::Name##ReleaseValueInst: \
330+
case SILInstructionKind::Copy##Name##ValueInst:
331+
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
332+
case SILInstructionKind::Name##RetainInst: \
333+
case SILInstructionKind::Name##ReleaseInst: \
334+
case SILInstructionKind::StrongRetain##Name##Inst: \
335+
case SILInstructionKind::Copy##Name##ValueInst:
333336
#include "swift/AST/ReferenceStorage.def"
334337
return true;
335338
}

lib/SIL/MemAccessUtils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,8 @@ void swift::visitAccessedAddress(SILInstruction *I,
746746
}
747747
// Non-access cases: these are marked with memory side effects, but, by
748748
// themselves, do not access formal memory.
749+
#define UNCHECKED_REF_STORAGE(Name, ...) \
750+
case SILInstructionKind::Copy##Name##ValueInst:
749751
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
750752
case SILInstructionKind::Copy##Name##ValueInst:
751753
#include "swift/AST/ReferenceStorage.def"

lib/SIL/OperandOwnership.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ ACCEPTS_ANY_OWNERSHIP_INST(ConvertEscapeToNoEscape)
284284
ACCEPTS_ANY_OWNERSHIP_INST(RefTo##Name) \
285285
ACCEPTS_ANY_OWNERSHIP_INST(Name##ToRef) \
286286
ACCEPTS_ANY_OWNERSHIP_INST(Copy##Name##Value)
287-
#define UNCHECKED_REF_STORAGE(Name, ...) ACCEPTS_ANY_OWNERSHIP_INST(RefTo##Name)
287+
#define UNCHECKED_REF_STORAGE(Name, ...) \
288+
ACCEPTS_ANY_OWNERSHIP_INST(RefTo##Name) \
289+
ACCEPTS_ANY_OWNERSHIP_INST(Copy##Name##Value)
288290
#include "swift/AST/ReferenceStorage.def"
289291
#undef ACCEPTS_ANY_OWNERSHIP_INST
290292

lib/SIL/SILBuilder.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ static bool couldReduceStrongRefcount(SILInstruction *Inst) {
280280
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
281281
case SILInstructionKind::Store##Name##Inst: \
282282
ALWAYS_LOADABLE_CHECKED_REF_STORAGE(Name, "...")
283+
#define UNCHECKED_REF_STORAGE(Name, ...) \
284+
case SILInstructionKind::Copy##Name##ValueInst: \
285+
return false;
283286
#include "swift/AST/ReferenceStorage.def"
284287
case SILInstructionKind::LoadInst:
285288
case SILInstructionKind::StoreInst:

lib/SIL/SILInstruction.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,10 @@ namespace {
734734
return true;
735735
}
736736

737-
#define LOADABLE_REF_STORAGE_HELPER(Name) \
738-
bool visit##Name##ToRefInst(Name##ToRefInst *RHS) { return true; } \
739-
bool visitRefTo##Name##Inst(RefTo##Name##Inst *RHS) { return true; }
737+
#define LOADABLE_REF_STORAGE_HELPER(Name) \
738+
bool visit##Name##ToRefInst(Name##ToRefInst *RHS) { return true; } \
739+
bool visitRefTo##Name##Inst(RefTo##Name##Inst *RHS) { return true; } \
740+
bool visitCopy##Name##ValueInst(Copy##Name##ValueInst *RHS) { return true; }
740741
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
741742
LOADABLE_REF_STORAGE_HELPER(Name) \
742743
bool visitStrongRetain##Name##Inst(const StrongRetain##Name##Inst *RHS) { \

lib/SIL/SILPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,11 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
15381538
*this << getIDAndType(I->getOperand());
15391539
}
15401540

1541+
#define UNCHECKED_REF_STORAGE(Name, ...) \
1542+
void visitCopy##Name##ValueInst(Copy##Name##ValueInst *I) { \
1543+
*this << getIDAndType(I->getOperand()); \
1544+
}
1545+
15411546
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
15421547
void visitCopy##Name##ValueInst(Copy##Name##ValueInst *I) { \
15431548
*this << getIDAndType(I->getOperand()); \

0 commit comments

Comments
 (0)