Skip to content

Commit 0c9025c

Browse files
committed
[SILGen] Factor out emitUnreachableValue
1 parent a69dbb3 commit 0c9025c

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ namespace {
499499
emitFunctionCvtFromExecutionCallerToGlobalActor(FunctionConversionExpr *E,
500500
SGFContext C);
501501

502+
/// Helper to emit a value of arbitrary type in an unreachable codepath.
503+
RValue emitUnreachableValue(SILLocation loc, CanType ty);
504+
502505
RValue visitActorIsolationErasureExpr(ActorIsolationErasureExpr *E,
503506
SGFContext C);
504507
RValue visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E,
@@ -2136,6 +2139,32 @@ RValue RValueEmitter::emitFunctionCvtFromExecutionCallerToGlobalActor(
21362139
return RValue(SGF, e, destType, result);
21372140
}
21382141

2142+
RValue RValueEmitter::emitUnreachableValue(SILLocation loc, CanType ty) {
2143+
ASSERT(!SGF.B.hasValidInsertionPoint() && "Must be unreachable");
2144+
2145+
// Continue code generation in a block with no predecessors.
2146+
// Whatever code is emitted here is guaranteed to be removed by SIL passes.
2147+
SGF.B.emitBlock(SGF.createBasicBlock());
2148+
2149+
// Since the type is uninhabited, use a SILUndef of so that we can return
2150+
// some sort of RValue from this API.
2151+
auto &lowering = SGF.getTypeLowering(ty);
2152+
auto loweredTy = lowering.getLoweredType();
2153+
auto undef = SILUndef::get(SGF.F, loweredTy);
2154+
2155+
// Create an alloc initialized with contents from the undefined addr type.
2156+
// It seems pack addresses do not satisfy isPlusOneOrTrivial, so we need an
2157+
// actual allocation.
2158+
if (loweredTy.isAddress()) {
2159+
auto resultAddr = SGF.emitTemporaryAllocation(loc, loweredTy);
2160+
SGF.emitSemanticStore(loc, undef, resultAddr, lowering, IsInitialization);
2161+
return RValue(SGF, loc, ty, SGF.emitManagedRValueWithCleanup(resultAddr));
2162+
}
2163+
2164+
// Otherwise, if it's not an address, just emit the undef value itself.
2165+
return RValue(SGF, loc, ty, ManagedValue::forRValueWithoutOwnership(undef));
2166+
}
2167+
21392168
RValue RValueEmitter::visitFunctionConversionExpr(FunctionConversionExpr *e,
21402169
SGFContext C) {
21412170
CanAnyFunctionType srcType =
@@ -2660,28 +2689,7 @@ RValue RValueEmitter::visitUnreachableExpr(UnreachableExpr *E, SGFContext C) {
26602689
// Emit the expression, followed by an unreachable.
26612690
SGF.emitIgnoredExpr(E->getSubExpr());
26622691
SGF.B.createUnreachable(E);
2663-
2664-
// Continue code generation in a block with no predecessors.
2665-
// Whatever code is emitted here is guaranteed to be removed by SIL passes.
2666-
SGF.B.emitBlock(SGF.createBasicBlock());
2667-
2668-
// Since the type is uninhabited, use a SILUndef of so that we can return
2669-
// some sort of RValue from this API.
2670-
auto &lowering = SGF.getTypeLowering(E->getType());
2671-
auto loweredTy = lowering.getLoweredType();
2672-
auto undef = SILUndef::get(SGF.F, loweredTy);
2673-
2674-
// Create an alloc initialized with contents from the undefined addr type.
2675-
// It seems pack addresses do not satisfy isPlusOneOrTrivial, so we need an
2676-
// actual allocation.
2677-
if (loweredTy.isAddress()) {
2678-
auto resultAddr = SGF.emitTemporaryAllocation(E, loweredTy);
2679-
SGF.emitSemanticStore(E, undef, resultAddr, lowering, IsInitialization);
2680-
return RValue(SGF, E, SGF.emitManagedRValueWithCleanup(resultAddr));
2681-
}
2682-
2683-
// Otherwise, if it's not an address, just emit the undef value itself.
2684-
return RValue(SGF, E, ManagedValue::forRValueWithoutOwnership(undef));
2692+
return emitUnreachableValue(E, E->getType()->getCanonicalType());
26852693
}
26862694

26872695
static SILValue getArrayBuffer(SILValue array, SILGenFunction &SGF, SILLocation loc) {

0 commit comments

Comments
 (0)