Skip to content

Commit 014fd2b

Browse files
committed
Small cleanup
1 parent ec0750c commit 014fd2b

File tree

2 files changed

+49
-55
lines changed

2 files changed

+49
-55
lines changed

lib/SILGen/SILGenDestructor.cpp

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "ArgumentScope.h"
14+
#include "RValue.h"
1315
#include "SILGenFunction.h"
1416
#include "SILGenFunctionBuilder.h"
15-
#include "RValue.h"
16-
#include "ArgumentScope.h"
17-
#include "llvm/ADT/SmallSet.h"
1817
#include "swift/AST/GenericSignature.h"
1918
#include "swift/AST/SubstitutionMap.h"
2019
#include "swift/SIL/TypeLowering.h"
20+
#include "llvm/ADT/SmallSet.h"
2121

2222
using namespace swift;
2323
using namespace Lowering;
@@ -220,76 +220,79 @@ void SILGenFunction::destroyClassMember(SILLocation cleanupLoc,
220220
}
221221
}
222222

223-
llvm::SmallSetVector<VarDecl*, 4> findRecursiveLinks(DeclContext* DC, ClassDecl *cd) {
223+
llvm::SmallSetVector<VarDecl *, 4> findRecursiveLinks(DeclContext *DC,
224+
ClassDecl *cd) {
224225
auto SelfTy = DC->mapTypeIntoContext(cd->getDeclaredType());
225226

226227
// Collect all stored properties that would form a recursive structure,
227228
// so we can remove the recursion and prevent the call stack from
228229
// overflowing.
229-
llvm::SmallSetVector<VarDecl*, 4> recursiveLinks;
230+
llvm::SmallSetVector<VarDecl *, 4> recursiveLinks;
230231
for (VarDecl *vd : cd->getStoredProperties()) {
231232
auto Ty = vd->getType()->getOptionalObjectType();
232233
if (Ty && Ty->getCanonicalType() == SelfTy->getCanonicalType()) {
233234
recursiveLinks.insert(vd);
234235
}
235236
}
236237

237-
// NOTE: Right now we only optimize linear recursion, so if there is more than one link,
238-
// clear out the set and don't perform any recursion optimization.
238+
// NOTE: Right now we only optimize linear recursion, so if there is more than
239+
// one link, clear out the set and don't perform any recursion optimization.
239240
if (recursiveLinks.size() < 1) {
240241
recursiveLinks.clear();
241242
}
242243

243244
return recursiveLinks;
244245
}
245246

246-
247-
void SILGenFunction::emitRecursiveChainDestruction(ManagedValue selfValue,
248-
ClassDecl *cd,
249-
SmallSetVector<VarDecl*, 4> recursiveLinks,
250-
CleanupLocation cleanupLoc) {
247+
void SILGenFunction::emitRecursiveChainDestruction(
248+
ManagedValue selfValue, ClassDecl *cd,
249+
SmallSetVector<VarDecl *, 4> recursiveLinks, CleanupLocation cleanupLoc) {
251250
auto SelfTy = F.mapTypeIntoContext(cd->getDeclaredType());
252251

253252
assert(recursiveLinks.size() <= 1 && "Only linear recursion supported.");
254253

255254
auto SelfTyLowered = getTypeLowering(SelfTy).getLoweredType();
256-
for (VarDecl* vd : recursiveLinks) {
257-
SILBasicBlock* cleanBB = createBasicBlock();
258-
SILBasicBlock* noneBB = createBasicBlock();
259-
SILBasicBlock* notUniqueBB = createBasicBlock();
260-
SILBasicBlock* uniqueBB = createBasicBlock();
261-
SILBasicBlock* someBB = createBasicBlock();
262-
SILBasicBlock* loopBB = createBasicBlock();
255+
for (VarDecl *vd : recursiveLinks) {
256+
SILBasicBlock *cleanBB = createBasicBlock();
257+
SILBasicBlock *noneBB = createBasicBlock();
258+
SILBasicBlock *notUniqueBB = createBasicBlock();
259+
SILBasicBlock *uniqueBB = createBasicBlock();
260+
SILBasicBlock *someBB = createBasicBlock();
261+
SILBasicBlock *loopBB = createBasicBlock();
263262

264263
// var iter = self.link
265264
// self.link = nil
266265
auto Ty = getTypeLowering(vd->getType()).getLoweredType();
267266
auto optionalNone = B.createOptionalNone(cleanupLoc, Ty);
268-
SILValue varAddr =
269-
B.createRefElementAddr(cleanupLoc, selfValue.getValue(), vd,
270-
Ty.getAddressType());
267+
SILValue varAddr = B.createRefElementAddr(cleanupLoc, selfValue.getValue(),
268+
vd, Ty.getAddressType());
271269
auto iterAddr = B.createAllocStack(cleanupLoc, Ty);
272-
SILValue addr = B.createBeginAccess(
273-
cleanupLoc, varAddr, SILAccessKind::Modify, SILAccessEnforcement::Static,
274-
false /*noNestedConflict*/, false /*fromBuiltin*/);
275-
SILValue iter = B.createLoad(cleanupLoc, addr, LoadOwnershipQualifier::Copy);
276-
B.createStore(cleanupLoc, optionalNone, addr, StoreOwnershipQualifier::Assign);
270+
SILValue addr =
271+
B.createBeginAccess(cleanupLoc, varAddr, SILAccessKind::Modify,
272+
SILAccessEnforcement::Static,
273+
false /*noNestedConflict*/, false /*fromBuiltin*/);
274+
SILValue iter =
275+
B.createLoad(cleanupLoc, addr, LoadOwnershipQualifier::Copy);
276+
B.createStore(cleanupLoc, optionalNone, addr,
277+
StoreOwnershipQualifier::Assign);
277278
B.createEndAccess(cleanupLoc, addr, false /*is aborting*/);
278279
B.createStore(cleanupLoc, iter, iterAddr, StoreOwnershipQualifier::Init);
279280

280281
B.createBranch(cleanupLoc, loopBB);
281282

282283
// while iter != nil {
283284
B.emitBlock(loopBB);
284-
SILValue operand = B.createLoad(cleanupLoc, iterAddr, LoadOwnershipQualifier::Copy);
285+
SILValue operand =
286+
B.createLoad(cleanupLoc, iterAddr, LoadOwnershipQualifier::Copy);
285287
auto operandCopy = B.createCopyValue(cleanupLoc, operand);
286288
auto operandAddr = B.createAllocStack(cleanupLoc, Ty);
287-
B.createStore(cleanupLoc, operandCopy, operandAddr, StoreOwnershipQualifier::Init);
289+
B.createStore(cleanupLoc, operandCopy, operandAddr,
290+
StoreOwnershipQualifier::Init);
288291
B.createDestroyValue(cleanupLoc, operand);
289292
B.createSwitchEnumAddr(
290-
cleanupLoc, operandAddr, nullptr,
291-
{{getASTContext().getOptionalSomeDecl(), someBB},
292-
{std::make_pair(getASTContext().getOptionalNoneDecl(), noneBB)}});
293+
cleanupLoc, operandAddr, nullptr,
294+
{{getASTContext().getOptionalSomeDecl(), someBB},
295+
{std::make_pair(getASTContext().getOptionalNoneDecl(), noneBB)}});
293296

294297
// if isKnownUniquelyReferenced(&iter) {
295298
B.emitBlock(someBB);
@@ -298,7 +301,6 @@ void SILGenFunction::emitRecursiveChainDestruction(ManagedValue selfValue,
298301
auto isUnique = B.createIsUnique(cleanupLoc, iterAddr);
299302
B.createCondBranch(cleanupLoc, isUnique, uniqueBB, notUniqueBB);
300303

301-
302304
// we have a uniquely referenced link, so we need to deinit
303305
B.emitBlock(uniqueBB);
304306

@@ -309,32 +311,24 @@ void SILGenFunction::emitRecursiveChainDestruction(ManagedValue selfValue,
309311

310312
// let tail = iter.unsafelyUnwrapped.next
311313
// iter = tail
312-
SILValue _iter = B.createLoad(cleanupLoc, iterAddr, LoadOwnershipQualifier::Copy);
314+
SILValue _iter =
315+
B.createLoad(cleanupLoc, iterAddr, LoadOwnershipQualifier::Copy);
313316
auto iterBorrow = B.createBeginBorrow(cleanupLoc, _iter);
314-
auto iterBorrowAddr = B.createAllocStack(cleanupLoc, Ty);
315-
B.createStoreBorrow(cleanupLoc, iterBorrow, iterBorrowAddr);
316-
auto xx = B.createLoadBorrow(cleanupLoc, iterBorrowAddr);
317-
auto* link = B.createUncheckedEnumData(cleanupLoc,
318-
xx,
319-
getASTContext().getOptionalSomeDecl(),
320-
SelfTyLowered);
321-
varAddr = B.createRefElementAddr(cleanupLoc,
322-
link,
323-
vd,
324-
Ty.getAddressType());
317+
auto *link = B.createUncheckedEnumData(
318+
cleanupLoc, iterBorrow, getASTContext().getOptionalSomeDecl(),
319+
SelfTyLowered);
320+
varAddr = B.createRefElementAddr(cleanupLoc, link, vd, Ty.getAddressType());
325321

326322
addr = B.createBeginAccess(
327-
cleanupLoc, varAddr, SILAccessKind::Read, SILAccessEnforcement::Static,
328-
false /* noNestedConflict */, false /*fromBuiltin*/);
323+
cleanupLoc, varAddr, SILAccessKind::Read, SILAccessEnforcement::Static,
324+
false /* noNestedConflict */, false /*fromBuiltin*/);
329325
iter = B.createLoad(cleanupLoc, addr, LoadOwnershipQualifier::Copy);
330326
B.createEndAccess(cleanupLoc, addr, false /*is aborting*/);
331327
B.createStore(cleanupLoc, iter, iterAddr, StoreOwnershipQualifier::Assign);
332328

333-
B.createEndBorrow(cleanupLoc, xx);
334329
B.createEndBorrow(cleanupLoc, iterBorrow);
335330

336331
B.createDestroyValue(cleanupLoc, _iter);
337-
B.createDeallocStack(cleanupLoc, iterBorrowAddr);
338332

339333
B.createBranch(cleanupLoc, loopBB);
340334

@@ -347,7 +341,6 @@ void SILGenFunction::emitRecursiveChainDestruction(ManagedValue selfValue,
347341
B.createDeallocStack(cleanupLoc, operandAddr);
348342
B.createBranch(cleanupLoc, cleanBB);
349343

350-
351344
B.emitBlock(cleanBB);
352345
B.createDestroyAddr(cleanupLoc, iterAddr);
353346
B.createDeallocStack(cleanupLoc, iterAddr);
@@ -374,8 +367,9 @@ void SILGenFunction::emitClassMemberDestruction(ManagedValue selfValue,
374367
/// A distributed actor may be 'remote' in which case there is no need to
375368
/// destroy "all" members, because they never had storage to begin with.
376369
if (cd->isDistributedActor()) {
377-
normalMemberDestroyBB = createBasicBlock();
378370
finishBB = createBasicBlock();
371+
normalMemberDestroyBB = createBasicBlock();
372+
379373
emitDistributedActorClassMemberDestruction(cleanupLoc, selfValue, cd,
380374
normalMemberDestroyBB,
381375
finishBB);

lib/SILGen/SILGenFunction.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -687,10 +687,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
687687
/// \param cd The class declaration whose members are being destroyed.
688688
/// \param recursiveLinks The set of stored properties that form the
689689
/// recursive data structure.
690-
void emitRecursiveChainDestruction(ManagedValue selfValue,
691-
ClassDecl *cd,
692-
llvm::SmallSetVector<VarDecl*, 4> recursiveLinks,
693-
CleanupLocation cleanupLoc);
690+
void emitRecursiveChainDestruction(
691+
ManagedValue selfValue, ClassDecl *cd,
692+
llvm::SmallSetVector<VarDecl *, 4> recursiveLinks,
693+
CleanupLocation cleanupLoc);
694694

695695
/// Generates a thunk from a foreign function to the native Swift convention.
696696
void emitForeignToNativeThunk(SILDeclRef thunk);

0 commit comments

Comments
 (0)