Skip to content

Commit ec8e171

Browse files
committed
SIL: fix memory behavior of bind_memory and rebind_memory
Since dead-store-elimination is now more accurate, it's important that the `bind_memory` instructions are also defined to _read_ memory. Otherwise they are not considered as barriers for dead-store-elimination. Fixes a miscompile. rdar://112150142
1 parent aad2b6e commit ec8e171

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

include/swift/SIL/SILNodes.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,12 +694,12 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
694694
SingleValueInstruction, MayHaveSideEffects, DoesNotRelease)
695695

696696
// BindMemory and RebindMemory have no physical side effect. Semantically they
697-
// write to their affected memory region because any reads or writes accessing
697+
// access their affected memory region because any reads or writes accessing
698698
// that memory must be dependent on the bind operation.
699699
SINGLE_VALUE_INST(BindMemoryInst, bind_memory,
700-
SILInstruction, MayWrite, DoesNotRelease)
700+
SILInstruction, MayReadWrite, DoesNotRelease)
701701
SINGLE_VALUE_INST(RebindMemoryInst, rebind_memory,
702-
SILInstruction, MayWrite, DoesNotRelease)
702+
SILInstruction, MayReadWrite, DoesNotRelease)
703703

704704
SINGLE_VALUE_INST_RANGE(SingleValueInstruction, AllocStackInst, RebindMemoryInst)
705705
NODE_RANGE(ValueBase, SILPhiArgument, RebindMemoryInst)

test/SILOptimizer/dead_store_elim.sil

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,3 +1613,21 @@ bb0(%0 : $Int, %1 : @owned $IntAndFoo, %2 : $*IntAndFoo):
16131613
%r = tuple ()
16141614
return %r : $()
16151615
}
1616+
1617+
// CHECK-LABEL: sil @test_bind_memory :
1618+
// CHECK: store %0
1619+
// CHECK: end sil function 'test_bind_memory'
1620+
sil @test_bind_memory : $@convention(thin) (UInt64, Builtin.Word) -> Builtin.Int8 {
1621+
bb0(%0 : $UInt64, %1 : $Builtin.Word):
1622+
%35 = alloc_stack $(UInt64, UInt64)
1623+
%36 = tuple_element_addr %35 : $*(UInt64, UInt64), 0
1624+
store %0 to %36 : $*UInt64
1625+
%50 = address_to_pointer %35 : $*(UInt64, UInt64) to $Builtin.RawPointer
1626+
%52 = bind_memory %50 : $Builtin.RawPointer, %1 : $Builtin.Word to $*UInt8
1627+
%72 = pointer_to_address %50 : $Builtin.RawPointer to [strict] $*UInt8
1628+
%75 = struct_element_addr %72 : $*UInt8, #UInt8._value
1629+
%76 = load %75 : $*Builtin.Int8
1630+
dealloc_stack %35 : $*(UInt64, UInt64)
1631+
return %76 : $Builtin.Int8
1632+
}
1633+

0 commit comments

Comments
 (0)