@@ -4274,12 +4274,37 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
42744274 *this, E, GetIntrinsicID(E->getArg(0)->getType()), "rdx.min"));
42754275 }
42764276
4277- case Builtin::BI__builtin_reduce_add:
4277+ case Builtin::BI__builtin_reduce_add: {
4278+ // Note: vector_reduce_fadd takes two arguments a
4279+ // scalar start value and a vector. That would mean to
4280+ // correctly call it we would need emitBuiltinWithOneOverloadedType<2>
4281+ // To keep the builtin sema behavior the same despite type we will
4282+ // popululate vector_reduce_fadd scalar value with a 0.
4283+ if (E->getArg(0)->getType()->hasFloatingRepresentation()) {
4284+ Value *X = EmitScalarExpr(E->getArg(0));
4285+ auto EltTy = X->getType()->getScalarType();
4286+ Value *Seed = ConstantFP::get(EltTy, 0);
4287+ return RValue::get(Builder.CreateIntrinsic(
4288+ /*ReturnType=*/EltTy, llvm::Intrinsic::vector_reduce_fadd,
4289+ ArrayRef<Value *>{Seed, X}, nullptr, "rdx.fadd"));
4290+ }
4291+ assert(E->getArg(0)->getType()->hasIntegerRepresentation());
42784292 return RValue::get(emitBuiltinWithOneOverloadedType<1>(
42794293 *this, E, llvm::Intrinsic::vector_reduce_add, "rdx.add"));
4280- case Builtin::BI__builtin_reduce_mul:
4294+ }
4295+ case Builtin::BI__builtin_reduce_mul: {
4296+ if (E->getArg(0)->getType()->hasFloatingRepresentation()) {
4297+ Value *X = EmitScalarExpr(E->getArg(0));
4298+ auto EltTy = X->getType()->getScalarType();
4299+ Value *Seed = ConstantFP::get(EltTy, 0);
4300+ return RValue::get(Builder.CreateIntrinsic(
4301+ /*ReturnType=*/EltTy, llvm::Intrinsic::vector_reduce_fmul,
4302+ ArrayRef<Value *>{Seed, X}, nullptr, "rdx.fmul"));
4303+ }
4304+ assert(E->getArg(0)->getType()->hasIntegerRepresentation());
42814305 return RValue::get(emitBuiltinWithOneOverloadedType<1>(
42824306 *this, E, llvm::Intrinsic::vector_reduce_mul, "rdx.mul"));
4307+ }
42834308 case Builtin::BI__builtin_reduce_xor:
42844309 return RValue::get(emitBuiltinWithOneOverloadedType<1>(
42854310 *this, E, llvm::Intrinsic::vector_reduce_xor, "rdx.xor"));
0 commit comments