[flang-commits] [flang] 9ceb0a7 - Fix nested block constructs for SELECT CASE

Carlos Eduardo Seo via flang-commits flang-commits at lists.llvm.org
Thu May 25 09:52:45 PDT 2023


Author: Carlos Eduardo Seo
Date: 2023-05-25T16:52:32Z
New Revision: 9ceb0a7bc0e73c4aab6cfade225f3ab33c949b83

URL: https://github.com/llvm/llvm-project/commit/9ceb0a7bc0e73c4aab6cfade225f3ab33c949b83
DIFF: https://github.com/llvm/llvm-project/commit/9ceb0a7bc0e73c4aab6cfade225f3ab33c949b83.diff

LOG: Fix nested block constructs for SELECT CASE

In some scenarios, a SELECT CASE could cause an error while lowering to FIR.
This was caused by a spurious extra branch added after the end statement.

Fixes #62726

Differential Revision: https://reviews.llvm.org/D151118

Added: 
    

Modified: 
    flang/lib/Lower/Bridge.cpp
    flang/test/Lower/select-case-statement.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index b5b5691bce40..9d8e2a37cf51 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -1847,8 +1847,12 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     Fortran::lower::pft::Evaluation &eval = getEval();
     Fortran::lower::StatementContext stmtCtx;
     pushActiveConstruct(eval, stmtCtx);
-    for (Fortran::lower::pft::Evaluation &e : getEval().getNestedEvaluations())
-      genFIR(e);
+    for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
+      if (e.getIf<Fortran::parser::EndSelectStmt>())
+        maybeStartBlock(e.block);
+      else
+        genFIR(e);
+    }
     popActiveConstruct();
   }
 
@@ -2708,7 +2712,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
         builder->restoreInsertionPoint(crtInsPt);
         ++typeGuardIdx;
       } else if (eval.getIf<Fortran::parser::EndSelectStmt>()) {
-        genFIR(eval);
+        maybeStartBlock(eval.block);
         if (hasLocalScope)
           localSymbols.popScope();
       } else {

diff  --git a/flang/test/Lower/select-case-statement.f90 b/flang/test/Lower/select-case-statement.f90
index d7f6a51d82bd..1ba844c0f52e 100644
--- a/flang/test/Lower/select-case-statement.f90
+++ b/flang/test/Lower/select-case-statement.f90
@@ -413,6 +413,27 @@ subroutine sforall(num)
     print*, array(1)
   end subroutine sforall
 
+  ! CHECK-LABEL: func @_QPsnested
+  subroutine snested(str)
+    character(*), optional :: str
+    integer :: num
+
+    if (present(str)) then
+      select case (trim(str))
+        case ('a')
+          num = 10
+        case default
+          num = 20
+      end select
+      ! CHECK: ^bb5:  // 2 preds: ^bb3, ^bb4
+      ! CHECK: fir.freemem %{{[0-9]+}} : !fir.heap<!fir.char<1,?>>
+      ! CHECK: cf.br ^bb7
+    else
+      num = 30
+    end if
+    ! CHECK: ^bb7:  // 2 preds: ^bb5, ^bb6
+  end subroutine snested
+
   ! CHECK-LABEL: main
   program p
     integer sinteger, v(10)


        


More information about the flang-commits mailing list