Skip to content

SILOptimizer: fix a compiler crash when optimizing a global variable with an empty tuple. #36447

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 2 commits into from
Mar 16, 2021
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
5 changes: 5 additions & 0 deletions lib/SIL/IR/SILGlobalVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ BuiltinInst *SILGlobalVariable::getOffsetSubtract(const TupleExtractInst *TE,

bool SILGlobalVariable::isValidStaticInitializerInst(const SILInstruction *I,
SILModule &M) {
for (const Operand &op : I->getAllOperands()) {
// Rule out SILUndef and SILArgument.
if (!isa<SingleValueInstruction>(op.get()))
return false;
}
switch (I->getKind()) {
case SILInstructionKind::BuiltinInst: {
auto *bi = cast<BuiltinInst>(I);
Expand Down
14 changes: 13 additions & 1 deletion lib/SILOptimizer/Transforms/SILMem2Reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,18 @@ StackAllocationPromoter::promoteAllocationInBlock(SILBasicBlock *BB) {
return LastStore;
}

/// Create a tuple value for an empty tuple or a tuple of empty tuples.
SILValue createValueForEmptyTuple(SILType ty, SILInstruction *insertionPoint) {
auto tupleTy = ty.castTo<TupleType>();
SmallVector<SILValue, 4> elements;
for (unsigned idx = 0, end = tupleTy->getNumElements(); idx < end; ++ idx) {
SILType elementTy = ty.getTupleElementType(idx);
elements.push_back(createValueForEmptyTuple(elementTy, insertionPoint));
}
SILBuilder builder(insertionPoint);
return builder.createTuple(insertionPoint->getLoc(), ty, elements);
}

void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *ASI) {
LLVM_DEBUG(llvm::dbgs() << "*** Promoting in-block: " << *ASI);

Expand All @@ -596,7 +608,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *ASI) {
if (!RunningVal) {
// Loading without a previous store is only acceptable if the type is
// Void (= empty tuple) or a tuple of Voids.
RunningVal = SILUndef::get(ASI->getElementType(), *ASI->getFunction());
RunningVal = createValueForEmptyTuple(ASI->getElementType(), Inst);
}
replaceLoad(cast<LoadInst>(Inst), RunningVal, ASI);
++NumInstRemoved;
Expand Down
6 changes: 6 additions & 0 deletions test/SILOptimizer/globalopt_let_propagation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ var VPI = 3.1415
var VI = 100
var VS = "String2"

struct GenericStruct<T> {
var x: T
}

// Define some static let variables inside a struct.
struct B {
static let PI = 3.1415
Expand Down Expand Up @@ -109,6 +113,8 @@ struct B {
static let IT1 = ((10, 20), 30, 40)

static let IT2 = (100, 200, 300)

static let emptyStruct = GenericStruct(x: ())
}

// Define some static let variables inside a class.
Expand Down
32 changes: 32 additions & 0 deletions test/SILOptimizer/globalopt_trivial_nontrivial_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class TClass {
deinit
}

struct GenericStruct<T> {
var x: T
}

let nontrivialglobal: TClass

// CHECK-LABEL: sil_global hidden [let] @$trivialglobal : $TStruct = {
Expand All @@ -32,6 +36,9 @@ sil_global private @globalinit_nontrivialglobal_token : $Builtin.Word

sil_global hidden [let] @$nontrivialglobal : $TClass

sil_global hidden [let] @empty_global : $GenericStruct<()>
sil_global private @empty_global_token : $Builtin.Word

sil private [global_init_once_fn] [ossa] @globalinit_trivialglobal_func : $@convention(c) () -> () {
bb0:
alloc_global @$trivialglobal
Expand Down Expand Up @@ -126,3 +133,28 @@ bb0:
return %5 : $Int32
}

// Check that we don't crash on an initializer struct with an "undef" operand.
// CHECK-LABEL: sil hidden [global_init_once_fn] [ossa] @globalinit_empty_global :
// CHECK: struct $GenericStruct<()> (undef : $())
// CHECK-LABEL: } // end sil function 'globalinit_empty_global'
sil hidden [global_init_once_fn] [ossa] @globalinit_empty_global : $@convention(c) () -> () {
bb0:
alloc_global @empty_global
%1 = global_addr @empty_global : $*GenericStruct<()>
%2 = struct $GenericStruct<()> (undef : $())
store %2 to [trivial] %1 : $*GenericStruct<()>
%4 = tuple ()
return %4 : $()
}

sil hidden [global_init] [ossa] @empty_global_accessor : $@convention(thin) () -> Builtin.RawPointer {
bb0:
%0 = global_addr @empty_global_token : $*Builtin.Word
%1 = address_to_pointer %0 : $*Builtin.Word to $Builtin.RawPointer
%2 = function_ref @globalinit_empty_global : $@convention(c) () -> ()
%3 = builtin "once"(%1 : $Builtin.RawPointer, %2 : $@convention(c) () -> ()) : $()
%4 = global_addr @empty_global : $*GenericStruct<()>
%5 = address_to_pointer %4 : $*GenericStruct<()> to $Builtin.RawPointer
return %5 : $Builtin.RawPointer
}

4 changes: 2 additions & 2 deletions test/SILOptimizer/mem2reg.sil
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ bb3(%20 : $((), ())):
}

// CHECK-LABEL: sil @load_tuple_of_void
// CHECK-NOT: alloc_stack
// CHECK: return undef : $((), ())
// CHECK: [[T:%[0-9]+]] = tuple (%{{[0-9]+}} : $(), %{{[0-9]+}} : $())
// CHECK: return [[T]] : $((), ())
// CHECK: } // end sil function 'load_tuple_of_void'
sil @load_tuple_of_void : $@convention(thin) () -> ((), ()) {
bb0:
Expand Down
3 changes: 2 additions & 1 deletion test/SILOptimizer/mem2reg_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ bb3(%20 : $((), ())):

// CHECK-LABEL: sil [ossa] @load_tuple_of_void :
// CHECK-NOT: alloc_stack
// CHECK: return undef : $((), ())
// CHECK: [[T:%[0-9]+]] = tuple (%{{[0-9]+}} : $(), %{{[0-9]+}} : $())
// CHECK: return [[T]] : $((), ())
// CHECK: } // end sil function 'load_tuple_of_void'
sil [ossa] @load_tuple_of_void : $@convention(thin) () -> ((), ()) {
bb0:
Expand Down