Skip to content

Enzyme miscompiles nested branch with outer: uninitialized taped predicate causes inactive path to execute #2629

@minansys

Description

@minansys

@wsmoses

In this code

https://fwd.gymni.ch/rn0wMS

When fan = false, or true, it runs fine. When bool fan = (argc > 1) ? true : false; it will crash

Moreover, when fan = false, the result is wrong as well.

Here is the investigation from chatgpt


Summary

When differentiating a function that has:

an outer branch guarded by a runtime bool fan passed as enzyme_const, and

an inner branch guarded by an active predicate (e.g. radius > 0 where radius depends on differentiable inputs),

Enzyme sometimes generates IR that branches on an uninitialized (undef) taped predicate when fan == false. This can make the reverse/adjoint execute code that should be unreachable, leading to a crash (e.g., a nullptr dereference) or silent misbehavior.

Commenting out the inner if (radius > 0) (or replacing it with a non-branching normalization) avoids the issue.

Expected behavior

If fan == false, the if (fan) { ... } region should not execute in either primal or the differentiated code. No inner work, no loads/stores dependent on that region, no crash.

Actual behavior

With Enzyme autodiff enabled, running with fan == false sometimes triggers execution of code inside the if (fan) region, as if the inner predicate branch is taken despite fan being false. This can crash (e.g. nullptr deref) or produce incorrect behavior.```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions