Skip to content

[Mem2Reg] When materializing an empty type projected from a non-empty aggregate, populate the non-empty fields of the aggregate with undef. #71521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions lib/SILOptimizer/Transforms/SILMem2Reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,18 +679,18 @@ replaceLoad(SILInstruction *inst, SILValue newValue, AllocStackInst *asi,
}
}

/// Instantiate the specified empty type by recursively tupling and structing
/// the empty types aggregated together at each level.
static SILValue createValueForEmptyType(SILType ty,
SILInstruction *insertionPoint,
SILBuilderContext &ctx) {
/// Instantiate the specified type by recursively tupling and structing the
/// unique instances of the empty types and undef "instances" of the non-empty
/// types aggregated together at each level.
static SILValue createEmptyAndUndefValue(SILType ty,
SILInstruction *insertionPoint,
SILBuilderContext &ctx) {
auto *function = insertionPoint->getFunction();
assert(ty.isEmpty(*function));
if (auto tupleTy = ty.getAs<TupleType>()) {
SmallVector<SILValue, 4> elements;
for (unsigned idx : range(tupleTy->getNumElements())) {
SILType elementTy = ty.getTupleElementType(idx);
auto element = createValueForEmptyType(elementTy, insertionPoint, ctx);
auto element = createEmptyAndUndefValue(elementTy, insertionPoint, ctx);
elements.push_back(element);
}
SILBuilderWithScope builder(insertionPoint, ctx);
Expand All @@ -707,15 +707,14 @@ static SILValue createValueForEmptyType(SILType ty,
SmallVector<SILValue, 4> elements;
for (auto *field : decl->getStoredProperties()) {
auto elementTy = ty.getFieldType(field, module, tec);
auto element = createValueForEmptyType(elementTy, insertionPoint, ctx);
auto element = createEmptyAndUndefValue(elementTy, insertionPoint, ctx);
elements.push_back(element);
}
SILBuilderWithScope builder(insertionPoint, ctx);
return builder.createStruct(insertionPoint->getLoc(), ty, elements);
} else {
return SILUndef::get(ty, *insertionPoint->getFunction());
}
llvm::errs() << "Attempting to create value for illegal empty type:\n";
ty.print(llvm::errs());
llvm::report_fatal_error("illegal empty type: neither tuple nor struct.");
}

/// Whether lexical lifetimes should be added for the values stored into the
Expand Down Expand Up @@ -1982,7 +1981,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
// empty--an aggregate of types without storage.
runningVals = {
LiveValues::toReplace(asi,
/*replacement=*/createValueForEmptyType(
/*replacement=*/createEmptyAndUndefValue(
asi->getElementType(), inst, ctx)),
/*isStorageValid=*/true};
}
Expand Down
16 changes: 16 additions & 0 deletions test/SILOptimizer/mem2reg.sil
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,19 @@ sil @dont_canonicalize_undef : $@convention(thin) () -> () {
%retval = tuple ()
return %retval : $()
}

// CHECK-LABEL: sil @undef_for_load_of_empty_type_from_nontrivial_aggregate : {{.*}} {
// CHECK: [[EMPTY:%[^,]+]] = tuple ()
// CHECK: [[AGGREGATE:%[^,]+]] = tuple (undef : $Builtin.BridgeObject, [[EMPTY]] : $())
// CHECK: tuple_extract [[AGGREGATE]] : $(Builtin.BridgeObject, ()), 1
// CHECK-LABEL: } // end sil function 'undef_for_load_of_empty_type_from_nontrivial_aggregate'
sil @undef_for_load_of_empty_type_from_nontrivial_aggregate : $@convention(thin) () -> () {
bb0:
%11 = alloc_stack $(Builtin.BridgeObject, ())
%12 = tuple_element_addr %11 : $*(Builtin.BridgeObject, ()), 1
%13 = load %12 : $*()
%empty = tuple ()
%14 = tuple (%empty : $(), %13 : $())
dealloc_stack %11 : $*(Builtin.BridgeObject, ())
return undef : $()
}