diff --git a/lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp b/lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp index 74fdaa359b546..2ae1a5fc2b900 100644 --- a/lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp +++ b/lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp @@ -2969,35 +2969,34 @@ SILValue LifetimeChecker::handleConditionalInitAssign() { // Create the control variable as the first instruction in the function (so // that it is easy to destroy the stack location. - SILBuilder B(TheMemory.getFunctionEntryPoint()); - B.setCurrentDebugScope(TheMemory.getFunction().getDebugScope()); SILType IVType = SILType::getBuiltinIntegerType(NumMemoryElements, Module.getASTContext()); // Use an empty location for the alloc_stack. If Loc is variable declaration // the alloc_stack would look like the storage of that variable. - auto *ControlVariableBox = B.createAllocStack( - RegularLocation::getAutoGeneratedLocation(), IVType); + auto *ControlVariableBox = + SILBuilderWithScope(TheMemory.getFunctionEntryPoint()) + .createAllocStack(RegularLocation::getAutoGeneratedLocation(), + IVType); // Find all the return blocks in the function, inserting a dealloc_stack // before the return. for (auto &BB : TheMemory.getFunction()) { auto *Term = BB.getTerminator(); if (Term->isFunctionExiting()) { - B.setInsertionPoint(Term); - B.setCurrentDebugScope(Term->getDebugScope()); - B.createDeallocStack(Loc, ControlVariableBox); + SILBuilderWithScope(Term).createDeallocStack(Loc, ControlVariableBox); } } // Before the memory allocation, store zero in the control variable. - auto *InsertPoint = - &*std::next(TheMemory.getUninitializedValue()->getIterator()); - B.setInsertionPoint(InsertPoint); - B.setCurrentDebugScope(InsertPoint->getDebugScope()); SILValue ControlVariableAddr = ControlVariableBox; - auto Zero = B.createIntegerLiteral(Loc, IVType, 0); - B.createStore(Loc, Zero, ControlVariableAddr, - StoreOwnershipQualifier::Trivial); + { + auto *InsertPoint = + &*std::next(TheMemory.getUninitializedValue()->getIterator()); + SILBuilderWithScope B(InsertPoint); + auto Zero = B.createIntegerLiteral(Loc, IVType, 0); + B.createStore(Loc, Zero, ControlVariableAddr, + StoreOwnershipQualifier::Trivial); + } Identifier OrFn; @@ -3021,7 +3020,7 @@ SILValue LifetimeChecker::handleConditionalInitAssign() { Use.onlyTouchesTrivialElements(TheMemory)) continue; - B.setInsertionPoint(Use.Inst); + SILBuilderWithScope B(Use.Inst); // Only full initializations make something live. inout uses, escapes, and // assignments only happen when some kind of init made the element live. @@ -3110,7 +3109,7 @@ SILValue LifetimeChecker::handleConditionalInitAssign() { if (HasConditionalSelfInitialized) { for (auto *I : StoresToSelf) { auto *bb = I->getParent(); - B.setInsertionPoint(bb->begin()); + SILBuilderWithScope B(bb->begin()); // Set the most significant bit. APInt Bitmask = APInt::getHighBitsSet(NumMemoryElements, 1); diff --git a/test/SILOptimizer/definite_init_nsmanagedvalue.swift b/test/SILOptimizer/definite_init_nsmanagedvalue.swift index 40637d7748a0c..86facfc02df7e 100644 --- a/test/SILOptimizer/definite_init_nsmanagedvalue.swift +++ b/test/SILOptimizer/definite_init_nsmanagedvalue.swift @@ -1,4 +1,4 @@ -// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -I %S/../ClangImporter/Inputs/custom-modules %s -emit-sil +// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -I %S/../ClangImporter/Inputs/custom-modules %s -emit-sil -g | %FileCheck %s // REQUIRES: objc_interop @@ -27,3 +27,9 @@ class Person : NSManagedObject { extension Person { @NSManaged var name: String } + +// Verify that the DI instructions share the scope of the adjacent instructions. +// CHECK: sil {{.*}}$s28definite_init_nsmanagedvalue6PersonCyACSiKcfc +// CHECK: bb{{.*}}(%{{.*}} : $NSManagedObject): +// CHECK-NEXT: integer_literal $Builtin.Int2, {{.*}}, scope [[SCOPE:[0-9]+]] +// CHECK-NEXT: store {{.*}}, scope [[SCOPE]]