[llvm] [InstCombine] Fold binary op of reductions. (PR #121567)
Mikhail Gudim via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 8 07:34:10 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;
----------------
mgudim wrote:
I don't think we should. `getReductionForBinop` return reduction corresponding to binop. And in principle can be used in other places in the future. There is no reduction correspongind to `Sub`.
https://github.com/llvm/llvm-project/pull/121567
More information about the llvm-commits
mailing list