Skip to content

Commit 9e6685a

Browse files
rjnwschweitzpgi
authored andcommitted
rewrite for forwarding index constants through convert (#407)
1 parent ce5e5b4 commit 9e6685a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

flang/include/flang/Optimizer/Transforms/RewritePatterns.td

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
#define FIR_REWRITE_PATTERNS
1616

1717
include "mlir/IR/OpBase.td"
18+
include "mlir/Dialect/StandardOps/IR/Ops.td"
1819
include "flang/Optimizer/Dialect/FIROps.td"
1920

2021
def IdenticalTypePred : Constraint<CPred<"$0.getType() == $1.getType()">>;
2122
def IntegerTypePred : Constraint<CPred<"fir::isa_integer($0.getType())">>;
23+
def IndexTypePred : Constraint<CPred<"$0.getType().isa<mlir::IndexType>()">>;
2224

2325
def SmallerWidthPred
24-
: Constraint<CPred<"$0.getType().getIntOrFloatBitWidth() < $1.getType().getIntOrFloatBitWidth()">>;
26+
: Constraint<CPred<"$0.getType().getIntOrFloatBitWidth() "
27+
"<= $1.getType().getIntOrFloatBitWidth()">>;
2528

2629
def ConvertConvertOptPattern
2730
: Pat<(fir_ConvertOp (fir_ConvertOp $arg)),
@@ -42,4 +45,14 @@ def CombineConvertOptPattern
4245
,(IntegerTypePred $irm)
4346
,(SmallerWidthPred $arg, $irm)]>;
4447

48+
def createConstantOp
49+
: NativeCodeCall<"$_builder.create<mlir::ConstantOp>"
50+
"($_loc, $_builder.getIndexType(), "
51+
"rewriter.getIndexAttr($1.dyn_cast<IntegerAttr>().getInt()))">;
52+
53+
def ForwardConstantConvertPattern
54+
: Pat<(fir_ConvertOp:$res (ConstantOp $attr)),
55+
(createConstantOp $res, $attr),
56+
[(IndexTypePred $res)]>;
57+
4558
#endif // FIR_REWRITE_PATTERNS

flang/lib/Optimizer/Dialect/FIROps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ mlir::ParseResult fir::parseCmpcOp(mlir::OpAsmParser &parser,
323323
void fir::ConvertOp::getCanonicalizationPatterns(
324324
OwningRewritePatternList &results, MLIRContext *context) {
325325
results.insert<ConvertConvertOptPattern, RedundantConvertOptPattern,
326-
CombineConvertOptPattern>(context);
326+
CombineConvertOptPattern, ForwardConstantConvertPattern>(context);
327327
}
328328

329329
mlir::OpFoldResult fir::ConvertOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) {

0 commit comments

Comments
 (0)