Skip to content

Commit 7e544f3

Browse files
committed
Stop getUnderlyingObject at mark_dependence instructions for LSLocations
This helps remove redundant loads from mark_dependence instructions while our projection path can't handle them. Fix for rdar://27138023
1 parent 3f4ddd4 commit 7e544f3

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

include/swift/SIL/InstructionUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@ namespace swift {
2020
/// Strip off casts/indexing insts/address projections from V until there is
2121
/// nothing left to strip.
2222
SILValue getUnderlyingObject(SILValue V);
23+
SILValue getUnderlyingObjectStopAtMarkDependence(SILValue V);
2324

2425
SILValue stripSinglePredecessorArgs(SILValue V);
2526

2627
/// Return the underlying SILValue after stripping off all casts from the
2728
/// current SILValue.
2829
SILValue stripCasts(SILValue V);
2930

31+
/// Return the underlying SILValue after stripping off all casts (but
32+
/// mark_dependence) from the current SILValue.
33+
SILValue stripCastsWithoutMarkDependence(SILValue V);
34+
3035
/// Return the underlying SILValue after stripping off all upcasts from the
3136
/// current SILValue.
3237
SILValue stripUpCasts(SILValue V);

lib/SIL/InstructionUtils.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ SILValue swift::getUnderlyingObject(SILValue V) {
3030
}
3131
}
3232

33+
SILValue swift::getUnderlyingObjectStopAtMarkDependence(SILValue V) {
34+
while (true) {
35+
SILValue V2 = stripIndexingInsts(stripAddressProjections(stripCastsWithoutMarkDependence(V)));
36+
if (V2 == V)
37+
return V2;
38+
V = V2;
39+
}
40+
}
41+
3342
static bool isRCIdentityPreservingCast(ValueKind Kind) {
3443
switch (Kind) {
3544
case ValueKind::UpcastInst:
@@ -88,6 +97,21 @@ SILValue swift::stripSinglePredecessorArgs(SILValue V) {
8897
}
8998
}
9099

100+
SILValue swift::stripCastsWithoutMarkDependence(SILValue V) {
101+
while (true) {
102+
V = stripSinglePredecessorArgs(V);
103+
104+
auto K = V->getKind();
105+
if (isRCIdentityPreservingCast(K) ||
106+
K == ValueKind::UncheckedTrivialBitCastInst) {
107+
V = cast<SILInstruction>(V)->getOperand(0);
108+
continue;
109+
}
110+
111+
return V;
112+
}
113+
}
114+
91115
SILValue swift::stripCasts(SILValue V) {
92116
while (true) {
93117
V = stripSinglePredecessorArgs(V);

lib/SILOptimizer/Utils/LoadStoreOptUtils.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ LSLocation::enumerateLSLocation(SILModule *M, SILValue Mem,
214214
return;
215215

216216
// Construct a Location to represent the memory written by this instruction.
217-
SILValue UO = getUnderlyingObject(Mem);
217+
// ProjectionPath currently does not handle mark_dependence so stop our
218+
// underlying object search at these instructions.
219+
// We still get a benefit if we cse mark_dependence instructions and then
220+
// merge loads from them.
221+
SILValue UO = getUnderlyingObjectStopAtMarkDependence(Mem);
218222
LSLocation L(UO, ProjectionPath::getProjectionPath(UO, Mem));
219223

220224
// If we can't figure out the Base or Projection Path for the memory location,

test/SILOptimizer/redundant_load_elim.sil

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,3 +1146,12 @@ bb0(%0 : $*Builtin.Int64, %1 : $*Builtin.Int64):
11461146
%4 = load %1 : $*Builtin.Int64
11471147
return %4 : $Builtin.Int64
11481148
}
1149+
1150+
sil @redundant_load_mark_dependence : $@convention(thin) (@inout Builtin.Int64, @guaranteed Builtin.NativeObject) -> (Builtin.Int64, Builtin.Int64) {
1151+
bb0(%0 : $*Builtin.Int64, %1 : $Builtin.NativeObject):
1152+
%2 = mark_dependence %0 : $*Builtin.Int64 on %1 : $Builtin.NativeObject
1153+
%4 = load %2 : $*Builtin.Int64
1154+
%5 = load %2 : $*Builtin.Int64
1155+
%6 = tuple(%4 : $Builtin.Int64, %5 : $Builtin.Int64)
1156+
return %6 : $(Builtin.Int64, Builtin.Int64)
1157+
}

0 commit comments

Comments
 (0)