Skip to content

[AST] Reference ownership macro fixes and cleanup #23003

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -3929,30 +3929,26 @@ class StoreReferenceInstBase : public InstructionBase<K, NonValueInstruction> {
/// \param loc The location of the expression that caused the load.
/// \param lvalue The SILValue representing the address to
/// use for the load.
#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
class Load##Name##Inst \
: public LoadReferenceInstBase<SILInstructionKind::Load##Name##Inst> { \
friend SILBuilder; \
Load##Name##Inst(SILDebugLocation loc, SILValue lvalue, IsTake_t isTake) \
: LoadReferenceInstBase(loc, lvalue, isTake) {} \
};
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, "...")
#include "swift/AST/ReferenceStorage.def"

/// Represents a store to a dynamic reference storage memory location.
/// This is only required for address-only scenarios; for loadable
/// references, it's better to use a ref_to_##name and a store.
#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
class Store##Name##Inst \
: public StoreReferenceInstBase<SILInstructionKind::Store##Name##Inst> { \
friend SILBuilder; \
Store##Name##Inst(SILDebugLocation loc, SILValue src, SILValue dest, \
IsInitialization_t isInit) \
: StoreReferenceInstBase(loc, src, dest, isInit) {} \
};
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, "...")
#include "swift/AST/ReferenceStorage.def"

/// CopyAddrInst - Represents a copy from one memory location to another. This
Expand Down
4 changes: 1 addition & 3 deletions lib/IRGen/GenHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ RESULT IRGenFunction::emit##KIND(TYPE1 val1, ReferenceCounting style) { \
llvm_unreachable("bad refcounting style"); \
}

#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
DEFINE_BINARY_OPERATION(Name##CopyInit, void, Address, Address) \
DEFINE_BINARY_OPERATION(Name##TakeInit, void, Address, Address) \
DEFINE_BINARY_OPERATION(Name##CopyAssign, void, Address, Address) \
Expand All @@ -1087,8 +1087,6 @@ RESULT IRGenFunction::emit##KIND(TYPE1 val1, ReferenceCounting style) { \
DEFINE_BINARY_OPERATION(Name##LoadStrong, llvm::Value *, Address,llvm::Type*)\
DEFINE_BINARY_OPERATION(Name##TakeStrong, llvm::Value *, Address,llvm::Type*)\
DEFINE_UNARY_OPERATION(Name##Destroy, void, Address)
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, "...")
#include "swift/AST/ReferenceStorage.def"


Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/InstructionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ struct OwnershipQualifiedKindVisitor : SILInstructionVisitor<OwnershipQualifiedK
QUALIFIED_INST(LoadBorrowInst)
QUALIFIED_INST(CopyValueInst)
QUALIFIED_INST(DestroyValueInst)
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
QUALIFIED_INST(Copy##Name##ValueInst)
#include "swift/AST/ReferenceStorage.def"
#undef QUALIFIED_INST
Expand Down
15 changes: 4 additions & 11 deletions lib/SIL/MemAccessUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,7 @@ bool swift::memInstMustInitialize(Operand *memOper) {
return cast<StoreInst>(memInst)->getOwnershipQualifier()
== StoreOwnershipQualifier::Init;

#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Store##Name##Inst: \
return cast<Store##Name##Inst>(memInst)->isInitializationOfDest();
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Store##Name##Inst: \
return cast<Store##Name##Inst>(memInst)->isInitializationOfDest();
#include "swift/AST/ReferenceStorage.def"
Expand Down Expand Up @@ -694,9 +691,7 @@ void swift::visitAccessedAddress(SILInstruction *I,
visitor(&I->getAllOperands()[CopyAddrInst::Dest]);
return;

#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Store##Name##Inst:
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Store##Name##Inst:
#include "swift/AST/ReferenceStorage.def"
case SILInstructionKind::StoreInst:
Expand All @@ -708,9 +703,7 @@ void swift::visitAccessedAddress(SILInstruction *I,
visitor(&I->getAllOperands()[0]);
return;

#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Load##Name##Inst:
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Load##Name##Inst:
#include "swift/AST/ReferenceStorage.def"
case SILInstructionKind::InitExistentialAddrInst:
Expand All @@ -732,7 +725,7 @@ void swift::visitAccessedAddress(SILInstruction *I,
}
// Non-access cases: these are marked with memory side effects, but, by
// themselves, do not access formal memory.
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
case SILInstructionKind::Copy##Name##ValueInst:
#include "swift/AST/ReferenceStorage.def"
case SILInstructionKind::AbortApplyInst:
Expand Down
4 changes: 1 addition & 3 deletions lib/SIL/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
printDebugVar(DVAI->getVarInfo());
}

#define NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
void visitLoad##Name##Inst(Load##Name##Inst *LI) { \
if (LI->isTake()) \
*this << "[take] "; \
Expand All @@ -1357,8 +1357,6 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
*this << "[initialization] "; \
*this << getIDAndType(SI->getDest()); \
}
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, "...")
#include "swift/AST/ReferenceStorage.def"

void visitCopyAddrInst(CopyAddrInst *CI) {
Expand Down
59 changes: 20 additions & 39 deletions lib/SIL/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1814,74 +1814,55 @@ 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(Name, name, ...) \
LOADABLE_REF_STORAGE_HELPER(Name, name) \
void checkStrongRetain##Name##Inst(StrongRetain##Name##Inst *RI) { \
requireObjectType(Name##StorageType, RI->getOperand(), \
"Operand of strong_retain_" #name); \
require(!F.hasOwnership(), "strong_retain_" #name " is only in " \
"functions with unqualified " \
"ownership"); \
} \
void check##Name##RetainInst(Name##RetainInst *RI) { \
requireObjectType(Name##StorageType, RI->getOperand(), \
"Operand of " #name "_retain"); \
require(!F.hasOwnership(), \
#name "_retain is only in functions with unqualified ownership"); \
} \
void check##Name##ReleaseInst(Name##ReleaseInst *RI) { \
requireObjectType(Name##StorageType, RI->getOperand(), \
"Operand of " #name "_release"); \
require(!F.hasOwnership(), \
#name "_release is only in functions with unqualified ownership"); \
} \
void checkCopy##Name##ValueInst(Copy##Name##ValueInst *I) { \
requireObjectType(Name##StorageType, I->getOperand(), \
"Operand of " #name "_retain"); \
require(F.hasOwnership(), \
"copy_" #name "_value is only valid in functions with qualified " \
"ownership"); \
}
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, name, ...) \
NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, name, "...") \
LOADABLE_REF_STORAGE_HELPER(Name, name) \
#define ALWAYS_LOADABLE_CHECKED_REF_STORAGE_HELPER(Name, name, closure) \
void checkStrongRetain##Name##Inst(StrongRetain##Name##Inst *RI) { \
auto ty = requireObjectType(Name##StorageType, RI->getOperand(), \
"Operand of strong_retain_" #name); \
require(ty->isLoadable(ResilienceExpansion::Maximal), \
"strong_retain_" #name " requires '" #name "' type to be loadable"); \
closure(); \
(void)ty; \
require(!F.hasOwnership(), "strong_retain_" #name " is only in " \
"functions with unqualified " \
"ownership"); \
} \
void check##Name##RetainInst(Name##RetainInst *RI) { \
auto ty = requireObjectType(Name##StorageType, RI->getOperand(), \
"Operand of " #name "_retain"); \
require(ty->isLoadable(ResilienceExpansion::Maximal), \
#name "_retain requires '" #name "' type to be loadable"); \
closure(); \
(void)ty; \
require(!F.hasOwnership(), \
#name "_retain is only in functions with unqualified ownership"); \
} \
void check##Name##ReleaseInst(Name##ReleaseInst *RI) { \
auto ty = requireObjectType(Name##StorageType, RI->getOperand(), \
"Operand of " #name "_release"); \
require(ty->isLoadable(ResilienceExpansion::Maximal), \
#name "_release requires '" #name "' type to be loadable"); \
closure(); \
(void)ty; \
require(!F.hasOwnership(), \
#name "_release is only in functions with unqualified ownership"); \
} \
void checkCopy##Name##ValueInst(Copy##Name##ValueInst *I) { \
auto ty = requireObjectType(Name##StorageType, I->getOperand(), \
"Operand of " #name "_retain"); \
require(ty->isLoadable(ResilienceExpansion::Maximal), \
#name "_retain requires '" #name "' type to be loadable"); \
closure(); \
(void)ty; \
/* *NOTE* We allow copy_##name##_value to be used throughout the entire */ \
/* pipeline even though it is a higher level instruction. */ \
}
#define ALWAYS_LOADABLE_CHECKED_REF_STORAGE(Name, name, ...) \
LOADABLE_REF_STORAGE_HELPER(Name, name) \
ALWAYS_LOADABLE_CHECKED_REF_STORAGE_HELPER(Name, name, []{})
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, name, ...) \
LOADABLE_REF_STORAGE_HELPER(Name, name) \
NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, name, "...") \
ALWAYS_LOADABLE_CHECKED_REF_STORAGE_HELPER(Name, name, [&]{ \
require(ty->isLoadable(ResilienceExpansion::Maximal), \
"'" #name "' type must be loadable"); \
})
#define UNCHECKED_REF_STORAGE(Name, name, ...) \
LOADABLE_REF_STORAGE_HELPER(Name, name)
#include "swift/AST/ReferenceStorage.def"
#undef LOADABLE_REF_STORAGE_HELPER
#undef ALWAYS_LOADABLE_CHECKED_REF_STORAGE_HELPER

void checkMarkUninitializedInst(MarkUninitializedInst *MU) {
SILValue Src = MU->getOperand();
Expand Down
8 changes: 7 additions & 1 deletion lib/SIL/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ namespace {
};

/// A type lowering for loadable @unowned types.
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
class Loadable##Name##TypeLowering final : public LeafLoadableTypeLowering { \
public: \
Loadable##Name##TypeLowering(SILType type) \
Expand Down Expand Up @@ -1173,6 +1173,12 @@ namespace {
return new (TC, Dependent) OpaqueValueTypeLowering(silType, properties);
}

#define ALWAYS_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
TypeLowering * \
visit##Name##StorageType(Can##Name##StorageType type) { \
return new (TC, Dependent) Loadable##Name##TypeLowering( \
SILType::getPrimitiveObjectType(type)); \
}
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
TypeLowering * \
visitLoadable##Name##StorageType(Can##Name##StorageType type) { \
Expand Down