[MLIR] Create folders for extsi/extui

Create folders/canonicalizers for extsi/extui. Specifically,

extui(extui(x)) -> extui(x)
extsi(extsi(x)) -> extsi(x)
extsi(extui(x)) -> extui(x)

Reviewed By: mehdi_amini

Differential Revision: https://reviews.llvm.org/D116515
This commit is contained in:
William S. Moses 2022-01-02 22:06:57 -05:00
parent 89af17c0c7
commit 1bb9f4e482
4 changed files with 53 additions and 0 deletions

View File

@ -818,6 +818,7 @@ def Arith_ExtSIOp : Arith_IToICastOp<"extsi"> {
}];
let hasFolder = 1;
let hasCanonicalizer = 1;
let verifier = [{ return verifyExtOp<IntegerType>(*this); }];
}

View File

@ -128,4 +128,12 @@ def IndexCastOfExtSI :
def BitcastOfBitcast :
Pat<(Arith_BitcastOp (Arith_BitcastOp $x)), (replaceWithValue $x)>;
//===----------------------------------------------------------------------===//
// ExtSIOp
//===----------------------------------------------------------------------===//
// extsi(extui(x iN : iM) : iL) -> extui(x : iL)
def ExtSIOfExtUI :
Pat<(Arith_ExtSIOp (Arith_ExtUIOp $x)), (Arith_ExtUIOp $x)>;
#endif // ARITHMETIC_PATTERNS

View File

@ -788,6 +788,11 @@ OpFoldResult arith::ExtUIOp::fold(ArrayRef<Attribute> operands) {
return IntegerAttr::get(
getType(), lhs.getValue().zext(getType().getIntOrFloatBitWidth()));
if (auto lhs = getIn().getDefiningOp<ExtUIOp>()) {
getInMutable().assign(lhs.getIn());
return getResult();
}
return {};
}
@ -804,6 +809,11 @@ OpFoldResult arith::ExtSIOp::fold(ArrayRef<Attribute> operands) {
return IntegerAttr::get(
getType(), lhs.getValue().sext(getType().getIntOrFloatBitWidth()));
if (auto lhs = getIn().getDefiningOp<ExtSIOp>()) {
getInMutable().assign(lhs.getIn());
return getResult();
}
return {};
}
@ -811,6 +821,11 @@ bool arith::ExtSIOp::areCastCompatible(TypeRange inputs, TypeRange outputs) {
return checkWidthChangeCast<std::greater, IntegerType>(inputs, outputs);
}
void arith::ExtSIOp::getCanonicalizationPatterns(
OwningRewritePatternList &patterns, MLIRContext *context) {
patterns.insert<ExtSIOfExtUI>(context);
}
//===----------------------------------------------------------------------===//
// ExtFOp
//===----------------------------------------------------------------------===//

View File

@ -70,6 +70,35 @@ func @cmpOfExtUI(%arg0: i1) -> i1 {
// -----
// CHECK-LABEL: @extSIOfExtUI
// CHECK: %[[res:.+]] = arith.extui %arg0 : i1 to i64
// CHECK: return %[[res]]
func @extSIOfExtUI(%arg0: i1) -> i64 {
%ext1 = arith.extui %arg0 : i1 to i8
%ext2 = arith.extsi %ext1 : i8 to i64
return %ext2 : i64
}
// CHECK-LABEL: @extUIOfExtUI
// CHECK: %[[res:.+]] = arith.extui %arg0 : i1 to i64
// CHECK: return %[[res]]
func @extUIOfExtUI(%arg0: i1) -> i64 {
%ext1 = arith.extui %arg0 : i1 to i8
%ext2 = arith.extui %ext1 : i8 to i64
return %ext2 : i64
}
// CHECK-LABEL: @extSIOfExtSI
// CHECK: %[[res:.+]] = arith.extsi %arg0 : i1 to i64
// CHECK: return %[[res]]
func @extSIOfExtSI(%arg0: i1) -> i64 {
%ext1 = arith.extsi %arg0 : i1 to i8
%ext2 = arith.extsi %ext1 : i8 to i64
return %ext2 : i64
}
// -----
// CHECK-LABEL: @indexCastOfSignExtend
// CHECK: %[[res:.+]] = arith.index_cast %arg0 : i8 to index
// CHECK: return %[[res]]