Skip to content

Commit 28a5eee

Browse files
committed
SILMem2Reg: don't create an "undef" as a replacement of a load of an empty tuple.
Instead create the empty tuple value. In general, it's not a good idea to create undef values in SIL.
1 parent 91be112 commit 28a5eee

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,18 @@ StackAllocationPromoter::promoteAllocationInBlock(SILBasicBlock *BB) {
576576
return LastStore;
577577
}
578578

579+
/// Create a tuple value for an empty tuple or a tuple of empty tuples.
580+
SILValue createValueForEmptyTuple(SILType ty, SILInstruction *insertionPoint) {
581+
auto tupleTy = ty.castTo<TupleType>();
582+
SmallVector<SILValue, 4> elements;
583+
for (unsigned idx = 0, end = tupleTy->getNumElements(); idx < end; ++ idx) {
584+
SILType elementTy = ty.getTupleElementType(idx);
585+
elements.push_back(createValueForEmptyTuple(elementTy, insertionPoint));
586+
}
587+
SILBuilder builder(insertionPoint);
588+
return builder.createTuple(insertionPoint->getLoc(), ty, elements);
589+
}
590+
579591
void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *ASI) {
580592
LLVM_DEBUG(llvm::dbgs() << "*** Promoting in-block: " << *ASI);
581593

@@ -596,7 +608,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *ASI) {
596608
if (!RunningVal) {
597609
// Loading without a previous store is only acceptable if the type is
598610
// Void (= empty tuple) or a tuple of Voids.
599-
RunningVal = SILUndef::get(ASI->getElementType(), *ASI->getFunction());
611+
RunningVal = createValueForEmptyTuple(ASI->getElementType(), Inst);
600612
}
601613
replaceLoad(cast<LoadInst>(Inst), RunningVal, ASI);
602614
++NumInstRemoved;

test/SILOptimizer/mem2reg.sil

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ bb3(%20 : $((), ())):
438438
}
439439

440440
// CHECK-LABEL: sil @load_tuple_of_void
441-
// CHECK-NOT: alloc_stack
442-
// CHECK: return undef : $((), ())
441+
// CHECK: [[T:%[0-9]+]] = tuple (%{{[0-9]+}} : $(), %{{[0-9]+}} : $())
442+
// CHECK: return [[T]] : $((), ())
443443
// CHECK: } // end sil function 'load_tuple_of_void'
444444
sil @load_tuple_of_void : $@convention(thin) () -> ((), ()) {
445445
bb0:

test/SILOptimizer/mem2reg_ossa.sil

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,8 @@ bb3(%20 : $((), ())):
478478

479479
// CHECK-LABEL: sil [ossa] @load_tuple_of_void :
480480
// CHECK-NOT: alloc_stack
481-
// CHECK: return undef : $((), ())
481+
// CHECK: [[T:%[0-9]+]] = tuple (%{{[0-9]+}} : $(), %{{[0-9]+}} : $())
482+
// CHECK: return [[T]] : $((), ())
482483
// CHECK: } // end sil function 'load_tuple_of_void'
483484
sil [ossa] @load_tuple_of_void : $@convention(thin) () -> ((), ()) {
484485
bb0:

0 commit comments

Comments
 (0)