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

0 commit comments

Comments
 (0)