@@ -52,59 +52,14 @@ static Value buildZero(OpBuilder &b, Location loc, Type elemType) {
5252
5353static Value buildReduceIdentity (OpBuilder &b, Location loc,
5454 ReduceMethod method, Type elemType) {
55- if (isa<FloatType>(elemType)) {
56- const auto &sem = cast<FloatType>(elemType).getFloatSemantics ();
57- switch (method) {
58- case ReduceMethod::ADD :
59- return b.create <arith::ConstantOp>(
60- loc, FloatAttr::get (elemType, APFloat::getZero (sem)));
61- case ReduceMethod::MUL :
62- return b.create <arith::ConstantOp>(
63- loc, FloatAttr::get (elemType, APFloat (sem, 1 )));
64- case ReduceMethod::MAX :
65- return b.create <arith::ConstantOp>(
66- loc,
67- FloatAttr::get (elemType, APFloat::getInf (sem, /* negative=*/ true )));
68- case ReduceMethod::MIN :
69- return b.create <arith::ConstantOp>(
70- loc,
71- FloatAttr::get (elemType, APFloat::getInf (sem, /* negative=*/ false )));
72- }
73- }
74- unsigned width = cast<IntegerType>(elemType).getWidth ();
75- switch (method) {
76- case ReduceMethod::ADD :
77- return b.create <arith::ConstantIntOp>(loc, elemType, 0 );
78- case ReduceMethod::MUL :
79- return b.create <arith::ConstantIntOp>(loc, elemType, 1 );
80- case ReduceMethod::MAX :
81- return b.create <arith::ConstantOp>(
82- loc, IntegerAttr::get (elemType, APInt::getSignedMinValue (width)));
83- case ReduceMethod::MIN :
84- return b.create <arith::ConstantOp>(
85- loc, IntegerAttr::get (elemType, APInt::getSignedMaxValue (width)));
86- }
87- llvm_unreachable (" unknown ReduceMethod" );
55+ auto arithConst = cinm::getArithConstant (method, elemType);
56+ return arith::getIdentityValue (arithConst, elemType, b, loc);
8857}
8958
9059static Value emitReduceCombine (OpBuilder &b, Location loc, ReduceMethod method,
9160 Value elem, Value acc, Type elemType) {
92- bool isFloat = isa<FloatType>(elemType);
93- switch (method) {
94- case ReduceMethod::ADD :
95- return isFloat ? b.create <arith::AddFOp>(loc, elem, acc).getResult ()
96- : b.create <arith::AddIOp>(loc, elem, acc).getResult ();
97- case ReduceMethod::MUL :
98- return isFloat ? b.create <arith::MulFOp>(loc, elem, acc).getResult ()
99- : b.create <arith::MulIOp>(loc, elem, acc).getResult ();
100- case ReduceMethod::MAX :
101- return isFloat ? b.create <arith::MaxNumFOp>(loc, elem, acc).getResult ()
102- : b.create <arith::MaxSIOp>(loc, elem, acc).getResult ();
103- case ReduceMethod::MIN :
104- return isFloat ? b.create <arith::MinNumFOp>(loc, elem, acc).getResult ()
105- : b.create <arith::MinSIOp>(loc, elem, acc).getResult ();
106- }
107- llvm_unreachable (" unknown ReduceMethod" );
61+ auto arithConst = cinm::getArithConstant (method, elemType);
62+ return arith::getReductionOp (arithConst, b, loc, elem, acc);
10863}
10964
11065// Build the outs init for a gemm-like op, in priority order:
@@ -409,7 +364,8 @@ struct ConvertGemvToLinalg : public OpConversionPattern<cinm::GemvOp> {
409364
410365 auto loc = op.getLoc ();
411366 auto resultTy = cast<RankedTensorType>(op.getResult ().getType ());
412- Value init = buildGemmInit (rewriter, loc, adaptor.getOut (), adaptor.getBias (), resultTy);
367+ Value init = buildGemmInit (rewriter, loc, adaptor.getOut (),
368+ adaptor.getBias (), resultTy);
413369
414370 Value result = rewriter
415371 .create <linalg::MatvecOp>(
@@ -437,7 +393,8 @@ struct ConvertGemmToLinalg : public OpConversionPattern<cinm::GemmOp> {
437393
438394 auto loc = op.getLoc ();
439395 auto resultTy = cast<RankedTensorType>(op.getResult ().getType ());
440- Value init = buildGemmInit (rewriter, loc, adaptor.getOut (), adaptor.getBias (), resultTy);
396+ Value init = buildGemmInit (rewriter, loc, adaptor.getOut (),
397+ adaptor.getBias (), resultTy);
441398
442399 Value result = rewriter
443400 .create <linalg::MatmulOp>(
@@ -466,7 +423,8 @@ struct ConvertBatchGemmToLinalg
466423
467424 auto loc = op.getLoc ();
468425 auto resultTy = cast<RankedTensorType>(op.getResult ().getType ());
469- Value init = buildGemmInit (rewriter, loc, adaptor.getOut (), adaptor.getBias (), resultTy);
426+ Value init = buildGemmInit (rewriter, loc, adaptor.getOut (),
427+ adaptor.getBias (), resultTy);
470428
471429 Value result = rewriter
472430 .create <linalg::BatchMatmulOp>(
@@ -513,7 +471,8 @@ struct ConvertBatchGemvToLinalg
513471 linalg::IteratorTypeAttr::get (ctx, utils::IteratorType::reduction),
514472 };
515473
516- Value init = buildGemmInit (rewriter, loc, adaptor.getOut (), adaptor.getBias (), resultTy);
474+ Value init = buildGemmInit (rewriter, loc, adaptor.getOut (),
475+ adaptor.getBias (), resultTy);
517476 auto generic = rewriter.create <linalg::GenericOp>(
518477 loc, TypeRange{resultTy},
519478 ValueRange{adaptor.getLhs (), adaptor.getRhs ()}, ValueRange{init},
0 commit comments