[llvm] c3a3d20 - [LV] Add analysis remark for mixed precision conversions
Joseph Huber via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 17 18:38:34 PST 2021
Author: Joseph Huber
Date: 2021-02-17T21:37:08-05:00
New Revision: c3a3d200932347837283019a3870f185734f702d
URL: https://github.com/llvm/llvm-project/commit/c3a3d200932347837283019a3870f185734f702d
DIFF: https://github.com/llvm/llvm-project/commit/c3a3d200932347837283019a3870f185734f702d.diff
LOG: [LV] Add analysis remark for mixed precision conversions
Floating point conversions inside vectorized loops have performance
implications but are very subtle. The user could specify a floating
point constant, or call a function without realizing that it will
force a change in the vector width. An example of this behaviour is
seen in https://godbolt.org/z/M3nT6c . The vectorizer should indicate
when this happens becuase it is most likely unintended behaviour.
This patch adds a simple check for this behaviour by following floating
point stores in the original loop and checking if a floating point
conversion operation occurs.
Reviewed By: fhahn
Differential Revision: https://reviews.llvm.org/D95539
Added:
llvm/test/Transforms/LoopVectorize/mixed-precision-remarks.ll
Modified:
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index a88c2b63eb4c..109686c7742c 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -9441,6 +9441,51 @@ static bool processLoopInVPlanNativePath(
return true;
}
+// Emit a remark if there are stores to floats that required a floating point
+// extension. If the vectorized loop was generated with floating point there
+// will be a performance penalty from the conversion overhead and the change in
+// the vector width.
+static void checkMixedPrecision(Loop *L, OptimizationRemarkEmitter *ORE) {
+ SmallVector<Instruction *, 4> Worklist;
+ for (BasicBlock *BB : L->getBlocks()) {
+ for (Instruction &Inst : *BB) {
+ if (auto *S = dyn_cast<StoreInst>(&Inst)) {
+ if (S->getValueOperand()->getType()->isFloatTy())
+ Worklist.push_back(S);
+ }
+ }
+ }
+
+ // Traverse the floating point stores upwards searching, for floating point
+ // conversions.
+ SmallPtrSet<const Instruction *, 4> Visited;
+ SmallPtrSet<const Instruction *, 4> EmittedRemark;
+ while (!Worklist.empty()) {
+ auto *I = Worklist.pop_back_val();
+ if (!L->contains(I))
+ continue;
+ if (!Visited.insert(I).second)
+ continue;
+
+ // Emit a remark if the floating point store required a floating
+ // point conversion.
+ // TODO: More work could be done to identify the root cause such as a
+ // constant or a function return type and point the user to it.
+ if (isa<FPExtInst>(I) && EmittedRemark.insert(I).second)
+ ORE->emit([&]() {
+ return OptimizationRemarkAnalysis(LV_NAME, "VectorMixedPrecision",
+ I->getDebugLoc(), L->getHeader())
+ << "floating point conversion changes vector width. "
+ << "Mixed floating point precision requires an up/down "
+ << "cast that will negatively impact performance.";
+ });
+
+ for (Use &Op : I->operands())
+ if (auto *OpI = dyn_cast<Instruction>(Op))
+ Worklist.push_back(OpI);
+ }
+}
+
LoopVectorizePass::LoopVectorizePass(LoopVectorizeOptions Opts)
: InterleaveOnlyWhenForced(Opts.InterleaveOnlyWhenForced ||
!EnableLoopInterleaving),
@@ -9759,6 +9804,9 @@ bool LoopVectorizePass::processLoop(Loop *L) {
<< NV("VectorizationFactor", VF.Width)
<< ", interleaved count: " << NV("InterleaveCount", IC) << ")";
});
+
+ if (ORE->allowExtraAnalysis(LV_NAME))
+ checkMixedPrecision(L, ORE);
}
Optional<MDNode *> RemainderLoopID =
diff --git a/llvm/test/Transforms/LoopVectorize/mixed-precision-remarks.ll b/llvm/test/Transforms/LoopVectorize/mixed-precision-remarks.ll
new file mode 100644
index 000000000000..7e708c66c00c
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/mixed-precision-remarks.ll
@@ -0,0 +1,69 @@
+; RUN: opt -force-vector-interleave=2 -force-vector-width=4 -loop-vectorize -pass-remarks-analysis=loop-vectorize -disable-output < %s 2>&1 | FileCheck %s
+
+; CHECK: remark: mixed-precision.c:3:26: floating point conversion changes vector width. Mixed floating point precision requires an up/down cast that will negatively impact performance.
+define void @f(float* noalias nocapture %X, i64 %N) {
+entry:
+ br label %for.body
+
+for.cond.cleanup:
+ ret void
+
+for.body:
+ %i = phi i64 [ %inc, %for.body ], [ 0, %entry ]
+ %arrayidx = getelementptr inbounds float, float* %X, i64 %i
+ %0 = load float, float* %arrayidx, align 4
+ %conv = fpext float %0 to double, !dbg !9
+ %mul = fmul double %conv, 0x3FD5555555555555
+ %conv3 = fptrunc double %mul to float
+ store float %conv3, float* %arrayidx, align 4
+ %inc = add nuw i64 %i, 1
+ %exitcond.not = icmp eq i64 %inc, %N
+ br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
+}
+
+; CHECK: remark: mixed-precision.c:8:8: floating point conversion changes vector width. Mixed floating point precision requires an up/down cast that will negatively impact performance.
+; CHECK: remark: mixed-precision.c:7:16: floating point conversion changes vector width. Mixed floating point precision requires an up/down cast that will negatively impact performance.
+; CHECK-NOT: remark: mixed-precision.c:7:16: floating point conversion changes vector width. Mixed floating point precision requires an up/down cast that will negatively impact performance.
+define void @g(float* noalias nocapture %X, float* noalias nocapture %Y, i64 %N) {
+entry:
+ %pi = alloca double
+ store double 0x400921FB54442D18, double* %pi
+ %fac = load double, double* %pi
+ br label %for.body
+
+for.body:
+ %i = phi i64 [ %inc, %for.body ], [ 0, %entry ]
+ %arrayidx = getelementptr inbounds float, float* %X, i64 %i
+ %0 = load float, float* %arrayidx, align 4
+ %conv = fpext float %0 to double, !dbg !10
+ %mul = fmul double %conv, %fac
+ %conv1 = fptrunc double %mul to float
+ store float %conv1, float* %arrayidx, align 4
+ %arrayidx5 = getelementptr inbounds float, float* %Y, i64 %i
+ %1 = load float, float* %arrayidx5, align 4
+ %conv2 = fpext float %1 to double, !dbg !11
+ %mul2 = fmul double %conv2, %fac
+ %conv3 = fptrunc double %mul2 to float
+ store float %conv3, float* %arrayidx5, align 4
+ %inc = add nuw nsw i64 %i, 1
+ %exitcond.not = icmp eq i64 %inc, %N
+ br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
+
+for.cond.cleanup:
+ ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "mixed-precision.c", directory: "/tmp/mixed-precision.c")
+!2 = !{}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = !{i32 1, !"wchar_size", i32 4}
+!6 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
+!7 = distinct !DISubprogram(name: "g", scope: !1, file: !1, line: 5, type: !8, scopeLine: 5, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
+!8 = !DISubroutineType(types: !2)
+!9 = !DILocation(line: 3, column: 26, scope: !6)
+!10 = !DILocation(line: 7, column: 16, scope: !7)
+!11 = !DILocation(line: 8, column: 8, scope: !7)
More information about the llvm-commits
mailing list