From f4483b10f0a6396c0cb4f03aabd22df35b4a758e Mon Sep 17 00:00:00 2001 From: V Donaldson Date: Thu, 29 Aug 2024 12:36:10 -0700 Subject: [PATCH] [flang] Don't generate empty else blocks Code lowering always generates fir.if else blocks for source level if statements, whether needed or not. Change this to only generate else blocks that are needed. --- flang/lib/Lower/Bridge.cpp | 7 +++++-- flang/test/HLFIR/assumed_shape_with_value_keyword.f90 | 2 -- flang/test/Lower/HLFIR/select-rank.f90 | 1 - flang/test/Lower/Intrinsics/system_clock.f90 | 2 -- flang/test/Lower/OpenMP/master.f90 | 2 +- flang/test/Lower/OpenMP/unstructured.f90 | 1 - flang/test/Lower/OpenMP/wsloop-reduction-max-byref.f90 | 1 - flang/test/Lower/OpenMP/wsloop-reduction-max.f90 | 1 - flang/test/Lower/OpenMP/wsloop-reduction-min-byref.f90 | 1 - flang/test/Lower/OpenMP/wsloop-reduction-min.f90 | 1 - flang/test/Lower/OpenMP/wsloop-variable.f90 | 1 - 11 files changed, 6 insertions(+), 14 deletions(-) diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp index c48daba8cf7fa..a1644743e9f01 100644 --- a/flang/lib/Lower/Bridge.cpp +++ b/flang/lib/Lower/Bridge.cpp @@ -2348,8 +2348,11 @@ class FirConverter : public Fortran::lower::AbstractConverter { fir::IfOp topIfOp, currentIfOp; for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) { auto genIfOp = [&](mlir::Value cond) { - auto ifOp = - builder->create(toLocation(), cond, /*withElse=*/true); + Fortran::lower::pft::Evaluation &succ = *e.controlSuccessor; + bool hasElse = succ.isA() || + succ.isA(); + auto ifOp = builder->create(toLocation(), cond, + /*withElseRegion=*/hasElse); builder->setInsertionPointToStart(&ifOp.getThenRegion().front()); return ifOp; }; diff --git a/flang/test/HLFIR/assumed_shape_with_value_keyword.f90 b/flang/test/HLFIR/assumed_shape_with_value_keyword.f90 index 197efc08422c6..208f22badda28 100644 --- a/flang/test/HLFIR/assumed_shape_with_value_keyword.f90 +++ b/flang/test/HLFIR/assumed_shape_with_value_keyword.f90 @@ -102,7 +102,6 @@ subroutine test_optional1(x) ! CHECK: %[[VAL_3:.*]] = fir.box_addr %[[VAL_2]]#0 : (!fir.box>) -> !fir.ref> ! CHECK: fir.call @_QPinternal_call7(%[[VAL_3]]) fastmath : (!fir.ref>) -> () ! CHECK: hlfir.copy_out %[[TMP_BOX]], %[[VAL_2]]#1 to %[[VAL_0]]#0 : (!fir.ref>>>, i1, !fir.box>) -> () -! CHECK: } else { ! CHECK: } ! CHECK: return ! CHECK: } @@ -122,7 +121,6 @@ subroutine test_optional2(x) ! CHECK: %[[VAL_3:.*]] = fir.box_addr %[[VAL_2]]#0 : (!fir.box>) -> !fir.ref> ! CHECK: fir.call @_QPinternal_call8(%[[VAL_3]]) fastmath : (!fir.ref>) -> () ! CHECK: hlfir.copy_out %[[TMP_BOX]], %[[VAL_2]]#1 to %[[VAL_0]]#0 : (!fir.ref>>>, i1, !fir.box>) -> () -! CHECK: } else { ! CHECK: } ! CHECK: return ! CHECK: } diff --git a/flang/test/Lower/HLFIR/select-rank.f90 b/flang/test/Lower/HLFIR/select-rank.f90 index 211b7565bab8a..d27a6d732ffc7 100644 --- a/flang/test/Lower/HLFIR/select-rank.f90 +++ b/flang/test/Lower/HLFIR/select-rank.f90 @@ -796,7 +796,6 @@ subroutine test_branching(x) ! CHECK: %[[VAL_10:.*]] = arith.xori %[[VAL_8]], %[[VAL_9]] : i1 ! CHECK: fir.if %[[VAL_10]] { ! CHECK: fir.call @_QPone() fastmath : () -> () -! CHECK: } else { ! CHECK: } ! CHECK: fir.call @_QPrdefault(%[[VAL_6]]#0) fastmath : (!fir.box>) -> () ! CHECK: cf.br ^bb7 diff --git a/flang/test/Lower/Intrinsics/system_clock.f90 b/flang/test/Lower/Intrinsics/system_clock.f90 index ca36920c04eb3..9eae3a58884fa 100644 --- a/flang/test/Lower/Intrinsics/system_clock.f90 +++ b/flang/test/Lower/Intrinsics/system_clock.f90 @@ -104,7 +104,6 @@ subroutine ss(count) ! CHECK: fir.if %[[V_17]] { ! CHECK: %[[C_0:c[0-9a-z_]+]] = arith.constant 0 : i64 ! CHECK: fir.store %[[C_0]] to %arg0 : !fir.ref - ! CHECK: } else { ! CHECK: } ! CHECK: %[[V_18:[0-9]+]] = fir.zero_bits !fir.ptr ! CHECK: fir.store %[[V_18]] to %[[V_4]] : !fir.ref> @@ -137,7 +136,6 @@ subroutine ss(count) ! CHECK: %[[V_32]] = fir.load %arg0 : !fir.ref ! CHECK: %[[V_33]] = fir.call @_FortranAioOutputInteger64(%[[V_31]], %[[V_32]]) {{.*}}: (!fir.ref, i64) -> i1 ! CHECK: %[[V_34]] = fir.call @_FortranAioEndIoStatement(%[[V_31]]) {{.*}}: (!fir.ref) -> i32 - ! CHECK: } else { ! CHECK: } ! CHECK: return ! CHECK: } diff --git a/flang/test/Lower/OpenMP/master.f90 b/flang/test/Lower/OpenMP/master.f90 index 7db1be4f005b5..9f98ac89fb1fd 100644 --- a/flang/test/Lower/OpenMP/master.f90 +++ b/flang/test/Lower/OpenMP/master.f90 @@ -91,7 +91,7 @@ subroutine omp_master_parallel() !CHECK: hlfir.assign %{{.*}} to %{{.*}}#0 : i32, !fir.ref beta = alpha + gama end if - !CHECK: else + !CHECK: } !CHECK: omp.terminator !$omp end master diff --git a/flang/test/Lower/OpenMP/unstructured.f90 b/flang/test/Lower/OpenMP/unstructured.f90 index 9c3527eda5bb4..bd030b918033e 100644 --- a/flang/test/Lower/OpenMP/unstructured.f90 +++ b/flang/test/Lower/OpenMP/unstructured.f90 @@ -141,7 +141,6 @@ subroutine ss3(n) ! nested unstructured OpenMP constructs ! CHECK: @_FortranAioBeginExternalListOutput ! CHECK: %[[LOAD:.*]] = fir.load %[[OMP_LOOP_J_DECL]]#0 : !fir.ref ! CHECK: @_FortranAioOutputInteger32(%{{.*}}, %[[LOAD]]) -! CHECK: } else { ! CHECK: } ! CHECK-NEXT: omp.yield ! CHECK-NEXT: } diff --git a/flang/test/Lower/OpenMP/wsloop-reduction-max-byref.f90 b/flang/test/Lower/OpenMP/wsloop-reduction-max-byref.f90 index 7e4890dd00fea..56a43abca42a7 100644 --- a/flang/test/Lower/OpenMP/wsloop-reduction-max-byref.f90 +++ b/flang/test/Lower/OpenMP/wsloop-reduction-max-byref.f90 @@ -118,7 +118,6 @@ ! CHECK: %[[VAL_46:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_45]]) : (!fir.box>, i64) -> !fir.ref ! CHECK: %[[VAL_47:.*]] = fir.load %[[VAL_46]] : !fir.ref ! CHECK: hlfir.assign %[[VAL_47]] to %[[VAL_37]]#0 : f32, !fir.ref -! CHECK: } else { ! CHECK: } ! CHECK: omp.yield ! CHECK: omp.terminator diff --git a/flang/test/Lower/OpenMP/wsloop-reduction-max.f90 b/flang/test/Lower/OpenMP/wsloop-reduction-max.f90 index 9a93c75f5bd1a..775554fd3dcca 100644 --- a/flang/test/Lower/OpenMP/wsloop-reduction-max.f90 +++ b/flang/test/Lower/OpenMP/wsloop-reduction-max.f90 @@ -108,7 +108,6 @@ ! CHECK: %[[VAL_46:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_45]]) : (!fir.box>, i64) -> !fir.ref ! CHECK: %[[VAL_47:.*]] = fir.load %[[VAL_46]] : !fir.ref ! CHECK: hlfir.assign %[[VAL_47]] to %[[VAL_37]]#0 : f32, !fir.ref -! CHECK: } else { ! CHECK: } ! CHECK: omp.yield ! CHECK: omp.terminator diff --git a/flang/test/Lower/OpenMP/wsloop-reduction-min-byref.f90 b/flang/test/Lower/OpenMP/wsloop-reduction-min-byref.f90 index 41fcc979cdc9d..d16de4a867a24 100644 --- a/flang/test/Lower/OpenMP/wsloop-reduction-min-byref.f90 +++ b/flang/test/Lower/OpenMP/wsloop-reduction-min-byref.f90 @@ -120,7 +120,6 @@ ! CHECK: %[[VAL_46:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_45]]) : (!fir.box>, i64) -> !fir.ref ! CHECK: %[[VAL_47:.*]] = fir.load %[[VAL_46]] : !fir.ref ! CHECK: hlfir.assign %[[VAL_47]] to %[[VAL_37]]#0 : f32, !fir.ref -! CHECK: } else { ! CHECK: } ! CHECK: omp.yield ! CHECK: omp.terminator diff --git a/flang/test/Lower/OpenMP/wsloop-reduction-min.f90 b/flang/test/Lower/OpenMP/wsloop-reduction-min.f90 index 50b2db9463d23..04957c7287eae 100644 --- a/flang/test/Lower/OpenMP/wsloop-reduction-min.f90 +++ b/flang/test/Lower/OpenMP/wsloop-reduction-min.f90 @@ -110,7 +110,6 @@ ! CHECK: %[[VAL_46:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_45]]) : (!fir.box>, i64) -> !fir.ref ! CHECK: %[[VAL_47:.*]] = fir.load %[[VAL_46]] : !fir.ref ! CHECK: hlfir.assign %[[VAL_47]] to %[[VAL_37]]#0 : f32, !fir.ref -! CHECK: } else { ! CHECK: } ! CHECK: omp.yield ! CHECK: omp.terminator diff --git a/flang/test/Lower/OpenMP/wsloop-variable.f90 b/flang/test/Lower/OpenMP/wsloop-variable.f90 index dc2acf881f482..7bfb9274f389a 100644 --- a/flang/test/Lower/OpenMP/wsloop-variable.f90 +++ b/flang/test/Lower/OpenMP/wsloop-variable.f90 @@ -190,7 +190,6 @@ subroutine wsloop_variable_sub !CHECK: %[[VAL_56:.*]] = fir.load %[[VAL_19]]#0 : !fir.ref !CHECK: %[[VAL_57:.*]] = arith.cmpi eq, %[[VAL_55]], %[[VAL_56]] : i8 !CHECK: fir.if %[[VAL_57]] { -!CHECK: } else { !CHECK: } !CHECK: omp.yield !CHECK: }