Skip to content

[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

Merged

Conversation

marcrasi
Copy link

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 before differentiability_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 the sil_differentiability_witness constraints and the differentiability_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:

  • Datastructure & utility changes:
    • DifferentiabilityWitnessFunctionInst now contains a SILDifferentiabilityWitness* instead of a pointer to original function, indices, and generic signature. (credit to @dan-zheng, I just copied these changes from his commit)
    • Added functions in SILModule for looking up SILDifferentiabilityWitness* because parsing and deserialization need to find the right SILDifferentiabilityWitness* to put in the inst.
  • SIL printing changes:
    • Print sil_differentiability_witness declarations before function declarations
    • Print <T where T ...> instead of [where T ...] in sil_differentiability_witness declarations.
  • SIL parsing changes:
    • Factor out a common parseSILDifferentiabilityWitnessConfigAndFunction, and use this for parsing both sil_differentiability_witness and differentiability_witness_function. This simplifies the code and avoids the problem [1].
    • When parsing a differentiability_witness_function, look for the SILDifferentiabilityWitness* in the module.
    • Bugfix in ParseStmt so that it can parse sil_differentiability_witness declarations that come at the beginning of the file.
  • Serialization changes:
    • Deleted SILInstDifferentiabilityWitnessFunctionLayout from the serialization format, because the DifferentiabilityWitnessFunctionInst now fits into the SILOneOperandLayout.
    • Changed serialization to serialize DifferentiabilityWitnessFunctionInst a SILOneOperandLayout with the mangled witness name.
    • Changed deserialization to deserialize DifferentiabilityWitnessFunctionInst by reading the name and deserializing the corresponding witness.
    • Changed deserialization to treat declaration-only witnesses as "fully deserialized". Otherwise it tries to deserialize them twice and the module doesn't like having duplicate witnesses.

[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 parses differentiability_witness_function constraints, which does not suffer from this problem.

@marcrasi marcrasi requested review from rxwei and dan-zheng October 29, 2019 00:32
Copy link
Contributor

@dan-zheng dan-zheng left a 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!

@marcrasi
Copy link
Author

@swift-ci please test tensorflow

Copy link
Contributor

@rxwei rxwei left a 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!

// Check whether original function generic signature and parsed witness
// generic have the same generic parameters.
auto areGenericParametersConsistent = [&]() {
llvm::DenseSet<GenericParamKey> genericParamKeys;
Copy link
Contributor

@rxwei rxwei Oct 29, 2019

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(
Copy link
Contributor

Choose a reason for hiding this comment

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

I love this!

auto *resultIndexSet = IndexSubset::get(
P.Context, origFnType->getNumResults(), resultIndices);
AutoDiffConfig config(parameterIndexSet, resultIndexSet, witnessGenSig);
return {{config, originalFunction}};
Copy link
Contributor

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:

Suggested change
return {{config, originalFunction}};
return std::make_pair(config, originalFunction);

@rxwei
Copy link
Contributor

rxwei commented Oct 29, 2019

Please feel free to merge it now and address my minor comments later. I don't want to block progress getting retrodiff landed!

@marcrasi
Copy link
Author

@swift-ci please test tensorflow

1 similar comment
@marcrasi
Copy link
Author

@swift-ci please test tensorflow

@marcrasi
Copy link
Author

@swift-ci please test tensorflow macos

@marcrasi marcrasi merged commit 41015c3 into swiftlang:tensorflow Oct 29, 2019
@marcrasi marcrasi deleted the diff-wit-inst-store-wit-pointer branch October 29, 2019 22:29
dan-zheng added a commit to dan-zheng/swift that referenced this pull request Oct 30, 2019
Remove unused variable `Attr3`. It is no longer necessary after
swiftlang#27919.
dan-zheng added a commit that referenced this pull request Oct 30, 2019
Remove unused variable `Attr3`. It is no longer necessary after
#27919.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants