[llvm] [VPlan] Fold safe divisors into VP intrinsics with EVL (PR #148828)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 4 09:36:07 PDT 2025


================
@@ -2176,6 +2176,52 @@ static VPRecipeBase *optimizeMaskToEVL(VPValue *HeaderMask,
       .Default([&](VPRecipeBase *R) { return nullptr; });
 }
 
+/// Try to optimize safe divisors away by converting their users to VP
+/// intrinsics:
+///
+/// udiv x, (vp.merge allones, y, 1, evl) -> vp.udiv x, y, allones, evl
+///
+/// Note the lanes past EVL will be changed from x to poison. This only works
+/// for the EVL-based IV and not any arbitrary EVL, because we know nothing
+/// will read the lanes past the EVL-based IV.
----------------
lukel97 wrote:

Yeah that's exactly the reason why I've chosen to do this in the loop vectorizer as opposed to e.g. in the RISC-V backend, because here I'm assuming that no lanes past EVL are read, so it's fine to replace them with poison.

I think it might be safer though to just emit a masked VP intrinsic during VPlan construction, where we set the lanes to poison to begin with. I'll look into doing this in a separate PR.

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


More information about the llvm-commits mailing list