Skip to content

[AutoDiff] Fix semantic member accessor linear map structs. #31745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/SILOptimizer/Differentiation/LinearMapInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ void LinearMapInfo::generateDifferentiationDataStructures(
linearMapStructEnumFields.insert({linearMapStruct, traceEnumField});
}

// Do not add linear map fields for semantic member accessors, which have
// special-case pullback generation. Linear map structs should be empty.
if (isSemanticMemberAccessor(original))
return;

// Add linear map fields to the linear map structs.
for (auto &origBB : *original) {
for (auto &inst : origBB) {
Expand Down
21 changes: 21 additions & 0 deletions test/AutoDiff/SILOptimizer/property_wrappers.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-swift-frontend -emit-sil -verify %s %S/Inputs/nontrivial_loadable_type.swift
// REQUIRES: asserts

// Test property wrapper differentiation coverage for a variety of property
// types: trivial, non-trivial loadable, and address-only.
Expand All @@ -25,17 +26,35 @@ struct Wrapper<Value> {
}
}

// `DifferentiableWrapper` conditionally conforms to `Differentiable`.
@propertyWrapper
struct DifferentiableWrapper<Value> {
private var value: Value
var wrappedValue: Value { // computed property
get { value }
set { value = newValue }
}

init(wrappedValue: Value) {
self.value = wrappedValue
}
}
extension DifferentiableWrapper: Differentiable where Value: Differentiable {}

// MARK: Types with wrapped properties

struct Struct: Differentiable {
@Wrapper @SimpleWrapper var trivial: Float = 10
@Wrapper @SimpleWrapper var tracked: Tracked<Float> = 20
@Wrapper @SimpleWrapper var nontrivial: NontrivialLoadable<Float> = 30
// Tests SR-12800: semantic member accessors should have empty linear map structs.
@DifferentiableWrapper var differentiableWrapped: Float = 40

static func testGetters() {
let _: @differentiable (Self) -> Float = { $0.trivial }
let _: @differentiable (Self) -> Tracked<Float> = { $0.tracked }
let _: @differentiable (Self) -> NontrivialLoadable<Float> = { $0.nontrivial }
let _: @differentiable (Self) -> Float = { $0.differentiableWrapped }
}

static func testSetters() {
Expand All @@ -45,6 +64,8 @@ struct Struct: Differentiable {
{ $0.tracked = $1 }
let _: @differentiable (inout Self, NontrivialLoadable<Float>) -> Void =
{ $0.nontrivial = $1 }
let _: @differentiable (inout Self, Float) -> Void =
{ $0.differentiableWrapped = $1 }
}
}

Expand Down