[llvm] [InstCombine] Fold binary op of reductions. (PR #121567)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 3 05:27:18 PST 2025
================
@@ -2296,6 +2296,58 @@ Instruction *InstCombinerImpl::foldVectorBinop(BinaryOperator &Inst) {
return nullptr;
}
+static Intrinsic::ID getReductionForBinop(Instruction::BinaryOps Opc) {
+ switch (Opc) {
+ default:
+ break;
+ case Instruction::Add:
+ return Intrinsic::vector_reduce_add;
+ case Instruction::Mul:
+ return Intrinsic::vector_reduce_mul;
+ case Instruction::And:
+ return Intrinsic::vector_reduce_and;
+ case Instruction::Or:
+ return Intrinsic::vector_reduce_or;
+ case Instruction::Xor:
+ return Intrinsic::vector_reduce_xor;
+ }
+ return Intrinsic::num_intrinsics;
+}
+
+Instruction *InstCombinerImpl::foldBinopOfReductions(BinaryOperator &Inst) {
+ IntrinsicInst *II0 = dyn_cast<IntrinsicInst>(Inst.getOperand(0));
+ if (!II0)
+ return nullptr;
+ IntrinsicInst *II1 = dyn_cast<IntrinsicInst>(Inst.getOperand(1));
+ if (!II1)
+ return nullptr;
+
+ Instruction::BinaryOps BinOpOpc = Inst.getOpcode();
+ Intrinsic::ID ReductionIID = getReductionForBinop(BinOpOpc);
+ if (BinOpOpc == Instruction::Sub)
+ ReductionIID = Intrinsic::vector_reduce_add;
+
+ if (ReductionIID == Intrinsic::num_intrinsics)
+ return nullptr;
+ if (II0->getIntrinsicID() != ReductionIID)
+ return nullptr;
+ if (II1->getIntrinsicID() != ReductionIID)
+ return nullptr;
+
+ Value *V0 = II0->getArgOperand(0);
+ Value *V1 = II1->getArgOperand(0);
+ Type *VTy = V0->getType();
+ if (V1->getType() != VTy)
+ return nullptr;
+
+ Value *VectorBO = Builder.CreateBinOp(BinOpOpc, V0, V1);
+ // if (auto *VectorInstBO = dyn_cast<BinaryOperator>(VectorBO))
----------------
dtcxzyw wrote:
I don't think it is possible to propagate these flags.
https://github.com/llvm/llvm-project/pull/121567
More information about the llvm-commits
mailing list