@@ -92,12 +92,12 @@ class ConvertAtenMinMaxDimOp : public OpConversionPattern<OpTy> {
9292 return rewriter.notifyMatchFailure (op, " dim is not a valid dim" );
9393
9494 Type inElementType = inputType.getElementType ();
95- bool isUnsigned = false ;
95+ bool useUnsigned = false ;
9696 if (!isa<mlir::FloatType>(inElementType)) {
9797 if (isa<mlir::IntegerType>(inElementType)) {
98- auto integerTy = dyn_cast<mlir::IntegerType>(
98+ auto torchIntTy = dyn_cast<mlir::IntegerType>(
9999 cast<BaseTensorType>(op.getSelf ().getType ()).getDtype ());
100- isUnsigned = integerTy. isUnsigned ( );
100+ useUnsigned = useUnsignedIntegerSemantics (torchIntTy );
101101 } else {
102102 return rewriter.notifyMatchFailure (
103103 op, opName + " to linalg.* requires Float or Integer "
@@ -129,15 +129,15 @@ class ConvertAtenMinMaxDimOp : public OpConversionPattern<OpTy> {
129129 inElementType,
130130 getFloatInf (cast<mlir::FloatType>(inElementType),
131131 /* Negative=*/ isMax, this ->allowNonFinites )));
132- } else if (!isUnsigned) {
133- auto width = cast<mlir::IntegerType>(inElementType).getWidth ();
134- auto init = isMax ? APSInt::getSignedMinValue (width)
135- : APSInt::getSignedMaxValue (width);
136- fillValue = arith::ConstantOp::create (
137- rewriter, loc, rewriter.getIntegerAttr (inElementType, init));
138- } else if (isUnsigned) {
132+ } else {
139133 auto width = cast<mlir::IntegerType>(inElementType).getWidth ();
140- auto init = isMax ? APInt::getMinValue (width) : APInt::getMaxValue (width);
134+ APInt init;
135+ if (useUnsigned) {
136+ init = isMax ? APInt::getMinValue (width) : APInt::getMaxValue (width);
137+ } else {
138+ init = isMax ? APSInt::getSignedMinValue (width)
139+ : APSInt::getSignedMaxValue (width);
140+ }
141141 fillValue = arith::ConstantOp::create (
142142 rewriter, loc, rewriter.getIntegerAttr (inElementType, init));
143143 }
@@ -198,19 +198,19 @@ class ConvertAtenMinMaxDimOp : public OpConversionPattern<OpTy> {
198198 } else {
199199 arith::CmpIPredicate predType;
200200 if (isMax) {
201- predType = isUnsigned ? arith::CmpIPredicate::ugt
202- : arith::CmpIPredicate::sgt;
203- if (isUnsigned ) {
201+ predType = useUnsigned ? arith::CmpIPredicate::ugt
202+ : arith::CmpIPredicate::sgt;
203+ if (useUnsigned ) {
204204 resultVal = arith::MaxUIOp::create (rewriter, nestedLoc,
205205 newValue, oldValue);
206206 } else {
207207 resultVal = arith::MaxSIOp::create (rewriter, nestedLoc,
208208 newValue, oldValue);
209209 }
210210 } else {
211- predType = isUnsigned ? arith::CmpIPredicate::ult
212- : arith::CmpIPredicate::slt;
213- if (isUnsigned ) {
211+ predType = useUnsigned ? arith::CmpIPredicate::ult
212+ : arith::CmpIPredicate::slt;
213+ if (useUnsigned ) {
214214 resultVal = arith::MinUIOp::create (rewriter, nestedLoc,
215215 newValue, oldValue);
216216 } else {
@@ -318,12 +318,15 @@ static Value createInitElementForReduceOp(OpBuilder &b, Location loc,
318318 getFloatInf (cast<mlir::FloatType>(elementType),
319319 /* Negative=*/ true , allowNonFinites)));
320320 else if (isa<mlir::IntegerType>(elementType) &&
321- elementType.getIntOrFloatBitWidth () != 8 )
322- return arith::ConstantOp::create (
323- b, loc,
324- b.getIntegerAttr (
325- elementType,
326- APSInt::getSignedMinValue (elementType.getIntOrFloatBitWidth ())));
321+ elementType.getIntOrFloatBitWidth () != 8 ) {
322+ unsigned width = elementType.getIntOrFloatBitWidth ();
323+ auto init =
324+ useUnsignedIntegerSemantics (cast<mlir::IntegerType>(elementType))
325+ ? APInt::getMinValue (width)
326+ : APSInt::getSignedMinValue (width);
327+ return arith::ConstantOp::create (b, loc,
328+ b.getIntegerAttr (elementType, init));
329+ }
327330 }
328331
329332 if (isa<AtenMinOp>(op)) {
@@ -334,12 +337,15 @@ static Value createInitElementForReduceOp(OpBuilder &b, Location loc,
334337 getFloatInf (cast<mlir::FloatType>(elementType),
335338 /* Negative=*/ false , allowNonFinites)));
336339 else if (isa<mlir::IntegerType>(elementType) &&
337- elementType.getIntOrFloatBitWidth () != 8 )
338- return arith::ConstantOp::create (
339- b, loc,
340- b.getIntegerAttr (
341- elementType,
342- APSInt::getSignedMaxValue (elementType.getIntOrFloatBitWidth ())));
340+ elementType.getIntOrFloatBitWidth () != 8 ) {
341+ unsigned width = elementType.getIntOrFloatBitWidth ();
342+ auto init =
343+ useUnsignedIntegerSemantics (cast<mlir::IntegerType>(elementType))
344+ ? APInt::getMaxValue (width)
345+ : APSInt::getSignedMaxValue (width);
346+ return arith::ConstantOp::create (b, loc,
347+ b.getIntegerAttr (elementType, init));
348+ }
343349 }
344350
345351 if (isa<AtenLinalgVectorNormOp>(op) || isa<AtenFrobeniusNormDimOp>(op) ||
@@ -388,10 +394,9 @@ static Value createLinalgPayloadForReduceOp(OpBuilder &b, Location loc,
388394 else if (isa<mlir::IntegerType>(resultElementType)) {
389395 IntegerType intType = dyn_cast<mlir::IntegerType>(
390396 cast<BaseTensorType>(max.getSelf ().getType ()).getDtype ());
391- if (intType. isUnsigned ( ))
397+ if (useUnsignedIntegerSemantics (intType ))
392398 return arith::MaxUIOp::create (b, loc, self, result);
393- if (intType.isSigned ())
394- return arith::MaxSIOp::create (b, loc, self, result);
399+ return arith::MaxSIOp::create (b, loc, self, result);
395400 }
396401 } else if (auto min = dyn_cast<AtenMinOp>(op)) {
397402 Value self =
@@ -402,10 +407,9 @@ static Value createLinalgPayloadForReduceOp(OpBuilder &b, Location loc,
402407 else if (isa<mlir::IntegerType>(resultElementType)) {
403408 IntegerType intType = dyn_cast<mlir::IntegerType>(
404409 cast<BaseTensorType>(min.getSelf ().getType ()).getDtype ());
405- if (intType. isUnsigned ( ))
410+ if (useUnsignedIntegerSemantics (intType ))
406411 return arith::MinUIOp::create (b, loc, self, result);
407- if (intType.isSigned ())
408- return arith::MinSIOp::create (b, loc, self, result);
412+ return arith::MinSIOp::create (b, loc, self, result);
409413 }
410414 } else if (isa<AtenNormScalarOp>(op)) {
411415 // This creates payload for only the first of the two linalg.generic ops.
0 commit comments