Skip to content

[AutoDiff] fix array subscript lookup when there are multiple #31723

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 1 commit into from
May 12, 2020
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
16 changes: 14 additions & 2 deletions lib/SILOptimizer/Differentiation/PullbackEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,7 @@ void PullbackEmitter::visitSILInstruction(SILInstruction *inst) {
AllocStackInst *
PullbackEmitter::getArrayAdjointElementBuffer(SILValue arrayAdjoint,
int eltIndex, SILLocation loc) {
auto &ctx = builder.getASTContext();
auto arrayTanType = cast<StructType>(arrayAdjoint->getType().getASTType());
auto arrayType = arrayTanType->getParent()->castTo<BoundGenericStructType>();
auto eltTanType = arrayType->getGenericArgs().front()->getCanonicalType();
Expand All @@ -1340,7 +1341,19 @@ PullbackEmitter::getArrayAdjointElementBuffer(SILValue arrayAdjoint,
auto *arrayTanStructDecl = arrayTanType->getStructOrBoundGenericStruct();
auto subscriptLookup =
arrayTanStructDecl->lookupDirect(DeclBaseName::createSubscript());
auto *subscriptDecl = cast<SubscriptDecl>(subscriptLookup.front());
SubscriptDecl *subscriptDecl = nullptr;
for (auto *candidate : subscriptLookup) {
auto candidateModule = candidate->getModuleContext();
if (candidateModule->getName() == ctx.Id_Differentiation ||
candidateModule->isStdlibModule()) {
assert(!subscriptDecl && "Multiple `Array.TangentVector.subscript`s");
subscriptDecl = cast<SubscriptDecl>(candidate);
#ifdef NDEBUG
break;
#endif
}
}
assert(subscriptDecl && "No `Array.TangentVector.subscript`");
auto *subscriptGetterDecl = subscriptDecl->getAccessor(AccessorKind::Get);
assert(subscriptGetterDecl && "No `Array.TangentVector.subscript` getter");
SILOptFunctionBuilder fb(getContext().getTransform());
Expand All @@ -1352,7 +1365,6 @@ PullbackEmitter::getArrayAdjointElementBuffer(SILValue arrayAdjoint,
subscriptGetterFn->getLoweredFunctionType()->getSubstGenericSignature();
// Apply `Array.TangentVector.subscript.getter` to get array element adjoint
// buffer.
auto &ctx = builder.getASTContext();
// %index_literal = integer_literal $Builtin.IntXX, <index>
auto builtinIntType =
SILType::getPrimitiveObjectType(ctx.getIntDecl()
Expand Down
9 changes: 9 additions & 0 deletions test/AutoDiff/stdlib/array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ var ArrayAutoDiffTests = TestSuite("ArrayAutoDiff")

typealias FloatArrayTan = Array<Float>.TangentVector

extension Array.DifferentiableView {
/// A subscript that always fatal errors.
///
/// The differentiation transform should never emit calls to this.
subscript(alwaysFatalError: Int) -> Element {
fatalError("wrong subscript")
}
}

ArrayAutoDiffTests.test("ArrayIdentity") {
func arrayIdentity(_ x: [Float]) -> [Float] {
return x
Expand Down