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

via flang-commits flang-commits at lists.llvm.org
Thu Nov 20 06:59:45 PST 2025


================
@@ -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
----------------
jeanPerier wrote:

nit: unterminated sentence.
I think the comment can be shorter "// PAUSE does not terminate the current block because the program can be resumed by the user and continue normal execution."

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


More information about the flang-commits mailing list