Skip to content

Conversation

dan-zheng
Copy link
Contributor

  • Mangle @differentiable and @differentiable(linear) function types,
    both escaping and non-escaping variants.
  • Update TargetFunctionTypeFlags to represent linear functions.
  • Add TypeDecoder tests.

@dan-zheng dan-zheng added the tensorflow This is for "tensorflow" branch PRs. label Jun 26, 2019
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried using a single DifferentiabilityMask, similar to the one in AnyFunctionType::ExtInfo:

      DifferentiabilityMask  = 0xF8000000U,
      DifferentiabilityShift = 24U

But I didn't figure out how to make it work all the way. Things are tricky because the mask is F8 instead of FF, which requires extra bitshifting.

@dan-zheng dan-zheng requested review from rxwei and slavapestov and removed request for slavapestov June 26, 2019 23:24
Copy link
Contributor

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?

Copy link
Contributor Author

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:

Modifier Demangle node kind
- NoEscapeFunctionType
@escaping FunctionType (escaping)
@autoclosure AutoClosureType
@escaping @autoclosure EscapingAutoClosureType
... ...

Here's what it looks like:

$ $SWIFT_BIN/swift-demangle --expand sS2fXfD
Demangling for $sS2fXfD
kind=Global
  kind=TypeMangling
    kind=Type
      kind=ThinFunctionType
        kind=ArgumentTuple
          kind=Type
            kind=Structure
              kind=Module, text="Swift"
              kind=Identifier, text="Float"
        kind=ReturnType
          kind=Type
            kind=Structure
              kind=Module, text="Swift"
              kind=Identifier, text="Float"
$sS2fXfD ---> @convention(thin) (Swift.Float) -> Swift.Float

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:

      kind=FunctionType
        kind=FunctionTypeModifiers
          kind=ThinFunction
          kind=EscapingFunction
          kind=DifferentiableFunction
        kind=ArgumentTuple
          kind=Type
            kind=Structure
              kind=Module, text="Swift"
              kind=Identifier, text="Float"
        kind=ReturnType
          kind=Type
            kind=Structure
              kind=Module, text="Swift"
              kind=Identifier, text="Float"

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.

@dan-zheng
Copy link
Contributor Author

@swift-ci Please clean test tensorflow

@dan-zheng dan-zheng force-pushed the differentiable-fn-type-mangling branch 2 times, most recently from 220f45d to 32273b1 Compare June 28, 2019 00:36
- Mangle `@differentiable` and `@differentiable(linear)` function types,
  both escaping and non-escaping variants.
- Add `FunctionMetadataDifferentiabilityKind` to MetadataValues.h.
  Use it in `TargetFunctionTypeFlags` to represent differentiable/linear
  functions.
- Add TypeDecoder tests.
@dan-zheng dan-zheng force-pushed the differentiable-fn-type-mangling branch from 32273b1 to 3c1f021 Compare June 28, 2019 00:40
@dan-zheng
Copy link
Contributor Author

@swift-ci Please test tensorflow

@dan-zheng dan-zheng merged commit 2e8ef90 into swiftlang:tensorflow Jun 28, 2019
@dan-zheng dan-zheng deleted the differentiable-fn-type-mangling branch June 28, 2019 03:12
dan-zheng added a commit to dan-zheng/swift that referenced this pull request Aug 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tensorflow This is for "tensorflow" branch PRs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants