Index: llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h =================================================================== --- llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h +++ llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h @@ -313,6 +313,8 @@ // Returns true if the NoNaN attribute is set on the function. bool hasFunNoNaNAttr() const { return HasFunNoNaNAttr; } + bool isLegalToInterleave(); + private: /// Return true if the pre-header, exiting and latch blocks of \p Lp and all /// its nested loops are considered legal for vectorization. These legal Index: llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp =================================================================== --- llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp +++ llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp @@ -1263,4 +1263,18 @@ return true; } +bool LoopVectorizationLegality::isLegalToInterleave() { + // Check if reduction has invalid type for interleaving. + for (auto &Reduction : *getReductionVars()) { + auto Descriptor = Reduction.second; + auto ComputedType = Descriptor.getRecurrenceType(); + auto IsSigned = Descriptor.isSigned(); + // Eliminate degenerate types. + if (ComputedType->isIntegerTy(1) && IsSigned) { + return false; + } + } + return true; +} + } // namespace llvm Index: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp =================================================================== --- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -8066,6 +8066,13 @@ IC = CM.selectInterleaveCount(VF.Width, VF.Cost, LVI); } + // Check if it is legal to interleave the loop. + if (IC > 1 && !LVL.isLegalToInterleave()) { + LLVM_DEBUG(dbgs() << "LV: Not vectorizing: Can't interleave reduction.\n"); + Hints.emitRemarkWithHints(); + return false; + } + // Identify the diagnostic messages that should be produced. std::pair VecDiagMsg, IntDiagMsg; bool VectorizeLoop = true, InterleaveLoop = true;