Skip to content

Commit 9511f40

Browse files
authored
[AutoDiff] [NFC] Mark places to diagnose "no AdditiveArithmetic conformance". (#22620)
Mark places to diagnose "no `AdditiveArithmetic` conformance" in differentiation pass. Setup for fixing TF-202.
1 parent a3cb68e commit 9511f40

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

lib/SILOptimizer/Mandatory/Differentiation.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4604,7 +4604,7 @@ void AdjointEmitter::materializeAdjointIndirectHelper(
46044604

46054605
void AdjointEmitter::emitZeroIndirect(CanType type, SILValue bufferAccess,
46064606
SILLocation loc) {
4607-
// Lookup `AdditiveArithmetic.zero.getter`.
4607+
// Look up `AdditiveArithmetic.zero.getter`.
46084608
auto *additiveArithmeticProto =
46094609
getASTContext().getProtocol(KnownProtocolKind::AdditiveArithmetic);
46104610
auto initDeclLookup =
@@ -4615,20 +4615,21 @@ void AdjointEmitter::emitZeroIndirect(CanType type, SILValue bufferAccess,
46154615
SILDeclRef accessorDeclRef(accessorDecl, SILDeclRef::Kind::Func);
46164616
auto methodType =
46174617
getContext().getTypeConverter().getConstantType(accessorDeclRef);
4618-
// Lookup conformance to `AdditiveArithmetic`.
4618+
// Look up conformance to `AdditiveArithmetic`.
46194619
auto *swiftMod = getModule().getSwiftModule();
4620-
auto conf = swiftMod->lookupConformance(type, additiveArithmeticProto);
4621-
assert(conf.hasValue() && "No conformance to AdditiveArithmetic?");
4622-
ProtocolConformanceRef confRef(*conf);
4620+
auto confRef = swiftMod->lookupConformance(type, additiveArithmeticProto);
4621+
// TODO(TF-202): Diagnose no `AdditiveArithmetic` due to generic signature
4622+
// minimization bug.
4623+
assert(confRef.hasValue() && "Missing conformance to `AdditiveArithmetic`");
46234624
// %wm = witness_method ...
4624-
auto *getter = builder.createWitnessMethod(loc, type, confRef,
4625+
auto *getter = builder.createWitnessMethod(loc, type, *confRef,
46254626
accessorDeclRef, methodType);
46264627
// %metatype = metatype $T
46274628
auto metatypeType = CanMetatypeType::get(type, MetatypeRepresentation::Thick);
46284629
auto metatype = builder.createMetatype(
46294630
loc, SILType::getPrimitiveObjectType(metatypeType));
46304631
auto subMap = SubstitutionMap::getProtocolSubstitutions(
4631-
additiveArithmeticProto, type, confRef);
4632+
additiveArithmeticProto, type, *confRef);
46324633
builder.createApply(loc, getter, subMap, {bufferAccess, metatype},
46334634
/*isNonThrowing*/ false);
46344635
}
@@ -4838,14 +4839,18 @@ void AdjointEmitter::accumulateIndirect(
48384839
auto adjointParentModule = cotangentSpace->getNominal()
48394840
? cotangentSpace->getNominal()->getModuleContext()
48404841
: getModule().getSwiftModule();
4841-
auto confRef = *adjointParentModule->lookupConformance(adjointASTTy, proto);
4842+
auto confRef = adjointParentModule->lookupConformance(adjointASTTy, proto);
4843+
// TODO(TF-202): Diagnose no `AdditiveArithmetic` due to generic signature
4844+
// minimization bug.
4845+
assert(confRef.hasValue() && "Missing conformance to `AdditiveArithmetic`");
48424846
SILDeclRef declRef(combinerFuncDecl, SILDeclRef::Kind::Func);
48434847
auto silFnTy = getContext().getTypeConverter().getConstantType(declRef);
48444848
// %0 = witness_method @+
48454849
auto witnessMethod = builder.createWitnessMethod(loc, adjointASTTy,
4846-
confRef, declRef, silFnTy);
4847-
auto subMap =
4848-
SubstitutionMap::getProtocolSubstitutions(proto, adjointASTTy, confRef);
4850+
*confRef, declRef,
4851+
silFnTy);
4852+
auto subMap = SubstitutionMap::getProtocolSubstitutions(
4853+
proto, adjointASTTy, *confRef);
48494854
// %1 = metatype $T.Type
48504855
auto metatypeType =
48514856
CanMetatypeType::get(adjointASTTy, MetatypeRepresentation::Thick);
@@ -4891,14 +4896,15 @@ void AdjointEmitter::accumulateIndirect(SILValue lhsDestAccess,
48914896
auto *proto = getContext().getAdditiveArithmeticProtocol();
48924897
auto *accumulatorFuncDecl = getContext().getPlusEqualDecl();
48934898
// Call the combiner function and return.
4894-
auto confRef = *swiftMod->lookupConformance(astType, proto);
4899+
auto confRef = swiftMod->lookupConformance(astType, proto);
4900+
assert(confRef.hasValue() && "Missing conformance to `AdditiveArithmetic`");
48954901
SILDeclRef declRef(accumulatorFuncDecl, SILDeclRef::Kind::Func);
48964902
auto silFnTy = getContext().getTypeConverter().getConstantType(declRef);
48974903
// %0 = witness_method @+=
48984904
auto witnessMethod =
4899-
builder.createWitnessMethod(loc, astType, confRef, declRef, silFnTy);
4905+
builder.createWitnessMethod(loc, astType, *confRef, declRef, silFnTy);
49004906
auto subMap =
4901-
SubstitutionMap::getProtocolSubstitutions(proto, astType, confRef);
4907+
SubstitutionMap::getProtocolSubstitutions(proto, astType, *confRef);
49024908
// %1 = metatype $T.Type
49034909
auto metatypeType =
49044910
CanMetatypeType::get(astType, MetatypeRepresentation::Thick);

0 commit comments

Comments
 (0)