Adding a "does X conform to Y" request

Hi compiler gurus,

I wonder if there are plans to add a "does X conform to Y" request to the request evaluator system (or other ways to unify conformance lookup logic)?

I'm curious because there multiple ways to perform conformance lookup (and find associated type witnesses), some of which have caused bugs for the differentiable programming project, which needs Differentiable conformance lookup and associated type witness lookup during Sema, SILGen, and SIL transforms.

In my experience, the static function TypeChecker::conformsToProtocol has been the most reliable way of obtaining a conformance (over ModuleDecl::lookupConformance and ModuleDecl::conformsToProtocol), but it is available only during Sema, not during SIL.

It's totally possible that the current system is fine and we're just using bad APIs - TypeBase::getAutoDiffAssociatedTangentSpace(LookupConformanceFn lookupConformance) is one of the worst offenders, using DependentMemberType::substBaseType and a LookupConformanceFn to find associated type witnesses.

If anyone knows better ways for finding associated type witnesses (via APIs that work from SIL, not just Sema), that would be a big help too.

cc @Slava_Pestov @Douglas_Gregor

TypeChecker::conformsToProtocol() is a static method so you can totally call it from SIL as well as Sema. Even non-static methods can be used now because the TypeChecker instance lives forever (but we're trying to gut and eventually remove the TypeChecker singleton altogether).

The only difference between lookupConformance() and conformsToProtocol() is the latter records a dependency for incremental builds, and checks conditional requirements. We want to recover incremental dependencies from the request graph one day. Once the remaining distinction between the two is conditional conformance checking, it would be great to merge them into a single request or function.

I'm wondering what kind of issue you encountered with the other two entry points though. lookupConformnace() is the underlying operation used by TypeChecker::conformsToProtocol(), and ModuleDecl::conformsToProtocol() just calls TypeChecker::conformsToProtocol().

1 Like

The issue is TypeChecker.h lives in lib/Sema instead of include/swift/Sema, so it can't be included/called from SIL.

Could we please move TypeChecker.h to include/swift/Sema? That directory is currently mostly empty for some reason, maybe it's intentional for separation of concerns.

Thanks for the clarification about lookupConformance and conformsToProtocol!

I can't remember specific unresolved issues with ModuleDecl::lookupConformance in SIL but I'll make a note if I come across any. (You previously helped with a SubstitutionMap question - thanks again.)

Type-checking @differentiable attributes where clauses (looking up Differentiable protocol conformances using the right APIs and arguments) has been a longstanding source of issues though. I posted a separate question about @differentiable attribute and cc'd you.