[clang] [llvm] [LV] Support generating masks for switch terminators. (PR #99808)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 24 09:14:11 PDT 2024


================
@@ -7763,6 +7763,41 @@ VPValue *VPRecipeBuilder::createEdgeMask(BasicBlock *Src, BasicBlock *Dst) {
 
   VPValue *SrcMask = getBlockInMask(Src);
 
+  if (auto *SI = dyn_cast<SwitchInst>(Src->getTerminator())) {
+    // Create mask where the terminator in Src is a switch. We need to handle 2
+    // separate cases:
+    // 1. Dst is not the default desintation. Dst is reached if any of the cases
+    // with destination == Dst are taken. Join the conditions for each case
+    // where destination == Dst using a logical OR.
+    // 2. Dst is the default destination. Dst is reached if none of the cases
+    // with destination != Dst are taken. Join the conditions for each case
+    // where the destination is != Dst using a logical OR and negate it.
+    VPValue *Mask = nullptr;
+    VPValue *Cond = getVPValueOrAddLiveIn(SI->getCondition(), Plan);
+    bool IsDefault = SI->getDefaultDest() == Dst;
+    for (auto &C : SI->cases()) {
----------------
ayalz wrote:

Note that the number of cases N may be large. All N cases are traversed here per successor, potentially in O(N^2). This potential compile-time concern could be addressed by building a reverse mapping from destinations to conditions, or creating all edge masks from Src to all its successors together.
There's also a potential cost/profitability concern: as N increases the performance advantage of vectorizing diminishes, as it applies if-conversion with a mask per successor, while the original scalar version may use table lookups or cascading conditional branches. Worth bounding the number of cases to something "reasonable", and/or checking that CM considers the associated cost "accurately"?

https://github.com/llvm/llvm-project/pull/99808


More information about the cfe-commits mailing list