diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -9276,6 +9276,51 @@ 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 Worklist; + for (BasicBlock *BB : L->getBlocks()) { + for (Instruction &Inst : *BB) { + if (auto *S = dyn_cast(&Inst)) { + if (S->getValueOperand()->getType()->isFloatTy()) + Worklist.push_back(S); + } + } + } + + // Traverse the floating point stores upwards searching, for floating point + // conversions. + SmallPtrSet Visited; + SmallPtrSet 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(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(Op)) + Worklist.push_back(OpI); + } +} + LoopVectorizePass::LoopVectorizePass(LoopVectorizeOptions Opts) : InterleaveOnlyWhenForced(Opts.InterleaveOnlyWhenForced || !EnableLoopInterleaving), @@ -9594,6 +9639,9 @@ << NV("VectorizationFactor", VF.Width) << ", interleaved count: " << NV("InterleaveCount", IC) << ")"; }); + + if (ORE->allowExtraAnalysis(LV_NAME)) + checkMixedPrecision(L, ORE); } Optional 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 --- /dev/null +++ b/llvm/test/Transforms/LoopVectorize/mixed-precision-remarks.ll @@ -0,0 +1,75 @@ +; RUN: opt -loop-vectorize -pass-remarks-analysis=loop-vectorize -disable-output < %s 2>&1 | FileCheck %s +; ModuleID = 'mixed-precision.c' +source_filename = "mixed-precision.c" +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; void f(float * restrict X, unsigned long N) { +; __builtin_assume(N > 32 && N % 32 == 0); +; for (unsigned long i = 0; i < N; ++i) +; X[i] = (1.0 / 3.0) * X[i]; +; } + +; CHECK: remark: mixed-precision.c:4:26: floating point conversion changes vector width. Mixed floating point precision requires an up/down cast that will negatively impact performance. +define dso_local void @f(float* noalias nocapture %X, i64 %N) local_unnamed_addr #0 !dbg !6 { +entry: + %cmp = icmp ugt i64 %N, 32, !dbg !8 + %rem = and i64 %N, 31, !dbg !9 + %cmp1 = icmp eq i64 %rem, 0, !dbg !9 + call void @llvm.assume(i1 %cmp), !dbg !10 + call void @llvm.assume(i1 %cmp1), !dbg !10 + br label %for.body, !dbg !11 + +for.cond.cleanup: ; preds = %for.body + ret void, !dbg !12 + +for.body: ; preds = %entry, %for.body + %i.013 = phi i64 [ %inc, %for.body ], [ 0, %entry ] + %arrayidx = getelementptr inbounds float, float* %X, i64 %i.013, !dbg !13 + %0 = load float, float* %arrayidx, align 4, !dbg !13, !tbaa !14 + %conv = fpext float %0 to double, !dbg !13 + %mul = fmul double %conv, 0x3FD5555555555555, !dbg !18 + %conv3 = fptrunc double %mul to float, !dbg !19 + store float %conv3, float* %arrayidx, align 4, !dbg !20, !tbaa !14 + %inc = add nuw i64 %i.013, 1, !dbg !21 + %exitcond.not = icmp eq i64 %inc, %N, !dbg !22 + br i1 %exitcond.not, label %for.cond.cleanup, label %for.body, !dbg !11, !llvm.loop !23 +} + +; Function Attrs: nofree nosync nounwind willreturn +declare void @llvm.assume(i1 noundef) #1 + +attributes #0 = { nofree nounwind uwtable "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nofree nosync nounwind willreturn } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4} +!llvm.ident = !{!5} + +!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} +!5 = !{!"clang version 12.0.0"} +!6 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !7, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2) +!7 = !DISubroutineType(types: !2) +!8 = !DILocation(line: 2, column: 22, scope: !6) +!9 = !DILocation(line: 2, column: 27, scope: !6) +!10 = !DILocation(line: 2, column: 3, scope: !6) +!11 = !DILocation(line: 3, column: 3, scope: !6) +!12 = !DILocation(line: 6, column: 1, scope: !6) +!13 = !DILocation(line: 4, column: 26, scope: !6) +!14 = !{!15, !15, i64 0} +!15 = !{!"float", !16, i64 0} +!16 = !{!"omnipotent char", !17, i64 0} +!17 = !{!"Simple C/C++ TBAA"} +!18 = !DILocation(line: 4, column: 24, scope: !6) +!19 = !DILocation(line: 4, column: 12, scope: !6) +!20 = !DILocation(line: 4, column: 10, scope: !6) +!21 = !DILocation(line: 3, column: 36, scope: !6) +!22 = !DILocation(line: 3, column: 31, scope: !6) +!23 = distinct !{!23, !11, !24, !25, !26} +!24 = !DILocation(line: 5, column: 3, scope: !6) +!25 = !{!"llvm.loop.mustprogress"} +!26 = !{!"llvm.loop.unroll.disable"}