Skip to content

Commit d7d1a2b

Browse files
authored
Merge pull request swiftlang#21827 from Azoy/sr-9466
2 parents 3db7b4a + 5af2663 commit d7d1a2b

22 files changed

+336
-236
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,9 +812,11 @@ class SILBuilder {
812812
getSILDebugLocation(loc), buffer, enforcement, aborted, fromBuiltin));
813813
}
814814

815-
AssignInst *createAssign(SILLocation Loc, SILValue Src, SILValue DestAddr) {
815+
AssignInst *createAssign(SILLocation Loc, SILValue Src, SILValue DestAddr,
816+
AssignOwnershipQualifier Qualifier) {
816817
return insert(new (getModule())
817-
AssignInst(getSILDebugLocation(Loc), Src, DestAddr));
818+
AssignInst(getSILDebugLocation(Loc), Src, DestAddr,
819+
Qualifier));
818820
}
819821

820822
StoreBorrowInst *createStoreBorrow(SILLocation Loc, SILValue Src,

include/swift/SIL/SILCloner.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,8 @@ void SILCloner<ImplClass>::visitAssignInst(AssignInst *Inst) {
12001200
recordClonedInstruction(
12011201
Inst, getBuilder().createAssign(getOpLocation(Inst->getLoc()),
12021202
getOpValue(Inst->getSrc()),
1203-
getOpValue(Inst->getDest())));
1203+
getOpValue(Inst->getDest()),
1204+
Inst->getOwnershipQualifier()));
12041205
}
12051206

12061207
template<typename ImplClass>

include/swift/SIL/SILInstruction.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3605,6 +3605,25 @@ class EndUnpairedAccessInst
36053605
}
36063606
};
36073607

3608+
// *NOTE* When serializing, we can only represent up to 4 values here. If more
3609+
// qualifiers are added, SIL serialization must be updated.
3610+
enum class AssignOwnershipQualifier {
3611+
/// Unknown initialization method
3612+
Unknown,
3613+
3614+
/// The box contains a fully-initialized value.
3615+
Reassign,
3616+
3617+
/// The box contains a class instance that we own, but the instance has
3618+
/// not been initialized and should be freed with a special SIL
3619+
/// instruction made for this purpose.
3620+
Reinit,
3621+
3622+
/// The box contains an undefined value that should be ignored.
3623+
Init,
3624+
};
3625+
static_assert(2 == SILNode::NumAssignOwnershipQualifierBits, "Size mismatch");
3626+
36083627
/// AssignInst - Represents an abstract assignment to a memory location, which
36093628
/// may either be an initialization or a store sequence. This is only valid in
36103629
/// Raw SIL.
@@ -3615,7 +3634,9 @@ class AssignInst
36153634

36163635
FixedOperandList<2> Operands;
36173636

3618-
AssignInst(SILDebugLocation DebugLoc, SILValue Src, SILValue Dest);
3637+
AssignInst(SILDebugLocation DebugLoc, SILValue Src, SILValue Dest,
3638+
AssignOwnershipQualifier Qualifier =
3639+
AssignOwnershipQualifier::Unknown);
36193640

36203641
public:
36213642
enum {
@@ -3630,6 +3651,14 @@ class AssignInst
36303651

36313652
ArrayRef<Operand> getAllOperands() const { return Operands.asArray(); }
36323653
MutableArrayRef<Operand> getAllOperands() { return Operands.asArray(); }
3654+
3655+
AssignOwnershipQualifier getOwnershipQualifier() const {
3656+
return AssignOwnershipQualifier(
3657+
SILInstruction::Bits.AssignInst.OwnershipQualifier);
3658+
}
3659+
void setOwnershipQualifier(AssignOwnershipQualifier qualifier) {
3660+
SILInstruction::Bits.AssignInst.OwnershipQualifier = unsigned(qualifier);
3661+
}
36333662
};
36343663

36353664
/// Indicates that a memory location is uninitialized at this point and needs to

include/swift/SIL/SILNode.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class alignas(8) SILNode {
9696
enum { NumVOKindBits = 3 };
9797
enum { NumStoreOwnershipQualifierBits = 2 };
9898
enum { NumLoadOwnershipQualifierBits = 2 };
99+
enum { NumAssignOwnershipQualifierBits = 2 };
99100
enum { NumSILAccessKindBits = 2 };
100101
enum { NumSILAccessEnforcementBits = 2 };
101102

@@ -280,6 +281,10 @@ class alignas(8) SILNode {
280281
NumLoadOwnershipQualifierBits,
281282
OwnershipQualifier : NumLoadOwnershipQualifierBits
282283
);
284+
SWIFT_INLINE_BITFIELD(AssignInst, NonValueInstruction,
285+
NumAssignOwnershipQualifierBits,
286+
OwnershipQualifier : NumAssignOwnershipQualifierBits
287+
);
283288

284289
SWIFT_INLINE_BITFIELD(UncheckedOwnershipConversionInst,SingleValueInstruction,
285290
NumVOKindBits,

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 = 472; // Last change: partial_apply [stack]
55+
const uint16_t SWIFTMODULE_VERSION_MINOR = 473; // Last change: assign ownership qualifier
5656

5757
using DeclIDField = BCFixed<31>;
5858

lib/ParseSIL/ParseSIL.cpp

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,6 +1956,32 @@ static bool parseStoreOwnershipQualifier(StoreOwnershipQualifier &Result,
19561956
return false;
19571957
}
19581958

1959+
static bool parseAssignOwnershipQualifier(AssignOwnershipQualifier &Result,
1960+
SILParser &P) {
1961+
StringRef Str;
1962+
// If we do not parse '[' ... ']', we have unknown. Set value and return.
1963+
if (!parseSILOptional(Str, P)) {
1964+
Result = AssignOwnershipQualifier::Unknown;
1965+
return false;
1966+
}
1967+
1968+
// Then try to parse one of our other initialization kinds. We do not support
1969+
// parsing unknown here so we use that as our fail value.
1970+
auto Tmp = llvm::StringSwitch<AssignOwnershipQualifier>(Str)
1971+
.Case("reassign", AssignOwnershipQualifier::Reassign)
1972+
.Case("reinit", AssignOwnershipQualifier::Reinit)
1973+
.Case("init", AssignOwnershipQualifier::Init)
1974+
.Default(AssignOwnershipQualifier::Unknown);
1975+
1976+
// Thus return true (following the conventions in this file) if we fail.
1977+
if (Tmp == AssignOwnershipQualifier::Unknown)
1978+
return true;
1979+
1980+
// Otherwise, assign Result and return false.
1981+
Result = Tmp;
1982+
return false;
1983+
}
1984+
19591985
bool SILParser::parseSILDeclRef(SILDeclRef &Member, bool FnTypeRequired) {
19601986
SourceLoc TyLoc;
19611987
SmallVector<ValueDecl *, 4> values;
@@ -3448,18 +3474,25 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
34483474
break;
34493475
}
34503476

3477+
case SILInstructionKind::AssignInst:
34513478
case SILInstructionKind::StoreInst: {
34523479
UnresolvedValueName From;
34533480
SourceLoc ToLoc, AddrLoc;
34543481
Identifier ToToken;
34553482
SILValue AddrVal;
3456-
StoreOwnershipQualifier Qualifier;
3483+
StoreOwnershipQualifier StoreQualifier;
3484+
AssignOwnershipQualifier AssignQualifier;
3485+
bool IsStore = Opcode == SILInstructionKind::StoreInst;
3486+
bool IsAssign = Opcode == SILInstructionKind::AssignInst;
34573487
if (parseValueName(From) ||
34583488
parseSILIdentifier(ToToken, ToLoc, diag::expected_tok_in_sil_instr,
34593489
"to"))
34603490
return true;
34613491

3462-
if (parseStoreOwnershipQualifier(Qualifier, *this))
3492+
if (IsStore && parseStoreOwnershipQualifier(StoreQualifier, *this))
3493+
return true;
3494+
3495+
if (IsAssign && parseAssignOwnershipQualifier(AssignQualifier, *this))
34633496
return true;
34643497

34653498
if (parseTypedValueRef(AddrVal, AddrLoc, B) ||
@@ -3479,8 +3512,18 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
34793512

34803513
SILType ValType = AddrVal->getType().getObjectType();
34813514

3482-
ResultVal = B.createStore(InstLoc, getLocalValue(From, ValType, InstLoc, B),
3483-
AddrVal, Qualifier);
3515+
if (IsStore) {
3516+
ResultVal = B.createStore(InstLoc,
3517+
getLocalValue(From, ValType, InstLoc, B),
3518+
AddrVal, StoreQualifier);
3519+
} else {
3520+
assert(IsAssign);
3521+
3522+
ResultVal = B.createAssign(InstLoc,
3523+
getLocalValue(From, ValType, InstLoc, B),
3524+
AddrVal, AssignQualifier);
3525+
}
3526+
34843527
break;
34853528
}
34863529

@@ -3619,8 +3662,7 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
36193662
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
36203663
case SILInstructionKind::Store##Name##Inst:
36213664
#include "swift/AST/ReferenceStorage.def"
3622-
case SILInstructionKind::StoreBorrowInst:
3623-
case SILInstructionKind::AssignInst: {
3665+
case SILInstructionKind::StoreBorrowInst: {
36243666
UnresolvedValueName from;
36253667
bool isRefStorage = false;
36263668
#define NEVER_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
@@ -3673,12 +3715,6 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
36733715
}
36743716
#include "swift/AST/ReferenceStorage.def"
36753717

3676-
SILType ValType = addrVal->getType().getObjectType();
3677-
3678-
assert(Opcode == SILInstructionKind::AssignInst);
3679-
ResultVal = B.createAssign(InstLoc,
3680-
getLocalValue(from, ValType, InstLoc, B),
3681-
addrVal);
36823718
break;
36833719
}
36843720
case SILInstructionKind::AllocStackInst:

lib/SIL/SILInstructions.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,11 @@ StringRef swift::getSILAccessEnforcementName(SILAccessEnforcement enforcement) {
812812
llvm_unreachable("bad access enforcement");
813813
}
814814

815-
AssignInst::AssignInst(SILDebugLocation Loc, SILValue Src, SILValue Dest)
816-
: InstructionBase(Loc), Operands(this, Src, Dest) {}
815+
AssignInst::AssignInst(SILDebugLocation Loc, SILValue Src, SILValue Dest,
816+
AssignOwnershipQualifier Qualifier)
817+
: InstructionBase(Loc), Operands(this, Src, Dest) {
818+
SILInstruction::Bits.AssignInst.OwnershipQualifier = unsigned(Qualifier);
819+
}
817820

818821
MarkFunctionEscapeInst *
819822
MarkFunctionEscapeInst::create(SILDebugLocation Loc,

lib/SIL/SILPrinter.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,22 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
12721272
}
12731273
}
12741274

1275+
void printAssignOwnershipQualifier(AssignOwnershipQualifier Qualifier) {
1276+
switch (Qualifier) {
1277+
case AssignOwnershipQualifier::Unknown:
1278+
return;
1279+
case AssignOwnershipQualifier::Init:
1280+
*this << "[init] ";
1281+
return;
1282+
case AssignOwnershipQualifier::Reassign:
1283+
*this << "[reassign] ";
1284+
return;
1285+
case AssignOwnershipQualifier::Reinit:
1286+
*this << "[reinit] ";
1287+
return;
1288+
}
1289+
}
1290+
12751291
void visitStoreInst(StoreInst *SI) {
12761292
*this << Ctx.getID(SI->getSrc()) << " to ";
12771293
printStoreOwnershipQualifier(SI->getOwnershipQualifier());
@@ -1288,7 +1304,9 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
12881304
}
12891305

12901306
void visitAssignInst(AssignInst *AI) {
1291-
*this << Ctx.getID(AI->getSrc()) << " to " << getIDAndType(AI->getDest());
1307+
*this << Ctx.getID(AI->getSrc()) << " to ";
1308+
printAssignOwnershipQualifier(AI->getOwnershipQualifier());
1309+
*this << getIDAndType(AI->getDest());
12921310
}
12931311

12941312
void visitMarkUninitializedInst(MarkUninitializedInst *MU) {

lib/SILGen/SILGenLValue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3475,7 +3475,7 @@ static void emitUnloweredStoreOfCopy(SILGenBuilder &B, SILLocation loc,
34753475
if (isInit) {
34763476
B.emitStoreValueOperation(loc, value, addr, StoreOwnershipQualifier::Init);
34773477
} else {
3478-
B.createAssign(loc, value, addr);
3478+
B.createAssign(loc, value, addr, AssignOwnershipQualifier::Unknown);
34793479
}
34803480
}
34813481

lib/SILOptimizer/Mandatory/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ silopt_register_sources(
1717
SemanticARCOpts.cpp
1818
ClosureLifetimeFixup.cpp
1919
RawSILInstLowering.cpp
20-
MandatoryOptUtils.cpp
2120
)

0 commit comments

Comments
 (0)