[llvm] [LV] Don't vectorize if trip count expansion may introduce UB. (PR #92177)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed May 15 04:54:12 PDT 2024


================
@@ -161,6 +161,11 @@ SCEVExpander::findInsertPointAfter(Instruction *I,
   return IP;
 }
 
+bool SCEVExpander::expansionMayIntroduceUB(const SCEV *Expr) {
+  return SCEVExprContains(Expr,
+                          [](const SCEV *Op) { return isa<SCEVUDivExpr>(Op); });
----------------
fhahn wrote:

Updated the PR to also check if the divisor is guaranteed to be non-zero. This may miss some safe cases, e.g. where the division happens outside the loop already. Not sure if we have any good way of checking that, except possibly the checks if there are existing equivalent instructions outside the loop during expansion?

> We could probably do something more clever if it mattered: the only way for the exit count to contain UB is if the branch never executes, so it would be correct to just transform a/b to a/umax(b, 1).

I think in some cases this may not be correct, e.g. we have two exit counts 1) c and 2) `a - (42/b)`, with the BTC being `umin(c, a - (42/b))`. If `b == 0` then the BTC should be `c`, but using 1 as divisor may result in `a - (42/(umax 1, b))` < `c` and we  would use an incorrect BTC?

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


More information about the llvm-commits mailing list