Skip to content

Commit e9c3f84

Browse files
authored
Merge pull request #62303 from adrian-prantl/102296138
Use SILBuilderWithScope in the DefiniteInitialization pass
2 parents b71f9d0 + c4695d5 commit e9c3f84

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2969,35 +2969,34 @@ SILValue LifetimeChecker::handleConditionalInitAssign() {
29692969

29702970
// Create the control variable as the first instruction in the function (so
29712971
// that it is easy to destroy the stack location.
2972-
SILBuilder B(TheMemory.getFunctionEntryPoint());
2973-
B.setCurrentDebugScope(TheMemory.getFunction().getDebugScope());
29742972
SILType IVType =
29752973
SILType::getBuiltinIntegerType(NumMemoryElements, Module.getASTContext());
29762974
// Use an empty location for the alloc_stack. If Loc is variable declaration
29772975
// the alloc_stack would look like the storage of that variable.
2978-
auto *ControlVariableBox = B.createAllocStack(
2979-
RegularLocation::getAutoGeneratedLocation(), IVType);
2976+
auto *ControlVariableBox =
2977+
SILBuilderWithScope(TheMemory.getFunctionEntryPoint())
2978+
.createAllocStack(RegularLocation::getAutoGeneratedLocation(),
2979+
IVType);
29802980

29812981
// Find all the return blocks in the function, inserting a dealloc_stack
29822982
// before the return.
29832983
for (auto &BB : TheMemory.getFunction()) {
29842984
auto *Term = BB.getTerminator();
29852985
if (Term->isFunctionExiting()) {
2986-
B.setInsertionPoint(Term);
2987-
B.setCurrentDebugScope(Term->getDebugScope());
2988-
B.createDeallocStack(Loc, ControlVariableBox);
2986+
SILBuilderWithScope(Term).createDeallocStack(Loc, ControlVariableBox);
29892987
}
29902988
}
29912989

29922990
// Before the memory allocation, store zero in the control variable.
2993-
auto *InsertPoint =
2994-
&*std::next(TheMemory.getUninitializedValue()->getIterator());
2995-
B.setInsertionPoint(InsertPoint);
2996-
B.setCurrentDebugScope(InsertPoint->getDebugScope());
29972991
SILValue ControlVariableAddr = ControlVariableBox;
2998-
auto Zero = B.createIntegerLiteral(Loc, IVType, 0);
2999-
B.createStore(Loc, Zero, ControlVariableAddr,
3000-
StoreOwnershipQualifier::Trivial);
2992+
{
2993+
auto *InsertPoint =
2994+
&*std::next(TheMemory.getUninitializedValue()->getIterator());
2995+
SILBuilderWithScope B(InsertPoint);
2996+
auto Zero = B.createIntegerLiteral(Loc, IVType, 0);
2997+
B.createStore(Loc, Zero, ControlVariableAddr,
2998+
StoreOwnershipQualifier::Trivial);
2999+
}
30013000

30023001
Identifier OrFn;
30033002

@@ -3021,7 +3020,7 @@ SILValue LifetimeChecker::handleConditionalInitAssign() {
30213020
Use.onlyTouchesTrivialElements(TheMemory))
30223021
continue;
30233022

3024-
B.setInsertionPoint(Use.Inst);
3023+
SILBuilderWithScope B(Use.Inst);
30253024

30263025
// Only full initializations make something live. inout uses, escapes, and
30273026
// assignments only happen when some kind of init made the element live.
@@ -3110,7 +3109,7 @@ SILValue LifetimeChecker::handleConditionalInitAssign() {
31103109
if (HasConditionalSelfInitialized) {
31113110
for (auto *I : StoresToSelf) {
31123111
auto *bb = I->getParent();
3113-
B.setInsertionPoint(bb->begin());
3112+
SILBuilderWithScope B(bb->begin());
31143113

31153114
// Set the most significant bit.
31163115
APInt Bitmask = APInt::getHighBitsSet(NumMemoryElements, 1);

test/SILOptimizer/definite_init_nsmanagedvalue.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -I %S/../ClangImporter/Inputs/custom-modules %s -emit-sil
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -I %S/../ClangImporter/Inputs/custom-modules %s -emit-sil -g | %FileCheck %s
22

33
// REQUIRES: objc_interop
44

@@ -27,3 +27,9 @@ class Person : NSManagedObject {
2727
extension Person {
2828
@NSManaged var name: String
2929
}
30+
31+
// Verify that the DI instructions share the scope of the adjacent instructions.
32+
// CHECK: sil {{.*}}$s28definite_init_nsmanagedvalue6PersonCyACSiKcfc
33+
// CHECK: bb{{.*}}(%{{.*}} : $NSManagedObject):
34+
// CHECK-NEXT: integer_literal $Builtin.Int2, {{.*}}, scope [[SCOPE:[0-9]+]]
35+
// CHECK-NEXT: store {{.*}}, scope [[SCOPE]]

0 commit comments

Comments
 (0)