Skip to content

Commit a58b1df

Browse files
dan-zhengbgogul
authored andcommitted
[AutoDiff] Fix invalid @differentiable attribute SILGen crash. (#28205)
Fix SILGen crash for invalid `@differentiable` attributes in non-primary files. `SILFunctionBuilder::addFunctionAttributes` now validates all `@differentiable` attributes by calling `DifferentiableAttr::getParameterIndices`. Resolves TF-953.
1 parent 43041ee commit a58b1df

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

lib/SIL/SILFunctionBuilder.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ void SILFunctionBuilder::addFunctionAttributes(SILFunction *F,
8383
!constant.autoDiffDerivativeFunctionIdentifier &&
8484
!constant.isStoredPropertyInitializer() &&
8585
!constant.isThunk()) {
86-
// NOTE: Validate `@differentiable` attributes on `AccessorDecl`s by calling
87-
// `getParameterIndices`. This is significant to prevent duplicate SIL
88-
// `[differentiable]` attribute generation: `getParameterIndices` deletes
89-
// `@differentiable` attributes whose original declaration is an
90-
// `AbstractStorageDecl`.
91-
if (isa<AccessorDecl>(decl))
92-
for (auto *A : Attrs.getAttributes<DifferentiableAttr>())
93-
(void)A->getParameterIndices();
86+
// NOTE: Validate `@differentiable` attributes by calling
87+
// `getParameterIndices`. This is important for:
88+
// - Skiping invalid `@differentiable` attributes in non-primary files.
89+
// - Preventing duplicate SIL `[differentiable]` attribute generation for
90+
// `@differentiable` attributes on `AbstractStorageDecl` declarations.
91+
// Such attributes are deleted and recreated on the getter `AccessorDecl`
92+
// of the `AbstractStorageDecl`.
93+
for (auto *A : Attrs.getAttributes<DifferentiableAttr>())
94+
(void)A->getParameterIndices();
9495
for (auto *A : Attrs.getAttributes<DifferentiableAttr>()) {
9596
// Get lowered argument indices.
9697
auto *paramIndices = A->getParameterIndices();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Invalid `@differentiable` attribute in non-primary-file should not crash
2+
// SILGen.
3+
@differentiable
4+
func foo(_ x: Int) -> Float {
5+
return Float(x)
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: not %target-swift-frontend -c -primary-file %s %S/Inputs/tf953-invalid-differentiable-attr-other-module.swift -module-name main
2+
3+
// Verify that invalid `@differentiable` attribute in non-primary file does not
4+
// crash SILGen.
5+
6+
func bar(_ x: Int) -> Float {
7+
return foo(2)
8+
}
9+
10+
// Assertion failed: (paramIndices && "Parameter indices should have been resolved"), function addFunctionAttributes, file /Users/swiftninjas/s4tf/swift/lib/SIL/SILFunctionBuilder.cpp, line 97.
11+
// Stack dump:
12+
// 1. Swift version 5.1.1-dev (Swift e242a8825f)
13+
// 2. While emitting SIL for 'bar(_:)' (at /Users/danielzheng/swift-merge/swift/test/AutoDiff/compiler_crashers/tf953-invalid-differentiable-attr-cross-module.swift:3:1)
14+
// 3. While silgen emitFunction SIL function "@$s4main3barySfSiF".
15+
// for 'bar(_:)' (at /Users/danielzheng/swift-merge/swift/test/AutoDiff/compiler_crashers/tf953-invalid-differentiable-attr-cross-module.swift:3:1)

0 commit comments

Comments
 (0)