Skip to content

Commit d2d66f7

Browse files
authored
Merge pull request #4480 from eeckstein/partial-apply-opt2
address gottesmm's and jrose-apple's comments on the partial_apply optimization
2 parents 9c943f0 + 01c6d4b commit d2d66f7

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

include/swift/SILOptimizer/Analysis/CallerAnalysis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class CallerAnalysis : public SILAnalysis {
5353
llvm::SmallSet<SILFunction *, 4> Callers;
5454

5555
/// The number of partial applied arguments of this function.
56+
///
5657
/// Specifically, it stores the minimum number of partial applied arguments
5758
/// of each function which contain one or multiple partial_applys of this
5859
/// function.
@@ -67,6 +68,7 @@ class CallerAnalysis : public SILAnalysis {
6768
}
6869

6970
/// Returns non zero if this function is partially applied anywhere.
71+
///
7072
/// The return value is the minimum number of partially applied arguments.
7173
/// Usually all partial applies of a function partially apply the same
7274
/// number of arguments anyway.

lib/SILOptimizer/IPO/CapturePropagation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ static bool isProfitable(SILFunction *Callee) {
286286

287287
/// Returns true if block \p BB only contains a return or throw of the first
288288
/// block argument and side-effect-free instructions.
289-
static bool isArgReturnOrThrow(SILBasicBlock *BB) {
289+
static bool onlyContainsReturnOrThrowOfArg(SILBasicBlock *BB) {
290290
for (SILInstruction &I : *BB) {
291291
if (isa<ReturnInst>(&I) || isa<ThrowInst>(&I)) {
292292
SILValue RetVal = I.getOperand(0);
@@ -349,8 +349,8 @@ static SILFunction *getSpecializedWithDeadParams(SILFunction *Orig,
349349

350350
if (TryApplyInst *TAI = dyn_cast<TryApplyInst>(&I)) {
351351
// Check the normal and throw blocks of the try_apply.
352-
if (isArgReturnOrThrow(TAI->getNormalBB()) &&
353-
isArgReturnOrThrow(TAI->getErrorBB()))
352+
if (onlyContainsReturnOrThrowOfArg(TAI->getNormalBB()) &&
353+
onlyContainsReturnOrThrowOfArg(TAI->getErrorBB()))
354354
return Specialized;
355355
return nullptr;
356356
}

test/SILOptimizer/functionsigopts.sil

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,27 @@ bb0(%0 : $Optional<(foo, foo)>):
13261326
}
13271327

13281328

1329+
// Check if externally available functions are optimized.
1330+
1331+
sil public_external [noinline] @externally_available_with_dead_arg : $@convention(thin) (@guaranteed foo) -> () {
1332+
bb0(%0 : $foo):
1333+
%r = tuple()
1334+
return %r : $()
1335+
}
1336+
1337+
// CHECK-LABEL: sil @call_externally_available
1338+
// CHECK: [[F:%[0-9]+]] = function_ref @_TTSf4d__externally_available_with_dead_arg : $@convention(thin) () -> ()
1339+
// CHECK: apply [[F]]()
1340+
// CHECK: return
1341+
sil @call_externally_available : $@convention(thin) (@guaranteed foo) -> () {
1342+
bb0(%0 : $foo):
1343+
%f = function_ref @externally_available_with_dead_arg : $@convention(thin) (@guaranteed foo) -> ()
1344+
%a = apply %f(%0) : $@convention(thin) (@guaranteed foo) -> ()
1345+
%r = tuple()
1346+
return %r : $()
1347+
}
1348+
1349+
13291350
// We should remove the array semantic from specialized calls.
13301351

13311352
// CHECK-LABEL: sil [fragile] [thunk] [always_inline] [_semantics "array.foobar"] @array_semantic : $@convention(method) (@owned Builtin.NativeObject) -> () {

0 commit comments

Comments
 (0)