Skip to content

Commit b973555

Browse files
authored
[AutoDiff] Minor activity analysis changes. (#28409)
Hoist activity marking visited value set out of loop over original bbs. This is safe because bbs directly start with dominator bbs's active values. Visit bb arguments for activity marking. This was accidentally deleted in #28225. Re-adding the logic doesn't seem to affect any tests.
1 parent b41adcd commit b973555

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

lib/SILOptimizer/Mandatory/Differentiation.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6311,6 +6311,8 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
63116311
// Adjoint values of dominated active values are passed as pullback block
63126312
// arguments.
63136313
DominanceOrder domOrder(original.getEntryBlock(), domInfo);
6314+
// Keep track of visited values.
6315+
SmallPtrSet<SILValue, 8> visited;
63146316
while (auto *bb = domOrder.getNext()) {
63156317
auto &bbActiveValues = activeValues[bb];
63166318
// If the current block has an immediate dominator, append the immediate
@@ -6320,13 +6322,12 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
63206322
bbActiveValues.append(domBBActiveValues.begin(),
63216323
domBBActiveValues.end());
63226324
}
6323-
SmallPtrSet<SILValue, 8> visited(bbActiveValues.begin(),
6324-
bbActiveValues.end());
6325-
// Register a value as active if it has not yet been visited.
63266325
bool diagnosedActiveEnumValue = false;
6327-
auto addActiveValue = [&](SILValue v) {
6326+
// Mark the activity of a value if it has not yet been visited.
6327+
auto markValueActivity = [&](SILValue v) {
63286328
if (visited.count(v))
63296329
return;
6330+
visited.insert(v);
63306331
// Diagnose active enum values. Differentiation of enum values requires
63316332
// special adjoint value handling and is not yet supported. Diagnose
63326333
// only the first active enum value to prevent too many diagnostics.
@@ -6342,17 +6343,19 @@ class PullbackEmitter final : public SILInstructionVisitor<PullbackEmitter> {
63426343
// become projections into their adjoint base buffer.
63436344
if (Projection::isAddressProjection(v))
63446345
return;
6345-
visited.insert(v);
63466346
bbActiveValues.push_back(v);
63476347
};
6348-
// Register bb arguments and all instruction operands/results.
6348+
// Visit bb arguments and all instruction operands/results.
6349+
for (auto *arg : bb->getArguments())
6350+
if (getActivityInfo().isActive(arg, getIndices()))
6351+
markValueActivity(arg);
63496352
for (auto &inst : *bb) {
63506353
for (auto op : inst.getOperandValues())
63516354
if (getActivityInfo().isActive(op, getIndices()))
6352-
addActiveValue(op);
6355+
markValueActivity(op);
63536356
for (auto result : inst.getResults())
63546357
if (getActivityInfo().isActive(result, getIndices()))
6355-
addActiveValue(result);
6358+
markValueActivity(result);
63566359
}
63576360
domOrder.pushChildren(bb);
63586361
if (errorOccurred)

0 commit comments

Comments
 (0)