[flang-commits] [flang] [flang] Implement lowering for the PAUSE statement (Fixes #166821) (PR #167115)

via flang-commits flang-commits at lists.llvm.org
Sat Nov 8 01:55:05 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-fir-hlfir

Author: None (sathvikreddy853)

<details>
<summary>Changes</summary>

Implements lowering for the Fortran `PAUSE` statement.

- Handles PAUSE with no operand.
- Handles PAUSE with integer argument.
- Handles PAUSE with character literal argument.
- Adds a new lowering test: flang/test/Lower/pause-statement.f90.

Unlike STOP, PAUSE does not unconditionally terminate control flow.
The lowering preserves labels and GOTOs, consistent with legacy Fortran behavior.

Fixes: #<!-- -->166821


---
Full diff: https://github.com/llvm/llvm-project/pull/167115.diff


2 Files Affected:

- (modified) flang/lib/Lower/Runtime.cpp (+48-4) 
- (modified) flang/test/Lower/pause-statement.f90 (+22-1) 


``````````diff
diff --git a/flang/lib/Lower/Runtime.cpp b/flang/lib/Lower/Runtime.cpp
index cb555249125f6..48c70f4180de0 100644
--- a/flang/lib/Lower/Runtime.cpp
+++ b/flang/lib/Lower/Runtime.cpp
@@ -263,12 +263,56 @@ void Fortran::lower::genSyncTeamStatement(
 
 void Fortran::lower::genPauseStatement(
     Fortran::lower::AbstractConverter &converter,
-    const Fortran::parser::PauseStmt &) {
+    const Fortran::parser::PauseStmt &stmt) {
+  
   fir::FirOpBuilder &builder = converter.getFirOpBuilder();
   mlir::Location loc = converter.getCurrentLocation();
-  mlir::func::FuncOp callee =
-      fir::runtime::getRuntimeFunc<mkRTKey(PauseStatement)>(loc, builder);
-  fir::CallOp::create(builder, loc, callee, mlir::ValueRange{});
+  Fortran::lower::StatementContext stmtCtx;
+
+  llvm::SmallVector<mlir::Value> operands;
+  mlir::func::FuncOp callee;
+  mlir::FunctionType calleeType;
+
+  if (stmt.v.has_value()) {
+    const auto &code = stmt.v.value();
+    auto expr = converter.genExprValue(*Fortran::semantics::GetExpr(code), stmtCtx);
+    LLVM_DEBUG(llvm::dbgs() << "pause expression: "; expr.dump(); llvm::dbgs() << '\n');
+    expr.match(
+        // Character-valued expression -> call PauseStatementText (CHAR, LEN)
+        [&](const fir::CharBoxValue &x) {
+          callee = fir::runtime::getRuntimeFunc<mkRTKey(PauseStatementText)>(loc, builder);
+          calleeType = callee.getFunctionType();
+
+          operands.push_back(
+              builder.createConvert(loc, calleeType.getInput(0), x.getAddr()));
+          operands.push_back(
+              builder.createConvert(loc, calleeType.getInput(1), x.getLen()));
+        },
+        // Numeric/unboxed value -> call PauseStatement which accepts an integer code.
+        [&](fir::UnboxedValue x) {
+           callee = fir::runtime::getRuntimeFunc<mkRTKey(PauseStatement)>(loc, builder);
+           calleeType = callee.getFunctionType();
+            if (calleeType.getNumInputs() >= 1) {
+              mlir::Value cast =
+                builder.createConvert(loc, calleeType.getInput(0), x);
+            operands.push_back(cast);
+            }
+        },
+        [&](auto) {
+          mlir::emitError(loc, "unhandled expression in PAUSE");
+          std::exit(1);
+        });
+  } else {
+    callee = fir::runtime::getRuntimeFunc<mkRTKey(PauseStatement)>(loc, builder);
+    calleeType = callee.getFunctionType();
+  }
+  
+  fir::CallOp::create(builder, loc, callee, operands);
+
+  // NOTE: PAUSE should not unconditionally terminate the current block.
+  // Unlike STOP, PAUSE does not necessarily abandon control flow, so do not
+  // subsequent control flow (e.g. GOTO/branches) to be generated.
+  // insert genUnreachable() here. Leaving the block un-terminated allows
 }
 
 void Fortran::lower::genPointerAssociate(fir::FirOpBuilder &builder,
diff --git a/flang/test/Lower/pause-statement.f90 b/flang/test/Lower/pause-statement.f90
index f4c8f6fbc4385..926f81e96cefd 100644
--- a/flang/test/Lower/pause-statement.f90
+++ b/flang/test/Lower/pause-statement.f90
@@ -1,4 +1,4 @@
-! RUN: bbc %s -emit-fir --canonicalize -o - | FileCheck %s
+! RUN: bbc %s -emit-fir -hlfir=false --canonicalize -o - | FileCheck %s
 
 ! CHECK-LABEL: pause_test
 subroutine pause_test()
@@ -6,3 +6,24 @@ subroutine pause_test()
   ! CHECK-NEXT: return
   pause
 end subroutine
+
+! CHECK-LABEL: pause_code
+subroutine pause_code()
+  pause 42
+ ! CHECK: fir.call @_Fortran{{.*}}PauseStatement
+ ! CHECK-NEXT: return
+end subroutine
+
+! CHECK-LABEL: pause_msg
+subroutine pause_msg()
+  pause "hello"
+  ! CHECK-DAG: %[[five:.*]] = arith.constant 5 : index
+  ! CHECK-DAG: %[[lit:.*]] = fir.address_of(@_QQ{{.*}}) : !fir.ref<!fir.char<1,5>>
+  ! CHECK-DAG: %[[buff:.*]] = fir.convert %[[lit]] : (!fir.ref<!fir.char<1,5>>) -> !fir.ref<i8>
+  ! CHECK-DAG: %[[len:.*]] = fir.convert %[[five]] : (index) -> i64
+  ! CHECK: fir.call @_Fortran{{.*}}PauseStatementText(%[[buff]], %[[len]])
+  ! CHECK-NEXT: return
+end subroutine
+
+! CHECK-DAG: func private @_Fortran{{.*}}PauseStatement
+! CHECK-DAG: func private @_Fortran{{.*}}PauseStatementText
\ No newline at end of file

``````````

</details>


https://github.com/llvm/llvm-project/pull/167115


More information about the flang-commits mailing list