[PATCH] D139074: Vectorization Of Conditional Statements Using BOSCC

Ashutosh Nema via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 30 22:20:37 PST 2022


Ashutosh created this revision.
Ashutosh added reviewers: llvm-commits, Ayal, rengolin, tim.schmielau, gilr.
Herald added subscribers: nlopes, rogfer01, bollu, hiraditya.
Herald added a project: All.
Ashutosh requested review of this revision.
Herald added subscribers: pcwang-thead, vkmr.
Herald added a project: LLVM.

This is a proposal about implementing vectorization of conditional statements using BOSCC(branches-on-superword-condition-codes).

Current LLVM's Loop vectorization deploy flattning of control flow where the guarded code is executed in all the paths with the help of predicate mask instructions. In many cases the execution of the instructions in all control paths is not optimal.

BOSCC inserts branches that skip a region if the predicate of the region entry evaluates to false.

Consider below loop:

  for (unsigned i = 0; i < len; i++) {
    if (X[i]) {
      A[i] = B[i] + C[i];
    } else {
      D[i] = E[i] * F[i];
    }
  }

Existing LLVM Flatten Style Vectorization:

  for (unsigned i = 0; i < len; i+=4) {
    VectorMask = (X[i,i+1,i+2,i+3] != <0,0,0,0>);
    FlipVectorMask = XOR VectorMask <true, true, true, true>
    Mask.Vector.Store.A[i,i+1,i+2,i+3] = Mask.Vector.Load.B[i,i+1,i+2,i+3] 
                                       + Mask.Vector.Load.C[i,i+1,i+2,i+3]; // Based on VectorMask
    Mask.Vector.Store.D[i,i+1,i+2,i+3] = Mask.Vector.Load.E[i,i+1,i+2,i+3] 
                                       * Mask.Vector.Load.F[i,i+1,i+2,i+3]; // Based on FlipVectorMask
  }

BOSCC Style Vectorization:

  for (unsigned i = 0; i < len; i+=4) {
    VectorMask = (X[i,i+1,i+2,i+3] != <0,0,0,0>);
    VectorMaskScalar = VectorToScalarCast VectorMask;
  
    if (VectorMaskScalar) {
      Mask.Vector.Store.A[i,i+1,i+2,i+3] = Mask.Vector.Load.B[i,i+1,i+2,i+3] 
                                         + Mask.Vector.Load.C[i,i+1,i+2,i+3]; // Based on VectorMask
   }
  
    FlipVectorMask = XOR VectorMask <true, true, true, true>
    FlipVectorMaskScalar = VectorToScalarCast FlipVectorMask;
  
    if (FlipVectorMaskScalar) {
      Mask.Vector.Store.D[i,i+1,i+2,i+3] = Mask.Vector.Load.E[i,i+1,i+2,i+3] 
                                         * Mask.Vector.Load.F[i,i+1,i+2,i+3]; // Based on FlipVectorMask
    }
  }

Under this change we introduce the following:
1: BOSCCBlockPlanner : Facilitates to generate the required block layout for BOSCC blocks during VPlan.
2: New Recipes:

  a: VPBranchOnBOSCCGuardSC : This recipe is responsible for generating the required conditional entry check on a vector block.
  b: VPBOSCCLiveOutRecipe : This recipe is responsible to generate PHI for the live out from the guarded vector blocks.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139074

Files:
  llvm/include/llvm/Analysis/VectorUtils.h
  llvm/lib/Analysis/VectorUtils.cpp
  llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
  llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
  llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
  llvm/lib/Transforms/Vectorize/VPlan.cpp
  llvm/lib/Transforms/Vectorize/VPlan.h
  llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
  llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
  llvm/lib/Transforms/Vectorize/VPlanValue.h
  llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp
  llvm/test/Transforms/LoopVectorize/boscc0.ll
  llvm/test/Transforms/LoopVectorize/boscc1.ll
  llvm/test/Transforms/LoopVectorize/boscc2.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139074.479169.patch
Type: text/x-patch
Size: 70602 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221201/3c818ef2/attachment-0001.bin>


More information about the llvm-commits mailing list