[GuardUtils] Add asserts about loop varying widenable conditions
authorAnna Thomas <anna@azul.com>
Thu, 6 Apr 2023 21:04:52 +0000 (17:04 -0400)
committerAnna Thomas <anna@azul.com>
Tue, 11 Apr 2023 14:54:07 +0000 (10:54 -0400)
commit5675757f5fc6e27ce01b3b12bdfd04044df53aa3
treedae7b1f0ee27f4bf0caa08b2dc0396487551c213
parente7f4ad13ae57f7a5170cd9caab3b1253d444c2f5
[GuardUtils] Add asserts about loop varying widenable conditions

We have now seen two miscompiles because of widening widenable
conditions at incorrect IR points and thereby changing a branch's loop
invariant condition to a loop-varying one (see PR60234 and PR61963).

This patch adds asserts in common guard utilities that we use for
widening to proactively catch these bugs in future.
Note that these asserts will not fire if we were to sink a widenable
condition from out of a loop into a loop (that's also incorrect for the
same reason as above).

Tested this without the fix for PR60234 (guard widening miscompile) and
confirmed the assert fires.

WARNING: Sometimes, the assert can fire if we failed to hoist the
invariant condition out of the loop. This is a pass-ordering issue or a
limitation in LICM, which would need an investigation. See details in
review.

Differential Revision: https://reviews.llvm.org/D147752
llvm/include/llvm/Transforms/Utils/GuardUtils.h
llvm/lib/Transforms/Scalar/GuardWidening.cpp
llvm/lib/Transforms/Scalar/LoopPredication.cpp
llvm/lib/Transforms/Utils/GuardUtils.cpp