Skip to content

Commit 8586d03

Browse files
authored
[flang] Don't generate empty else blocks (llvm#106618)
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.
1 parent ceb613a commit 8586d03

11 files changed

+6
-14
lines changed

flang/lib/Lower/Bridge.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,8 +2349,11 @@ class FirConverter : public Fortran::lower::AbstractConverter {
23492349
fir::IfOp topIfOp, currentIfOp;
23502350
for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
23512351
auto genIfOp = [&](mlir::Value cond) {
2352-
auto ifOp =
2353-
builder->create<fir::IfOp>(toLocation(), cond, /*withElse=*/true);
2352+
Fortran::lower::pft::Evaluation &succ = *e.controlSuccessor;
2353+
bool hasElse = succ.isA<Fortran::parser::ElseIfStmt>() ||
2354+
succ.isA<Fortran::parser::ElseStmt>();
2355+
auto ifOp = builder->create<fir::IfOp>(toLocation(), cond,
2356+
/*withElseRegion=*/hasElse);
23542357
builder->setInsertionPointToStart(&ifOp.getThenRegion().front());
23552358
return ifOp;
23562359
};

flang/test/HLFIR/assumed_shape_with_value_keyword.f90

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ subroutine test_optional1(x)
102102
! CHECK: %[[VAL_3:.*]] = fir.box_addr %[[VAL_2]]#0 : (!fir.box<!fir.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>>
103103
! CHECK: fir.call @_QPinternal_call7(%[[VAL_3]]) fastmath<contract> : (!fir.ref<!fir.array<?xf32>>) -> ()
104104
! CHECK: hlfir.copy_out %[[TMP_BOX]], %[[VAL_2]]#1 to %[[VAL_0]]#0 : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>, i1, !fir.box<!fir.array<?xf32>>) -> ()
105-
! CHECK: } else {
106105
! CHECK: }
107106
! CHECK: return
108107
! CHECK: }
@@ -122,7 +121,6 @@ subroutine test_optional2(x)
122121
! CHECK: %[[VAL_3:.*]] = fir.box_addr %[[VAL_2]]#0 : (!fir.box<!fir.array<?x?xf32>>) -> !fir.ref<!fir.array<?x?xf32>>
123122
! CHECK: fir.call @_QPinternal_call8(%[[VAL_3]]) fastmath<contract> : (!fir.ref<!fir.array<?x?xf32>>) -> ()
124123
! CHECK: hlfir.copy_out %[[TMP_BOX]], %[[VAL_2]]#1 to %[[VAL_0]]#0 : (!fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>, i1, !fir.box<!fir.array<?x?xf32>>) -> ()
125-
! CHECK: } else {
126124
! CHECK: }
127125
! CHECK: return
128126
! CHECK: }

flang/test/Lower/HLFIR/select-rank.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,6 @@ subroutine test_branching(x)
796796
! CHECK: %[[VAL_10:.*]] = arith.xori %[[VAL_8]], %[[VAL_9]] : i1
797797
! CHECK: fir.if %[[VAL_10]] {
798798
! CHECK: fir.call @_QPone() fastmath<contract> : () -> ()
799-
! CHECK: } else {
800799
! CHECK: }
801800
! CHECK: fir.call @_QPrdefault(%[[VAL_6]]#0) fastmath<contract> : (!fir.box<!fir.array<*:f32>>) -> ()
802801
! CHECK: cf.br ^bb7

flang/test/Lower/Intrinsics/system_clock.f90

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ subroutine ss(count)
104104
! CHECK: fir.if %[[V_17]] {
105105
! CHECK: %[[C_0:c[0-9a-z_]+]] = arith.constant 0 : i64
106106
! CHECK: fir.store %[[C_0]] to %arg0 : !fir.ref<i64>
107-
! CHECK: } else {
108107
! CHECK: }
109108
! CHECK: %[[V_18:[0-9]+]] = fir.zero_bits !fir.ptr<i64>
110109
! CHECK: fir.store %[[V_18]] to %[[V_4]] : !fir.ref<!fir.ptr<i64>>
@@ -137,7 +136,6 @@ subroutine ss(count)
137136
! CHECK: %[[V_32]] = fir.load %arg0 : !fir.ref<i64>
138137
! CHECK: %[[V_33]] = fir.call @_FortranAioOutputInteger64(%[[V_31]], %[[V_32]]) {{.*}}: (!fir.ref<i8>, i64) -> i1
139138
! CHECK: %[[V_34]] = fir.call @_FortranAioEndIoStatement(%[[V_31]]) {{.*}}: (!fir.ref<i8>) -> i32
140-
! CHECK: } else {
141139
! CHECK: }
142140
! CHECK: return
143141
! CHECK: }

flang/test/Lower/OpenMP/master.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ subroutine omp_master_parallel()
9191
!CHECK: hlfir.assign %{{.*}} to %{{.*}}#0 : i32, !fir.ref<i32>
9292
beta = alpha + gama
9393
end if
94-
!CHECK: else
94+
!CHECK: }
9595

9696
!CHECK: omp.terminator
9797
!$omp end master

flang/test/Lower/OpenMP/unstructured.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ subroutine ss3(n) ! nested unstructured OpenMP constructs
141141
! CHECK: @_FortranAioBeginExternalListOutput
142142
! CHECK: %[[LOAD:.*]] = fir.load %[[OMP_LOOP_J_DECL]]#0 : !fir.ref<i32>
143143
! CHECK: @_FortranAioOutputInteger32(%{{.*}}, %[[LOAD]])
144-
! CHECK: } else {
145144
! CHECK: }
146145
! CHECK-NEXT: omp.yield
147146
! CHECK-NEXT: }

flang/test/Lower/OpenMP/wsloop-reduction-max-byref.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118
! CHECK: %[[VAL_46:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_45]]) : (!fir.box<!fir.array<?xf32>>, i64) -> !fir.ref<f32>
119119
! CHECK: %[[VAL_47:.*]] = fir.load %[[VAL_46]] : !fir.ref<f32>
120120
! CHECK: hlfir.assign %[[VAL_47]] to %[[VAL_37]]#0 : f32, !fir.ref<f32>
121-
! CHECK: } else {
122121
! CHECK: }
123122
! CHECK: omp.yield
124123
! CHECK: omp.terminator

flang/test/Lower/OpenMP/wsloop-reduction-max.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
! CHECK: %[[VAL_46:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_45]]) : (!fir.box<!fir.array<?xf32>>, i64) -> !fir.ref<f32>
109109
! CHECK: %[[VAL_47:.*]] = fir.load %[[VAL_46]] : !fir.ref<f32>
110110
! CHECK: hlfir.assign %[[VAL_47]] to %[[VAL_37]]#0 : f32, !fir.ref<f32>
111-
! CHECK: } else {
112111
! CHECK: }
113112
! CHECK: omp.yield
114113
! CHECK: omp.terminator

flang/test/Lower/OpenMP/wsloop-reduction-min-byref.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@
120120
! CHECK: %[[VAL_46:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_45]]) : (!fir.box<!fir.array<?xf32>>, i64) -> !fir.ref<f32>
121121
! CHECK: %[[VAL_47:.*]] = fir.load %[[VAL_46]] : !fir.ref<f32>
122122
! CHECK: hlfir.assign %[[VAL_47]] to %[[VAL_37]]#0 : f32, !fir.ref<f32>
123-
! CHECK: } else {
124123
! CHECK: }
125124
! CHECK: omp.yield
126125
! CHECK: omp.terminator

flang/test/Lower/OpenMP/wsloop-reduction-min.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@
110110
! CHECK: %[[VAL_46:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_45]]) : (!fir.box<!fir.array<?xf32>>, i64) -> !fir.ref<f32>
111111
! CHECK: %[[VAL_47:.*]] = fir.load %[[VAL_46]] : !fir.ref<f32>
112112
! CHECK: hlfir.assign %[[VAL_47]] to %[[VAL_37]]#0 : f32, !fir.ref<f32>
113-
! CHECK: } else {
114113
! CHECK: }
115114
! CHECK: omp.yield
116115
! CHECK: omp.terminator

flang/test/Lower/OpenMP/wsloop-variable.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ subroutine wsloop_variable_sub
190190
!CHECK: %[[VAL_56:.*]] = fir.load %[[VAL_19]]#0 : !fir.ref<i8>
191191
!CHECK: %[[VAL_57:.*]] = arith.cmpi eq, %[[VAL_55]], %[[VAL_56]] : i8
192192
!CHECK: fir.if %[[VAL_57]] {
193-
!CHECK: } else {
194193
!CHECK: }
195194
!CHECK: omp.yield
196195
!CHECK: }

0 commit comments

Comments
 (0)