Skip to content

Commit 04dca63

Browse files
authored
[AutoDiff] Fix activity analysis use-after-free crash. (#28285)
Remove `DifferentiableActivityInfo::getLookupConformanceFunction`, which returned a `LookupConformanceFn` (type alias for `LookupConformanceFn`), which does not own the underlying callable. Add a `DifferentiableActivityInfo::hasTangentSpace` helper, which is easier to use and avoids the issue. Resolves TF-945.
1 parent 8796230 commit 04dca63

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

lib/SILOptimizer/Mandatory/Differentiation.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,19 +1425,19 @@ class DifferentiableActivityInfo {
14251425
/// The original function.
14261426
SILFunction &getFunction();
14271427

1428-
/// The conformance lookup function.
1429-
// FIXME(TF-945): `LookupConformanceFn` is a type alias for
1430-
// `llvm::function_ref`, which does not own the underlying callable and should
1431-
// not be stored. Instead, delete `getLookupConformanceFunction()` and define
1432-
// a `hasTangentSpace(SILValue)` helper.
1433-
LookupConformanceFn getLookupConformanceFunction() {
1434-
// Look up in derivative generic signature, if defined.
1435-
if (derivativeGenericSignature)
1436-
return LookUpConformanceInSignature(
1437-
derivativeGenericSignature.getPointer());
1438-
// Otherwise, look up in the module.
1439-
return LookUpConformanceInModule(
1440-
getFunction().getModule().getSwiftModule());
1428+
/// Returns true if the given SILValue has a tangent space.
1429+
bool hasTangentSpace(SILValue value) {
1430+
auto type = value->getType().getASTType();
1431+
// Remap archetypes in the derivative generic signature, if it exists.
1432+
if (derivativeGenericSignature && type->hasArchetype()) {
1433+
type = derivativeGenericSignature->getCanonicalTypeInContext(
1434+
type->mapTypeOutOfContext());
1435+
}
1436+
// Look up conformance in the current module.
1437+
auto lookupConformance =
1438+
LookUpConformanceInModule(getFunction().getModule().getSwiftModule());
1439+
return type->getAutoDiffAssociatedTangentSpace(
1440+
lookupConformance).hasValue();
14411441
}
14421442

14431443
/// Perform analysis and populate variedness and usefulness sets.
@@ -2003,12 +2003,7 @@ void DifferentiableActivityInfo::propagateVaried(
20032003
if (isVaried(teai->getOperand(), i)) {
20042004
// Propagate variedness only if the `tuple_element_addr` result has a
20052005
// tangent space. Otherwise, the result does not need a derivative.
2006-
auto projType = teai->getType().getASTType();
2007-
if (derivativeGenericSignature && projType->hasArchetype())
2008-
projType = derivativeGenericSignature->getCanonicalTypeInContext(
2009-
projType->mapTypeOutOfContext());
2010-
if (projType->getAutoDiffAssociatedTangentSpace(
2011-
getLookupConformanceFunction()))
2006+
if (hasTangentSpace(teai))
20122007
setVariedAndPropagateToUsers(teai, i);
20132008
}
20142009
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %target-swift-emit-sil %s
2+
// REQUIRES: asserts
3+
4+
// TF-945: Activity analysis crash because
5+
// `DifferentiableActivityInfo::getLookupConformanceFunction` returned a
6+
// `LookupConformanceFn` (type alias for `llvm::function_ref`), which does not
7+
// own the underlying callable.
8+
9+
@differentiable
10+
func TF_945(_ x: Float) -> Float {
11+
var result = (x, 1)
12+
let (x, y) = result
13+
return x
14+
}

0 commit comments

Comments
 (0)