-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[flang] Don't generate empty else blocks #106618
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
Conversation
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.
@llvm/pr-subscribers-flang-openmp @llvm/pr-subscribers-flang-fir-hlfir Author: None (vdonaldson) ChangesCode 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. Full diff: https://github.com/llvm/llvm-project/pull/106618.diff 11 Files Affected:
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index c48daba8cf7fab..a1644743e9f01d 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<fir::IfOp>(toLocation(), cond, /*withElse=*/true);
+ Fortran::lower::pft::Evaluation &succ = *e.controlSuccessor;
+ bool hasElse = succ.isA<Fortran::parser::ElseIfStmt>() ||
+ succ.isA<Fortran::parser::ElseStmt>();
+ auto ifOp = builder->create<fir::IfOp>(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 197efc08422c6e..208f22badda28d 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.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>>
! CHECK: fir.call @_QPinternal_call7(%[[VAL_3]]) fastmath<contract> : (!fir.ref<!fir.array<?xf32>>) -> ()
! 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>>) -> ()
-! 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.array<?x?xf32>>) -> !fir.ref<!fir.array<?x?xf32>>
! CHECK: fir.call @_QPinternal_call8(%[[VAL_3]]) fastmath<contract> : (!fir.ref<!fir.array<?x?xf32>>) -> ()
! 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>>) -> ()
-! 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 211b7565bab8a3..d27a6d732ffc71 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<contract> : () -> ()
-! CHECK: } else {
! CHECK: }
! CHECK: fir.call @_QPrdefault(%[[VAL_6]]#0) fastmath<contract> : (!fir.box<!fir.array<*:f32>>) -> ()
! CHECK: cf.br ^bb7
diff --git a/flang/test/Lower/Intrinsics/system_clock.f90 b/flang/test/Lower/Intrinsics/system_clock.f90
index ca36920c04eb3b..9eae3a58884faf 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<i64>
- ! CHECK: } else {
! CHECK: }
! CHECK: %[[V_18:[0-9]+]] = fir.zero_bits !fir.ptr<i64>
! CHECK: fir.store %[[V_18]] to %[[V_4]] : !fir.ref<!fir.ptr<i64>>
@@ -137,7 +136,6 @@ subroutine ss(count)
! CHECK: %[[V_32]] = fir.load %arg0 : !fir.ref<i64>
! CHECK: %[[V_33]] = fir.call @_FortranAioOutputInteger64(%[[V_31]], %[[V_32]]) {{.*}}: (!fir.ref<i8>, i64) -> i1
! CHECK: %[[V_34]] = fir.call @_FortranAioEndIoStatement(%[[V_31]]) {{.*}}: (!fir.ref<i8>) -> i32
- ! CHECK: } else {
! CHECK: }
! CHECK: return
! CHECK: }
diff --git a/flang/test/Lower/OpenMP/master.f90 b/flang/test/Lower/OpenMP/master.f90
index 7db1be4f005b57..9f98ac89fb1fd9 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<i32>
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 9c3527eda5bb43..bd030b918033e6 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<i32>
! 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 7e4890dd00fea3..56a43abca42a76 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<!fir.array<?xf32>>, i64) -> !fir.ref<f32>
! CHECK: %[[VAL_47:.*]] = fir.load %[[VAL_46]] : !fir.ref<f32>
! CHECK: hlfir.assign %[[VAL_47]] to %[[VAL_37]]#0 : f32, !fir.ref<f32>
-! 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 9a93c75f5bd1a8..775554fd3dcca1 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<!fir.array<?xf32>>, i64) -> !fir.ref<f32>
! CHECK: %[[VAL_47:.*]] = fir.load %[[VAL_46]] : !fir.ref<f32>
! CHECK: hlfir.assign %[[VAL_47]] to %[[VAL_37]]#0 : f32, !fir.ref<f32>
-! 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 41fcc979cdc9d9..d16de4a867a24c 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<!fir.array<?xf32>>, i64) -> !fir.ref<f32>
! CHECK: %[[VAL_47:.*]] = fir.load %[[VAL_46]] : !fir.ref<f32>
! CHECK: hlfir.assign %[[VAL_47]] to %[[VAL_37]]#0 : f32, !fir.ref<f32>
-! 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 50b2db9463d23d..04957c7287eae4 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<!fir.array<?xf32>>, i64) -> !fir.ref<f32>
! CHECK: %[[VAL_47:.*]] = fir.load %[[VAL_46]] : !fir.ref<f32>
! CHECK: hlfir.assign %[[VAL_47]] to %[[VAL_37]]#0 : f32, !fir.ref<f32>
-! 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 dc2acf881f482a..7bfb9274f389a3 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<i8>
!CHECK: %[[VAL_57:.*]] = arith.cmpi eq, %[[VAL_55]], %[[VAL_56]] : i8
!CHECK: fir.if %[[VAL_57]] {
-!CHECK: } else {
!CHECK: }
!CHECK: omp.yield
!CHECK: }
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All builds and tests correctly and looks good.
Thanks for cleaning this up!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Val
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.