@@ -470,19 +470,43 @@ replaceLoad(SILInstruction *inst, SILValue newValue, AllocStackInst *asi,
470470 }
471471}
472472
473- // / Create a tuple value for an empty tuple or a tuple of empty tuples.
474- static SILValue createValueForEmptyTuple (SILType ty,
475- SILInstruction *insertionPoint,
476- SILBuilderContext &ctx) {
477- auto tupleTy = ty.castTo <TupleType>();
478- SmallVector<SILValue, 4 > elements;
479- for (unsigned idx : range (tupleTy->getNumElements ())) {
480- SILType elementTy = ty.getTupleElementType (idx);
481- elements.push_back (
482- createValueForEmptyTuple (elementTy, insertionPoint, ctx));
473+ // / Instantiate the specified empty type by recursively tupling and structing
474+ // / the empty types aggregated together at each level.
475+ static SILValue createValueForEmptyType (SILType ty,
476+ SILInstruction *insertionPoint,
477+ SILBuilderContext &ctx) {
478+ auto *function = insertionPoint->getFunction ();
479+ assert (ty.isEmpty (*function));
480+ if (auto tupleTy = ty.getAs <TupleType>()) {
481+ SmallVector<SILValue, 4 > elements;
482+ for (unsigned idx : range (tupleTy->getNumElements ())) {
483+ SILType elementTy = ty.getTupleElementType (idx);
484+ auto element = createValueForEmptyType (elementTy, insertionPoint, ctx);
485+ elements.push_back (element);
486+ }
487+ SILBuilderWithScope builder (insertionPoint, ctx);
488+ return builder.createTuple (insertionPoint->getLoc (), ty, elements);
489+ } else if (auto *decl = ty.getStructOrBoundGenericStruct ()) {
490+ TypeExpansionContext tec = *function;
491+ auto &module = function->getModule ();
492+ if (decl->isResilient (tec.getContext ()->getParentModule (),
493+ tec.getResilienceExpansion ())) {
494+ llvm::errs () << " Attempting to create value for illegal empty type:\n " ;
495+ ty.print (llvm::errs ());
496+ llvm::report_fatal_error (" illegal empty type: resilient struct" );
497+ }
498+ SmallVector<SILValue, 4 > elements;
499+ for (auto *field : decl->getStoredProperties ()) {
500+ auto elementTy = ty.getFieldType (field, module , tec);
501+ auto element = createValueForEmptyType (elementTy, insertionPoint, ctx);
502+ elements.push_back (element);
503+ }
504+ SILBuilderWithScope builder (insertionPoint, ctx);
505+ return builder.createStruct (insertionPoint->getLoc (), ty, elements);
483506 }
484- SILBuilderWithScope builder (insertionPoint, ctx);
485- return builder.createTuple (insertionPoint->getLoc (), ty, elements);
507+ llvm::errs () << " Attempting to create value for illegal empty type:\n " ;
508+ ty.print (llvm::errs ());
509+ llvm::report_fatal_error (" illegal empty type: neither tuple nor struct." );
486510}
487511
488512// / Whether lexical lifetimes should be added for the values stored into the
@@ -1859,11 +1883,11 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
18591883 // with our running value.
18601884 if (isLoadFromStack (inst, asi)) {
18611885 if (!runningVals) {
1862- // Loading without a previous store is only acceptable if the type is
1863- // Void (= empty tuple) or a tuple of Voids .
1886+ // Loading from uninitialized memory is only acceptable if the type is
1887+ // empty--an aggregate of types without storage .
18641888 runningVals = {
18651889 LiveValues::toReplace (asi,
1866- /* replacement=*/ createValueForEmptyTuple (
1890+ /* replacement=*/ createValueForEmptyType (
18671891 asi->getElementType (), inst, ctx)),
18681892 /* isStorageValid=*/ true };
18691893 }
0 commit comments