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.