Skip to content

scf.for fix when iter-args and result args have shadow mismatch in fwd#2750

Draft
spikerheado1234 wants to merge 1 commit intomainfrom
scf.for-fix
Draft

scf.for fix when iter-args and result args have shadow mismatch in fwd#2750
spikerheado1234 wants to merge 1 commit intomainfrom
scf.for-fix

Conversation

@spikerheado1234
Copy link
Copy Markdown
Collaborator

@spikerheado1234 spikerheado1234 commented Mar 18, 2026

In forward mode AD, if an iter-arg is deemed a constant, yet its corresponding result has a tangent, then there will be a mismatch between the number of iter-args and result-args. This PR fixes that by unioning if either an iter-arg or its corresponding result is active, inserting a corresponding shadow arg if at least one is active.

llvm::enumerate(targetValues, operandRange)) {
if (gutils->isConstantValue(regionValue))
bool iterArgActive = !gutils->isConstantValue(regionValue);
bool resultActive = i < op->getNumResults() &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't generally correct since an input operand doesn't necessarily correspond to the outputs as here.

Instead really you need to construct this correctly [and separately] for scf for, or with an interface -- and pass said array to the general controlFlowForwardHandler which accepts arrays

//
// We also register the mapping in invertedPointers so that invertPointerM,
// which checks invertedPointers before isConstantValue, returns the shadow
// block arg instead of zero when body ops reference this iter arg.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be handled in createWithShadows, not here

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.

2 participants