Skip to content

Commit a88cb49

Browse files
committed
SIL: define a memory-read effect on the mark_dependence base value
If the base value of a mark_dependence is an address, that memory location must be initialized at the mark_dependence. This requires that the mark_dependence is considered to read from the base address.
1 parent d918b31 commit a88cb49

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/Analysis/AliasAnalysis.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ struct AliasAnalysis {
253253
case let storeBorrow as StoreBorrowInst:
254254
return memLoc.mayAlias(with: storeBorrow.destination, self) ? .init(write: true) : .noEffects
255255

256+
case let mdi as MarkDependenceInst:
257+
if mdi.base.type.isAddress && memLoc.mayAlias(with: mdi.base, self) {
258+
return .init(read: true)
259+
}
260+
return .noEffects
261+
256262
case let copy as SourceDestAddrInstruction:
257263
let mayRead = memLoc.mayAlias(with: copy.source, self)
258264
let mayWrite = memLoc.mayAlias(with: copy.destination, self)

SwiftCompilerSources/Sources/Optimizer/TestPasses/MemBehaviorDumper.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ private extension Instruction {
7474
is CopyAddrInst,
7575
is BuiltinInst,
7676
is StoreBorrowInst,
77+
is MarkDependenceInst,
7778
is DebugValueInst:
7879
return true
7980
default:

lib/SIL/IR/SILInstruction.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,12 @@ MemoryBehavior SILInstruction::getMemoryBehavior() const {
10811081
llvm_unreachable("Covered switch isn't covered?!");
10821082
}
10831083

1084+
if (auto *mdi = dyn_cast<MarkDependenceInst>(this)) {
1085+
if (mdi->getBase()->getType().isAddress())
1086+
return MemoryBehavior::MayRead;
1087+
return MemoryBehavior::None;
1088+
}
1089+
10841090
// TODO: An UncheckedTakeEnumDataAddr instruction has no memory behavior if
10851091
// it is nondestructive. Setting this currently causes LICM to miscompile
10861092
// because access paths do not account for enum projections.

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,6 +2829,7 @@ void swift::visitAccessedAddress(SILInstruction *I,
28292829
case SILInstructionKind::EndLifetimeInst:
28302830
case SILInstructionKind::ExistentialMetatypeInst:
28312831
case SILInstructionKind::FixLifetimeInst:
2832+
case SILInstructionKind::MarkDependenceInst:
28322833
case SILInstructionKind::GlobalAddrInst:
28332834
case SILInstructionKind::HasSymbolInst:
28342835
case SILInstructionKind::HopToExecutorInst:

test/SILOptimizer/mem-behavior.sil

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,3 +1851,32 @@ bb0(%0 : @owned $CL):
18511851
%3 = tuple ()
18521852
return %3
18531853
}
1854+
1855+
// CHECK-LABEL: @test_mark_dependence
1856+
// CHECK: PAIR #0.
1857+
// CHECK-NEXT: %3 = mark_dependence %1 : $*C on %2 : $C
1858+
// CHECK-NEXT: %0 = argument of bb0 : $*C
1859+
// CHECK-NEXT: r=0,w=0
1860+
// CHECK: PAIR #3.
1861+
// CHECK-NEXT: %4 = mark_dependence %2 : $C on %0 : $*C
1862+
// CHECK-NEXT: %0 = argument of bb0 : $*C
1863+
// CHECK-NEXT: r=1,w=0
1864+
// CHECK: PAIR #4.
1865+
// CHECK-NEXT: %4 = mark_dependence %2 : $C on %0 : $*C
1866+
// CHECK-NEXT: %1 = argument of bb0 : $*C
1867+
// CHECK-NEXT: r=0,w=0
1868+
// CHECK: PAIR #7.
1869+
// CHECK-NEXT: %5 = mark_dependence %1 : $*C on %0 : $*C
1870+
// CHECK-NEXT: %0 = argument of bb0 : $*C
1871+
// CHECK-NEXT: r=1,w=0
1872+
// CHECK: PAIR #8.
1873+
// CHECK-NEXT: %5 = mark_dependence %1 : $*C on %0 : $*C
1874+
// CHECK-NEXT: %1 = argument of bb0 : $*C
1875+
// CHECK-NEXT: r=0,w=0
1876+
sil [ossa] @test_mark_dependence : $@convention(thin) (@in_guaranteed C, @in_guaranteed C, @owned C) -> @owned C {
1877+
bb0(%0 : $*C, %1 : $*C, %2 : @owned $C):
1878+
%3 = mark_dependence %1 on %2
1879+
%4 = mark_dependence %2 on %0
1880+
%5 = mark_dependence %1 on %0
1881+
return %4
1882+
}

0 commit comments

Comments
 (0)