Skip to content

Commit a22bd27

Browse files
authored
Merge pull request #67593 from xedin/textual-sil-assign_or_init
[SIL] InitAccessors: Support `assign_or_init` in textual SIL
2 parents 16c59fa + 7777ed5 commit a22bd27

13 files changed

+195
-54
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,13 +956,15 @@ class SILBuilder {
956956
}
957957

958958
AssignOrInitInst *createAssignOrInit(SILLocation Loc,
959+
VarDecl *Property,
959960
SILValue Self,
960961
SILValue Src,
961962
SILValue Initializer,
962963
SILValue Setter,
963964
AssignOrInitInst::Mode Mode) {
964-
return insert(new (getModule()) AssignOrInitInst(
965-
getSILDebugLocation(Loc), Self, Src, Initializer, Setter, Mode));
965+
return insert(new (getModule())
966+
AssignOrInitInst(getSILDebugLocation(Loc), Property, Self,
967+
Src, Initializer, Setter, Mode));
966968
}
967969

968970
StoreBorrowInst *createStoreBorrow(SILLocation Loc, SILValue Src,

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,7 @@ void SILCloner<ImplClass>::visitAssignOrInitInst(AssignOrInitInst *Inst) {
13561356
recordClonedInstruction(
13571357
Inst, getBuilder().createAssignOrInit(
13581358
getOpLocation(Inst->getLoc()),
1359+
Inst->getProperty(),
13591360
getOpValue(Inst->getSelf()),
13601361
getOpValue(Inst->getSrc()),
13611362
getOpValue(Inst->getInitializer()),

include/swift/SIL/SILInstruction.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4820,6 +4820,9 @@ class AssignOrInitInst
48204820

48214821
FixedOperandList<4> Operands;
48224822

4823+
/// Property the init accessor is associated with.
4824+
VarDecl *Property;
4825+
48234826
/// Marks all of the properties in `initializes(...)` list that
48244827
/// have been initialized before this intruction to help Raw SIL
48254828
/// lowering to emit destroys.
@@ -4838,10 +4841,12 @@ class AssignOrInitInst
48384841
};
48394842

48404843
private:
4841-
AssignOrInitInst(SILDebugLocation DebugLoc, SILValue Self, SILValue Src,
4842-
SILValue Initializer, SILValue Setter, Mode mode);
4844+
AssignOrInitInst(SILDebugLocation DebugLoc, VarDecl *P, SILValue Self,
4845+
SILValue Src, SILValue Initializer, SILValue Setter,
4846+
Mode mode);
48434847

48444848
public:
4849+
VarDecl *getProperty() const { return Property; }
48454850
SILValue getSelf() const { return Operands[0].get(); }
48464851
SILValue getSrc() const { return Operands[1].get(); }
48474852
SILValue getInitializer() const { return Operands[2].get(); }

lib/AST/ASTPrinter.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
309309
}
310310
}
311311

312+
if (auto *accessor = dyn_cast<AccessorDecl>(D)) {
313+
if (accessor->isInitAccessor() && !options.PrintForSIL)
314+
return false;
315+
}
316+
312317
return ShouldPrintChecker::shouldPrint(D, options);
313318
}
314319
};
@@ -2259,8 +2264,12 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
22592264
accessorsToPrint.push_back(Accessor);
22602265
};
22612266

2267+
if (ASD->hasInitAccessor())
2268+
AddAccessorToPrint(AccessorKind::Init);
2269+
22622270
if (PrintAbstract) {
22632271
AddAccessorToPrint(AccessorKind::Get);
2272+
22642273
if (ASD->supportsMutation())
22652274
AddAccessorToPrint(AccessorKind::Set);
22662275
} else {

lib/AST/Attr.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,32 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
14851485
break;
14861486
}
14871487

1488+
case DAK_StorageRestrictions: {
1489+
auto *attr = cast<StorageRestrictionsAttr>(this);
1490+
Printer.printAttrName("@storageRestrictions");
1491+
Printer << "(";
1492+
1493+
auto initializes = attr->getInitializesNames();
1494+
auto accesses = attr->getAccessesNames();
1495+
1496+
bool needsComma = !initializes.empty() && !accesses.empty();
1497+
1498+
if (!initializes.empty()) {
1499+
Printer << "initializes: ";
1500+
interleave(initializes, Printer, ", ");
1501+
}
1502+
1503+
if (needsComma)
1504+
Printer << ", ";
1505+
1506+
if (!accesses.empty()) {
1507+
Printer << "accesses: ";
1508+
interleave(accesses, Printer, ", ");
1509+
}
1510+
Printer << ")";
1511+
break;
1512+
}
1513+
14881514
case DAK_Count:
14891515
llvm_unreachable("exceed declaration attribute kinds");
14901516

lib/Parse/ParseDecl.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7281,7 +7281,7 @@ static void diagnoseRedundantAccessors(Parser &P, SourceLoc loc,
72817281
/*already*/ true);
72827282
}
72837283

7284-
static bool isAllowedInProtocolRequirement(AccessorKind kind) {
7284+
static bool isAllowedWhenParsingLimitedSyntax(AccessorKind kind, bool forSIL) {
72857285
switch (kind) {
72867286
case AccessorKind::Get:
72877287
case AccessorKind::Set:
@@ -7293,8 +7293,10 @@ static bool isAllowedInProtocolRequirement(AccessorKind kind) {
72937293
case AccessorKind::DidSet:
72947294
case AccessorKind::Read:
72957295
case AccessorKind::Modify:
7296-
case AccessorKind::Init:
72977296
return false;
7297+
7298+
case AccessorKind::Init:
7299+
return forSIL;
72987300
}
72997301
llvm_unreachable("bad accessor kind");
73007302
}
@@ -7688,7 +7690,8 @@ ParserStatus Parser::parseGetSet(ParseDeclOptions Flags, ParameterList *Indices,
76887690

76897691
// For now, immediately reject illegal accessors in protocols just to
76907692
// avoid having to deal with them everywhere.
7691-
if (parsingLimitedSyntax && !isAllowedInProtocolRequirement(Kind)) {
7693+
if (parsingLimitedSyntax && !isAllowedWhenParsingLimitedSyntax(
7694+
Kind, SF.Kind == SourceFileKind::SIL)) {
76927695
diagnose(Loc, diag::expected_getset_in_protocol);
76937696
continue;
76947697
}

lib/SIL/IR/SILInstructions.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,12 +1259,13 @@ AssignByWrapperInst::AssignByWrapperInst(SILDebugLocation Loc,
12591259
sharedUInt8().AssignByWrapperInst.mode = uint8_t(mode);
12601260
}
12611261

1262-
AssignOrInitInst::AssignOrInitInst(SILDebugLocation Loc, SILValue Self,
1263-
SILValue Src, SILValue Initializer,
1264-
SILValue Setter, AssignOrInitInst::Mode Mode)
1262+
AssignOrInitInst::AssignOrInitInst(SILDebugLocation Loc, VarDecl *P,
1263+
SILValue Self, SILValue Src,
1264+
SILValue Initializer, SILValue Setter,
1265+
AssignOrInitInst::Mode Mode)
12651266
: InstructionBase<SILInstructionKind::AssignOrInitInst,
12661267
NonValueInstruction>(Loc),
1267-
Operands(this, Self, Src, Initializer, Setter) {
1268+
Operands(this, Self, Src, Initializer, Setter), Property(P) {
12681269
assert(Initializer->getType().is<SILFunctionType>());
12691270
sharedUInt8().AssignOrInitInst.mode = uint8_t(Mode);
12701271
Assignments.resize(getNumInitializedProperties());
@@ -1291,23 +1292,11 @@ bool AssignOrInitInst::isPropertyAlreadyInitialized(unsigned propertyIdx) {
12911292
}
12921293

12931294
StringRef AssignOrInitInst::getPropertyName() const {
1294-
auto *accessor = getReferencedInitAccessor();
1295-
assert(accessor);
1296-
return cast<VarDecl>(accessor->getStorage())->getNameStr();
1295+
return Property->getNameStr();
12971296
}
12981297

12991298
AccessorDecl *AssignOrInitInst::getReferencedInitAccessor() const {
1300-
SILValue initRef = getInitializer();
1301-
SILFunction *accessorFn = nullptr;
1302-
1303-
if (auto *PAI = dyn_cast<PartialApplyInst>(initRef)) {
1304-
accessorFn = PAI->getReferencedFunctionOrNull();
1305-
} else {
1306-
accessorFn = cast<FunctionRefInst>(initRef)->getReferencedFunctionOrNull();
1307-
}
1308-
1309-
assert(accessorFn);
1310-
return dyn_cast_or_null<AccessorDecl>(accessorFn->getDeclContext());
1299+
return Property->getOpaqueAccessor(AccessorKind::Init);
13111300
}
13121301

13131302
unsigned AssignOrInitInst::getNumInitializedProperties() const {

lib/SIL/IR/SILPrinter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1806,7 +1806,11 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
18061806
}
18071807
}
18081808

1809-
*this << "self " << getIDAndType(AI->getSelf());
1809+
*this << "#";
1810+
printFullContext(AI->getProperty()->getDeclContext(), PrintState.OS);
1811+
*this << AI->getPropertyName();
1812+
1813+
*this << ", self " << getIDAndType(AI->getSelf());
18101814
*this << ", value " << getIDAndType(AI->getSrc());
18111815
*this << ", init " << getIDAndType(AI->getInitializer())
18121816
<< ", set " << getIDAndType(AI->getSetter());

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4749,12 +4749,15 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
47494749
}
47504750

47514751
case SILInstructionKind::AssignOrInitInst: {
4752+
ValueDecl *Prop;
47524753
SILValue Self, Src, InitFn, SetFn;
47534754
AssignOrInitInst::Mode Mode;
47544755
llvm::SmallVector<unsigned, 2> assignments;
47554756

47564757
if (parseAssignOrInitMode(Mode, *this) ||
47574758
parseAssignOrInitAssignments(assignments, *this) ||
4759+
parseSILDottedPath(Prop) ||
4760+
P.parseToken(tok::comma, diag::expected_tok_in_sil_instr, ",") ||
47584761
parseVerbatim("self") || parseTypedValueRef(Self, B) ||
47594762
P.parseToken(tok::comma, diag::expected_tok_in_sil_instr, ",") ||
47604763
parseVerbatim("value") || parseTypedValueRef(Src, B) ||
@@ -4765,7 +4768,8 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
47654768
parseSILDebugLocation(InstLoc, B))
47664769
return true;
47674770

4768-
auto *AI = B.createAssignOrInit(InstLoc, Self, Src, InitFn, SetFn, Mode);
4771+
auto *AI = B.createAssignOrInit(InstLoc, cast<VarDecl>(Prop), Self, Src,
4772+
InitFn, SetFn, Mode);
47694773

47704774
for (unsigned index : assignments)
47714775
AI->markAsInitialized(index);

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2736,7 +2736,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
27362736
SILFunctionConventions initConv(initTy, AI->getModule());
27372737

27382738
require(initConv.getNumIndirectSILResults() ==
2739-
AI->getInitializedProperties().size(),
2739+
AI->getNumInitializedProperties(),
27402740
"init function has invalid number of indirect results");
27412741
checkAssigOrInitInstAccessorArgs(Src->getType(), initConv);
27422742
}

lib/SILGen/SILGenFunction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,7 @@ void SILGenFunction::emitAssignOrInit(SILLocation loc, ManagedValue selfValue,
18591859
setterFRef = SILUndef::get(initFRef->getType(), F);
18601860
}
18611861

1862-
B.createAssignOrInit(loc, selfValue.getValue(), newValue.forward(*this),
1863-
initFRef, setterFRef, AssignOrInitInst::Unknown);
1862+
B.createAssignOrInit(loc, field, selfValue.getValue(),
1863+
newValue.forward(*this), initFRef, setterFRef,
1864+
AssignOrInitInst::Unknown);
18641865
}

0 commit comments

Comments
 (0)