Skip to content

Commit 66b38a6

Browse files
committed
Refactor lowerAssignOrInitInstruction
1 parent 63888e3 commit 66b38a6

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

lib/SILOptimizer/Mandatory/RawSILInstLowering.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ lowerAssignOrInitInstruction(SILBuilderWithScope &b,
197197
assert(b.getModule().getASTContext().hadError() &&
198198
"assign_or_init must have a valid mode");
199199
// In case DefiniteInitialization already gave up with an error, just
200-
// treat the assign_or_init as an "init".
200+
// treat the assign_or_init with an "init" mode.
201201
LLVM_FALLTHROUGH;
202202
case AssignOrInitInst::Init: {
203203
SILValue initFn = inst->getInitializer();
@@ -210,20 +210,7 @@ lowerAssignOrInitInstruction(SILBuilderWithScope &b,
210210
bool isRefSelf =
211211
selfOrLocalValue->getType().getASTType()->mayHaveSuperclass();
212212

213-
SILValue selfRef = nullptr;
214-
if (!inLocalContext) {
215-
if (isRefSelf) {
216-
selfRef = b.emitBeginBorrowOperation(loc, selfOrLocalValue);
217-
} else {
218-
selfRef =
219-
b.createBeginAccess(loc, selfOrLocalValue, SILAccessKind::Modify,
220-
SILAccessEnforcement::Dynamic,
221-
/*noNestedConflict=*/false,
222-
/*fromBuiltin=*/false);
223-
}
224-
}
225-
226-
auto emitFieldReference = [&](VarDecl *field,
213+
auto emitFieldReference = [&](SILValue selfRef, VarDecl *field,
227214
bool emitDestroy = false) -> SILValue {
228215
SILValue fieldRef;
229216
if (isRefSelf) {
@@ -242,17 +229,26 @@ lowerAssignOrInitInstruction(SILBuilderWithScope &b,
242229

243230
// First, emit all of the properties listed in `initializes`. They
244231
// are passed as indirect results.
245-
{
246-
if (inLocalContext) {
247-
// add the local projection which is for the _x backing local storage
248-
arguments.push_back(selfOrLocalValue);
232+
SILValue selfRef = nullptr;
233+
if (inLocalContext) {
234+
// add the local projection which is for the _x backing local storage
235+
arguments.push_back(selfOrLocalValue);
236+
} else {
237+
if (isRefSelf) {
238+
selfRef = b.emitBeginBorrowOperation(loc, selfOrLocalValue);
249239
} else {
250-
auto toInitialize = inst->getInitializedProperties();
251-
for (unsigned index : indices(toInitialize)) {
252-
arguments.push_back(emitFieldReference(
253-
toInitialize[index],
254-
/*emitDestroy=*/inst->isPropertyAlreadyInitialized(index)));
255-
}
240+
selfRef =
241+
b.createBeginAccess(loc, selfOrLocalValue, SILAccessKind::Modify,
242+
SILAccessEnforcement::Dynamic,
243+
/*noNestedConflict=*/false,
244+
/*fromBuiltin=*/false);
245+
}
246+
247+
auto toInitialize = inst->getInitializedProperties();
248+
for (unsigned index : indices(toInitialize)) {
249+
arguments.push_back(emitFieldReference(
250+
selfRef, toInitialize[index],
251+
/*emitDestroy=*/inst->isPropertyAlreadyInitialized(index)));
256252
}
257253
}
258254

@@ -263,11 +259,11 @@ lowerAssignOrInitInstruction(SILBuilderWithScope &b,
263259

264260
// And finally, emit all of the `accesses` properties.
265261
for (auto *property : inst->getAccessedProperties())
266-
arguments.push_back(emitFieldReference(property));
262+
arguments.push_back(emitFieldReference(selfRef, property));
267263

268264
b.createApply(loc, initFn, SubstitutionMap(), arguments);
269265

270-
if (!inLocalContext) {
266+
if (selfRef) {
271267
if (isRefSelf) {
272268
if (selfRef != selfOrLocalValue)
273269
b.emitEndBorrowOperation(loc, selfRef);

0 commit comments

Comments
 (0)