Skip to content

[AutoDiff] Revamp differentiation transform. #24845

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
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
6 changes: 3 additions & 3 deletions include/swift/AST/AutoDiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +578,12 @@ bool getBuiltinAutoDiffApplyConfig(StringRef operationName,
unsigned &arity, unsigned &order,
bool &rethrows);

/// Computes the correct linkage for associated functions given the linkage of
/// Computes the correct linkage for an associated function given the linkage of
/// the original function. If the original linkage is not external and
/// `isAssocFnExported` is true, use the original function's linkage. Otherwise,
/// return hidden linkage.
SILLinkage getAutoDiffFunctionLinkage(SILLinkage originalLinkage,
bool isAssocFnExported);
SILLinkage getAutoDiffAssociatedFunctionLinkage(SILLinkage originalLinkage,
bool isAssocFnExported);

} // end namespace autodiff

Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -4153,7 +4153,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
AutoDiffIndexSubset *parameterIndices, unsigned resultIndex,
unsigned differentiationOrder, AutoDiffAssociatedFunctionKind kind,
SILModule &module, LookupConformanceFn lookupConformance,
GenericSignature *whereClauseGenericSignature = nullptr);
CanGenericSignature whereClauseGenericSignature = nullptr);

/// Returns a bit vector that specifices which parameters you can
/// differentiate with respect to for this differentiable function type. (e.g.
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/AutoDiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ bool autodiff::getBuiltinAutoDiffApplyConfig(
return operationName.empty();
}

SILLinkage autodiff::getAutoDiffFunctionLinkage(SILLinkage originalLinkage,
bool isAssocFnExported) {
SILLinkage autodiff::getAutoDiffAssociatedFunctionLinkage(
SILLinkage originalLinkage, bool isAssocFnExported) {
// If the original is defined externally, then the AD pass is just generating
// associated functions for use in the current module and therefore these
// associated functions should not be visible outside the module.
Expand Down
6 changes: 3 additions & 3 deletions lib/SIL/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,18 @@ CanSILFunctionType SILFunctionType::getAutoDiffAssociatedFunctionType(
AutoDiffIndexSubset *parameterIndices, unsigned resultIndex,
unsigned differentiationOrder, AutoDiffAssociatedFunctionKind kind,
SILModule &module, LookupConformanceFn lookupConformance,
GenericSignature *whereClauseGenSig) {
CanGenericSignature whereClauseGenSig) {
// JVP: (T...) -> ((R...),
// (T.TangentVector...) -> (R.TangentVector...))
// VJP: (T...) -> ((R...),
// (R.TangentVector...) -> (T.TangentVector...))

auto &ctx = getASTContext();
auto &typeConverter = module.Types;
Lowering::GenericContextScope
genericContextScope(module.Types, getGenericSignature());
if (!whereClauseGenSig)
whereClauseGenSig = getGenericSignature();
Lowering::GenericContextScope genericContextScope(
module.Types, whereClauseGenSig);

// Given a type, returns its formal SIL parameter info.
auto getTangentParameterInfoForOriginalResult = [&](
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3530,7 +3530,7 @@ SILGenModule::getOrCreateAutoDiffAssociatedFunctionReorderingThunk(

auto loc = assocFn->getLocation();
SILGenFunctionBuilder fb(*this);
auto linkage = autodiff::getAutoDiffFunctionLinkage(
auto linkage = autodiff::getAutoDiffAssociatedFunctionLinkage(
original->getLinkage(), /*isAssocFnExported*/ true);
auto *thunk = fb.getOrCreateFunction(
loc, name, linkage, targetType, IsBare, IsNotTransparent,
Expand Down
19 changes: 13 additions & 6 deletions lib/SILOptimizer/IPO/CapturePropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,20 @@ bool CapturePropagation::optimizePartialApply(PartialApplyInst *PAI) {
SILOptFunctionBuilder FuncBuilder(*this);
if (auto *NewFunc = getSpecializedWithDeadParams(FuncBuilder,
PAI, SubstF, PAI->getNumArguments(), GenericSpecialized)) {
rewritePartialApply(PAI, NewFunc);
if (GenericSpecialized.first) {
// Notify the pass manager about the new function.
addFunctionToPassManagerWorklist(GenericSpecialized.first,
GenericSpecialized.second);
// SWIFT_ENABLE_TENSORFLOW
// Add a previously unexercised check to prevent AD crash. Rewrite
// `partial_apply` only if the specialized function is `@convention(thin)`.
// Revert check when `VJPEmitter::visitApplyInst` no longer produces
// argumentless `partial_apply` instructions.
if (NewFunc->getRepresentation() == SILFunctionTypeRepresentation::Thin) {
rewritePartialApply(PAI, NewFunc);
if (GenericSpecialized.first) {
// Notify the pass manager about the new function.
addFunctionToPassManagerWorklist(GenericSpecialized.first,
GenericSpecialized.second);
}
return true;
}
return true;
}

// Second possibility: Are all partially applied arguments constant?
Expand Down
Loading