Skip to content

Use SILBuilderWithScope in the DefiniteInitialization pass #62303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion test/SILOptimizer/definite_init_nsmanagedvalue.swift
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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]]