Skip to content

Conversation

clackary
Copy link

@clackary clackary commented Jul 1, 2025

  • Explanation:
    This cherry-pick addresses an unsafe reinterpret_cast in IRGenModule::getAddrOfLLVMVariable when handling certain LinkEntity kinds.

  • Scope:
    This change is narrowly scoped to setForDifferentiabilityWitness and only affects code paths related to autodiff.

  • Issues:
    No public issue is filed. The fix resolves a latent bug triggered in certain compiler configurations or IR generation flows involving SILDifferentiabilityWitness.

  • Original PRs:
    [AutoDiff] Use LinkEntity::SecondaryPointer for diff witness #82412

  • Risk:
    Low. The patch follows existing patterns, and does not introduce new logic.

  • Testing:
    We have tested this change across an internal suite of projects that utilize autodiff features heavily.

  • Reviewers:
    @compnerd @asl @kovdan01

If `LinkEntity::isTypeKind()` is true, `IRGenModule::getAddrOfLLVMVariable`
assumes that we can safely call `LinkEntity::getType()`, which does
`reinterpret_cast` of `LinkEntity::Pointer` to `TypeBase *`. However, for SIL
differentiability witness, the pointer has `SILDifferentiabilityWitness *`
type, which is not derived from `TypeBase`. So, such a cast is not allowed.

Just as with `ProtocolWitnessTableLazyAccessFunction` and
`ProtocolWitnessTableLazyCacheVariable` link entity kinds (which are
also type kinds), we should use `SecondaryPointer` instead of `Pointer` for
storing payload here, while setting `Pointer` to `nullptr`.

(cherry picked from commit 77a3873)
@clackary clackary requested a review from a team as a code owner July 1, 2025 20:03
@clackary
Copy link
Author

clackary commented Jul 1, 2025

@swift-ci please test

@clackary
Copy link
Author

clackary commented Jul 1, 2025

cc @JaapWijnen

@clackary clackary merged commit 305125d into swiftlang:release/6.2 Jul 2, 2025
5 checks passed
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