Skip to content

Commit 4c741cd

Browse files
Merge pull request #3302 from aschwaighofer/cse_rle_mark_dependence
CSE and RLE based of mark_dependence instructions
2 parents be62dd7 + 7e544f3 commit 4c741cd

File tree

7 files changed

+67
-1
lines changed

7 files changed

+67
-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/SIL/SILInstruction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,10 @@ namespace {
619619
return true;
620620
}
621621

622+
bool visitMarkDependenceInst(const MarkDependenceInst *RHS) {
623+
return true;
624+
}
625+
622626
private:
623627
const SILInstruction *LHS;
624628
};

lib/SILOptimizer/Transforms/CSE.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ class HashVisitor : public SILInstructionVisitor<HashVisitor, llvm::hash_code> {
362362
Operands.begin(),
363363
Operands.end()));
364364
}
365+
hash_code visitMarkDependenceInst(MarkDependenceInst *X) {
366+
OperandValueArrayRef Operands(X->getAllOperands());
367+
return llvm::hash_combine(
368+
X->getKind(), X->getType(),
369+
llvm::hash_combine_range(Operands.begin(), Operands.end()));
370+
}
365371
};
366372
} // end anonymous namespace
367373

@@ -680,6 +686,7 @@ bool CSE::canHandle(SILInstruction *Inst) {
680686
case ValueKind::BridgeObjectToWordInst:
681687
case ValueKind::ThinFunctionToPointerInst:
682688
case ValueKind::PointerToThinFunctionInst:
689+
case ValueKind::MarkDependenceInst:
683690
return true;
684691
default:
685692
return false;

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/cse.sil

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,3 +1306,16 @@ bb2: // Preds: bb0 bb1
13061306
return %19 : $() // id: %20
13071307
}
13081308

1309+
// CHECK-LABEL: sil @cse_mark_dependence
1310+
// CHECK: mark_dependence
1311+
// CHECK-NOT: mark_dependence
1312+
// CHECK: return
1313+
sil @cse_mark_dependence : $@convention(thin) (@inout Builtin.Int64, @guaranteed Builtin.NativeObject) -> (Builtin.Int64, Builtin.Int64) {
1314+
bb0(%0 : $*Builtin.Int64, %1 : $Builtin.NativeObject):
1315+
%2 = mark_dependence %0 : $*Builtin.Int64 on %1 : $Builtin.NativeObject
1316+
%3 = mark_dependence %0 : $*Builtin.Int64 on %1 : $Builtin.NativeObject
1317+
%4 = load %2 : $*Builtin.Int64
1318+
%5 = load %3 : $*Builtin.Int64
1319+
%6 = tuple(%4 : $Builtin.Int64, %5 : $Builtin.Int64)
1320+
return %6 : $(Builtin.Int64, Builtin.Int64)
1321+
}

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)