[llvm] [LV][EVL] Attach a new metadata on EVL vectorized loops (PR #131000)

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 14 14:49:35 PDT 2025


================
@@ -7828,6 +7828,26 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
 
       LoopVectorizeHints Hints(L, true, *ORE);
       Hints.setAlreadyVectorized();
+
+      // Check if it's EVL-vectorized and mark the corresponding metadata.
+      bool IsEVLVectorized =
+          llvm::any_of(*HeaderVPBB, [](const VPRecipeBase &Recipe) {
+            // Looking for the ExplictVectorLength VPInstruction.
+            if (const auto *VI = dyn_cast<VPInstruction>(&Recipe))
+              return VI->getOpcode() == VPInstruction::ExplicitVectorLength;
+            return false;
+          });
+      if (IsEVLVectorized) {
+        LLVMContext &Context = L->getHeader()->getContext();
+        MDNode *LoopID = L->getLoopID();
+        auto *IsEVLVectorizedMD = MDNode::get(
+            Context,
+            {MDString::get(Context, "llvm.loop.isvectorized.tailfoldingstyle"),
+             MDString::get(Context, "evl")});
+        MDNode *NewLoopID = makePostTransformationMetadata(Context, LoopID, {},
+                                                           {IsEVLVectorizedMD});
+        L->setLoopID(NewLoopID);
----------------
mshockwave wrote:

(sorry I missed your comment) The LoopID MDNode owns all the loop-releated MDNodes of the associated loop, so we have to update the LoopID. While currently all LoopIDs are MDTuple, which is the only kind of resizable MDNode (so we can just append a new operand), there is no guarantee if it'll still be true in the future. So I feel like the most consistent way to update LoopID is still using `makePostTransformationMetadata`, which is also used by vectorizers to update `llvm.loop.isvectorized` under the hood.

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


More information about the llvm-commits mailing list