Skip to content

Commit c6330a7

Browse files
committed
Rev: Addressed feedback
1 parent 546b5af commit c6330a7

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ClosureSpecialization.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,20 @@
4343
/// return sin(x)
4444
/// }
4545
///
46-
/// //============== Before optimization ==============//
46+
/// //============== Before closure specialization ==============//
4747
/// // VJP of `foo`. Returns the original result and the Pullback of `foo`.
4848
/// sil @vjp_foo: $(Float) -> (originalResult: Float, pullback: (Float) -> Float) {
4949
/// bb0(%0: $Float):
50-
/// // vjp_sin returns the original result `sin(x)`, and the pullback of sin, `pb_sin`.
51-
/// // `pb_sin` is a closure.
52-
/// %(originalResult, pb_sin) = apply @vjp_sin(%0): $(Float) -> (Float, (Float) -> Float)
53-
///
50+
/// // __Inlined__ `vjp_sin`: It is important for all intermediate VJPs to have
51+
/// // been inlined in `vjp_foo`, otherwise `vjp_foo` will not be able to determine
52+
/// // that `pb_foo` is closing over other closures and no specialization will happen.
53+
/// \
54+
/// %originalResult = apply @sin(%0): $(Float) -> Float \__ Inlined `vjp_sin`
55+
/// %partially_applied_pb_sin = partial_apply pb_sin(%0): $(Float) -> Float /
56+
/// /
57+
///
5458
/// %pb_foo = function_ref @pb_foo: $@convention(thin) (Float, (Float) -> Float) -> Float
55-
/// %partially_applied_pb_foo = partial_apply %pb_foo(%pb_sin): $(Float, (Float) -> Float) -> Float
59+
/// %partially_applied_pb_foo = partial_apply %pb_foo(%partially_applied_pb_sin): $(Float, (Float) -> Float) -> Float
5660
///
5761
/// return (%originalResult, %partially_applied_pb_foo)
5862
/// }
@@ -72,10 +76,10 @@
7276
/// return %derivative_of_sin: Float
7377
/// }
7478
///
75-
/// //============== After optimization ==============//
79+
/// //============== After closure specialization ==============//
7680
/// sil @vjp_foo: $(Float) -> (originalResult: Float, pullback: (Float) -> Float) {
7781
/// bb0(%0: $Float):
78-
/// %1 = apply @sin(%0): $(Float) -> Float
82+
/// %originalResult = apply @sin(%0): $(Float) -> Float
7983
///
8084
/// // Before the optimization, pullback of `foo` used to take a closure for computing
8185
/// // pullback of `sin`. Now, the specialized pullback of `foo` takes the arguments that
@@ -84,7 +88,7 @@
8488
/// %specialized_pb_foo = function_ref @specialized_pb_foo: $@convention(thin) (Float, Float) -> Float
8589
/// %partially_applied_pb_foo = partial_apply %specialized_pb_foo(%0): $(Float, Float) -> Float
8690
///
87-
/// return (%1, %partially_applied_pb_foo)
91+
/// return (%originalResult, %partially_applied_pb_foo)
8892
/// }
8993
///
9094
/// sil @specialized_pb_foo: $(Float, Float) -> Float {
@@ -384,8 +388,8 @@ private func handleApplies(for rootClosure: SingleValueInstruction, callSiteMap:
384388
continue
385389
}
386390

387-
// If the callee uses a dynamic Self, we cannot specialize it, since the resulting specialization might longer have
388-
// 'self' as the last parameter.
391+
// If the callee uses a dynamic Self, we cannot specialize it, since the resulting specialization might no longer
392+
// have 'self' as the last parameter.
389393
//
390394
// TODO: We could fix this by inserting new arguments more carefully, or changing how we model dynamic Self
391395
// altogether.

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include <cassert>
1413
#define DEBUG_TYPE "sil-passmanager"
1514

1615
#include "swift/SILOptimizer/PassManager/PassManager.h"

test/SILOptimizer/experimental-swift-based-closure-specialization/closure_specialize.sil

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ bb0(%0 : $Int):
132132
// address and non-address only types) taken as @in or @in_guaranteed.
133133

134134
// This is a temporary limitation.
135+
// TODO: figure out what to do with non-inout indirect arguments
136+
// https://forums.swift.org/t/non-inout-indirect-types-not-supported-in-closure-specialization-optimization/70826
135137
// CHECK-LABEL: sil @address_closure : $@convention(thin) (@in Int) -> () {
136138
sil @address_closure : $@convention(thin) (@in Int) -> () {
137139
bb0(%0 : $*Int):

test/SILOptimizer/experimental-swift-based-closure-specialization/closure_specialize_dynamic_self.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -emit-sil -O -primary-file %s
1+
// RUN: %target-swift-frontend -emit-sil -O -experimental-swift-based-closure-specialization -primary-file %s
22

33
// Just make sure we skip the optimization and not crash here.
44
//

0 commit comments

Comments
 (0)