Skip to content

[flang] Canonicalize redundant pointer converts. #121864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions flang/include/flang/Optimizer/Dialect/CanonicalizationPatterns.td
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def StrictSmallerWidthPred : Constraint<CPred<
"$0.getType().getIntOrFloatBitWidth() < "
"$1.getType().getIntOrFloatBitWidth()">>;

def PointerCompatiblePred
: Constraint<CPred<"fir::ConvertOp::isPointerCompatible($0.getType())">>;

// floats or ints that undergo successive extensions or successive truncations.
def ConvertConvertOptPattern
: Pat<(fir_ConvertOp:$res (fir_ConvertOp:$irm $arg)),
Expand Down Expand Up @@ -112,4 +115,18 @@ def ForwardConstantConvertPattern
(createConstantOp $res, $attr),
[(IndexTypePred $res), (IntegerTypePred $cnt)]>;

// Optimize redundant pointer conversions, e.g.:
// %1 = fir.convert %0 :
// (!fir.heap<!fir.array<2xf32>>) -> !fir.ref<!fir.array<2xf32>>
// %2 = fir.convert %1 :
// (!fir.ref<!fir.array<2xf32>>) -> !fir.heap<!fir.array<2xf32>>
// Will be optimized into:
// %2 = fir.convert %0 :
// (!fir.heap<!fir.array<2xf32>>) -> !fir.heap<!fir.array<2xf32>>
// which is redundant due to RedundantConvertOptPattern.
def ChainedPointerConvertsPattern
: Pat<(fir_ConvertOp:$res(fir_ConvertOp:$irm $arg)), (fir_ConvertOp $arg),
[(PointerCompatiblePred $arg), (PointerCompatiblePred $irm),
(PointerCompatiblePred $res)]>;

#endif // FORTRAN_FIR_REWRITE_PATTERNS
3 changes: 2 additions & 1 deletion flang/lib/Optimizer/Dialect/FIROps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,8 @@ void fir::ConvertOp::getCanonicalizationPatterns(
results.insert<ConvertConvertOptPattern, ConvertAscendingIndexOptPattern,
ConvertDescendingIndexOptPattern, RedundantConvertOptPattern,
CombineConvertOptPattern, CombineConvertTruncOptPattern,
ForwardConstantConvertPattern>(context);
ForwardConstantConvertPattern, ChainedPointerConvertsPattern>(
context);
}

mlir::OpFoldResult fir::ConvertOp::fold(FoldAdaptor adaptor) {
Expand Down
9 changes: 9 additions & 0 deletions flang/test/Fir/convert-fold.fir
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ func.func @ctest() -> index {
// CHECK-NEXT: return %{{.*}} : index
return %2 : index
}

// CHECK-LABEL: func.func @ptrtest(
// CHECK-SAME: %[[VAL_0:.*]]: !fir.heap<!fir.array<2xf32>>) -> !fir.heap<!fir.array<2xf32>> {
func.func @ptrtest(%0 : !fir.heap<!fir.array<2xf32>>) -> !fir.heap<!fir.array<2xf32>> {
%1 = fir.convert %0 : (!fir.heap<!fir.array<2xf32>>) -> !fir.ref<!fir.array<2xf32>>
%2 = fir.convert %1 : (!fir.ref<!fir.array<2xf32>>) -> !fir.heap<!fir.array<2xf32>>
// CHECK: return %[[VAL_0]] : !fir.heap<!fir.array<2xf32>>
return %2 : !fir.heap<!fir.array<2xf32>>
}
3 changes: 1 addition & 2 deletions flang/test/Lower/array-substring.f90
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
! CHECK: %[[VAL_16:.*]] = fir.array_coor %[[VAL_7]](%[[VAL_9]]) {{\[}}%[[VAL_10]]] %[[VAL_15]] : (!fir.ref<!fir.array<1x!fir.char<1,12>>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<!fir.char<1,12>>
! CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_16]] : (!fir.ref<!fir.char<1,12>>) -> !fir.ref<!fir.array<12x!fir.char<1>>>
! CHECK: %[[VAL_18:.*]] = fir.coordinate_of %[[VAL_17]], %[[VAL_2]] : (!fir.ref<!fir.array<12x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
! CHECK: %[[VAL_19:.*]] = fir.convert %[[VAL_18]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
! CHECK: %[[VAL_20:.*]] = fir.array_coor %[[VAL_11]](%[[VAL_9]]) %[[VAL_15]] : (!fir.ref<!fir.array<1x!fir.char<1,8>>>, !fir.shape<1>, index) -> !fir.ref<!fir.char<1,8>>
! CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_19]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
! CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_18]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<i8>
! CHECK: %[[VAL_22:.*]] = fir.convert %[[VAL_20]] : (!fir.ref<!fir.char<1,8>>) -> !fir.ref<i8>
! CHECK: %[[VAL_23:.*]] = fir.convert %[[VAL_4]] : (index) -> i64
! CHECK: %[[VAL_24:.*]] = fir.call @_FortranACharacterCompareScalar1(%[[VAL_21]], %[[VAL_22]], %[[VAL_23]], %[[VAL_23]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i8>, i64, i64) -> i32
Expand Down
3 changes: 1 addition & 2 deletions flang/test/Lower/vector-subscript-io.f90
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,11 @@ subroutine substring(x, y, i, j)
! CHECK: %[[VAL_230:.*]] = arith.subi %[[VAL_216]], %[[VAL_210]] : index
! CHECK: %[[VAL_231:.*]] = fir.convert %[[VAL_228]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
! CHECK: %[[VAL_232:.*]] = fir.coordinate_of %[[VAL_231]], %[[VAL_230]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
! CHECK: %[[VAL_233:.*]] = fir.convert %[[VAL_232]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
! CHECK: %[[VAL_234:.*]] = arith.subi %[[VAL_219]], %[[VAL_216]] : index
! CHECK: %[[VAL_235:.*]] = arith.addi %[[VAL_234]], %[[VAL_210]] : index
! CHECK: %[[VAL_236:.*]] = arith.cmpi slt, %[[VAL_235]], %[[VAL_209]] : index
! CHECK: %[[VAL_237:.*]] = arith.select %[[VAL_236]], %[[VAL_209]], %[[VAL_235]] : index
! CHECK: %[[VAL_238:.*]] = fir.convert %[[VAL_233]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
! CHECK: %[[VAL_238:.*]] = fir.convert %[[VAL_232]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<i8>
! CHECK: %[[VAL_239:.*]] = fir.convert %[[VAL_237]] : (index) -> i64
! CHECK: %[[VAL_240:.*]] = fir.call @_FortranAioInputAscii(%[[VAL_213]], %[[VAL_238]], %[[VAL_239]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
! CHECK: %[[VAL_241:.*]] = arith.addi %[[VAL_221]], %[[VAL_210]] overflow<nsw> : index
Expand Down
Loading