Skip to content

Commit 8fc0992

Browse files
authored
[NFC] [AutoDiff] Gardening: @differentiable function type mangling. (#26600)
- Add todo comments tracking TF-750. - Optimize `SWIFT_ENABLE_TENSORFLOW` comments. - Clarify comment in test/AutoDiff/differentiable_func_debuginfo.swift. Addresses feedback from #26595.
1 parent 51dc28d commit 8fc0992

File tree

6 files changed

+28
-12
lines changed

6 files changed

+28
-12
lines changed

lib/Demangling/OldRemangler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,15 +1269,15 @@ void Remangler::mangleImplResult(Node *node) {
12691269

12701270
// SWIFT_ENABLE_TENSORFLOW
12711271
void Remangler::mangleImplDifferentiable(Node *node) {
1272-
// The old mangler does not encode `@differentiable` function types.
1272+
// TODO(TF-750): Check if this code path actually triggers and add a test.
12731273
Buffer << 'd';
12741274
}
12751275

1276-
// SWIFT_ENABLE_TENSORFLOW
12771276
void Remangler::mangleImplLinear(Node *node) {
1278-
// The old mangler does not encode `@differentiable(linear)` function types.
1277+
// TODO(TF-750): Check if this code path actually triggers and add a test.
12791278
Buffer << 'l';
12801279
}
1280+
// SWIFT_ENABLE_TENSORFLOW END
12811281

12821282
void Remangler::mangleImplEscaping(Node *node) {
12831283
// The old mangler does not encode escaping.

lib/Demangling/Remangler.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,23 +1204,21 @@ void Remangler::mangleDifferentiableFunctionType(Node *node) {
12041204
Buffer << "XF";
12051205
}
12061206

1207-
// SWIFT_ENABLE_TENSORFLOW
12081207
void Remangler::mangleEscapingDifferentiableFunctionType(Node *node) {
12091208
mangleFunctionSignature(node);
12101209
Buffer << "XG";
12111210
}
12121211

1213-
// SWIFT_ENABLE_TENSORFLOW
12141212
void Remangler::mangleLinearFunctionType(Node *node) {
12151213
mangleFunctionSignature(node);
12161214
Buffer << "XH";
12171215
}
12181216

1219-
// SWIFT_ENABLE_TENSORFLOW
12201217
void Remangler::mangleEscapingLinearFunctionType(Node *node) {
12211218
mangleFunctionSignature(node);
12221219
Buffer << "XI";
12231220
}
1221+
// SWIFT_ENABLE_TENSORFLOW END
12241222

12251223
void Remangler::mangleGenericProtocolWitnessTable(Node *node) {
12261224
mangleSingleChildNode(node);
@@ -1384,10 +1382,10 @@ void Remangler::mangleImplDifferentiable(Node *node) {
13841382
Buffer << 'd';
13851383
}
13861384

1387-
// SWIFT_ENABLE_TENSORFLOW
13881385
void Remangler::mangleImplLinear(Node *node) {
13891386
Buffer << 'l';
13901387
}
1388+
// SWIFT_ENABLE_TENSORFLOW END
13911389

13921390
void Remangler::mangleImplEscaping(Node *node) {
13931391
Buffer << 'e';

test/AutoDiff/differentiable_func_debuginfo.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ public protocol Differentiable {
2727
associatedtype TangentVector
2828
}
2929

30-
// This declaration is necessary to reproduce the crash for some reason.
30+
// This declaration is necessary to reproduce the crash.
31+
// `Builtin.autodiffApply_vjp` constructs a use of the `tf597ProblematicVarDecl`
32+
// type, which was mangled without `@differentiable` attribute. The parameter
33+
// for `blackHole` is of type `$@noescape @callee_guaranteed (@in_guaranteed T) -> @out U`,
34+
// which matched the mangled name for the type of the parameter of `blackHole`.
35+
// As a result, the types are uniqued when generating debug info. The type of
36+
// the parameter of `blackHole` is smaller than the `@differentiable` function
37+
// type, causing IRGenDebugInfo to crash.
3138
public func blackHole<T, U>(_: (T) -> U) {}
3239

3340
public func pullback<T, R>(

test/Demangle/Inputs/manglings.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,4 @@ $s3red4testyAA7OurTypeOy4them05TheirD0Vy5AssocQzGAjE0F8ProtocolAAxAA0c7DerivedH0
354354
// SWIFT_ENABLE_TENSORFLOW
355355
$sxq_Idgnr_D ---> @differentiable @callee_guaranteed (@in_guaranteed A) -> (@out B)
356356
$sxq_Ilgnr_D ---> @differentiable(linear) @callee_guaranteed (@in_guaranteed A) -> (@out B)
357+
// SWIFT_ENABLE_TENSORFLOW END

test/SILGen/mangling.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,6 @@ func varargsVsArray(arr: [Int]..., n: String) { }
189189
// CHECK-LABEL: sil hidden [ossa] @$s8mangling15funcVsDiffFunc12fnyS2fXE_tF : $@convention(thin) (@noescape @callee_guaranteed (Float) -> Float) -> ()
190190
func funcVsDiffFunc1(fn: (Float) -> Float) {}
191191

192-
// SWIFT_ENABLE_TENSORFLOW
193192
// CHECK-LABEL: sil hidden [ossa] @$s8mangling15funcVsDiffFunc22fnyS2fXF_tF : $@convention(thin) (@differentiable @noescape @callee_guaranteed (Float) -> Float) -> ()
194193
func funcVsDiffFunc2(fn: @differentiable (Float) -> Float) {}
194+
// SWIFT_ENABLE_TENSORFLOW END

test/TypeDecoder/structural_types.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ do {
140140
_ = f
141141
}
142142

143-
// SWIFT_ENABLE_TENSORFLOW
144143
do {
145144
let f: (@escaping @differentiable (Float) -> Float) -> () = { _ in }
146145
// FIXME(TF-123): `@differentiable` function type + opaque abstraction
@@ -151,7 +150,6 @@ do {
151150

152151
// TODO: Uncomment when `@differentiable(linear)` function types are enabled.
153152
/*
154-
// SWIFT_ENABLE_TENSORFLOW
155153
do {
156154
let f: @differentiable(linear) (Float) -> Float = { $0 }
157155
// FIXME(TF-123): `@differentiable` function type + opaque abstraction
@@ -160,7 +158,6 @@ do {
160158
_ = f
161159
}
162160

163-
// SWIFT_ENABLE_TENSORFLOW
164161
do {
165162
let f: (@escaping @differentiable(linear) (Float) -> Float) -> () = { _ in }
166163
// FIXME(TF-123): `@differentiable` function type + opaque abstraction
@@ -169,6 +166,7 @@ do {
169166
_ = f
170167
}
171168
*/
169+
// SWIFT_ENABLE_TENSORFLOW END
172170

173171
// DEMANGLE: $syycD
174172
// DEMANGLE: $sySSzcD
@@ -188,10 +186,13 @@ do {
188186
// DEMANGLE: $syyyccD
189187
// DEMANGLE: $sSayyyXCGD
190188
// DEMANGLE: $sSayyyyXL_yyXBtcGD
189+
190+
// SWIFT_ENABLE_TENSORFLOW
191191
// DEMANGLE: $sS2fXFD
192192
// DEMANGLE: $sS2fXGD
193193
// DEMANGLE: $sS2fXHD
194194
// DEMANGLE: $sS2fXID
195+
// SWIFT_ENABLE_TENSORFLOW END
195196

196197
// CHECK: () -> ()
197198
// CHECK: (inout String) -> ()
@@ -211,10 +212,13 @@ do {
211212
// CHECK: (@escaping () -> ()) -> ()
212213
// CHECK: Array<@convention(c) () -> ()>
213214
// CHECK: Array<(@escaping @convention(block) () -> (), @convention(block) () -> ()) -> ()>
215+
216+
// SWIFT_ENABLE_TENSORFLOW
214217
// CHECK: @differentiable (Float) -> Float
215218
// CHECK: @differentiable (Float) -> Float
216219
// CHECK: @differentiable(linear) (Float) -> Float
217220
// CHECK: @differentiable(linear) (Float) -> Float
221+
// SWIFT_ENABLE_TENSORFLOW END
218222

219223
// DEMANGLE: $sSimD
220224
// DEMANGLE: $syycmD
@@ -235,10 +239,13 @@ do {
235239
// DEMANGLE: $syyyccmD
236240
// DEMANGLE: $sSayyyXCGmD
237241
// DEMANGLE: $sSayyyyXL_yyXBtcGmD
242+
243+
// SWIFT_ENABLE_TENSORFLOW
238244
// DEMANGLE: $sS2fXFmD
239245
// DEMANGLE: $sS2fXGmD
240246
// DEMANGLE: $sS2fXHmD
241247
// DEMANGLE: $sS2fXImD
248+
// SWIFT_ENABLE_TENSORFLOW END
242249

243250
// CHECK: Int.Type
244251
// CHECK: ((inout String) -> ()).Type
@@ -258,7 +265,10 @@ do {
258265
// CHECK: ((@escaping () -> ()) -> ()).Type
259266
// CHECK: Array<@convention(c) () -> ()>.Type
260267
// CHECK: Array<(@escaping @convention(block) () -> (), @convention(block) () -> ()) -> ()>.Type
268+
269+
// SWIFT_ENABLE_TENSORFLOW
261270
// CHECK: (@differentiable (Float) -> Float).Type
262271
// CHECK: (@differentiable (Float) -> Float).Type
263272
// CHECK: (@differentiable(linear) (Float) -> Float).Type
264273
// CHECK: (@differentiable(linear) (Float) -> Float).Type
274+
// SWIFT_ENABLE_TENSORFLOW END

0 commit comments

Comments
 (0)