Skip to content
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

[SR-13511] Mangle AutoDiff-generated structs and enums #54966

Closed
dan-zheng opened this issue Apr 6, 2020 · 2 comments
Closed

[SR-13511] Mangle AutoDiff-generated structs and enums #54966

dan-zheng opened this issue Apr 6, 2020 · 2 comments
Assignees

Comments

@dan-zheng
Copy link
Collaborator

Previous ID SR-13511
Radar None
Original Reporter @dan-zheng
Type Sub-task
Status Closed
Resolution Won't Do
Additional Detail from JIRA
Votes 0
Component/s
Labels Sub-task
Assignee @dan-zheng
Priority Medium

md5: 8ba730207e6c9b8443e7c80770d0f6b6

Parent-Task:

  • SR-13506 [AD] Use principled mangling for AD-generated symbols

Issue Description:

Mangle AutoDiff-generated linear map structs and branching trace enums.

Without proper mangling, an IRGenDebugInfo assertion triggers:

import _Differentiation

extension Float {
  @_silgen_name("add")
  static func add(_ x: Float, _ y: Float) -> Float {
    return x + y
  }

  @derivative(of: add)
  static func addVJP(_ x: Float, _ y: Float) -> (
    value: Float, pullback: (Float) -> (Float, Float)
  ) {
    return (add(x, y), { v in (v, v) })
  }
}

@differentiable
func foo(_ x: Float) -> Float {
  let y = Float.add(x, x)
  return y
}

// Failed to reconstruct type for $s4main42_AD__$s4main3fooyS2fF_bb0__PB__src_0_wrt_0VmD
// Original type:
// (metatype_type
//   (struct_type decl=main.(file)._AD__$s4main3fooyS2fF_bb0__PB__src_0_wrt_0))
// Assertion failed: (isa<X>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file /Users/danielzheng/swift-merge/llvm-project/llvm/include/llvm/Support/Casting.h, line 264.

This blocks end-to-end AutoDiff testing on master branch.

@dan-zheng
Copy link
Collaborator Author

Incomplete attempt made in https://github.com/dan-zheng/swift/tree/autodiff-generated-decl-mangling. Details:

[WIP] Failed attempt to fix TF-1232.

Plumb type reconstruction for AutoDiff-generated declarations.
Ultimately fails due to poor name lookup support for declarations generated late
(SILOptimizer) after name lookup caches have been populated.

In `ASTBuilder::findTypeDecl`: `ModuleDecl::lookupMember` currently returns
duplicate results. This may be fixed by mangling the `DeclContext`
(e.g. the `FileUnit`) to which generated declarations are added, instead of
always using `ModuleDecl`.

Hacks:
- Added calls to `FileUnit::addTopLevelDecl` and `ModuleDecl::clearLookupCache`
  in LinearMapInfo.cpp. These methods are declared with "do not add more uses".
- Mangle `DeclContext` instead of `ModuleDecl`.

Better support for generated declarations and lookup may facilitate this: https://forums.swift.org/t/question-re-adding-derivedfileunit/26427

Abandoning in favor of disabling debug type verification for now.

@dan-zheng
Copy link
Collaborator Author

Obsolete/fixed in #30863

It turns out AutoDiff-generated declarations don't need special mangling support, they just need to be correctly added to file units so that they can be found via name lookup.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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

No branches or pull requests

1 participant