[PATCH] D119429: [flang] Lower simple RETURN statement

Valentin Clement via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 10 04:54:17 PST 2022


clementval created this revision.
clementval added reviewers: jeanPerier, kiranchandramohan, PeteSteinfeld, schweitz, svedanayagam.
Herald added a subscriber: mehdi_amini.
Herald added a project: Flang.
clementval requested review of this revision.
Herald added subscribers: llvm-commits, jdoerfert.
Herald added a project: LLVM.

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.

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>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119429

Files:
  flang/lib/Lower/Bridge.cpp
  flang/test/Lower/return-statement.f90


Index: flang/test/Lower/return-statement.f90
===================================================================
--- /dev/null
+++ flang/test/Lower/return-statement.f90
@@ -0,0 +1,18 @@
+! 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
Index: flang/lib/Lower/Bridge.cpp
===================================================================
--- flang/lib/Lower/Bridge.cpp
+++ flang/lib/Lower/Bridge.cpp
@@ -337,6 +337,13 @@
 
   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 @@
   }
 
   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 &) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119429.407480.patch
Type: text/x-patch
Size: 2235 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220210/29ed056b/attachment-0001.bin>


More information about the llvm-commits mailing list