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,53 @@ 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. +void checkMixedPrecision(Loop *L, OptimizationRemarkEmitter *ORE) { + for (BasicBlock *BB : L->getBlocks()) { + for (Instruction &Inst : *BB) { + if (auto *S = dyn_cast(&Inst)) { + if (!S->getPointerOperandType()->getPointerElementType()->isFloatTy()) + continue; + + // Traverse the uses of this floating point store inside the loop. + DenseMap Visited; + SmallVector Worklist; + Worklist.push_back(S); + while (!Worklist.empty()) { + auto *I = Worklist.pop_back_val(); + if (!L->contains(I)) + continue; + if (Visited[I]) + continue; + Visited[I] = true; + + // 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)) + 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), @@ -9586,6 +9633,7 @@ DisableRuntimeUnroll = true; } + // runtime checks that get used. // Report the vectorization decision. ORE->emit([&]() { return OptimizationRemark(LV_NAME, "Vectorized", L->getStartLoc(), @@ -9594,6 +9642,10 @@ << NV("VectorizationFactor", VF.Width) << ", interleaved count: " << NV("InterleaveCount", IC) << ")"; }); + + auto &Ctx = L->getHeader()->getModule()->getContext(); + if (Ctx.getDiagHandlerPtr()->isAnyRemarkEnabled(LV_NAME)) + checkMixedPrecision(L, ORE); } Optional RemainderLoopID =