1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
|
//===--- DerivedConformanceDifferentiable.cpp - Derived Differentiable ----===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements explicit derivation of the Differentiable protocol for
// struct and class types.
//
//===----------------------------------------------------------------------===//
#include "CodeSynthesis.h"
#include "TypeChecker.h"
#include "TypeCheckType.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "swift/AST/AutoDiff.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Module.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/PropertyWrappers.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/Types.h"
#include "DerivedConformances.h"
using namespace swift;
/// Return true if `move(by:)` can be invoked on the given `Differentiable`-
/// conforming property.
///
/// If the given property is a `var`, return true because `move(by:)` can be
/// invoked regardless. Otherwise, return true if and only if the property's
/// type's 'Differentiable.move(by:)' witness is non-mutating.
static bool canInvokeMoveByOnProperty(
VarDecl *vd, ProtocolConformanceRef diffableConformance) {
assert(diffableConformance && "Property must conform to 'Differentiable'");
// `var` always supports `move(by:)` since it is mutable.
if (vd->getIntroducer() == VarDecl::Introducer::Var)
return true;
// When the property is a `let`, the only case that would be supported is when
// it has a `move(by:)` protocol requirement witness that is non-mutating.
auto interfaceType = vd->getInterfaceType();
auto &C = vd->getASTContext();
auto witness = diffableConformance.getWitnessByName(
interfaceType, DeclName(C, C.Id_move, {C.Id_by}));
if (!witness)
return false;
auto *decl = cast<FuncDecl>(witness.getDecl());
return !decl->isMutating();
}
/// Get the stored properties of a nominal type that are relevant for
/// differentiation, except the ones tagged `@noDerivative`.
static void
getStoredPropertiesForDifferentiation(
NominalTypeDecl *nominal, DeclContext *DC,
SmallVectorImpl<VarDecl *> &result,
bool includeLetPropertiesWithNonmutatingMoveBy = false) {
auto &C = nominal->getASTContext();
auto *diffableProto = C.getProtocol(KnownProtocolKind::Differentiable);
for (auto *vd : nominal->getStoredProperties()) {
// Peer through property wrappers: use original wrapped properties instead.
if (auto *originalProperty = vd->getOriginalWrappedProperty()) {
// Skip immutable wrapped properties. `mutating func move(by:)` cannot
// be synthesized to update these properties.
if (!originalProperty->isSettable(DC))
continue;
// Use the original wrapped property.
vd = originalProperty;
}
// Skip stored properties with `@noDerivative` attribute.
if (vd->getAttrs().hasAttribute<NoDerivativeAttr>())
continue;
if (vd->getInterfaceType()->hasError())
continue;
auto varType = DC->mapTypeIntoContext(vd->getValueInterfaceType());
auto conformance = DC->getParentModule()->checkConformance(
varType, diffableProto);
if (!conformance)
continue;
// Skip `let` stored properties with a mutating `move(by:)` if requested.
// `mutating func move(by:)` cannot be synthesized to update `let`
// properties.
if (!includeLetPropertiesWithNonmutatingMoveBy &&
!canInvokeMoveByOnProperty(vd, conformance))
continue;
result.push_back(vd);
}
}
/// Convert the given `ValueDecl` to a `StructDecl` if it is a `StructDecl` or a
/// `TypeDecl` with an underlying struct type. Otherwise, return `nullptr`.
static StructDecl *convertToStructDecl(ValueDecl *v) {
if (auto *structDecl = dyn_cast<StructDecl>(v))
return structDecl;
auto *typeDecl = dyn_cast<TypeDecl>(v);
if (!typeDecl)
return nullptr;
return dyn_cast_or_null<StructDecl>(
typeDecl->getDeclaredInterfaceType()->getAnyNominal());
}
/// Get the `Differentiable` protocol `TangentVector` associated type witness
/// for the given interface type and declaration context.
static Type getTangentVectorInterfaceType(Type contextualType,
DeclContext *DC) {
auto &C = DC->getASTContext();
auto *diffableProto = C.getProtocol(KnownProtocolKind::Differentiable);
assert(diffableProto && "`Differentiable` protocol not found");
auto conf =
DC->getParentModule()->checkConformance(contextualType, diffableProto);
assert(conf && "Contextual type must conform to `Differentiable`");
if (!conf)
return nullptr;
auto tanType = conf.getTypeWitnessByName(contextualType, C.Id_TangentVector);
return tanType->hasArchetype() ? tanType->mapTypeOutOfContext() : tanType;
}
/// Returns true iff the given nominal type declaration can derive
/// `TangentVector` as `Self` in the given conformance context.
static bool canDeriveTangentVectorAsSelf(NominalTypeDecl *nominal,
DeclContext *DC) {
// `Self` must not be a class declaration.
if (nominal->getSelfClassDecl())
return false;
auto nominalTypeInContext =
DC->mapTypeIntoContext(nominal->getDeclaredInterfaceType());
auto &C = nominal->getASTContext();
auto *diffableProto = C.getProtocol(KnownProtocolKind::Differentiable);
auto *addArithProto = C.getProtocol(KnownProtocolKind::AdditiveArithmetic);
// `Self` must conform to `AdditiveArithmetic`.
if (!DC->getParentModule()->checkConformance(nominalTypeInContext, addArithProto))
return false;
for (auto *field : nominal->getStoredProperties()) {
// `Self` must not have any `@noDerivative` stored properties.
if (field->getAttrs().hasAttribute<NoDerivativeAttr>())
return false;
// `Self` must have all stored properties satisfy `Self == TangentVector`.
auto fieldType = DC->mapTypeIntoContext(field->getValueInterfaceType());
auto conf = DC->getParentModule()->checkConformance(fieldType, diffableProto);
if (!conf)
return false;
auto tangentType = conf.getTypeWitnessByName(fieldType, C.Id_TangentVector);
if (!fieldType->isEqual(tangentType))
return false;
}
return true;
}
bool DerivedConformance::canDeriveDifferentiable(NominalTypeDecl *nominal,
DeclContext *DC,
ValueDecl *requirement) {
// Experimental differentiable programming must be enabled.
if (auto *SF = DC->getParentSourceFile())
if (!isDifferentiableProgrammingEnabled(*SF))
return false;
auto &C = nominal->getASTContext();
// If there are any `TangentVector` type witness candidates, check whether
// there exists only a single valid candidate.
bool canUseTangentVectorAsSelf = canDeriveTangentVectorAsSelf(nominal, DC);
auto isValidTangentVectorCandidate = [&](ValueDecl *v) -> bool {
// Valid candidate must be a struct or a typealias to a struct.
auto *structDecl = convertToStructDecl(v);
if (!structDecl)
return false;
// Valid candidate must either:
// 1. Be implicit (previously synthesized).
if (structDecl->isImplicit())
return true;
// 2. Equal nominal, when the nominal can derive `TangentVector` as `Self`.
// Nominal type must not customize `TangentVector` to anything other than
// `Self`. Otherwise, synthesis is semantically unsupported.
if (structDecl == nominal && canUseTangentVectorAsSelf)
return true;
// Otherwise, candidate is invalid.
return false;
};
auto tangentDecls = nominal->lookupDirect(C.Id_TangentVector);
// There can be at most one valid `TangentVector` type.
if (tangentDecls.size() > 1)
return false;
// There cannot be any invalid `TangentVector` types.
if (tangentDecls.size() == 1) {
auto *tangentDecl = tangentDecls.front();
if (!isValidTangentVectorCandidate(tangentDecl))
return false;
}
// Check `TangentVector` struct derivation conditions.
// Nominal type must be a struct or class. (No stored properties is okay.)
if (!isa<StructDecl>(nominal) && !isa<ClassDecl>(nominal))
return false;
// If there are no `TangentVector` candidates, derivation is possible if all
// differentiation stored properties conform to `Differentiable`.
SmallVector<VarDecl *, 16> diffProperties;
getStoredPropertiesForDifferentiation(nominal, DC, diffProperties);
auto *diffableProto = C.getProtocol(KnownProtocolKind::Differentiable);
return llvm::all_of(diffProperties, [&](VarDecl *v) {
if (v->getInterfaceType()->hasError())
return false;
auto varType = DC->mapTypeIntoContext(v->getValueInterfaceType());
return (bool) DC->getParentModule()->checkConformance(varType, diffableProto);
});
}
/// Synthesize body for `move(by:)`.
static std::pair<BraceStmt *, bool>
deriveBodyDifferentiable_move(AbstractFunctionDecl *funcDecl, void *) {
auto &C = funcDecl->getASTContext();
auto *parentDC = funcDecl->getParent();
auto *nominal = parentDC->getSelfNominalTypeDecl();
// Get `Differentiable.move(by:)` protocol requirement.
auto *diffProto = C.getProtocol(KnownProtocolKind::Differentiable);
auto *requirement = getProtocolRequirement(diffProto, C.Id_move);
// Get references to `self` and parameter declarations.
auto *selfDecl = funcDecl->getImplicitSelfDecl();
auto *selfDRE =
new (C) DeclRefExpr(selfDecl, DeclNameLoc(), /*Implicit*/ true);
auto *paramDecl = funcDecl->getParameters()->get(0);
auto *paramDRE =
new (C) DeclRefExpr(paramDecl, DeclNameLoc(), /*Implicit*/ true);
SmallVector<VarDecl *, 8> diffProperties;
getStoredPropertiesForDifferentiation(nominal, parentDC, diffProperties);
// Create call expression applying a member `move(by:)` method to a
// parameter member: `self.<member>.move(by: offset.<member>)`.
auto createMemberMethodCallExpr = [&](VarDecl *member) -> Expr * {
auto *module = nominal->getModuleContext();
auto memberType =
parentDC->mapTypeIntoContext(member->getValueInterfaceType());
auto confRef = module->lookupConformance(memberType, diffProto);
assert(confRef && "Member does not conform to `Differentiable`");
// Get member type's requirement witness: `<Member>.move(by:)`.
ValueDecl *memberWitnessDecl = requirement;
if (confRef.isConcrete())
if (auto *witness = confRef.getConcrete()->getWitnessDecl(requirement))
memberWitnessDecl = witness;
assert(memberWitnessDecl && "Member witness declaration must exist");
// Create reference to member method: `self.<member>.move(by:)`.
Expr *memberExpr =
new (C) MemberRefExpr(selfDRE, SourceLoc(), member, DeclNameLoc(),
/*Implicit*/ true);
auto *memberMethodExpr =
new (C) MemberRefExpr(memberExpr, SourceLoc(), memberWitnessDecl,
DeclNameLoc(), /*Implicit*/ true);
// Create reference to parameter member: `offset.<member>`.
VarDecl *paramMember = nullptr;
auto *paramNominal = paramDecl->getTypeInContext()->getAnyNominal();
assert(paramNominal && "Parameter should have a nominal type");
// Find parameter member corresponding to returned nominal member.
for (auto *candidate : paramNominal->getStoredProperties()) {
if (candidate->getName() == member->getName()) {
paramMember = candidate;
break;
}
}
assert(paramMember && "Could not find corresponding parameter member");
auto *paramMemberExpr =
new (C) MemberRefExpr(paramDRE, SourceLoc(), paramMember, DeclNameLoc(),
/*Implicit*/ true);
// Create expression: `self.<member>.move(by: offset.<member>)`.
auto *args = ArgumentList::forImplicitSingle(C, C.Id_by, paramMemberExpr);
return CallExpr::createImplicit(C, memberMethodExpr, args);
};
// Collect member `move(by:)` method call expressions.
SmallVector<ASTNode, 2> memberMethodCallExprs;
SmallVector<Identifier, 2> memberNames;
for (auto *member : diffProperties) {
memberMethodCallExprs.push_back(createMemberMethodCallExpr(member));
memberNames.push_back(member->getName());
}
auto *braceStmt = BraceStmt::create(C, SourceLoc(), memberMethodCallExprs,
SourceLoc(), true);
return std::pair<BraceStmt *, bool>(braceStmt, false);
}
/// Synthesize function declaration for a `Differentiable` method requirement.
static ValueDecl *deriveDifferentiable_method(
DerivedConformance &derived, Identifier methodName, Identifier argumentName,
Identifier parameterName, Type parameterType, Type returnType,
AbstractFunctionDecl::BodySynthesizer bodySynthesizer) {
auto *nominal = derived.Nominal;
auto &C = derived.Context;
auto *parentDC = derived.getConformanceContext();
auto *param = new (C) ParamDecl(SourceLoc(), SourceLoc(), argumentName,
SourceLoc(), parameterName, parentDC);
param->setSpecifier(ParamDecl::Specifier::Default);
param->setInterfaceType(parameterType);
param->setImplicit();
ParameterList *params = ParameterList::create(C, {param});
DeclName declName(C, methodName, params);
auto *const funcDecl = FuncDecl::createImplicit(
C, StaticSpellingKind::None, declName, /*NameLoc=*/SourceLoc(),
/*Async=*/false,
/*Throws=*/false,
/*ThrownType=*/Type(),
/*GenericParams=*/nullptr, params, returnType, parentDC);
funcDecl->setSynthesized();
if (!nominal->getSelfClassDecl())
funcDecl->setSelfAccessKind(SelfAccessKind::Mutating);
funcDecl->setBodySynthesizer(bodySynthesizer.Fn, bodySynthesizer.Context);
funcDecl->setGenericSignature(parentDC->getGenericSignatureOfContext());
funcDecl->copyFormalAccessFrom(nominal, /*sourceIsParentContext*/ true);
derived.addMembersToConformanceContext({funcDecl});
return funcDecl;
}
/// Synthesize the `move(by:)` function declaration.
static ValueDecl *deriveDifferentiable_move(DerivedConformance &derived) {
auto &C = derived.Context;
auto *parentDC = derived.getConformanceContext();
auto tangentType =
getTangentVectorInterfaceType(parentDC->getSelfTypeInContext(), parentDC);
return deriveDifferentiable_method(
derived, C.Id_move, C.Id_by, C.Id_offset, tangentType,
C.TheEmptyTupleType, {deriveBodyDifferentiable_move, nullptr});
}
/// Return associated `TangentVector` struct for a nominal type, if it exists.
/// If not, synthesize the struct.
static StructDecl *
getOrSynthesizeTangentVectorStruct(DerivedConformance &derived, Identifier id) {
auto *parentDC = derived.getConformanceContext();
auto *nominal = derived.Nominal;
auto &C = nominal->getASTContext();
// If the associated struct already exists, return it.
auto lookup = nominal->lookupDirect(C.Id_TangentVector);
assert(lookup.size() < 2 &&
"Expected at most one associated type named `TangentVector`");
if (lookup.size() == 1) {
auto *structDecl = convertToStructDecl(lookup.front());
assert(structDecl && "Expected lookup result to be a struct");
return structDecl;
}
// Otherwise, synthesize a new struct.
// Compute `tvDesiredProtos`, the set of protocols that the new `TangentVector` struct must
// inherit, by collecting all the `TangentVector` conformance requirements imposed by the
// protocols that `derived.ConformanceDecl` inherits.
//
// Note that, for example, this will always find `AdditiveArithmetic` and `Differentiable` because
// the `Differentiable` protocol itself requires that its `TangentVector` conforms to
// `AdditiveArithmetic` and `Differentiable`.
llvm::SmallSetVector<ProtocolDecl *, 4> tvDesiredProtos;
auto *diffableProto = C.getProtocol(KnownProtocolKind::Differentiable);
auto *tvAssocType = diffableProto->getAssociatedType(C.Id_TangentVector);
auto localProtos = cast<IterableDeclContext>(derived.ConformanceDecl)
->getLocalProtocols();
for (auto proto : localProtos) {
for (auto req : proto->getRequirementSignature().getRequirements()) {
if (req.getKind() != RequirementKind::Conformance)
continue;
auto *firstType = req.getFirstType()->getAs<DependentMemberType>();
if (!firstType || firstType->getAssocType() != tvAssocType)
continue;
tvDesiredProtos.insert(req.getProtocolDecl());
}
}
SmallVector<InheritedEntry, 4> tvDesiredProtoInherited;
for (auto *p : tvDesiredProtos)
tvDesiredProtoInherited.push_back(
InheritedEntry(TypeLoc::withoutLoc(p->getDeclaredInterfaceType())));
// Cache original members and their associated types for later use.
SmallVector<VarDecl *, 8> diffProperties;
getStoredPropertiesForDifferentiation(nominal, parentDC, diffProperties);
auto synthesizedLoc = derived.ConformanceDecl->getEndLoc();
auto *structDecl =
new (C) StructDecl(synthesizedLoc, C.Id_TangentVector, synthesizedLoc,
/*Inherited*/ C.AllocateCopy(tvDesiredProtoInherited),
/*GenericParams*/ {}, parentDC);
structDecl->setBraces({synthesizedLoc, synthesizedLoc});
structDecl->setImplicit();
structDecl->setSynthesized();
structDecl->copyFormalAccessFrom(nominal, /*sourceIsParentContext*/ true);
// Add stored properties to the `TangentVector` struct.
for (auto *member : diffProperties) {
// Add a tangent stored property to the `TangentVector` struct, with the
// name and `TangentVector` type of the original property.
auto *tangentProperty = new (C) VarDecl(
member->isStatic(), member->getIntroducer(),
/*NameLoc*/ SourceLoc(), member->getName(), structDecl);
// Note: `tangentProperty` is not marked as implicit or synthesized here,
// because that incorrectly affects memberwise initializer synthesis and
// causes the type checker to not guarantee the order of these members.
auto memberContextualType =
parentDC->mapTypeIntoContext(member->getValueInterfaceType());
auto memberTanType =
getTangentVectorInterfaceType(memberContextualType, parentDC);
tangentProperty->setInterfaceType(memberTanType);
Pattern *memberPattern =
NamedPattern::createImplicit(C, tangentProperty, memberTanType);
memberPattern =
TypedPattern::createImplicit(C, memberPattern, memberTanType);
memberPattern->setType(memberTanType);
auto *memberBinding = PatternBindingDecl::createImplicit(
C, StaticSpellingKind::None, memberPattern, /*initExpr*/ nullptr,
structDecl);
structDecl->addMember(tangentProperty);
structDecl->addMember(memberBinding);
tangentProperty->copyFormalAccessFrom(member,
/*sourceIsParentContext*/ true);
tangentProperty->setSetterAccess(member->getFormalAccess());
// Cache the tangent property.
C.evaluator.cacheOutput(TangentStoredPropertyRequest{member, CanType()},
TangentPropertyInfo(tangentProperty));
// Now that the original property has a corresponding tangent property, it
// should be marked `@differentiable` so that the differentiation transform
// will synthesize derivative functions for its accessors. We only add this
// to public stored properties, because their access outside the module will
// go through accessor declarations.
if (member->getEffectiveAccess() > AccessLevel::Internal &&
!member->getAttrs().hasAttribute<DifferentiableAttr>()) {
auto *getter = member->getSynthesizedAccessor(AccessorKind::Get);
(void)getter->getInterfaceType();
// If member or its getter already has a `@differentiable` attribute,
// continue.
if (member->getAttrs().hasAttribute<DifferentiableAttr>() ||
getter->getAttrs().hasAttribute<DifferentiableAttr>())
continue;
GenericSignature derivativeGenericSignature =
getter->getGenericSignature();
// If the parent declaration context is an extension, the nominal type may
// conditionally conform to `Differentiable`. Use the extension generic
// requirements in getter `@differentiable` attributes.
if (auto *extDecl = dyn_cast<ExtensionDecl>(parentDC->getAsDecl()))
if (auto extGenSig = extDecl->getGenericSignature())
derivativeGenericSignature = extGenSig;
auto *diffableAttr = DifferentiableAttr::create(
getter, /*implicit*/ true, SourceLoc(), SourceLoc(),
DifferentiabilityKind::Reverse,
/*parameterIndices*/ IndexSubset::get(C, 1, {0}),
derivativeGenericSignature);
member->getAttrs().add(diffableAttr);
}
}
// If nominal type is `@frozen`, also mark `TangentVector` struct.
if (nominal->getAttrs().hasAttribute<FrozenAttr>())
structDecl->getAttrs().add(new (C) FrozenAttr(/*implicit*/ true));
// Add `typealias TangentVector = Self` so that the `TangentVector` itself
// won't need its own conformance derivation.
auto *tangentEqualsSelfAlias = new (C) TypeAliasDecl(
SourceLoc(), SourceLoc(), C.Id_TangentVector, SourceLoc(),
/*GenericParams*/ nullptr, structDecl);
tangentEqualsSelfAlias->setUnderlyingType(structDecl->getDeclaredInterfaceType());
tangentEqualsSelfAlias->copyFormalAccessFrom(structDecl,
/*sourceIsParentContext*/ true);
tangentEqualsSelfAlias->setImplicit();
tangentEqualsSelfAlias->setSynthesized();
structDecl->addMember(tangentEqualsSelfAlias);
// The implicit memberwise constructor must be explicitly created so that it
// can called in `AdditiveArithmetic` and `Differentiable` methods. Normally,
// the memberwise constructor is synthesized during SILGen, which is too late.
TypeChecker::addImplicitConstructors(structDecl);
// After memberwise initializer is synthesized, mark members as implicit.
for (auto *member : structDecl->getStoredProperties())
member->setImplicit();
derived.addMembersToConformanceContext({structDecl});
TypeChecker::checkConformancesInContext(structDecl);
return structDecl;
}
/// Diagnose stored properties in the nominal that do not have an explicit
/// `@noDerivative` attribute, but either:
/// - Do not conform to `Differentiable`.
/// - Are a `let` stored property.
/// Emit a warning and a fixit so that users will make the attribute explicit.
static void checkAndDiagnoseImplicitNoDerivative(ASTContext &Context,
NominalTypeDecl *nominal,
DeclContext *DC) {
// If nominal type can conform to `AdditiveArithmetic`, suggest adding a
// conformance to `AdditiveArithmetic` in fix-its.
// `Differentiable` protocol requirements all have default implementations
// when `Self` conforms to `AdditiveArithmetic`, so `Differentiable`
// derived conformances will no longer be necessary.
bool nominalCanDeriveAdditiveArithmetic =
DerivedConformance::canDeriveAdditiveArithmetic(nominal, DC);
auto *diffableProto = Context.getProtocol(KnownProtocolKind::Differentiable);
// Check all stored properties.
for (auto *vd : nominal->getStoredProperties()) {
// Peer through property wrappers: use original wrapped properties.
if (auto *originalProperty = vd->getOriginalWrappedProperty()) {
// Skip wrapped properties with `@noDerivative` attribute.
if (originalProperty->getAttrs().hasAttribute<NoDerivativeAttr>())
continue;
// Diagnose wrapped properties whose property wrappers do not define
// `wrappedValue.set`. `mutating func move(by:)` cannot be synthesized
// to update these properties.
if (!originalProperty->isSettable(DC)) {
auto *wrapperDecl =
vd->getInterfaceType()->getNominalOrBoundGenericNominal();
auto loc =
originalProperty->getAttributeInsertionLoc(/*forModifier*/ false);
Context.Diags
.diagnose(
loc,
diag::
differentiable_immutable_wrapper_implicit_noderivative_fixit,
wrapperDecl->getName(), nominal->getName(),
nominalCanDeriveAdditiveArithmetic)
.fixItInsert(loc, "@noDerivative ");
// Add an implicit `@noDerivative` attribute.
originalProperty->getAttrs().add(
new (Context) NoDerivativeAttr(/*Implicit*/ true));
continue;
}
// Use the original wrapped property.
vd = originalProperty;
}
if (vd->getInterfaceType()->hasError())
continue;
// Skip stored properties with `@noDerivative` attribute.
if (vd->getAttrs().hasAttribute<NoDerivativeAttr>())
continue;
// Check whether to diagnose stored property.
auto varType = DC->mapTypeIntoContext(vd->getValueInterfaceType());
auto diffableConformance =
DC->getParentModule()->checkConformance(varType, diffableProto);
// If stored property should not be diagnosed, continue.
if (diffableConformance &&
canInvokeMoveByOnProperty(vd, diffableConformance))
continue;
// Otherwise, add an implicit `@noDerivative` attribute.
vd->getAttrs().add(new (Context) NoDerivativeAttr(/*Implicit*/ true));
auto loc = vd->getAttributeInsertionLoc(/*forModifier*/ false);
assert(loc.isValid() && "Expected valid source location");
// Diagnose properties that do not conform to `Differentiable`.
if (!diffableConformance) {
Context.Diags
.diagnose(
loc,
diag::differentiable_nondiff_type_implicit_noderivative_fixit,
vd->getName(), vd->getTypeInContext(), nominal->getName(),
nominalCanDeriveAdditiveArithmetic)
.fixItInsert(loc, "@noDerivative ");
continue;
}
// Otherwise, diagnose `let` property.
Context.Diags
.diagnose(loc,
diag::differentiable_let_property_implicit_noderivative_fixit,
nominal->getName(), nominalCanDeriveAdditiveArithmetic)
.fixItInsert(loc, "@noDerivative ");
}
}
/// Get or synthesize `TangentVector` struct type.
static std::pair<Type, TypeDecl *>
getOrSynthesizeTangentVectorStructType(DerivedConformance &derived) {
auto *parentDC = derived.getConformanceContext();
auto *nominal = derived.Nominal;
auto &C = nominal->getASTContext();
// Get or synthesize `TangentVector` struct.
auto *tangentStruct =
getOrSynthesizeTangentVectorStruct(derived, C.Id_TangentVector);
if (!tangentStruct)
return std::make_pair(nullptr, nullptr);
// Check and emit warnings for implicit `@noDerivative` members.
checkAndDiagnoseImplicitNoDerivative(C, nominal, parentDC);
// Return the `TangentVector` struct type.
return std::make_pair(
parentDC->mapTypeIntoContext(
tangentStruct->getDeclaredInterfaceType()),
tangentStruct);
}
/// Synthesize the `TangentVector` struct type.
static std::pair<Type, TypeDecl *>
deriveDifferentiable_TangentVectorStruct(DerivedConformance &derived) {
auto *parentDC = derived.getConformanceContext();
auto *nominal = derived.Nominal;
// If nominal type can derive `TangentVector` as the contextual `Self` type,
// return it.
if (canDeriveTangentVectorAsSelf(nominal, parentDC))
return std::make_pair(parentDC->getSelfTypeInContext(), nullptr);
// Otherwise, get or synthesize `TangentVector` struct type.
return getOrSynthesizeTangentVectorStructType(derived);
}
ValueDecl *DerivedConformance::deriveDifferentiable(ValueDecl *requirement) {
// Diagnose unknown requirements.
if (requirement->getBaseName() != Context.Id_move) {
Context.Diags.diagnose(requirement->getLoc(),
diag::broken_differentiable_requirement);
return nullptr;
}
// Diagnose conformances in disallowed contexts.
if (checkAndDiagnoseDisallowedContext(requirement))
return nullptr;
// Start an error diagnostic before attempting derivation.
// If derivation succeeds, cancel the diagnostic.
DiagnosticTransaction diagnosticTransaction(Context.Diags);
ConformanceDecl->diagnose(diag::type_does_not_conform,
Nominal->getDeclaredType(), getProtocolType());
requirement->diagnose(diag::no_witnesses,
getProtocolRequirementKind(requirement),
requirement, getProtocolType(), /*AddFixIt=*/false);
// If derivation is possible, cancel the diagnostic and perform derivation.
if (canDeriveDifferentiable(Nominal, getConformanceContext(), requirement)) {
diagnosticTransaction.abort();
if (requirement->getBaseName() == Context.Id_move)
return deriveDifferentiable_move(*this);
}
// Otherwise, return nullptr.
return nullptr;
}
std::pair<Type, TypeDecl *>
DerivedConformance::deriveDifferentiable(AssociatedTypeDecl *requirement) {
// Diagnose unknown requirements.
if (requirement->getBaseName() != Context.Id_TangentVector) {
Context.Diags.diagnose(requirement->getLoc(),
diag::broken_differentiable_requirement);
return std::make_pair(nullptr, nullptr);
}
// Start an error diagnostic before attempting derivation.
// If derivation succeeds, cancel the diagnostic.
DiagnosticTransaction diagnosticTransaction(Context.Diags);
ConformanceDecl->diagnose(diag::type_does_not_conform,
Nominal->getDeclaredType(), getProtocolType());
requirement->diagnose(diag::no_witnesses_type, requirement);
// If derivation is possible, cancel the diagnostic and perform derivation.
if (canDeriveDifferentiable(Nominal, getConformanceContext(), requirement)) {
diagnosticTransaction.abort();
return deriveDifferentiable_TangentVectorStruct(*this);
}
// Otherwise, return nullptr.
return std::make_pair(nullptr, nullptr);
}
|