[llvm-dev] LoopVectorize fails to vectorize code with condition on reduction

Alex Susu via llvm-dev llvm-dev at lists.llvm.org
Mon Jun 11 02:40:33 PDT 2018


   Hello.
     I'm not able to vectorize this simple C loop doing basically what could be called 
predicated sum-reduction:
       #define NMAX 1000
       int colOccupied[NMAX];
       void Func(int N) {
         int numSol = 0;
         for (int c = 0; c < N; c++) {
             if (colOccupied[c] == 0)
                 numSol++;
         }
         return numSol;
       }

     The compiler command I used was:
         clang -O3 -emit-llvm -S -mllvm -debug -ffast-math A.cpp -fvectorize -mllvm 
-force-vector-width=16

     Following is the output of LLVM's opt explaining why it fails to vectorize:
        LV: Checking a loop in "_Z6Queensi" from A_DFS_last_level_wo_rec.cpp
        LV: Loop hints: force=? width=16 unroll=0
        LV: Found a loop: for.body
        LV: PHI is not a poly recurrence.
        LV: PHI is not a poly recurrence.
        LV: Found an unidentified PHI.  %2 = phi i32 [ 0, %for.body.lr.ph ], [ %4, %for.inc ]
        LV: Can't vectorize the instructions or CFG
        LV: Not vectorizing: Cannot prove legality.

   This was obtained with the latest LLVM - SVN rev 334367, from Jun 10 2018.

   Could you please tell me why do you think LoopVectorize is not being able to vectorize 
the above C code.


   BTW, I was hoping that LoopVectorize can change the code to be amenable to simple 
if-conversion like the following C code manually transformed:
       void Func_manually_transformed(int N) {
         int numSol = 0;
         for (int c = 0; c < N; c++) {
             if (colOccupied[c] == 0)
                 select[c] = 1;
             else
                 select[c] = 0;
             numSol += select[c];
         }
         return numSol;
       }
     Note that this manually transformed code is being vectorized by LLVM.

   Thank you,
     Alex


More information about the llvm-dev mailing list