Skip to content

Commit e089e95

Browse files
committed
[Mem2Reg] Instantiate arbitrary empty types.
Currently, memory locations whose type is empty (`SILType::isEmpty`) are regarded as viable sources for loads. Previously, though, Mem2Reg only handled loads from empty types formed only by tupling. Here, support is added for types formed also by struct'ing. As before, this entails recursively instantiating the empty types until reaching the innermost empty types (which aggregate nothing) and then aggregating the resulting instances. rdar://106224845
1 parent c6ff96f commit e089e95

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

test/SILOptimizer/mem2reg.sil

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ struct LargeCodesizeStruct {
2222
var s5: SmallCodesizeStruct
2323
}
2424

25+
struct EmptyStruct {}
26+
struct Pair<T, U> {
27+
var t: T
28+
var u: U
29+
}
30+
2531
///////////
2632
// Tests //
2733
///////////
@@ -492,3 +498,23 @@ bb3:
492498
%11 = tuple ()
493499
return %11 : $()
494500
}
501+
502+
// CHECK-LABEL: sil @load_from_uninitialized_empty : {{.*}} {
503+
// CHECK: [[V_3_0:%[^,]+]] = struct $EmptyStruct
504+
// CHECK: [[V_3_1:%[^,]+]] = struct $EmptyStruct
505+
// CHECK: [[V_2_0:%[^,]+]] = struct $Pair{{.*}} ([[V_3_0]]{{.*}}, [[V_3_1]]
506+
// CHECK: [[V_2_1:%[^,]+]] = struct $EmptyStruct ()
507+
// CHECK: [[V_1_0:%[^,]+]] = struct $Pair{{.*}} ([[V_2_0]]{{.*}}, [[V_2_1]]
508+
// CHECK: [[V_2_2:%[^,]+]] = struct $EmptyStruct
509+
// CHECK: [[V_2_3:%[^,]+]] = struct $EmptyStruct
510+
// CHECK: [[V_1_1:%[^,]+]] = tuple ([[V_2_2]]{{.*}}, [[V_2_3]]
511+
// CHECK: [[V_1_2:%[^,]+]] = struct $EmptyStruct
512+
// CHECK: [[V_0:%[^,]+]] = tuple ([[V_1_0]]{{.*}}, [[V_1_1]]{{.*}}, [[V_1_2]]{{.*}})
513+
// CHECK: return [[V_0]]
514+
// CHECK-LABEL: } // end sil function 'load_from_uninitialized_empty'
515+
sil @load_from_uninitialized_empty : $@convention(thin) () -> (Pair<Pair<EmptyStruct, EmptyStruct>, EmptyStruct>, (EmptyStruct, EmptyStruct), EmptyStruct) {
516+
%addr = alloc_stack $(Pair<Pair<EmptyStruct, EmptyStruct>, EmptyStruct>, (EmptyStruct, EmptyStruct), EmptyStruct)
517+
%value = load %addr : $*(Pair<Pair<EmptyStruct, EmptyStruct>, EmptyStruct>, (EmptyStruct, EmptyStruct), EmptyStruct)
518+
dealloc_stack %addr : $*(Pair<Pair<EmptyStruct, EmptyStruct>, EmptyStruct>, (EmptyStruct, EmptyStruct), EmptyStruct)
519+
return %value : $(Pair<Pair<EmptyStruct, EmptyStruct>, EmptyStruct>, (EmptyStruct, EmptyStruct), EmptyStruct)
520+
}

0 commit comments

Comments
 (0)