@@ -5937,23 +5937,19 @@ SILValue ADContext::promoteToDifferentiableFunction(
5937
5937
5938
5938
// / Fold `autodiff_function_extract` users of the given `autodiff_function`
5939
5939
// / instruction, directly replacing them with `autodiff_function` instruction
5940
- // / operands. If the `autodiff_function` instruction has no
5941
- // / non-`autodiff_function_extract` users, delete the instruction itself after
5942
- // / folding.
5940
+ // / operands. If the `autodiff_function` instruction has no remaining uses,
5941
+ // / delete the instruction itself after folding.
5943
5942
// /
5944
5943
// / Folding can be disabled by the `SkipFoldingAutoDiffFunctionExtraction` flag
5945
5944
// / for SIL testing purposes.
5946
5945
static void foldAutoDiffFunctionExtraction (AutoDiffFunctionInst *source) {
5947
- bool hasOnlyAutoDiffFunctionExtractUsers = true ;
5948
5946
// Iterate through all `autodiff_function` instruction uses.
5949
5947
for (auto use : source->getUses ()) {
5950
5948
auto *adfei = dyn_cast<AutoDiffFunctionExtractInst>(use->getUser ());
5951
5949
// If user is not an `autodiff_function_extract` instruction, set flag to
5952
5950
// false.
5953
- if (!adfei) {
5954
- hasOnlyAutoDiffFunctionExtractUsers = false ;
5951
+ if (!adfei)
5955
5952
continue ;
5956
- }
5957
5953
// Fold original function extractors.
5958
5954
if (adfei->getExtractee () == AutoDiffFunctionExtractee::Original) {
5959
5955
auto originalFnValue = source->getOriginalFunction ();
@@ -5967,9 +5963,8 @@ static void foldAutoDiffFunctionExtraction(AutoDiffFunctionInst *source) {
5967
5963
adfei->replaceAllUsesWith (assocFnValue);
5968
5964
adfei->eraseFromParent ();
5969
5965
}
5970
- // If all users are `autodiff_function_extract` instructions, erase the
5971
- // `autodiff_function` instruction itself.
5972
- if (hasOnlyAutoDiffFunctionExtractUsers)
5966
+ // If the `autodiff_function` instruction has no remaining uses, erase it.
5967
+ if (isInstructionTriviallyDead (source))
5973
5968
source->eraseFromParent ();
5974
5969
}
5975
5970
0 commit comments