Skip to content

Commit 941a742

Browse files
committed
handle upcast instructions in statically initialized globals
This allows statically initialized multi-dimensional arrays in embedded swift.
1 parent 1a32cfe commit 941a742

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ extension Instruction {
370370
is VectorInst,
371371
is AllocVectorInst,
372372
is UncheckedRefCastInst,
373+
is UpcastInst,
373374
is ValueToBridgeObjectInst,
374375
is ConvertFunctionInst,
375376
is ThinToThickFunctionInst,

include/swift/SIL/SILBuilder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,10 @@ class SILBuilder {
12511251
UpcastInst *createUpcast(SILLocation Loc, SILValue Op, SILType Ty,
12521252
ValueOwnershipKind forwardingOwnershipKind) {
12531253
assert(Ty.isObject());
1254+
if (isInsertingIntoGlobal()) {
1255+
return insert(UpcastInst::create(getSILDebugLocation(Loc), Op, Ty,
1256+
getModule(), forwardingOwnershipKind));
1257+
}
12541258
return insert(UpcastInst::create(getSILDebugLocation(Loc), Op, Ty,
12551259
getFunction(), forwardingOwnershipKind));
12561260
}

include/swift/SIL/SILInstruction.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5879,6 +5879,10 @@ class UpcastInst final : public UnaryInstructionWithTypeDependentOperandsBase<
58795879
forwardingOwnershipKind) {
58805880
}
58815881

5882+
static UpcastInst *create(SILDebugLocation DebugLoc, SILValue Operand,
5883+
SILType Ty, SILModule &Mod,
5884+
ValueOwnershipKind forwardingOwnershipKind);
5885+
58825886
static UpcastInst *create(SILDebugLocation DebugLoc, SILValue Operand,
58835887
SILType Ty, SILFunction &F,
58845888
ValueOwnershipKind forwardingOwnershipKind);

lib/IRGen/GenConstant.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ Explosion irgen::emitConstantValue(IRGenModule &IGM, SILValue operand,
358358
} else if (auto *URCI = dyn_cast<UncheckedRefCastInst>(operand)) {
359359
return emitConstantValue(IGM, URCI->getOperand(), flatten);
360360

361+
} else if (auto *UCI = dyn_cast<UpcastInst>(operand)) {
362+
return emitConstantValue(IGM, UCI->getOperand(), flatten);
363+
361364
} else if (auto *T2TFI = dyn_cast<ThinToThickFunctionInst>(operand)) {
362365
SILType type = operand->getType();
363366
auto *sTy = cast<llvm::StructType>(IGM.getTypeInfo(type).getStorageType());

lib/SIL/IR/SILInstructions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,6 +2714,15 @@ MetatypeInst *MetatypeInst::create(SILDebugLocation Loc, SILType Ty,
27142714
return ::new (Buffer) MetatypeInst(Loc, Ty, TypeDependentOperands);
27152715
}
27162716

2717+
UpcastInst *UpcastInst::create(SILDebugLocation DebugLoc, SILValue Operand,
2718+
SILType Ty, SILModule &Mod,
2719+
ValueOwnershipKind forwardingOwnershipKind) {
2720+
unsigned size = totalSizeToAlloc<swift::Operand>(1);
2721+
void *Buffer = Mod.allocateInst(size, alignof(UpcastInst));
2722+
return ::new (Buffer) UpcastInst(DebugLoc, Operand, {}, Ty,
2723+
forwardingOwnershipKind);
2724+
}
2725+
27172726
UpcastInst *UpcastInst::create(SILDebugLocation DebugLoc, SILValue Operand,
27182727
SILType Ty, SILFunction &F,
27192728
ValueOwnershipKind forwardingOwnershipKind) {

0 commit comments

Comments
 (0)