-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff] DifferentiabilityWitnessFunctionInst stores pointer to witness #27919
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
[AutoDiff] DifferentiabilityWitnessFunctionInst stores pointer to witness #27919
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unifying syntax for sil_differentiability_witness
and differentiability_witness_function
instruction, and avoiding forward declarations SGTM!
@swift-ci please test tensorflow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Thanks for doing this!
lib/ParseSIL/ParseSIL.cpp
Outdated
// Check whether original function generic signature and parsed witness | ||
// generic have the same generic parameters. | ||
auto areGenericParametersConsistent = [&]() { | ||
llvm::DenseSet<GenericParamKey> genericParamKeys; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
llvm::SmallDenseSet
?
dwfi->getParameterIndices()->getNumIndices(), | ||
dwfi->getResultIndices()->getNumIndices(), | ||
parameterAndResultIndices); | ||
SILOneOperandLayout::emitRecord( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this!
lib/ParseSIL/ParseSIL.cpp
Outdated
auto *resultIndexSet = IndexSubset::get( | ||
P.Context, origFnType->getNumResults(), resultIndices); | ||
AutoDiffConfig config(parameterIndexSet, resultIndexSet, witnessGenSig); | ||
return {{config, originalFunction}}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double curly braces are a bit cryptic. How about:
return {{config, originalFunction}}; | |
return std::make_pair(config, originalFunction); |
Please feel free to merge it now and address my minor comments later. I don't want to block progress getting retrodiff landed! |
@swift-ci please test tensorflow |
1 similar comment
@swift-ci please test tensorflow |
@swift-ci please test tensorflow macos |
Remove unused variable `Attr3`. It is no longer necessary after swiftlang#27919.
Remove unused variable `Attr3`. It is no longer necessary after #27919.
We want the
DifferentiabilityWitnessFunctionInst
to store a pointer to the witness because this makes the instruction data structure smaller and simpler, and makes misuse (e.g. referencing a witness that's not declared in the SIL module) harder.This PR mostly preserves the existing SIL syntax & rules. The changes are:
sil_differentiability_witness
declarations are now required to come beforedifferentiability_witness_function
instructions that reference them, so that we don't have to add "forward declaration" logic for them.sil_differentiability_witness
constraints now look like<τ_0_0 where τ_0_0 : _Differentiable>
instead of[where T : _Differentiable]
. This allows me to share parsing logic between thesil_differentiability_witness
constraints and thedifferentiability_witness_function
, which simplifies the code and also avoids a problem [1].This PR changes the
DifferentiabilityWitnessFunctionInst
serialization format to store the witness mangled name rather than the original function name plus indices and generic constraints.Detailed outline of changes:
DifferentiabilityWitnessFunctionInst
now contains aSILDifferentiabilityWitness*
instead of a pointer to original function, indices, and generic signature. (credit to @dan-zheng, I just copied these changes from his commit)SILModule
for looking upSILDifferentiabilityWitness*
because parsing and deserialization need to find the rightSILDifferentiabilityWitness*
to put in the inst.sil_differentiability_witness
declarations before function declarations<T where T ...>
instead of[where T ...]
insil_differentiability_witness
declarations.parseSILDifferentiabilityWitnessConfigAndFunction
, and use this for parsing bothsil_differentiability_witness
anddifferentiability_witness_function
. This simplifies the code and avoids the problem [1].differentiability_witness_function
, look for theSILDifferentiabilityWitness*
in the module.sil_differentiability_witness
declarations that come at the beginning of the file.SILInstDifferentiabilityWitnessFunctionLayout
from the serialization format, because theDifferentiabilityWitnessFunctionInst
now fits into theSILOneOperandLayout
.DifferentiabilityWitnessFunctionInst
aSILOneOperandLayout
with the mangled witness name.DifferentiabilityWitnessFunctionInst
by reading the name and deserializing the corresponding witness.[1] The avoided problem: The previous code for parsing
sil_differentiability_witness
constraints of the form[where T : _Differentiable]
fails to handle the case where the original function is declaration-only, because the code assumes that there is a generic environment, and declaration-only functions do not have a generic environment. Rather than fixing this code, it seemed better to delete it and use the existing code that parsesdifferentiability_witness_function
constraints, which does not suffer from this problem.