Skip to content

Commit 644f364

Browse files
authored
[AutoDiff] Use LinkEntity::SecondaryPointer for diff witness (#82412)
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`.
1 parent 31a9b79 commit 644f364

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

include/swift/IRGen/Linking.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class LinkEntity {
132132
/// ValueDecl*, SILFunction*, or TypeBase*, depending on Kind.
133133
void *Pointer;
134134

135-
/// ProtocolConformance*, depending on Kind.
135+
/// ProtocolConformance* or SILDifferentiabilityWitness*, depending on Kind.
136136
void *SecondaryPointer;
137137

138138
/// A hand-rolled bitfield with the following layout:
@@ -772,8 +772,8 @@ class LinkEntity {
772772
void
773773
setForDifferentiabilityWitness(Kind kind,
774774
const SILDifferentiabilityWitness *witness) {
775-
Pointer = const_cast<void *>(static_cast<const void *>(witness));
776-
SecondaryPointer = nullptr;
775+
Pointer = nullptr;
776+
SecondaryPointer = const_cast<void *>(static_cast<const void *>(witness));
777777
Data = LINKENTITY_SET_FIELD(Kind, unsigned(kind));
778778
}
779779

@@ -1684,7 +1684,7 @@ class LinkEntity {
16841684

16851685
SILDifferentiabilityWitness *getSILDifferentiabilityWitness() const {
16861686
assert(getKind() == Kind::DifferentiabilityWitness);
1687-
return reinterpret_cast<SILDifferentiabilityWitness *>(Pointer);
1687+
return reinterpret_cast<SILDifferentiabilityWitness *>(SecondaryPointer);
16881688
}
16891689

16901690
const RootProtocolConformance *getRootProtocolConformance() const {

lib/IRGen/IRGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ bool IRGenModule::IsWellKnownBuiltinOrStructralType(CanType T) const {
14451445
T == Context.getAnyObjectType())
14461446
return true;
14471447

1448-
if (auto IntTy = dyn_cast<BuiltinIntegerType>(T)) {
1448+
if (auto IntTy = dyn_cast_or_null<BuiltinIntegerType>(T)) {
14491449
auto Width = IntTy->getWidth();
14501450
if (Width.isPointerWidth())
14511451
return true;

0 commit comments

Comments
 (0)