Skip to content

Commit ac3109a

Browse files
committed
[eager-specializer] Fix for ownership and add a bunch of missing code coverage for ossa.
Specifically, we were missing a bunch of coverage around specializing guaranteed parameters and non-trivial values in general.
1 parent 5e36ae1 commit ac3109a

15 files changed

+1326
-93
lines changed

include/swift/SIL/SILBuilder.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -1853,7 +1853,18 @@ class SILBuilder {
18531853
// Unchecked cast helpers
18541854
//===--------------------------------------------------------------------===//
18551855

1856-
// Create the appropriate cast instruction based on result type.
1856+
/// Create the appropriate cast instruction based on result type.
1857+
///
1858+
/// NOTE: We allow for non-layout compatible casts that shrink the underlying
1859+
/// type we are bit casting!
1860+
SingleValueInstruction *
1861+
createUncheckedReinterpretCast(SILLocation Loc, SILValue Op, SILType Ty);
1862+
1863+
/// Create an appropriate cast instruction based on result type.
1864+
///
1865+
/// NOTE: This assumes that the input and the result cast are layout
1866+
/// compatible. Reduces to createUncheckedReinterpretCast when ownership is
1867+
/// disabled.
18571868
SingleValueInstruction *createUncheckedBitCast(SILLocation Loc, SILValue Op,
18581869
SILType Ty);
18591870

include/swift/SILOptimizer/Utils/CFGOptUtils.h

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ bool splitCriticalEdgesFrom(SILBasicBlock *fromBB,
107107
DominanceInfo *domInfo = nullptr,
108108
SILLoopInfo *loopInfo = nullptr);
109109

110+
/// Splits all critical edges that have `toBB` as a destination.
111+
bool splitCriticalEdgesTo(SILBasicBlock *toBB, DominanceInfo *domInfo = nullptr,
112+
SILLoopInfo *loopInfo = nullptr);
113+
110114
/// Splits the edges between two basic blocks.
111115
///
112116
/// Updates dominance information and loop information if not null.

lib/IRGen/LoadableByAddress.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ void LoadableStorageAllocation::
15441544
storageType);
15451545
if (pass.containsDifferentFunctionSignature(pass.F->getLoweredFunctionType(),
15461546
storageType)) {
1547-
auto *castInstr = argBuilder.createUncheckedBitCast(
1547+
auto *castInstr = argBuilder.createUncheckedReinterpretCast(
15481548
RegularLocation(const_cast<ValueDecl *>(arg->getDecl())), arg,
15491549
newSILType);
15501550
arg->replaceAllUsesWith(castInstr);
@@ -1912,8 +1912,8 @@ static void castTupleInstr(SingleValueInstruction *instr, IRGenModule &Mod,
19121912
switch (instr->getKind()) {
19131913
// Add cast to the new sil function type:
19141914
case SILInstructionKind::TupleExtractInst: {
1915-
castInstr = castBuilder.createUncheckedBitCast(instr->getLoc(), instr,
1916-
newSILType.getObjectType());
1915+
castInstr = castBuilder.createUncheckedReinterpretCast(
1916+
instr->getLoc(), instr, newSILType.getObjectType());
19171917
break;
19181918
}
19191919
case SILInstructionKind::TupleElementAddrInst: {
@@ -2471,8 +2471,8 @@ getOperandTypeWithCastIfNecessary(SILInstruction *containingInstr, SILValue op,
24712471
}
24722472
assert(currSILType.isObject() && "Expected an object type");
24732473
if (newSILType != currSILType) {
2474-
auto castInstr = builder.createUncheckedBitCast(containingInstr->getLoc(),
2475-
op, newSILType);
2474+
auto castInstr = builder.createUncheckedReinterpretCast(
2475+
containingInstr->getLoc(), op, newSILType);
24762476
return castInstr;
24772477
}
24782478
}
@@ -2653,8 +2653,8 @@ bool LoadableByAddress::recreateUncheckedEnumDataInstr(
26532653
auto *takeEnum = enumBuilder.createUncheckedEnumData(
26542654
enumInstr->getLoc(), enumInstr->getOperand(), enumInstr->getElement(),
26552655
caseTy);
2656-
newInstr = enumBuilder.createUncheckedBitCast(enumInstr->getLoc(), takeEnum,
2657-
newType);
2656+
newInstr = enumBuilder.createUncheckedReinterpretCast(enumInstr->getLoc(),
2657+
takeEnum, newType);
26582658
} else {
26592659
newInstr = enumBuilder.createUncheckedEnumData(
26602660
enumInstr->getLoc(), enumInstr->getOperand(), enumInstr->getElement(),
@@ -2708,7 +2708,7 @@ bool LoadableByAddress::fixStoreToBlockStorageInstr(
27082708
if (destType.getObjectType() != srcType) {
27092709
// Add cast to destType
27102710
SILBuilderWithScope castBuilder(instr);
2711-
auto *castInstr = castBuilder.createUncheckedBitCast(
2711+
auto *castInstr = castBuilder.createUncheckedReinterpretCast(
27122712
instr->getLoc(), src, destType.getObjectType());
27132713
instr->setOperand(StoreInst::Src, castInstr);
27142714
}

lib/SIL/IR/SILBuilder.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ SILBuilder::createClassifyBridgeObject(SILLocation Loc, SILValue value) {
141141

142142
// Create the appropriate cast instruction based on result type.
143143
SingleValueInstruction *
144-
SILBuilder::createUncheckedBitCast(SILLocation Loc, SILValue Op, SILType Ty) {
144+
SILBuilder::createUncheckedReinterpretCast(SILLocation Loc, SILValue Op,
145+
SILType Ty) {
145146
assert(isLoadableOrOpaque(Ty));
146147
if (Ty.isTrivial(getFunction()))
147148
return insert(UncheckedTrivialBitCastInst::create(
@@ -156,6 +157,26 @@ SILBuilder::createUncheckedBitCast(SILLocation Loc, SILValue Op, SILType Ty) {
156157
getSILDebugLocation(Loc), Op, Ty, getFunction(), C.OpenedArchetypes));
157158
}
158159

160+
// Create the appropriate cast instruction based on result type.
161+
SingleValueInstruction *
162+
SILBuilder::createUncheckedBitCast(SILLocation Loc, SILValue Op, SILType Ty) {
163+
// Without ownership, delegate to unchecked reinterpret cast.
164+
if (!hasOwnership())
165+
return createUncheckedReinterpretCast(Loc, Op, Ty);
166+
167+
assert(isLoadableOrOpaque(Ty));
168+
if (Ty.isTrivial(getFunction()))
169+
return insert(UncheckedTrivialBitCastInst::create(
170+
getSILDebugLocation(Loc), Op, Ty, getFunction(), C.OpenedArchetypes));
171+
172+
if (SILType::canRefCast(Op->getType(), Ty, getModule()))
173+
return createUncheckedRefCast(Loc, Op, Ty);
174+
175+
// The destination type is nontrivial, and may be smaller than the source
176+
// type, so RC identity cannot be assumed.
177+
return createUncheckedValueCast(Loc, Op, Ty);
178+
}
179+
159180
BranchInst *SILBuilder::createBranch(SILLocation Loc,
160181
SILBasicBlock *TargetBlock,
161182
OperandValueArrayRef Args) {

lib/SILGen/SILGenApply.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1340,8 +1340,8 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
13401340
&& loweredResultTy.getASTType()->hasDynamicSelfType()) {
13411341
assert(selfMetaTy);
13421342
selfValue = SGF.emitManagedRValueWithCleanup(
1343-
SGF.B.createUncheckedBitCast(loc, selfValue.forward(SGF),
1344-
loweredResultTy));
1343+
SGF.B.createUncheckedReinterpretCast(loc, selfValue.forward(SGF),
1344+
loweredResultTy));
13451345
} else {
13461346
selfValue = SGF.emitManagedRValueWithCleanup(
13471347
SGF.B.createUpcast(loc, selfValue.forward(SGF), loweredResultTy));

lib/SILGen/SILGenBuilder.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ ManagedValue SILGenBuilder::createUncheckedBitCast(SILLocation loc,
610610
ManagedValue value,
611611
SILType type) {
612612
CleanupCloner cloner(*this, value);
613-
SILValue cast = createUncheckedBitCast(loc, value.getValue(), type);
613+
SILValue cast = createUncheckedReinterpretCast(loc, value.getValue(), type);
614614

615615
// Currently createUncheckedBitCast only produces these
616616
// instructions. We assert here to make sure if this changes, this code is

lib/SILGen/SILGenBuilder.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class SILGenBuilder : public SILBuilder {
279279
ManagedValue createUncheckedAddrCast(SILLocation loc, ManagedValue op,
280280
SILType resultTy);
281281

282-
using SILBuilder::createUncheckedBitCast;
282+
using SILBuilder::createUncheckedReinterpretCast;
283283
ManagedValue createUncheckedBitCast(SILLocation loc, ManagedValue original,
284284
SILType type);
285285

lib/SILGen/SILGenExpr.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2066,8 +2066,8 @@ RValue RValueEmitter::visitUnderlyingToOpaqueExpr(UnderlyingToOpaqueExpr *E,
20662066
if (value.getType() == opaqueTL.getLoweredType())
20672067
return RValue(SGF, E, value);
20682068

2069-
auto cast = SGF.B.createUncheckedBitCast(E, value.forward(SGF),
2070-
opaqueTL.getLoweredType());
2069+
auto cast = SGF.B.createUncheckedReinterpretCast(E, value.forward(SGF),
2070+
opaqueTL.getLoweredType());
20712071
value = SGF.emitManagedRValueWithCleanup(cast);
20722072

20732073
return RValue(SGF, E, value);

0 commit comments

Comments
 (0)