Skip to content

Commit 5ba9cbd

Browse files
committed
Made metadata specific to FP reductions.
1 parent 9511b6e commit 5ba9cbd

File tree

5 files changed

+57
-38
lines changed

5 files changed

+57
-38
lines changed

llvm/docs/LangRef.rst

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7593,21 +7593,23 @@ Note that setting ``llvm.loop.interleave.count`` to 1 disables interleaving
75937593
multiple iterations of the loop. If ``llvm.loop.interleave.count`` is set to 0
75947594
then the interleave count will be determined automatically.
75957595

7596-
'``llvm.loop.vectorize.reassociation.enable``' Metadata
7597-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7596+
'``llvm.loop.vectorize.reassociate_fpreductions.enable``' Metadata
7597+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
75987598

7599-
This metadata selectively allows or disallows reassociating computations,
7600-
which otherwise may be unsafe to reassociate, during the loop vectorization.
7601-
For example, a floating point ``ADD`` reduction without ``reassoc`` fast-math
7602-
flags may be vectorized provided that this metadata allows it. The first
7603-
operand is the string ``llvm.loop.vectorize.reassociation.enable``
7599+
This metadata selectively allows or disallows reassociating floating-point
7600+
reductions, which otherwise may be unsafe to reassociate, during the loop
7601+
vectorization. For example, a floating point ``ADD`` reduction without
7602+
``reassoc`` fast-math flags may be vectorized provided that this metadata
7603+
allows it. The first operand is the string
7604+
``llvm.loop.vectorize.reassociate_fpreductions.enable``
76047605
and the second operand is a bit. If the bit operand value is 1 unsafe
7605-
reassociations aqre enabled. A value of 0 disables unsafe reassociations.
7606+
reduction reassociations are enabled. A value of 0 disables unsafe
7607+
reduction reassociations.
76067608

76077609
.. code-block:: llvm
76087610

7609-
!0 = !{!"llvm.loop.vectorize.reassociation.enable", i1 0}
7610-
!1 = !{!"llvm.loop.vectorize.reassociation.enable", i1 1}
7611+
!0 = !{!"llvm.loop.vectorize.reassociate_fpreductions.enable", i1 0}
7612+
!1 = !{!"llvm.loop.vectorize.reassociate_fpreductions.enable", i1 1}
76117613

76127614
'``llvm.loop.vectorize.enable``' Metadata
76137615
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class LoopVectorizeHints {
6565
HK_ISVECTORIZED,
6666
HK_PREDICATE,
6767
HK_SCALABLE,
68-
HK_REASSOCIATE,
68+
HK_REASSOCIATE_FP_REDUCTIONS,
6969
};
7070

7171
/// Hint - associates name and validation with the hint value.
@@ -98,9 +98,9 @@ class LoopVectorizeHints {
9898
/// Says whether we should use fixed width or scalable vectorization.
9999
Hint Scalable;
100100

101-
/// Says whether unsafe reassociation of computations is allowed
101+
/// Says whether unsafe reassociation of reductions is allowed
102102
/// during the loop vectorization.
103-
Hint Reassociate;
103+
Hint ReassociateFPReductions;
104104

105105
/// Return the loop metadata prefix.
106106
static StringRef Prefix() { return "llvm.loop."; }
@@ -167,11 +167,11 @@ class LoopVectorizeHints {
167167
return (ScalableForceKind)Scalable.Value == SK_FixedWidthOnly;
168168
}
169169

170-
enum ForceKind getReassociate() const {
171-
if ((ForceKind)Reassociate.Value == FK_Undefined &&
170+
enum ForceKind getReassociateFPReductions() const {
171+
if ((ForceKind)ReassociateFPReductions.Value == FK_Undefined &&
172172
hasDisableAllTransformsHint(TheLoop))
173173
return FK_Disabled;
174-
return (ForceKind)Reassociate.Value;
174+
return (ForceKind)ReassociateFPReductions.Value;
175175
}
176176

177177
/// If hints are provided that force vectorization, use the AlwaysPrint
@@ -185,6 +185,10 @@ class LoopVectorizeHints {
185185
/// error accumulates in the loop.
186186
bool allowReordering() const;
187187

188+
/// Returns true iff the loop hints allow reassociating floating-point
189+
/// reductions for the purpose of vectorization.
190+
bool allowFPReductionReassociation() const;
191+
188192
bool isPotentiallyUnsafe() const {
189193
// Avoid FP vectorization if the target is unsure about proper support.
190194
// This may be related to the SIMD unit in the target not handling

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ bool LoopVectorizeHints::Hint::validate(unsigned Val) {
9797
case HK_ISVECTORIZED:
9898
case HK_PREDICATE:
9999
case HK_SCALABLE:
100-
case HK_REASSOCIATE:
100+
case HK_REASSOCIATE_FP_REDUCTIONS:
101101
return (Val == 0 || Val == 1);
102102
}
103103
return false;
@@ -113,8 +113,8 @@ LoopVectorizeHints::LoopVectorizeHints(const Loop *L,
113113
IsVectorized("isvectorized", 0, HK_ISVECTORIZED),
114114
Predicate("vectorize.predicate.enable", FK_Undefined, HK_PREDICATE),
115115
Scalable("vectorize.scalable.enable", SK_Unspecified, HK_SCALABLE),
116-
Reassociate("vectorize.reassociation.enable", FK_Undefined,
117-
HK_REASSOCIATE),
116+
ReassociateFPReductions("vectorize.reassociate_fpreductions.enable",
117+
FK_Undefined, HK_REASSOCIATE_FP_REDUCTIONS),
118118
TheLoop(L), ORE(ORE) {
119119
// Populate values with existing loop metadata.
120120
getHintsFromMetadata();
@@ -254,10 +254,14 @@ bool LoopVectorizeHints::allowReordering() const {
254254
ElementCount EC = getWidth();
255255
return HintsAllowReordering &&
256256
(getForce() == LoopVectorizeHints::FK_Enabled ||
257-
getReassociate() == LoopVectorizeHints::FK_Enabled ||
258257
EC.getKnownMinValue() > 1);
259258
}
260259

260+
bool LoopVectorizeHints::allowFPReductionReassociation() const {
261+
return HintsAllowReordering &&
262+
getReassociateFPReductions() == LoopVectorizeHints::FK_Enabled;
263+
}
264+
261265
void LoopVectorizeHints::getHintsFromMetadata() {
262266
MDNode *LoopID = TheLoop->getLoopID();
263267
if (!LoopID)
@@ -304,8 +308,13 @@ void LoopVectorizeHints::setHint(StringRef Name, Metadata *Arg) {
304308
return;
305309
unsigned Val = C->getZExtValue();
306310

307-
Hint *Hints[] = {&Width, &Interleave, &Force, &IsVectorized,
308-
&Predicate, &Scalable, &Reassociate};
311+
Hint *Hints[] = {&Width,
312+
&Interleave,
313+
&Force,
314+
&IsVectorized,
315+
&Predicate,
316+
&Scalable,
317+
&ReassociateFPReductions};
309318
for (auto *H : Hints) {
310319
if (Name == H->Name) {
311320
if (H->validate(Val))
@@ -1315,22 +1324,25 @@ bool LoopVectorizationLegality::canVectorizeFPMath(
13151324
return true;
13161325

13171326
// If the above is false, we have ExactFPMath & do not allow reordering.
1318-
// If the EnableStrictReductions flag is set, first check if we have any
1319-
// Exact FP induction vars, which we cannot vectorize.
1320-
if (!EnableStrictReductions ||
1321-
any_of(getInductionVars(), [&](auto &Induction) -> bool {
1327+
// First check if we have any Exact FP induction vars, which we cannot
1328+
// vectorize.
1329+
if (any_of(getInductionVars(), [&](auto &Induction) -> bool {
13221330
InductionDescriptor IndDesc = Induction.second;
13231331
return IndDesc.getExactFPMathInst();
13241332
}))
13251333
return false;
13261334

1327-
// We can now only vectorize if all reductions with Exact FP math also
1328-
// have the isOrdered flag set, which indicates that we can move the
1329-
// reduction operations in-loop.
1330-
return (all_of(getReductionVars(), [&](auto &Reduction) -> bool {
1331-
const RecurrenceDescriptor &RdxDesc = Reduction.second;
1332-
return !RdxDesc.hasExactFPMath() || RdxDesc.isOrdered();
1333-
}));
1335+
// We can now only vectorize if EnableStrictReductions flag is set and
1336+
// all reductions with Exact FP math also have the isOrdered flag set,
1337+
// which indicates that we can move the reduction operations in-loop.
1338+
// If the hints allow reassociating FP reductions, then skip
1339+
// all the checks.
1340+
return (Hints->allowFPReductionReassociation() ||
1341+
all_of(getReductionVars(), [&](auto &Reduction) -> bool {
1342+
const RecurrenceDescriptor &RdxDesc = Reduction.second;
1343+
return !RdxDesc.hasExactFPMath() ||
1344+
(EnableStrictReductions && RdxDesc.isOrdered());
1345+
}));
13341346
}
13351347

13361348
bool LoopVectorizationLegality::isInvariantStoreOfReduction(StoreInst *SI) {

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,9 +1000,10 @@ class LoopVectorizationCostModel {
10001000
/// Returns true if we should use strict in-order reductions for the given
10011001
/// RdxDesc. This is true if the -enable-strict-reductions flag is passed,
10021002
/// the IsOrdered flag of RdxDesc is set and we do not allow reordering
1003-
/// of FP operations.
1003+
/// of FP operations or FP reductions.
10041004
bool useOrderedReductions(const RecurrenceDescriptor &RdxDesc) const {
1005-
return !Hints->allowReordering() && RdxDesc.isOrdered();
1005+
return !Hints->allowReordering() &&
1006+
!Hints->allowFPReductionReassociation() && RdxDesc.isOrdered();
10061007
}
10071008

10081009
/// \returns The smallest bitwidth each instruction can be represented with.

llvm/test/Transforms/LoopVectorize/reduction-reassociate.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; Check that the loop with a floating-point reduction is vectorized
2-
; due to llvm.loop.vectorize.reassociation.enable metadata.
2+
; due to llvm.loop.vectorize.reassociate_fpreductions.enable metadata.
33
; RUN: opt -passes=loop-vectorize -S < %s 2>&1 | FileCheck %s
44

55
source_filename = "FIRModule"
@@ -40,8 +40,8 @@ attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) "ta
4040
!0 = !{!"flang version 21.0.0"}
4141
!1 = !{i32 2, !"Debug Info Version", i32 3}
4242
!2 = distinct !{!2, !3}
43-
!3 = !{!"llvm.loop.vectorize.reassociation.enable", i1 true}
43+
!3 = !{!"llvm.loop.vectorize.reassociate_fpreductions.enable", i1 true}
4444

45-
; CHECK-NOT: llvm.loop.vectorize.reassociation.enable
45+
; CHECK-NOT: llvm.loop.vectorize.reassociate_fpreductions.enable
4646
; CHECK: !{!"llvm.loop.isvectorized", i32 1}
4747
; CHECK: !{!"llvm.loop.unroll.runtime.disable"}

0 commit comments

Comments
 (0)