Skip to content

Commit e42bf07

Browse files
committed
[gardening] Always create SILBasicBlocks via SILFunction::createBasicBlock.
This eliminates all inline creation of SILBasicBlock via placement new. There are a few reasons to do this: 1. A SILBasicBlock is always created with a parent function. This commit formalizes this into the SILBasicBlock API by only allowing for SILFunctions to create SILBasicBlocks. This is implemented via the type system by making all SILBasicBlock constructors private. Since SILFunction is a friend of SILBasicBlock, SILFunction can still create a SILBasicBlock without issue. 2. Since all SILBasicBlocks will be created in only a few functions, it becomes very easy to determine using instruments the amount of memory being allocated for SILBasicBlocks by simply inverting the call tree in Allocations. With LTO+PGO, normal inlining can occur if profitable so there shouldn't be overhead that we care about in shipping compilers.
1 parent 2865657 commit e42bf07

File tree

20 files changed

+40
-36
lines changed

20 files changed

+40
-36
lines changed

include/swift/SIL/SILBasicBlock.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
5151
void operator=(const SILBasicBlock &) = delete;
5252
void operator delete(void *Ptr, size_t) = delete;
5353

54-
public:
5554
SILBasicBlock(SILFunction *F, SILBasicBlock *afterBB = nullptr);
55+
56+
public:
5657
~SILBasicBlock();
5758

5859
/// Gets the ID (= index in the function's block list) of the block.

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ SILCloner<ImplClass>::visitSILBasicBlock(SILBasicBlock* BB) {
397397
// Only visit a successor that has not already been visited.
398398
if (BBI == BBMap.end()) {
399399
// Map the successor to a new BB.
400-
auto MappedBB = new (F.getModule()) SILBasicBlock(&F);
400+
auto *MappedBB = F.createBasicBlock();
401401
BBMap.insert(std::make_pair(Succ.getBB(), MappedBB));
402402
// Create new arguments for each of the original block's arguments.
403403
for (auto &Arg : Succ.getBB()->getBBArgs()) {

include/swift/SIL/SILFunction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ class SILFunction
619619
const SILBasicBlock &front() const { return *begin(); }
620620

621621
SILBasicBlock *createBasicBlock();
622+
SILBasicBlock *createBasicBlock(SILBasicBlock *After);
622623

623624
/// Splice the body of \p F into this function at end.
624625
void spliceBody(SILFunction *F) {

include/swift/SILOptimizer/Utils/Local.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ class EdgeThreadingCloner : public BaseThreadingCloner {
368368
auto *Fn = BI->getFunction();
369369
auto *SrcBB = BI->getParent();
370370
auto *DestBB = BI->getDestBB();
371-
auto *EdgeBB = new (Fn->getModule()) SILBasicBlock(Fn, SrcBB);
371+
auto *EdgeBB = Fn->createBasicBlock(SrcBB);
372372

373373
// Create block arguments.
374374
unsigned ArgIdx = 0;

lib/Parse/ParseSIL.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,12 @@ SILFunction *SILParser::getGlobalNameForReference(Identifier Name,
477477
SILBasicBlock *SILParser::getBBForDefinition(Identifier Name, SourceLoc Loc) {
478478
// If there was no name specified for this block, just create a new one.
479479
if (Name.empty())
480-
return new (SILMod) SILBasicBlock(F);
481-
480+
return F->createBasicBlock();
481+
482482
SILBasicBlock *&BB = BlocksByName[Name];
483483
// If the block has never been named yet, just create it.
484484
if (BB == nullptr)
485-
return BB = new (SILMod) SILBasicBlock(F);
485+
return BB = F->createBasicBlock();
486486

487487
// If it already exists, it was either a forward reference or a redefinition.
488488
// If it is a forward reference, it should be in our undefined set.
@@ -491,7 +491,7 @@ SILBasicBlock *SILParser::getBBForDefinition(Identifier Name, SourceLoc Loc) {
491491
// instructions after the terminator.
492492
P.diagnose(Loc, diag::sil_basicblock_redefinition, Name);
493493
HadError = true;
494-
return new (SILMod) SILBasicBlock(F);
494+
return F->createBasicBlock();
495495
}
496496

497497
// FIXME: Splice the block to the end of the function so they come out in the
@@ -510,7 +510,7 @@ SILBasicBlock *SILParser::getBBForReference(Identifier Name, SourceLoc Loc) {
510510

511511
// Otherwise, create it and remember that this is a forward reference so
512512
// that we can diagnose use without definition problems.
513-
BB = new (SILMod) SILBasicBlock(F);
513+
BB = F->createBasicBlock();
514514
UndefinedBlocks[BB] = {Loc, Name};
515515
return BB;
516516
}

lib/SIL/SILBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void SILBuilder::emitBlock(SILBasicBlock *BB, SILLocation BranchLoc) {
138138
SILBasicBlock *SILBuilder::splitBlockForFallthrough() {
139139
// If we are concatenating, just create and return a new block.
140140
if (insertingAtEndOfBlock()) {
141-
return new (F.getModule()) SILBasicBlock(&F, BB);
141+
return F.createBasicBlock(BB);
142142
}
143143

144144
// Otherwise we need to split the current block at the insertion point.

lib/SIL/SILFunction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ SILBasicBlock *SILFunction::createBasicBlock() {
288288
return new (getModule()) SILBasicBlock(this);
289289
}
290290

291+
SILBasicBlock *SILFunction::createBasicBlock(SILBasicBlock *AfterBlock) {
292+
return new (getModule()) SILBasicBlock(this, AfterBlock);
293+
}
294+
291295
//===----------------------------------------------------------------------===//
292296
// View CFG Implementation
293297
//===----------------------------------------------------------------------===//

lib/SILGen/SILGenStmt.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ static void diagnose(ASTContext &Context, SourceLoc loc, Diag<T...> diag,
3434
SILBasicBlock *SILGenFunction::createBasicBlock(SILBasicBlock *afterBB) {
3535
// Honor an explicit placement if given.
3636
if (afterBB) {
37-
return new (F.getModule()) SILBasicBlock(&F, afterBB);
37+
return F.createBasicBlock(afterBB);
3838

39-
// If we don't have a requested placement, but we do have a current
40-
// insertion point, insert there.
39+
// If we don't have a requested placement, but we do have a current
40+
// insertion point, insert there.
4141
} else if (B.hasValidInsertionPoint()) {
42-
return new (F.getModule()) SILBasicBlock(&F, B.getInsertionBB());
42+
return F.createBasicBlock(B.getInsertionBB());
4343

44-
// Otherwise, insert at the end of the current section.
44+
// Otherwise, insert at the end of the current section.
4545
} else {
4646
return createBasicBlock(CurFunctionSection);
4747
}
@@ -55,13 +55,13 @@ SILBasicBlock *SILGenFunction::createBasicBlock(FunctionSection section) {
5555
SILBasicBlock *afterBB = (StartOfPostmatter != F.end())
5656
? &*std::prev(StartOfPostmatter)
5757
: nullptr;
58-
return new (F.getModule()) SILBasicBlock(&F, afterBB);
58+
return F.createBasicBlock(afterBB);
5959
}
6060

6161
case FunctionSection::Postmatter: {
6262
// The end of the postmatter section is always the end of the function.
6363
// Register the new block as the start of the postmatter if needed.
64-
SILBasicBlock *newBB = new (F.getModule()) SILBasicBlock(&F, nullptr);
64+
SILBasicBlock *newBB = F.createBasicBlock(nullptr);
6565
if (StartOfPostmatter == F.end())
6666
StartOfPostmatter = newBB->getIterator();
6767
return newBB;

lib/SILOptimizer/IPO/CapturePromotion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ ClosureCloner::populateCloned() {
467467

468468
// Create arguments for the entry block
469469
SILBasicBlock *OrigEntryBB = &*Orig->begin();
470-
SILBasicBlock *ClonedEntryBB = new (M) SILBasicBlock(Cloned);
470+
SILBasicBlock *ClonedEntryBB = Cloned->createBasicBlock();
471471
unsigned ArgNo = 0;
472472
auto I = OrigEntryBB->bbarg_begin(), E = OrigEntryBB->bbarg_end();
473473
while (I != E) {

lib/SILOptimizer/IPO/CapturePropagation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void CapturePropagationCloner::cloneBlocks(
165165

166166
// Create the entry basic block with the function arguments.
167167
SILBasicBlock *OrigEntryBB = &*OrigF->begin();
168-
SILBasicBlock *ClonedEntryBB = new (M) SILBasicBlock(&CloneF);
168+
SILBasicBlock *ClonedEntryBB = CloneF.createBasicBlock();
169169
CanSILFunctionType CloneFTy = CloneF.getLoweredFunctionType();
170170

171171
// Only clone the arguments that remain in the new function type. The trailing

lib/SILOptimizer/IPO/ClosureSpecializer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ void ClosureSpecCloner::populateCloned() {
572572

573573
// Create arguments for the entry block.
574574
SILBasicBlock *ClosureUserEntryBB = &*ClosureUser->begin();
575-
SILBasicBlock *ClonedEntryBB = new (M) SILBasicBlock(Cloned);
575+
SILBasicBlock *ClonedEntryBB = Cloned->createBasicBlock();
576576

577577
// Remove the closure argument.
578578
SILArgument *ClosureArg = nullptr;

lib/SILOptimizer/LoopTransforms/COWArrayOpt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1975,7 +1975,7 @@ class RegionCloner : public SILCloner<RegionCloner> {
19751975
}
19761976

19771977
// Create the cloned start basic block.
1978-
auto *ClonedStartBB = new (Mod) SILBasicBlock(CurFun);
1978+
auto *ClonedStartBB = CurFun->createBasicBlock();
19791979
BBMap[StartBB] = ClonedStartBB;
19801980

19811981
// Clone the arguments.

lib/SILOptimizer/LoopTransforms/LoopUnroll.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void LoopCloner::cloneLoop() {
7979
for (auto *ExitBB : ExitBlocks)
8080
BBMap[ExitBB] = ExitBB;
8181

82-
auto *ClonedHeader = new (Mod) SILBasicBlock(CurFun);
82+
auto *ClonedHeader = CurFun->createBasicBlock();
8383
BBMap[Header] = ClonedHeader;
8484

8585
// Clone the arguments.

lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ static void InsertCFGDiamond(SILValue Cond, SILLocation Loc, SILBuilder &B,
125125
SILBasicBlock *&FalseBB,
126126
SILBasicBlock *&ContBB) {
127127
SILBasicBlock *StartBB = B.getInsertionBB();
128-
SILModule &Module = StartBB->getModule();
129128

130129
// Start by splitting the current block.
131130
ContBB = StartBB->splitBasicBlock(B.getInsertionPoint());
@@ -136,7 +135,7 @@ static void InsertCFGDiamond(SILValue Cond, SILLocation Loc, SILBuilder &B,
136135
TrueDest = ContBB;
137136
TrueBB = nullptr;
138137
} else {
139-
TrueDest = new (Module) SILBasicBlock(StartBB->getParent());
138+
TrueDest = StartBB->getParent()->createBasicBlock();
140139
B.moveBlockTo(TrueDest, ContBB);
141140
B.setInsertionPoint(TrueDest);
142141
B.createBranch(Loc, ContBB);
@@ -149,7 +148,7 @@ static void InsertCFGDiamond(SILValue Cond, SILLocation Loc, SILBuilder &B,
149148
FalseDest = ContBB;
150149
FalseBB = nullptr;
151150
} else {
152-
FalseDest = new (Module) SILBasicBlock(StartBB->getParent());
151+
FalseDest = StartBB->getParent()->createBasicBlock();
153152
B.moveBlockTo(FalseDest, ContBB);
154153
B.setInsertionPoint(FalseDest);
155154
B.createBranch(Loc, ContBB);

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ PromotedParamCloner::populateCloned() {
589589

590590
// Create arguments for the entry block
591591
SILBasicBlock *OrigEntryBB = &*Orig->begin();
592-
SILBasicBlock *ClonedEntryBB = new (M) SILBasicBlock(Cloned);
592+
SILBasicBlock *ClonedEntryBB = Cloned->createBasicBlock();
593593
unsigned ArgNo = 0;
594594
auto I = OrigEntryBB->bbarg_begin(), E = OrigEntryBB->bbarg_end();
595595
while (I != E) {

lib/SILOptimizer/Utils/CFG.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ SILBasicBlock *swift::splitEdge(TermInst *T, unsigned EdgeIdx,
579579
SILBasicBlock *DestBB = T->getSuccessors()[EdgeIdx];
580580

581581
// Create a new basic block in the edge, and insert it after the SrcBB.
582-
auto *EdgeBB = new (Fn->getModule()) SILBasicBlock(Fn, SrcBB);
582+
auto *EdgeBB = Fn->createBasicBlock(SrcBB);
583583

584584
SmallVector<SILValue, 16> Args;
585585
getEdgeArgs(T, EdgeIdx, EdgeBB, Args);

lib/SILOptimizer/Utils/GenericCloner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void GenericCloner::populateCloned() {
6161

6262
// Create arguments for the entry block.
6363
SILBasicBlock *OrigEntryBB = &*Original.begin();
64-
SILBasicBlock *ClonedEntryBB = new (M) SILBasicBlock(Cloned);
64+
SILBasicBlock *ClonedEntryBB = Cloned->createBasicBlock();
6565
getBuilder().setInsertionPoint(ClonedEntryBB);
6666

6767
llvm::SmallVector<AllocStackInst *, 8> AllocStacks;

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static SILFunction *createReabstractionThunk(const ReabstractionInfo &ReInfo,
371371
if (!Thunk->empty())
372372
return Thunk;
373373

374-
SILBasicBlock *EntryBB = new (M) SILBasicBlock(Thunk);
374+
SILBasicBlock *EntryBB = Thunk->createBasicBlock();
375375
SILBuilder Builder(EntryBB);
376376
SILBasicBlock *SpecEntryBB = &*SpecializedFunc->begin();
377377
CanSILFunctionType SpecType = SpecializedFunc->getLoweredFunctionType();
@@ -421,8 +421,8 @@ static SILFunction *createReabstractionThunk(const ReabstractionInfo &ReInfo,
421421
SILValue ReturnValue;
422422
if (SpecType->hasErrorResult()) {
423423
// Create the logic for calling a throwing function.
424-
SILBasicBlock *NormalBB = new (M) SILBasicBlock(Thunk);
425-
SILBasicBlock *ErrorBB = new (M) SILBasicBlock(Thunk);
424+
SILBasicBlock *NormalBB = Thunk->createBasicBlock();
425+
SILBasicBlock *ErrorBB = Thunk->createBasicBlock();
426426
Builder.createTryApply(Loc, FRI, SpecializedFunc->getLoweredType(),
427427
{}, Arguments, NormalBB, ErrorBB);
428428
auto *ErrorVal = new (M) SILArgument(ErrorBB,

lib/SILOptimizer/Utils/LoopUtils.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
using namespace swift;
2525

2626
static SILBasicBlock *createInitialPreheader(SILBasicBlock *Header) {
27-
auto *Preheader = new (Header->getModule())
28-
SILBasicBlock(Header->getParent(), &*std::prev(Header->getIterator()));
27+
auto *Preheader =
28+
Header->getParent()->createBasicBlock(&*std::prev(Header->getIterator()));
2929

3030
// Clone the arguments from header into the pre-header.
3131
llvm::SmallVector<SILValue, 8> Args;
@@ -116,8 +116,7 @@ static SILBasicBlock *insertBackedgeBlock(SILLoop *L, DominanceInfo *DT,
116116
}
117117

118118
// Create and insert the new backedge block...
119-
SILBasicBlock *BEBlock =
120-
new (F->getModule()) SILBasicBlock(F, BackedgeBlocks.back());
119+
SILBasicBlock *BEBlock = F->createBasicBlock(BackedgeBlocks.back());
121120

122121
DEBUG(llvm::dbgs() << " Inserting unique backedge block " << *BEBlock
123122
<< "\n");

lib/Serialization/DeserializeSIL.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ SILBasicBlock *SILDeserializer::getBBForDefinition(SILFunction *Fn,
252252
SILBasicBlock *&BB = BlocksByID[ID];
253253
// If the block has never been named yet, just create it.
254254
if (BB == nullptr)
255-
return BB = new (SILMod) SILBasicBlock(Fn, Prev);
255+
return BB = Fn->createBasicBlock(Prev);
256256

257257
// If it already exists, it was either a forward reference or a redefinition.
258258
// The latter should never happen.
@@ -273,7 +273,7 @@ SILBasicBlock *SILDeserializer::getBBForReference(SILFunction *Fn,
273273
return BB;
274274

275275
// Otherwise, create it and remember that this is a forward reference
276-
BB = new (SILMod) SILBasicBlock(Fn);
276+
BB = Fn->createBasicBlock();
277277
UndefinedBlocks[BB] = ID;
278278
return BB;
279279
}

0 commit comments

Comments
 (0)