[PATCH] D95539: [LV] Add analysis remark for mixed precision conversions

Joseph Huber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 27 09:21:58 PST 2021


jhuber6 created this revision.
jhuber6 added reviewers: jdoerfert, bmahjour, SjoerdMeijer.
Herald added a subscriber: hiraditya.
jhuber6 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95539

Files:
  llvm/lib/Transforms/Vectorize/LoopVectorize.cpp


Index: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ 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<StoreInst>(&Inst)) {
+        if (!S->getPointerOperandType()->getPointerElementType()->isFloatTy())
+          continue;
+
+        // Traverse the uses of this floating point store inside the loop.
+        DenseMap<Instruction *, bool> Visited;
+        SmallVector<Instruction *, 8> 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<FPExtInst>(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<Instruction>(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<MDNode *> RemainderLoopID =


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95539.319597.patch
Type: text/x-patch
Size: 3057 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210127/16c2aa6c/attachment.bin>


More information about the llvm-commits mailing list