@@ -327,19 +327,43 @@ replaceLoad(SILInstruction *inst, SILValue newValue, AllocStackInst *asi,
327327 }
328328}
329329
330- // / Create a tuple value for an empty tuple or a tuple of empty tuples.
331- static SILValue createValueForEmptyTuple (SILType ty,
332- SILInstruction *insertionPoint,
333- SILBuilderContext &ctx) {
334- auto tupleTy = ty.castTo <TupleType>();
335- SmallVector<SILValue, 4 > elements;
336- for (unsigned idx : range (tupleTy->getNumElements ())) {
337- SILType elementTy = ty.getTupleElementType (idx);
338- elements.push_back (
339- createValueForEmptyTuple (elementTy, insertionPoint, ctx));
330+ // / Instantiate the specified empty type by recursively tupling and structing
331+ // / the empty types aggregated together at each level.
332+ static SILValue createValueForEmptyType (SILType ty,
333+ SILInstruction *insertionPoint,
334+ SILBuilderContext &ctx) {
335+ auto *function = insertionPoint->getFunction ();
336+ assert (ty.isEmpty (*function));
337+ if (auto tupleTy = ty.getAs <TupleType>()) {
338+ SmallVector<SILValue, 4 > elements;
339+ for (unsigned idx : range (tupleTy->getNumElements ())) {
340+ SILType elementTy = ty.getTupleElementType (idx);
341+ auto element = createValueForEmptyType (elementTy, insertionPoint, ctx);
342+ elements.push_back (element);
343+ }
344+ SILBuilderWithScope builder (insertionPoint, ctx);
345+ return builder.createTuple (insertionPoint->getLoc (), ty, elements);
346+ } else if (auto *decl = ty.getStructOrBoundGenericStruct ()) {
347+ TypeExpansionContext tec = *function;
348+ auto &module = function->getModule ();
349+ if (decl->isResilient (tec.getContext ()->getParentModule (),
350+ tec.getResilienceExpansion ())) {
351+ llvm::errs () << " Attempting to create value for illegal empty type:\n " ;
352+ ty.print (llvm::errs ());
353+ llvm::report_fatal_error (" illegal empty type: resilient struct" );
354+ }
355+ SmallVector<SILValue, 4 > elements;
356+ for (auto *field : decl->getStoredProperties ()) {
357+ auto elementTy = ty.getFieldType (field, module , tec);
358+ auto element = createValueForEmptyType (elementTy, insertionPoint, ctx);
359+ elements.push_back (element);
360+ }
361+ SILBuilderWithScope builder (insertionPoint, ctx);
362+ return builder.createStruct (insertionPoint->getLoc (), ty, elements);
340363 }
341- SILBuilderWithScope builder (insertionPoint, ctx);
342- return builder.createTuple (insertionPoint->getLoc (), ty, elements);
364+ llvm::errs () << " Attempting to create value for illegal empty type:\n " ;
365+ ty.print (llvm::errs ());
366+ llvm::report_fatal_error (" illegal empty type: neither tuple nor struct." );
343367}
344368
345369// / Whether lexical lifetimes should be added for the values stored into the
@@ -1607,11 +1631,11 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
16071631 // with our running value.
16081632 if (isLoadFromStack (inst, asi)) {
16091633 if (!runningVals) {
1610- // Loading without a previous store is only acceptable if the type is
1611- // Void (= empty tuple) or a tuple of Voids .
1634+ // Loading from uninitialized memory is only acceptable if the type is
1635+ // empty--an aggregate of types without storage .
16121636 runningVals = {
16131637 LiveValues::toReplace (asi,
1614- /* replacement=*/ createValueForEmptyTuple (
1638+ /* replacement=*/ createValueForEmptyType (
16151639 asi->getElementType (), inst, ctx)),
16161640 /* isStorageValid=*/ true };
16171641 }
0 commit comments