[flang-commits] [flang] 85b89ed - [flang] Lower simple RETURN statement

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Thu Feb 10 09:36:12 PST 2022


Author: Valentin Clement
Date: 2022-02-10T18:36:04+01:00
New Revision: 85b89ed213c41a8d7dafff957c8d20a247e6d9df

URL: https://github.com/llvm/llvm-project/commit/85b89ed213c41a8d7dafff957c8d20a247e6d9df
DIFF: https://github.com/llvm/llvm-project/commit/85b89ed213c41a8d7dafff957c8d20a247e6d9df.diff

LOG: [flang] Lower simple RETURN statement

This patch adds the lowering for the RETURN statement
without alternate returns in the main program or in subroutine
and functions.

This patch is part of the upstreaming effort from fir-dev branch.

Reviewed By: kiranchandramohan

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

Co-authored-by: V Donaldson <vdonaldson at nvidia.com>
Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>
Co-authored-by: Jean Perier <jperier at nvidia.com>

Added: 
    flang/test/Lower/return-statement.f90

Modified: 
    flang/lib/Lower/Bridge.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index ffaec4df07051..59f31b5ed2459 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -337,6 +337,13 @@ class FirConverter : public Fortran::lower::AbstractConverter {
 
   void genFIRProcedureExit(Fortran::lower::pft::FunctionLikeUnit &funit,
                            const Fortran::semantics::Symbol &symbol) {
+    if (mlir::Block *finalBlock = funit.finalBlock) {
+      // The current block must end with a terminator.
+      if (blockIsUnterminated())
+        builder->create<mlir::cf::BranchOp>(toLocation(), finalBlock);
+      // Set insertion point to final block.
+      builder->setInsertionPoint(finalBlock, finalBlock->end());
+    }
     if (Fortran::semantics::IsFunction(symbol)) {
       TODO(toLocation(), "Function lowering");
     } else {
@@ -652,7 +659,24 @@ class FirConverter : public Fortran::lower::AbstractConverter {
   }
 
   void genFIR(const Fortran::parser::ReturnStmt &stmt) {
-    TODO(toLocation(), "ReturnStmt lowering");
+    Fortran::lower::pft::FunctionLikeUnit *funit =
+        getEval().getOwningProcedure();
+    assert(funit && "not inside main program, function or subroutine");
+    if (funit->isMainProgram()) {
+      genExitRoutine();
+      return;
+    }
+    mlir::Location loc = toLocation();
+    if (stmt.v) {
+      TODO(loc, "Alternate return statement");
+    }
+    // Branch to the last block of the SUBROUTINE, which has the actual return.
+    if (!funit->finalBlock) {
+      mlir::OpBuilder::InsertPoint insPt = builder->saveInsertionPoint();
+      funit->finalBlock = builder->createBlock(&builder->getRegion());
+      builder->restoreInsertionPoint(insPt);
+    }
+    builder->create<mlir::cf::BranchOp>(loc, funit->finalBlock);
   }
 
   void genFIR(const Fortran::parser::CycleStmt &) {

diff  --git a/flang/test/Lower/return-statement.f90 b/flang/test/Lower/return-statement.f90
new file mode 100644
index 0000000000000..99ede9da20707
--- /dev/null
+++ b/flang/test/Lower/return-statement.f90
@@ -0,0 +1,34 @@
+! RUN: bbc %s -o "-" -emit-fir | FileCheck %s
+
+program basic
+  return
+end program
+
+! CHECK-LABEL: func @_QQmain() {
+! CHECK:         return
+! CHECK:       }
+
+subroutine sub1()
+  return
+end
+
+! CHECK-LABEL: func @_QPsub1() {
+! CHECK:         cf.br ^bb1
+! CHECK:       ^bb1:  // pred: ^bb0
+! CHECK:         return
+
+subroutine sub2()
+  goto 3
+  2 return
+  3 goto 2
+end
+
+! CHECK-LABEL: func @_QPsub2() {
+! CHECK:         cf.br ^bb2
+! CHECK:       ^bb1:  // pred: ^bb2
+! CHECK:         cf.br ^bb3
+! CHECK:       ^bb2:  // pred: ^bb0
+! CHECK:         cf.br ^bb1
+! CHECK:       ^bb3:  // pred: ^bb1
+! CHECK:         return
+


        


More information about the flang-commits mailing list