Skip to content

Commit c8c7d2b

Browse files
authored
Merge pull request swiftlang#12817 from gottesmm/predmemopt_extract_at_store_not_load
PredMemOpt Extract at Store site instead of load site
2 parents f95a313 + b17935a commit c8c7d2b

File tree

11 files changed

+622
-120
lines changed

11 files changed

+622
-120
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,35 @@ class SILBuilderWithScope : public SILBuilder {
20492049
}
20502050
};
20512051

2052+
class SavedInsertionPointRAII {
2053+
SILBuilder &Builder;
2054+
PointerUnion<SILInstruction *, SILBasicBlock *> SavedIP;
2055+
2056+
public:
2057+
SavedInsertionPointRAII(SILBuilder &B, SILInstruction *NewIP)
2058+
: Builder(B), SavedIP(&*B.getInsertionPoint()) {
2059+
Builder.setInsertionPoint(NewIP);
2060+
}
2061+
2062+
SavedInsertionPointRAII(SILBuilder &B, SILBasicBlock *NewIP)
2063+
: Builder(B), SavedIP(B.getInsertionBB()) {
2064+
Builder.setInsertionPoint(NewIP);
2065+
}
2066+
2067+
SavedInsertionPointRAII(const SavedInsertionPointRAII &) = delete;
2068+
SavedInsertionPointRAII &operator=(const SavedInsertionPointRAII &) = delete;
2069+
SavedInsertionPointRAII(SavedInsertionPointRAII &&) = delete;
2070+
SavedInsertionPointRAII &operator=(SavedInsertionPointRAII &&) = delete;
2071+
2072+
~SavedInsertionPointRAII() {
2073+
if (SavedIP.is<SILInstruction *>()) {
2074+
Builder.setInsertionPoint(SavedIP.get<SILInstruction *>());
2075+
} else {
2076+
Builder.setInsertionPoint(SavedIP.get<SILBasicBlock *>());
2077+
}
2078+
}
2079+
};
2080+
20522081
} // end swift namespace
20532082

20542083
#endif

lib/SILGen/Cleanup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ SILBasicBlock *CleanupManager::emitBlockForCleanups(JumpDest dest,
138138

139139
// Otherwise, create and emit a new block.
140140
auto *newBlock = SGF.createBasicBlock();
141-
SavedInsertionPoint IPRAII(SGF, newBlock);
141+
SILGenSavedInsertionPoint IPRAII(SGF, newBlock);
142142
emitBranchAndCleanups(dest, branchLoc, args);
143143
return newBlock;
144144
}

lib/SILGen/SILGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,8 +1324,8 @@ class SourceFileScope {
13241324
SGF.B.emitBlock(returnBB);
13251325

13261326
// Emit the rethrow block.
1327-
SavedInsertionPoint savedIP(SGF, rethrowBB,
1328-
FunctionSection::Postmatter);
1327+
SILGenSavedInsertionPoint savedIP(SGF, rethrowBB,
1328+
FunctionSection::Postmatter);
13291329

13301330
// Log the error.
13311331
SILValue error = rethrowBB->getArgument(0);

lib/SILGen/SILGenConstructor.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ void SILGenFunction::emitValueConstructor(ConstructorDecl *ctor) {
229229

230230
// On failure, we'll clean up everything (except self, which should have
231231
// been cleaned up before jumping here) and return nil instead.
232-
SavedInsertionPoint savedIP(*this, failureBB, FunctionSection::Postmatter);
232+
SILGenSavedInsertionPoint savedIP(*this, failureBB,
233+
FunctionSection::Postmatter);
233234
failureExitBB = createBasicBlock();
234235
Cleanups.emitCleanupsForReturn(ctor);
235236
// Return nil.
@@ -275,7 +276,7 @@ void SILGenFunction::emitValueConstructor(ConstructorDecl *ctor) {
275276
// SIL representation.
276277
SILValue selfValue;
277278
{
278-
SavedInsertionPoint savedIP(*this, ReturnDest.getBlock());
279+
SILGenSavedInsertionPoint savedIP(*this, ReturnDest.getBlock());
279280
assert(B.getInsertionBB()->empty() && "Epilog already set up?");
280281

281282
auto cleanupLoc = CleanupLocation::get(ctor);
@@ -621,7 +622,8 @@ void SILGenFunction::emitClassConstructorInitializer(ConstructorDecl *ctor) {
621622
loc.markAutoGenerated();
622623

623624
// On failure, we'll clean up everything and return nil instead.
624-
SavedInsertionPoint savedIP(*this, failureBB, FunctionSection::Postmatter);
625+
SILGenSavedInsertionPoint savedIP(*this, failureBB,
626+
FunctionSection::Postmatter);
625627

626628
failureExitBB = createBasicBlock();
627629
failureExitArg = failureExitBB->createPHIArgument(
@@ -674,7 +676,7 @@ void SILGenFunction::emitClassConstructorInitializer(ConstructorDecl *ctor) {
674676
ReturnDest.getDepth()) &&
675677
"emitting epilog in wrong scope");
676678

677-
SavedInsertionPoint savedIP(*this, ReturnDest.getBlock());
679+
SILGenSavedInsertionPoint savedIP(*this, ReturnDest.getBlock());
678680
assert(B.getInsertionBB()->empty() && "Epilog already set up?");
679681
auto cleanupLoc = CleanupLocation(ctor);
680682

lib/SILGen/SILGenConvert.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ SILGenFunction::emitOptionalToOptional(SILLocation loc,
396396
finalResult = emitManagedBufferWithCleanup(
397397
emitTemporaryAllocation(loc, resultTy), resultTL);
398398
} else {
399-
SavedInsertionPoint IP(*this, contBB);
399+
SILGenSavedInsertionPoint IP(*this, contBB);
400400
finalResult = B.createOwnedPHIArgument(resultTL.getLoweredType());
401401
}
402402

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ void SILGenFunction::ForceTryEmission::finish() {
14171417
SGF.eraseBasicBlock(catchBB);
14181418
} else {
14191419
// Otherwise, we need to emit it.
1420-
SavedInsertionPoint scope(SGF, catchBB, FunctionSection::Postmatter);
1420+
SILGenSavedInsertionPoint scope(SGF, catchBB, FunctionSection::Postmatter);
14211421

14221422
ASTContext &ctx = SGF.getASTContext();
14231423
auto error = catchBB->createPHIArgument(SILType::getExceptionType(ctx),

lib/SILGen/SILGenForeignError.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ emitBridgeReturnValueForForeignError(SILLocation loc,
253253
void SILGenFunction::emitForeignErrorBlock(SILLocation loc,
254254
SILBasicBlock *errorBB,
255255
Optional<ManagedValue> errorSlot) {
256-
SavedInsertionPoint savedIP(*this, errorBB, FunctionSection::Postmatter);
256+
SILGenSavedInsertionPoint savedIP(*this, errorBB,
257+
FunctionSection::Postmatter);
257258

258259
// Load the error (taking responsibility for it). In theory, this
259260
// is happening within conditional code, so we need to be only

lib/SILGen/SILGenFunction.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,15 +1810,15 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
18101810

18111811

18121812
/// A utility class for saving and restoring the insertion point.
1813-
class SavedInsertionPoint {
1813+
class SILGenSavedInsertionPoint {
18141814
SILGenFunction &SGF;
18151815
SILBasicBlock *SavedIP;
18161816
FunctionSection SavedSection;
18171817
public:
1818-
SavedInsertionPoint(SILGenFunction &SGF, SILBasicBlock *newIP,
1819-
Optional<FunctionSection> optSection = None)
1820-
: SGF(SGF), SavedIP(SGF.B.getInsertionBB()),
1821-
SavedSection(SGF.CurFunctionSection) {
1818+
SILGenSavedInsertionPoint(SILGenFunction &SGF, SILBasicBlock *newIP,
1819+
Optional<FunctionSection> optSection = None)
1820+
: SGF(SGF), SavedIP(SGF.B.getInsertionBB()),
1821+
SavedSection(SGF.CurFunctionSection) {
18221822
FunctionSection section = (optSection ? *optSection : SavedSection);
18231823
assert((section != FunctionSection::Postmatter ||
18241824
SGF.StartOfPostmatter != SGF.F.end()) &&
@@ -1829,10 +1829,11 @@ class SavedInsertionPoint {
18291829
SGF.CurFunctionSection = section;
18301830
}
18311831

1832-
SavedInsertionPoint(const SavedInsertionPoint &) = delete;
1833-
SavedInsertionPoint &operator=(const SavedInsertionPoint &) = delete;
1832+
SILGenSavedInsertionPoint(const SILGenSavedInsertionPoint &) = delete;
1833+
SILGenSavedInsertionPoint &
1834+
operator=(const SILGenSavedInsertionPoint &) = delete;
18341835

1835-
~SavedInsertionPoint() {
1836+
~SILGenSavedInsertionPoint() {
18361837
if (SavedIP) {
18371838
SGF.B.setInsertionPoint(SavedIP);
18381839
} else {

lib/SILGen/SILGenStmt.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ void StmtEmitter::visitGuardStmt(GuardStmt *S) {
550550
// Move the insertion point to the 'body' block temporarily and emit it.
551551
// Note that we don't push break/continue locations since they aren't valid
552552
// in this statement.
553-
SavedInsertionPoint savedIP(SGF, bodyBB.getBlock());
553+
SILGenSavedInsertionPoint savedIP(SGF, bodyBB.getBlock());
554554
SGF.emitProfilerIncrement(S->getBody());
555555
SGF.emitStmt(S->getBody());
556556

@@ -691,7 +691,7 @@ void StmtEmitter::visitDoCatchStmt(DoCatchStmt *S) {
691691
// has no predecessors, and SGF.ThrowDest may not be valid either.
692692
if (auto *BB = getOrEraseBlock(SGF, throwDest)) {
693693
// Move the insertion point to the throw destination.
694-
SavedInsertionPoint savedIP(SGF, BB, FunctionSection::Postmatter);
694+
SILGenSavedInsertionPoint savedIP(SGF, BB, FunctionSection::Postmatter);
695695

696696
// The exception cleanup should be getting forwarded around
697697
// correctly anyway, but push a scope to ensure it gets popped.
@@ -976,7 +976,7 @@ SILGenFunction::getTryApplyErrorDest(SILLocation loc,
976976
ValueOwnershipKind::Owned);
977977

978978
assert(B.hasValidInsertionPoint() && B.insertingAtEndOfBlock());
979-
SavedInsertionPoint savedIP(*this, destBB, FunctionSection::Postmatter);
979+
SILGenSavedInsertionPoint savedIP(*this, destBB, FunctionSection::Postmatter);
980980

981981
// If we're suppressing error paths, just wrap it up as unreachable
982982
// and return.

0 commit comments

Comments
 (0)