-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[AutoDiff] Mangle @differentiable
function types.
#25804
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
dan-zheng
merged 1 commit into
swiftlang:tensorflow
from
dan-zheng:differentiable-fn-type-mangling
Jun 28, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -733,6 +733,14 @@ enum class FunctionMetadataConvention: uint8_t { | |
CFunctionPointer = 3, | ||
}; | ||
|
||
/// Differentiability kind for function type metadata. | ||
/// Duplicates `DifferentiabilityKind` in AutoDiff.h. | ||
enum class FunctionMetadataDifferentiabilityKind: uint8_t { | ||
NonDifferentiable = 0b00, | ||
Normal = 0b01, | ||
Linear = 0b11 | ||
}; | ||
|
||
/// Flags in a function type metadata record. | ||
template <typename int_type> | ||
class TargetFunctionTypeFlags { | ||
|
@@ -747,7 +755,8 @@ class TargetFunctionTypeFlags { | |
ParamFlagsMask = 0x02000000U, | ||
EscapingMask = 0x04000000U, | ||
// SWIFT_ENABLE_TENSORFLOW | ||
DifferentiableMask = 0x08000000U | ||
DifferentiableMask = 0x08000000U, | ||
|
||
LinearMask = 0x10000000U | ||
}; | ||
int_type Data; | ||
|
||
|
@@ -785,10 +794,14 @@ class TargetFunctionTypeFlags { | |
} | ||
|
||
// SWIFT_ENABLE_TENSORFLOW | ||
constexpr TargetFunctionTypeFlags<int_type> | ||
withDifferentiable(bool isDifferentiable) const { | ||
return TargetFunctionTypeFlags<int_type>((Data & ~DifferentiableMask) | | ||
(isDifferentiable ? DifferentiableMask : 0)); | ||
constexpr TargetFunctionTypeFlags<int_type> withDifferentiabilityKind( | ||
FunctionMetadataDifferentiabilityKind differentiability) const { | ||
return TargetFunctionTypeFlags<int_type>( | ||
(Data & ~DifferentiableMask & ~LinearMask) | | ||
(differentiability == FunctionMetadataDifferentiabilityKind::Normal | ||
? DifferentiableMask : 0) | | ||
(differentiability == FunctionMetadataDifferentiabilityKind::Linear | ||
? LinearMask : 0)); | ||
} | ||
|
||
unsigned getNumParameters() const { return Data & NumParametersMask; } | ||
|
@@ -807,7 +820,15 @@ class TargetFunctionTypeFlags { | |
|
||
// SWIFT_ENABLE_TENSORFLOW | ||
bool isDifferentiable() const { | ||
return bool (Data & DifferentiableMask); | ||
return getDifferentiabilityKind() >= | ||
FunctionMetadataDifferentiabilityKind::Normal; | ||
} | ||
FunctionMetadataDifferentiabilityKind getDifferentiabilityKind() const { | ||
if (bool(Data & DifferentiableMask)) | ||
return FunctionMetadataDifferentiabilityKind::Normal; | ||
if (bool(Data & LinearMask)) | ||
return FunctionMetadataDifferentiabilityKind::Linear; | ||
return FunctionMetadataDifferentiabilityKind::NonDifferentiable; | ||
} | ||
|
||
bool hasParameterFlags() const { return bool(Data & ParamFlagsMask); } | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
How would you differentiate between
@differentiable @convention(thin)
and@differentiable
?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.
Good point. Currently, modifiers like
@convention(thin)
and@differentiable
have their own function type demangle node variant:-
NoEscapeFunctionType
@escaping
FunctionType
(escaping)@autoclosure
AutoClosureType
@escaping @autoclosure
EscapingAutoClosureType
Here's what it looks like:
This leads to an explosion of demangle node kinds for combinations of modifiers.
A more reflexible representation would be to have a single
FunctionType
demangle node kind, with modifier nodes as children in a list. Dummy view:Changing the mangling scheme certainly seems breaking though. The significance of type mangling isn't exactly clear to me: I imagine LLDB uses this mangling to show types of variables. Perhaps it's not important to capture all combinations of function type modifiers, only the semantically important ones (
@differentiable
is probably more semantically heavy than@escaping
).Started a discussion at https://forums.swift.org/t/questions-about-function-type-mangling/26360.