Skip to content

Commit 397b96c

Browse files
committed
[SILOptimizer] Don't lower AssignInst in DI
1 parent 0bebba8 commit 397b96c

File tree

6 files changed

+48
-35
lines changed

6 files changed

+48
-35
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3603,6 +3603,22 @@ class EndUnpairedAccessInst
36033603
}
36043604
};
36053605

3606+
enum class PartialInitializationKind {
3607+
/// Unknown initialization method
3608+
Unknown,
3609+
3610+
/// The box contains a fully-initialized value.
3611+
IsNotInitialization,
3612+
3613+
/// The box contains a class instance that we own, but the instance has
3614+
/// not been initialized and should be freed with a special SIL
3615+
/// instruction made for this purpose.
3616+
IsReinitialization,
3617+
3618+
/// The box contains an undefined value that should be ignored.
3619+
IsInitialization,
3620+
};
3621+
36063622
/// AssignInst - Represents an abstract assignment to a memory location, which
36073623
/// may either be an initialization or a store sequence. This is only valid in
36083624
/// Raw SIL.
@@ -3612,6 +3628,7 @@ class AssignInst
36123628
friend SILBuilder;
36133629

36143630
FixedOperandList<2> Operands;
3631+
PartialInitializationKind InitKind;
36153632

36163633
AssignInst(SILDebugLocation DebugLoc, SILValue Src, SILValue Dest);
36173634

@@ -3628,6 +3645,9 @@ class AssignInst
36283645

36293646
ArrayRef<Operand> getAllOperands() const { return Operands.asArray(); }
36303647
MutableArrayRef<Operand> getAllOperands() { return Operands.asArray(); }
3648+
3649+
PartialInitializationKind getInitKind() const { return InitKind; }
3650+
void setInitKind(PartialInitializationKind initKind) { InitKind = initKind; }
36313651
};
36323652

36333653
/// Indicates that a memory location is uninitialized at this point and needs to

lib/SIL/SILInstructions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,8 @@ StringRef swift::getSILAccessEnforcementName(SILAccessEnforcement enforcement) {
813813
}
814814

815815
AssignInst::AssignInst(SILDebugLocation Loc, SILValue Src, SILValue Dest)
816-
: InstructionBase(Loc), Operands(this, Src, Dest) {}
816+
: InstructionBase(Loc), Operands(this, Src, Dest),
817+
InitKind(PartialInitializationKind::Unknown) {}
817818

818819
MarkFunctionEscapeInst *
819820
MarkFunctionEscapeInst::create(SILDebugLocation Loc,

lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,20 +1884,16 @@ void LifetimeChecker::updateInstructionForInitState(DIMemoryUse &Use) {
18841884
Use.Inst = nullptr;
18851885
NonLoadUses.erase(Inst);
18861886

1887-
PartialInitializationKind PartialInitKind;
1888-
18891887
if (TheMemory.isClassInitSelf() &&
18901888
Use.Kind == DIUseKind::SelfInit) {
18911889
assert(InitKind == IsInitialization);
1892-
PartialInitKind = PartialInitializationKind::IsReinitialization;
1890+
AI->setInitKind(PartialInitializationKind::IsReinitialization);
18931891
} else {
1894-
PartialInitKind = (InitKind == IsInitialization
1895-
? PartialInitializationKind::IsInitialization
1896-
: PartialInitializationKind::IsNotInitialization);
1892+
AI->setInitKind((InitKind == IsInitialization
1893+
? PartialInitializationKind::IsInitialization
1894+
: PartialInitializationKind::IsNotInitialization));
18971895
}
18981896

1899-
SILBuilderWithScope B(Inst);
1900-
lowerAssignInstruction(B, AI, PartialInitKind);
19011897
return;
19021898
}
19031899

lib/SILOptimizer/Mandatory/MandatoryOptUtils.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,36 @@ using namespace swift;
3838
/// Emit the sequence that an assign instruction lowers to once we know
3939
/// if it is an initialization or an assignment. If it is an assignment,
4040
/// a live-in value can be provided to optimize out the reload.
41-
void swift::lowerAssignInstruction(SILBuilderWithScope &B, AssignInst *Inst,
42-
PartialInitializationKind isInitialization) {
41+
void swift::lowerAssignInstruction(SILBuilderWithScope &B, AssignInst *Inst) {
4342
LLVM_DEBUG(llvm::dbgs() << " *** Lowering [isInit="
44-
<< unsigned(isInitialization)
43+
<< unsigned(Inst->getInitKind())
4544
<< "]: " << *Inst << "\n");
4645

4746
++NumAssignRewritten;
4847

4948
SILValue Src = Inst->getSrc();
5049
SILLocation Loc = Inst->getLoc();
50+
PartialInitializationKind initKind = Inst->getInitKind();
5151

52-
if (isInitialization == PartialInitializationKind::IsInitialization ||
52+
// Unknown initKind is considered unprocessed. Just lower it as
53+
// NotInitialization
54+
if (initKind == PartialInitializationKind::Unknown) {
55+
initKind = PartialInitializationKind::IsNotInitialization;
56+
}
57+
58+
if (initKind == PartialInitializationKind::IsInitialization ||
5359
Inst->getDest()->getType().isTrivial(Inst->getModule())) {
5460

5561
// If this is an initialization, or the storage type is trivial, we
5662
// can just replace the assignment with a store.
57-
assert(isInitialization != PartialInitializationKind::IsReinitialization);
63+
assert(initKind != PartialInitializationKind::IsReinitialization);
5864
B.createTrivialStoreOr(Loc, Src, Inst->getDest(),
5965
StoreOwnershipQualifier::Init);
6066
Inst->eraseFromParent();
6167
return;
6268
}
6369

64-
if (isInitialization == PartialInitializationKind::IsReinitialization) {
70+
if (initKind == PartialInitializationKind::IsReinitialization) {
6571
// We have a case where a convenience initializer on a class
6672
// delegates to a factory initializer from a protocol extension.
6773
// Factory initializers give us a whole new instance, so the existing
@@ -81,7 +87,7 @@ void swift::lowerAssignInstruction(SILBuilderWithScope &B, AssignInst *Inst,
8187
return;
8288
}
8389

84-
assert(isInitialization == PartialInitializationKind::IsNotInitialization);
90+
assert(initKind == PartialInitializationKind::IsNotInitialization);
8591
// Otherwise, we need to replace the assignment with the full
8692
// load/store/release dance. Note that the new value is already
8793
// considered to be retained (by the semantics of the storage type),

lib/SILOptimizer/Mandatory/MandatoryOptUtils.h

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,8 @@ namespace swift {
2424
class SILBuilderWithScope;
2525
class AssignInst;
2626

27-
enum class PartialInitializationKind {
28-
/// The box contains a fully-initialized value.
29-
IsNotInitialization,
30-
31-
/// The box contains a class instance that we own, but the instance has
32-
/// not been initialized and should be freed with a special SIL
33-
/// instruction made for this purpose.
34-
IsReinitialization,
35-
36-
/// The box contains an undefined value that should be ignored.
37-
IsInitialization,
38-
};
39-
40-
void lowerAssignInstruction(SILBuilderWithScope &B, AssignInst *Inst,
41-
PartialInitializationKind isInitialization)
27+
void lowerAssignInstruction(SILBuilderWithScope &B, AssignInst *Inst)
4228
LLVM_LIBRARY_VISIBILITY;
43-
4429
} // namespace swift
4530

4631
#endif

lib/SILOptimizer/Mandatory/RawSILInstLowering.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ static bool lowerRawSILOperations(SILFunction &Fn) {
3030
SILInstruction *Inst = &*I;
3131
++I;
3232

33-
// Unprocessed assigns just lower into assignments, not initializations.
33+
// Lower 'assign' depending on initialization kind defined by definite
34+
// initialization.
35+
//
36+
// Unknown is considered unprocessed and is just NotInitialization
37+
// Initialization becomes an init store instruction
38+
// Reinititialization becomes a load, store, and a dealloc_partial_ref
39+
// NotInitialization becomes a take load, and an init store
3440
if (auto *AI = dyn_cast<AssignInst>(Inst)) {
3541
SILBuilderWithScope B(AI);
36-
lowerAssignInstruction(B, AI,
37-
PartialInitializationKind::IsNotInitialization);
42+
lowerAssignInstruction(B, AI);
3843
// Assign lowering may split the block. If it did,
3944
// reset our iteration range to the block after the insertion.
4045
if (B.getInsertionBB() != &BB)

0 commit comments

Comments
 (0)