Skip to content

Commit 3b85840

Browse files
committed
[SIL/DI] InitAccessors: Get all required info from AssignOrInit instead of instruction location
This is a more robust way of accessing the property information because instruction has a reference to init accessor declaration.
1 parent 7b140f8 commit 3b85840

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4935,15 +4935,16 @@ class AssignByWrapperInst
49354935
}
49364936
};
49374937

4938-
/// AssignByWrapperInst - Represents an abstract assignment via a wrapper,
4939-
/// which may either be an initialization or a store sequence. This is only
4940-
/// valid in Raw SIL.
4938+
/// AssignOrInitInst - Represents an abstract assignment via a init accessor
4939+
/// or a setter, which may either be an initialization or a store sequence.
4940+
/// This is only valid in Raw SIL.
49414941
///
49424942
/// Note that this instruction does not inherit from AssignInstBase because
49434943
/// there is no physical destination of the assignment. Both the init
49444944
/// and the setter are factored into functions.
49454945
class AssignOrInitInst
4946-
: public InstructionBase<SILInstructionKind::AssignOrInitInst, NonValueInstruction>,
4946+
: public InstructionBase<SILInstructionKind::AssignOrInitInst,
4947+
NonValueInstruction>,
49474948
public CopyLikeInstruction {
49484949
friend SILBuilder;
49494950
USE_SHARED_UINT8;
@@ -4969,7 +4970,7 @@ class AssignOrInitInst
49694970

49704971
public:
49714972
SILValue getSrc() const { return Operands[0].get(); }
4972-
SILValue getInitializer() { return Operands[1].get(); }
4973+
SILValue getInitializer() const { return Operands[1].get(); }
49734974
SILValue getSetter() { return Operands[2].get(); }
49744975

49754976
Mode getMode() const {
@@ -4980,8 +4981,13 @@ class AssignOrInitInst
49804981
sharedUInt8().AssignOrInitInst.mode = uint8_t(mode);
49814982
}
49824983

4984+
ArrayRef<VarDecl *> getInitializedProperties() const;
4985+
ArrayRef<VarDecl *> getAccessedProperties() const;
4986+
49834987
ArrayRef<Operand> getAllOperands() const { return Operands.asArray(); }
49844988
MutableArrayRef<Operand> getAllOperands() { return Operands.asArray(); }
4989+
4990+
AccessorDecl *getReferencedInitAccessor() const;
49854991
};
49864992

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

lib/SIL/IR/SILInstructions.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,29 @@ AssignOrInitInst::AssignOrInitInst(SILDebugLocation Loc, SILValue Src,
12651265
sharedUInt8().AssignOrInitInst.mode = uint8_t(Mode);
12661266
}
12671267

1268+
AccessorDecl *AssignOrInitInst::getReferencedInitAccessor() const {
1269+
auto *initRef =
1270+
cast<FunctionRefInst>(getInitializer()->getDefiningInstruction());
1271+
auto *accessorRef = initRef->getReferencedFunction();
1272+
return cast<AccessorDecl>(accessorRef->getDeclContext());
1273+
}
1274+
1275+
ArrayRef<VarDecl *> AssignOrInitInst::getInitializedProperties() const {
1276+
auto *accessor = getReferencedInitAccessor();
1277+
if (auto *initAttr = accessor->getAttrs().getAttribute<InitializesAttr>()) {
1278+
return initAttr->getPropertyDecls(accessor);
1279+
}
1280+
return {};
1281+
}
1282+
1283+
ArrayRef<VarDecl *> AssignOrInitInst::getAccessedProperties() const {
1284+
auto *accessor = getReferencedInitAccessor();
1285+
if (auto *accessAttr = accessor->getAttrs().getAttribute<AccessesAttr>()) {
1286+
return accessAttr->getPropertyDecls(accessor);
1287+
}
1288+
return {};
1289+
}
1290+
12681291
MarkFunctionEscapeInst *
12691292
MarkFunctionEscapeInst::create(SILDebugLocation Loc,
12701293
ArrayRef<SILValue> Elements, SILFunction &F) {

lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,22 +1187,20 @@ ElementUseCollector::collectAssignOrInitUses(PartialApplyInst *pai,
11871187
unsigned BaseEltNo) {
11881188
for (Operand *Op : pai->getUses()) {
11891189
SILInstruction *User = Op->getUser();
1190-
if (!isa<AssignOrInitInst>(User) || Op->getOperandNumber() != 1) {
1190+
if (!isa<AssignOrInitInst>(User) || Op->getOperandNumber() != 2) {
11911191
continue;
11921192
}
11931193

1194-
auto instLoc = User->getLoc();
1195-
auto *assignment = instLoc.getAsASTNode<AssignExpr>();
1196-
assert(assignment);
1194+
/// AssignOrInit doesn't operate on `self` so we need to make sure
1195+
/// that the flag is dropped before calling \c addElementUses.
1196+
llvm::SaveAndRestore<bool> X(IsSelfOfNonDelegatingInitializer, false);
11971197

1198-
auto propertyRef = cast<MemberRefExpr>(assignment->getDest())->getDecl();
1199-
1200-
auto initAccessor =
1201-
cast<VarDecl>(propertyRef.getDecl())->getAccessor(AccessorKind::Init);
1198+
auto *inst = cast<AssignOrInitInst>(User);
1199+
auto *typeDC = inst->getReferencedInitAccessor()
1200+
->getDeclContext()
1201+
->getSelfNominalTypeDecl();
12021202

12031203
auto selfTy = pai->getOperand(1)->getType();
1204-
auto *typeDC =
1205-
propertyRef.getDecl()->getDeclContext()->getSelfNominalTypeDecl();
12061204

12071205
auto addUse = [&](VarDecl *property, DIUseKind useKind) {
12081206
auto expansionContext = TypeExpansionContext(*pai->getFunction());
@@ -1211,17 +1209,11 @@ ElementUseCollector::collectAssignOrInitUses(PartialApplyInst *pai,
12111209
useKind);
12121210
};
12131211

1214-
if (auto *initializes =
1215-
initAccessor->getAttrs().getAttribute<InitializesAttr>()) {
1216-
for (auto *property : initializes->getPropertyDecls(initAccessor))
1217-
addUse(property, DIUseKind::InitOrAssign);
1218-
}
1212+
for (auto *property : inst->getInitializedProperties())
1213+
addUse(property, DIUseKind::InitOrAssign);
12191214

1220-
if (auto *initializes =
1221-
initAccessor->getAttrs().getAttribute<AccessesAttr>()) {
1222-
for (auto *property : initializes->getPropertyDecls(initAccessor))
1223-
addUse(property, DIUseKind::Load);
1224-
}
1215+
for (auto *property : inst->getAccessedProperties())
1216+
addUse(property, DIUseKind::Load);
12251217
}
12261218
}
12271219

0 commit comments

Comments
 (0)