Files
Adel Ejjeh f86d707db6 [LoopVectorize] Reland: Add metadata to distinguish vectorized loop body from scalar remainder (#194912)
Reland of #190258, reverted in #194901 due to a null-pointer dereference
when the scalar preheader is absent. Fixed by guarding the metadata
emission with a null check on `ScalarPH`. Also adds a dedicated test
(`vectorize-loop-kind-metadata.ll`) verifying the metadata is emitted
only when remarks are enabled.

---

Add two new loop metadata attributes — `llvm.loop.vectorize.body` and
`llvm.loop.vectorize.epilogue` — that the loop vectorizer sets on the
generated vector loop and epilogue loop respectively. The metadata is
only emitted when optimization remarks are enabled (`ORE->enabled()`),
so it has zero cost in normal compilation.

These enable downstream passes (LoopUnroll, WarnMissedTransforms) to
produce more precise optimization remarks. Instead of the generic "loop
not unrolled" warning on a source line that was vectorized, the unroller
can now report:
- **"vectorized loop"** for the main vector body
- **"epilogue loop"** for the scalar epilogue/remainder
- **"epilogue vectorized loop"** for an epilogue that was itself
vectorized during epilogue vectorization (carries both attributes)

A shared `getLoopVectorizeKindPrefix()` helper in
`LoopUtils.h`/`LoopUtils.cpp` reads the metadata and returns the
appropriate prefix string, used by both `LoopUnroll.cpp` and
`WarnMissedTransforms.cpp`. The metadata emission in `VPlan.cpp` uses
`Loop::addIntLoopAttribute` from the NFC PR #194676.

Two end-to-end tests exercise the full `loop-vectorize → loop-unroll`
pipeline with forced epilogue vectorization
(`-enable-epilogue-vectorization -epilogue-vectorization-force-VF=4`) to
produce all four loop categories from a single vectorizable function.
Each test also includes a plain (non-vectorized) function to cover the
baseline "loop" case. Both tests verify stderr diagnostic output and
YAML structured remarks.
**`LoopUnroll/vectorizer-loop-kind-remarks.ll`** checks for
successful-unroll remarks.
**`LoopTransformWarning/vectorizer-loop-kind-unroll-warning.ll`** checks
for failed-unroll warnings.

AI Disclaimer: this patch was generated with assistance of GitHub
Copilot/Claude Opus and reviewed by a human.
2026-05-01 09:00:59 -05:00

35 lines
1.3 KiB
LLVM

; RUN: opt < %s -passes=loop-vectorize -force-vector-interleave=1 -force-vector-width=4 \
; RUN: -pass-remarks-analysis=loop-vectorize -S | FileCheck %s --check-prefix=REMARKS
; RUN: opt < %s -passes=loop-vectorize -force-vector-interleave=1 -force-vector-width=4 -S \
; RUN: | FileCheck %s --check-prefix=NO-REMARKS
; Verify that llvm.loop.vectorize.body is attached to the vector loop and
; llvm.loop.vectorize.epilogue is attached to the scalar remainder when
; optimization remarks are enabled, and absent otherwise.
define void @test_metadata(ptr noalias %A, i64 %n) {
entry:
br label %loop
loop:
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
%gep = getelementptr inbounds i32, ptr %A, i64 %iv
%val = load i32, ptr %gep, align 4
%add = add i32 %val, 1
store i32 %add, ptr %gep, align 4
%iv.next = add i64 %iv, 1
%cmp = icmp slt i64 %iv.next, %n
br i1 %cmp, label %loop, label %exit
exit:
ret void
}
; With remarks enabled: vector loop has vectorize.body, scalar remainder has vectorize.epilogue.
; REMARKS-DAG: !{!"llvm.loop.vectorize.body", i32 1}
; REMARKS-DAG: !{!"llvm.loop.vectorize.epilogue", i32 1}
; Without remarks: neither metadata is present.
; NO-REMARKS-NOT: llvm.loop.vectorize.body
; NO-REMARKS-NOT: llvm.loop.vectorize.epilogue