[llvm] [VPlan] Make VPInstruction::AnyOf poison-safe. (PR #154156)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 19 07:30:30 PDT 2025


https://github.com/fhahn commented:

> > AnyOf reduces multiple input vectors to a single boolean value. When used for early-exit vectorization, we need to consider any lane after the early exit being poison. Any poison lane would result in poison after the AnyOf reduction. To prevent this, freeze all inputs to AnyOf.
> > Fixes #153946.
> > https://alive2.llvm.org/ce/z/FD-XxA
> 
> Good catch! Is the freezing required only in early-exit cases?

At the moment, AnyOf is only used for early-exit loops. There may be uses in the future which may or may not require poison -safety, e.g using it for an in-loop AnyOf reduction would not require poison-safety)

> @fhahn is this related to interleaving? If so, can we only introduce the freeze for interleaving?

It's required with and without interleaving. The first examples in the Alive2 link show the issue without interleaving. Any lane after the early-exit may be poison, and the OR reduction would yield poison. Currently the non-interleaving case may not be simplified to poison by instcombine though, as it seems like InstCombine is not tracking poison per vector lane at the moment.

> Isn't there still a bug here? I think the freezes for any-of are only partially fixing the problem, since there is the issue of outside use of the compares too. Consider

This woul depend  on how  exaclty`@llvm.experimental.cttz.elts.i64.v4i1` handles poison lanes. If the result would be poison if any lane is poison, then yes this would be an issue.

But I'm assuming that the result isn't poison if there's a 1 lane before any poison lanes, as the zero lanes can be counted up to the first 1 lane without hitting poison.

The way we generate the parts outside the loop is checking if the first part exited, if so we select the result, otherwise we check if the next part exited and so on.

This should ensure that we only select the first part where the condition has a lane that's true. All lanes before the early exit condition being true must be non-poison (otherwise the input itself would already have UB), so we can select the result without hitting poison.

For later parts, `@llvm.experimental.cttz.elts` may result in poison (e.g. if all lanes are poison), but the result is not selected, so should not cause problems

https://github.com/llvm/llvm-project/pull/154156


More information about the llvm-commits mailing list