[flang-commits] [PATCH] D139330: [flang] do not generate padding/truncation code when character length are equals

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Dec 5 07:40:11 PST 2022


jeanPerier created this revision.
jeanPerier added reviewers: clementval, PeteSteinfeld.
jeanPerier added a project: Flang.
Herald added subscribers: mehdi_amini, jdoerfert.
Herald added a project: All.
jeanPerier requested review of this revision.

When generating character assignment operations, the generic code
generates some code to handle truncation and padding when the length
differ at runtime. A bypass already exists when the length are compile
time constant and match, but it was not used for the trivial case where
the RHS and LHS length is the same SSA value. In such case, even though,
the length is not know at compile time, it is known to be the same.

This will simplify the code creating character temporaries from a
variable in HLFIR that will use this assignment code.

Note that this probably has little impact on performance (llvm may be clever enough
to later catch that for us). But it makes the generated IR a lot more readable at
little cost.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139330

Files:
  flang/lib/Optimizer/Builder/Character.cpp
  flang/test/Lower/OpenMP/omp-parallel-lastprivate-clause-scalar.f90
  flang/test/Lower/array-expression.f90


Index: flang/test/Lower/array-expression.f90
===================================================================
--- flang/test/Lower/array-expression.f90
+++ flang/test/Lower/array-expression.f90
@@ -999,20 +999,6 @@
 ! CHECK:         %[[VAL_30:.*]] = fir.do_loop %[[VAL_31:.*]] = %[[VAL_28]] to %[[VAL_29]] step %[[VAL_27]] unordered iter_args(%[[VAL_32:.*]] = %[[VAL_15]]) -> (!fir.array<70x!fir.char<4,?>>) {
 ! CHECK:           %[[VAL_33:.*]] = fir.array_access %[[VAL_24]], %[[VAL_31]] : (!fir.array<140x!fir.char<2,13>>, index) -> !fir.ref<!fir.char<2,13>>
 ! CHECK:           %[[VAL_34:.*]] = fir.alloca !fir.char<4,?>(%[[VAL_4]] : index)
-! CHECK:           %[[VAL_35:.*]] = arith.cmpi slt, %[[VAL_4]], %[[VAL_4]] : index
-! CHECK:           %[[VAL_36:.*]] = arith.select %[[VAL_35]], %[[VAL_4]], %[[VAL_4]] : index
-! CHECK:           fir.char_convert %[[VAL_33]] for %[[VAL_36]] to %[[VAL_34]] : !fir.ref<!fir.char<2,13>>, index, !fir.ref<!fir.char<4,?>>
-! CHECK:           %[[VAL_37:.*]] = arith.constant 1 : index
-! CHECK:           %[[VAL_38:.*]] = arith.subi %[[VAL_4]], %[[VAL_37]] : index
-! CHECK:           %[[VAL_39:.*]] = arith.constant 32 : i32
-! CHECK:           %[[VAL_40:.*]] = fir.undefined !fir.char<4>
-! CHECK:           %[[VAL_41:.*]] = fir.insert_value %[[VAL_40]], %[[VAL_39]], [0 : index] : (!fir.char<4>, i32) -> !fir.char<4>
-! CHECK:           %[[VAL_42:.*]] = arith.constant 1 : index
-! CHECK:           fir.do_loop %[[VAL_43:.*]] = %[[VAL_36]] to %[[VAL_38]] step %[[VAL_42]] {
-! CHECK:             %[[VAL_44:.*]] = fir.convert %[[VAL_34]] : (!fir.ref<!fir.char<4,?>>) -> !fir.ref<!fir.array<?x!fir.char<4>>>
-! CHECK:             %[[VAL_45:.*]] = fir.coordinate_of %[[VAL_44]], %[[VAL_43]] : (!fir.ref<!fir.array<?x!fir.char<4>>>, index) -> !fir.ref<!fir.char<4>>
-! CHECK:             fir.store %[[VAL_41]] to %[[VAL_45]] : !fir.ref<!fir.char<4>>
-! CHECK:           }
 ! CHECK:           %[[VAL_46:.*]] = fir.array_access %[[VAL_32]], %[[VAL_31]] typeparams %[[VAL_11]] : (!fir.array<70x!fir.char<4,?>>, index, i32) -> !fir.ref<!fir.char<4,?>>
 ! CHECK:           %[[VAL_47:.*]] = fir.convert %[[VAL_11]] : (i32) -> index
 ! CHECK:           %[[VAL_48:.*]] = fir.convert %[[VAL_26]] : (i64) -> index
Index: flang/test/Lower/OpenMP/omp-parallel-lastprivate-clause-scalar.f90
===================================================================
--- flang/test/Lower/OpenMP/omp-parallel-lastprivate-clause-scalar.f90
+++ flang/test/Lower/OpenMP/omp-parallel-lastprivate-clause-scalar.f90
@@ -30,15 +30,6 @@
 !CHECK-DAG: %[[CVT:.*]] = fir.convert %[[ARG1_UNBOX]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
 !CHECK-DAG: %[[CVT1:.*]] = fir.convert %[[ARG1_PVT]] : (!fir.ref<!fir.char<1,5>>) -> !fir.ref<i8>
 !CHECK-DAG: fir.call @llvm.memmove.p0.p0.i64(%[[CVT]], %[[CVT1]]{{.*}})
-!CHECK: %[[THIRTY_TWO:.*]] = arith.constant 32 : i8
-!CHECK-DAG: %[[UNDEF:.*]] = fir.undefined !fir.char<1>
-!CHECK-DAG: %[[INSERT:.*]] = fir.insert_value %[[UNDEF]], %[[THIRTY_TWO]], [0 : index] : (!fir.char<1>, i8) -> !fir.char<1>
-!CHECK-DAG: %[[ONE_3:.*]] = arith.constant 1 : index
-!CHECK: fir.do_loop %[[ARG2:.*]] = {{.*}} {
-!CHECK-DAG: %[[CVT_2:.*]] = fir.convert %[[ARG1_UNBOX]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
-!CHECK-DAG: %[[COORD:.*]] = fir.coordinate_of %[[CVT_2]], %[[ARG2]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
-!CHECK-DAG: fir.store %[[INSERT]] to %[[COORD]] : !fir.ref<!fir.char<1>>
-!CHECK-DAG: }
 !CHECK-DAG: } 
 !CHECK-DAG: omp.yield
 
Index: flang/lib/Optimizer/Builder/Character.cpp
===================================================================
--- flang/lib/Optimizer/Builder/Character.cpp
+++ flang/lib/Optimizer/Builder/Character.cpp
@@ -419,7 +419,8 @@
   auto rhsCstLen = getCompileTimeLength(rhs);
   auto lhsCstLen = getCompileTimeLength(lhs);
   bool compileTimeSameLength =
-      lhsCstLen && rhsCstLen && *lhsCstLen == *rhsCstLen;
+      (lhsCstLen && rhsCstLen && *lhsCstLen == *rhsCstLen) ||
+      (rhs.getLen() == lhs.getLen());
 
   if (compileTimeSameLength && *lhsCstLen == 1) {
     createLengthOneAssign(lhs, rhs);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139330.480106.patch
Type: text/x-patch
Size: 4164 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221205/6952ccd0/attachment.bin>


More information about the flang-commits mailing list