[PATCH] D118695: [flang] Add lowering for basic empty SUBROUTINE

Valentin Clement via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 1 05:22:12 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 ability to lower an empty subroutine.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118695

Files:
  flang/lib/Lower/Bridge.cpp
  flang/test/Lower/basic-subroutine.f90


Index: flang/test/Lower/basic-subroutine.f90
===================================================================
--- /dev/null
+++ flang/test/Lower/basic-subroutine.f90
@@ -0,0 +1,13 @@
+! RUN: bbc %s --pft-test | FileCheck %s
+! RUN: bbc %s -o "-" -emit-fir | FileCheck %s --check-prefix=FIR
+
+subroutine sub1()
+end subroutine
+
+! CHECK: 1 Subroutine sub1: subroutine sub1()
+! CHECK:   1 EndSubroutineStmt: end subroutine
+! CHECK: End Subroutine sub1
+
+! FIR-LABEL: func @_QPsub1() {
+! FIR:         return
+! FIR:       }
Index: flang/lib/Lower/Bridge.cpp
===================================================================
--- flang/lib/Lower/Bridge.cpp
+++ flang/lib/Lower/Bridge.cpp
@@ -175,6 +175,8 @@
     setCurrentPosition(Fortran::lower::pft::stmtSourceLoc(funit.endStmt));
     if (funit.isMainProgram())
       genExitRoutine();
+    else
+      genFIRProcedureExit(funit, funit.getSubprogramSymbol());
     funit.finalBlock = nullptr;
     LLVM_DEBUG(llvm::dbgs() << "*** Lowering result:\n\n"
                             << *builder->getFunction() << '\n');
@@ -240,6 +242,22 @@
   }
   void genFIR(const Fortran::parser::EndProgramStmt &) { genExitRoutine(); }
 
+  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::BranchOp>(toLocation(), finalBlock);
+      // Set insertion point to final block.
+      builder->setInsertionPoint(finalBlock, finalBlock->end());
+    }
+    if (Fortran::semantics::IsFunction(symbol)) {
+      TODO(toLocation(), "Function lowering");
+    } else {
+      genExitRoutine();
+    }
+  }
+
   void genFIR(const Fortran::parser::CallStmt &stmt) {
     TODO(toLocation(), "CallStmt lowering");
   }
@@ -606,9 +624,8 @@
     TODO(toLocation(), "EndSelectStmt lowering");
   }
 
-  void genFIR(const Fortran::parser::EndSubroutineStmt &) {
-    TODO(toLocation(), "EndSubroutineStmt lowering");
-  }
+  // Nop statements - No code, or code is generated at the construct level.
+  void genFIR(const Fortran::parser::EndSubroutineStmt &) {} // nop
 
   void genFIR(const Fortran::parser::EntryStmt &) {
     TODO(toLocation(), "EntryStmt lowering");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118695.404893.patch
Type: text/x-patch
Size: 2369 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220201/5a8221b3/attachment.bin>


More information about the llvm-commits mailing list