[llvm] [LoopVectorize] Add llvm.loop.vectorize.fp_reordering metadata (PR #219167)
Karthika Devi C via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 27 04:53:29 PDT 2026
https://github.com/kartcq updated https://github.com/llvm/llvm-project/pull/219167
>From 7dd426dd24b5496c1f59de9bf1eadcd16d1db74f Mon Sep 17 00:00:00 2001
From: Karthika Devi C <kartc at qti.qualcomm.com>
Date: Thu, 27 Aug 2026 02:40:40 -0700
Subject: [PATCH 1/2] [LoopVectorize] Add llvm.loop.vectorize.fp_reordering
metadata
Add a new per-loop metadata key llvm.loop.vectorize.fp_reordering that
takes a boolean operand to explicitly control whether the Loop Vectorizer
may reorder floating-point operations:
!{!"llvm.loop.vectorize.fp_reordering", i1 true} ; reordering allowed
!{!"llvm.loop.vectorize.fp_reordering", i1 false} ; reordering suppressed
When absent, allowReordering() retains its existing behaviour: vectorize.enable
or an explicit vector width > 1 grants FP reordering. When present, the value
takes precedence over that default.
This is the infrastructure patch for a proper long-term fix for #205756.
Refer #198726
---
llvm/docs/LangRef.md | 16 ++++++++++++++++
.../Vectorize/LoopVectorizationLegality.h | 12 ++++++++++++
.../Vectorize/LoopVectorizationLegality.cpp | 9 ++++++++-
3 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/llvm/docs/LangRef.md b/llvm/docs/LangRef.md
index 139c0501d410a..3dc2ba6a00a97 100644
--- a/llvm/docs/LangRef.md
+++ b/llvm/docs/LangRef.md
@@ -8101,6 +8101,22 @@ node has a single operand containing the name string:
!1 = !{!"llvm.loop.vectorize.disable"}
```
+When `llvm.loop.vectorize.enable` is present, the Loop Vectorizer is also
+permitted to reorder floating-point operations by default (equivalent to the
+behaviour of `#pragma clang loop vectorize(enable)`). This default can be
+overridden per-loop using `llvm.loop.vectorize.fp_reordering` (see below).
+
+#### '`llvm.loop.vectorize.fp_reordering`' Metadata
+
+This metadata gives the Loop Vectorizer an explicit per-loop instruction about
+whether floating-point operation reordering (reassociation) is permitted. It
+takes a single boolean operand:
+
+```llvm
+!0 = !{!"llvm.loop.vectorize.fp_reordering", i1 true} ; reordering allowed
+!1 = !{!"llvm.loop.vectorize.fp_reordering", i1 false} ; reordering suppressed
+```
+
#### '`llvm.loop.vectorize.predicate.enable`' and '`llvm.loop.vectorize.predicate.disable`' Metadata
This metadata selectively enables or disables creating predicated instructions
diff --git a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
index 7b8b27c6541e1..7bfa8cd3626d2 100644
--- a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
+++ b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
@@ -92,6 +92,13 @@ class LoopVectorizeHints {
/// a plain value because the enable/disable pair has no operand to validate.
unsigned Scalable;
+ /// FP reordering hint from llvm.loop.vectorize.fp_reordering; one of
+ /// ForceKind, carried as a plain unsigned like Force and Predicate.
+ /// FK_Undefined : not specified
+ /// FK_Disabled : reordering explicitly suppressed
+ /// FK_Enabled : reordering explicitly allowed
+ unsigned FPReordering;
+
/// Return the loop metadata prefix.
static StringRef Prefix() { return "llvm.loop."; }
@@ -175,6 +182,11 @@ class LoopVectorizeHints {
/// error accumulates in the loop.
LLVM_ABI bool allowReordering() const;
+ /// \return the per-loop FP reordering hint as a ForceKind:
+ enum ForceKind getFPReordering() const {
+ return (ForceKind)FPReordering;
+ }
+
bool isPotentiallyUnsafe() const {
// Avoid FP vectorization if the target is unsure about proper support.
// This may be related to the SIMD unit in the target not handling
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index f1d785b571367..dbc53730998cd 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -104,7 +104,8 @@ LoopVectorizeHints::LoopVectorizeHints(const Loop *L,
VectorizerParams::VectorizationFactor.getKnownMinValue(), HK_WIDTH),
Interleave("interleave.count", InterleaveOnlyWhenForced, HK_INTERLEAVE),
Force(FK_Undefined), IsVectorized("isvectorized", 0, HK_ISVECTORIZED),
- Predicate(FK_Undefined), Scalable(SK_Unspecified), TheLoop(L), ORE(ORE) {
+ Predicate(FK_Undefined), Scalable(SK_Unspecified),
+ FPReordering(FK_Undefined), TheLoop(L), ORE(ORE) {
// Populate values with existing loop metadata.
getHintsFromMetadata();
@@ -243,6 +244,9 @@ void LoopVectorizeHints::emitRemarkWithHints() const {
bool LoopVectorizeHints::allowReordering() const {
// Allow the vectorizer to change the order of operations if enabling
// loop hints are provided
+ if ((ForceKind)FPReordering != FK_Undefined)
+ return HintsAllowReordering && ((ForceKind)FPReordering == FK_Enabled);
+
ElementCount EC = getWidth();
return HintsAllowReordering &&
(getForce() == LoopVectorizeHints::FK_Enabled ||
@@ -322,6 +326,9 @@ void LoopVectorizeHints::setHint(StringRef Name, Metadata *Arg) {
break;
}
}
+
+ if (Name == "vectorize.fp_reordering")
+ FPReordering = (Val != 0) ? FK_Enabled : FK_Disabled;
}
// Return true if the inner loop \p Lp is uniform with regard to the outer loop
>From 4cd404f5c15156eecc5b2d109c6545e4d8f054d2 Mon Sep 17 00:00:00 2001
From: Karthika Devi C <kartc at qti.qualcomm.com>
Date: Thu, 27 Aug 2026 03:33:26 -0700
Subject: [PATCH 2/2] Fix Formatting
---
.../llvm/Transforms/Vectorize/LoopVectorizationLegality.h | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
index 7bfa8cd3626d2..7fcc55ce8348c 100644
--- a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
+++ b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
@@ -183,9 +183,7 @@ class LoopVectorizeHints {
LLVM_ABI bool allowReordering() const;
/// \return the per-loop FP reordering hint as a ForceKind:
- enum ForceKind getFPReordering() const {
- return (ForceKind)FPReordering;
- }
+ enum ForceKind getFPReordering() const { return (ForceKind)FPReordering; }
bool isPotentiallyUnsafe() const {
// Avoid FP vectorization if the target is unsure about proper support.
More information about the llvm-commits
mailing list