Skip to content

[AutoDiff] NFC: prettify commutative diagrams. #31525

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 4, 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
31 changes: 16 additions & 15 deletions include/swift/SIL/TypeSubstCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,28 +322,29 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
return;
}
// If the extractee is a derivative function, check whether the *remapped
// derivative function type* (BC) is equal to the *derivative remapped
// function type* (AD).
// derivative function type* (bc) is equal to the *derivative remapped
// function type* (ad).
//
// +----------------+ remap +-------------------------+
// | orig. fn type | -------(A)------> | remapped orig. fn type |
// +----------------+ +-------------------------+
// | |
// (B, SILGen) getAutoDiffDerivativeFunctionType (D, here)
// V V
// +----------------+ remap +-------------------------+
// | deriv. fn type | -------(C)------> | remapped deriv. fn type |
// +----------------+ +-------------------------+
// ┌────────────────┐ remap ┌─────────────────────────┐
// │ orig. fn type │ ───────(a)──────► │ remapped orig. fn type │
// └────────────────┘ └─────────────────────────┘
// │ │
// (b, SILGen) getAutoDiffDerivativeFunctionType (d, here)
// │ │
// ▼ ▼
// ┌────────────────┐ remap ┌─────────────────────────┐
// │ deriv. fn type │ ───────(c)──────► │ remapped deriv. fn type │
// └────────────────┘ └─────────────────────────┘
//
// (AD) does not always commute with (BC):
// - (AD) is the result of remapping, then computing the derivative type.
// (ad) does not always commute with (bc):
// - (ad) is the result of remapping, then computing the derivative type.
// This is the default cloning behavior, but may break invariants in the
// initial SIL generated by SILGen.
// - (BC) is the result of computing the derivative type (SILGen), then
// - (bc) is the result of computing the derivative type (SILGen), then
// remapping. This is the expected type, preserving invariants from
// earlier transforms.
//
// If (AD) is not equal to (BC), use (BC) as the explicit type.
// If (ad) is not equal to (bc), use (bc) as the explicit type.
SILType remappedOrigType = getOpType(dfei->getOperand()->getType());
auto remappedOrigFnType = remappedOrigType.castTo<SILFunctionType>();
auto derivativeRemappedFnType =
Expand Down
31 changes: 16 additions & 15 deletions lib/SIL/IR/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3088,30 +3088,31 @@ TypeConverter::getConstantInfo(TypeExpansionContext expansion,
// If the constant refers to a derivative function, get the SIL type of the
// original function and use it to compute the derivative SIL type.
//
// This is necessary because the "lowered AST derivative function type" (BC)
// This is necessary because the "lowered AST derivative function type" (bc)
// may differ from the "derivative type of the lowered original function type"
// (AD):
// (ad):
//
// +--------------------+ lowering +--------------------+
// | AST orig. fn type | -------(A)------> | SIL orig. fn type |
// +--------------------+ +--------------------+
// | |
// (B, Sema) getAutoDiffDerivativeFunctionType (D, here)
// V V
// +--------------------+ lowering +--------------------+
// | AST deriv. fn type | -------(C)------> | SIL deriv. fn type |
// +--------------------+ +--------------------+
// ┌────────────────────┐ lowering ┌────────────────────┐
// │ AST orig. fn type │ ───────(a)──────► │ SIL orig. fn type │
// └────────────────────┘ └────────────────────┘
// │ │
// (b, Sema) getAutoDiffDerivativeFunctionType (d, here)
// │ │
// ▼ ▼
// ┌────────────────────┐ lowering ┌────────────────────┐
// │ AST deriv. fn type │ ───────(c)──────► │ SIL deriv. fn type │
// └────────────────────┘ └────────────────────┘
//
// (AD) does not always commute with (BC):
// - (BC) is the result of computing the AST derivative type (Sema), then
// (ad) does not always commute with (bc):
// - (bc) is the result of computing the AST derivative type (Sema), then
// lowering it via SILGen. This is the default lowering behavior, but may
// break SIL typing invariants because expected lowered derivative types are
// computed from lowered original function types.
// - (AD) is the result of lowering the original function type, then computing
// - (ad) is the result of lowering the original function type, then computing
// its derivative type. This is the expected lowered derivative type,
// preserving SIL typing invariants.
//
// Always use (AD) to compute lowered derivative function types.
// Always use (ad) to compute lowered derivative function types.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice autodiff pun: Always use (ad)

if (auto *derivativeId = constant.derivativeFunctionIdentifier) {
// Get lowered original function type.
auto origFnConstantInfo = getConstantInfo(
Expand Down