-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[flang] Implement lowering for the PAUSE statement (Fixes #166821) #167115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flang] Implement lowering for the PAUSE statement (Fixes #166821) #167115
Conversation
This patch adds lowering support for the Fortran PAUSE statement to FIR. It mirrors the STOP statement lowering logic to handle: - PAUSE with no operand - PAUSE <integer> - PAUSE 'message' Fixes llvm#166821.
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-flang-fir-hlfir Author: None (sathvikreddy853) ChangesImplements lowering for the Fortran
Unlike STOP, PAUSE does not unconditionally terminate control flow. Fixes: #166821 Full diff: https://github.com/llvm/llvm-project/pull/167115.diff 2 Files Affected:
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
|
jeanPerier
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this.
|
Hey @jeanPerier, thanks for your review. I have made the required changes could you please review it once again. |
jeanPerier
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, few more nits, LGTM otherwise.
|
Once Jean approves this PR, I want to run some internal tests before it's merged. ( @sathvikreddy853 won't be able to merge it anyway, so I'll take care of merging once I run the tests.) |
|
Hey @jeanPerier and @eugeneepshteyn. I have made all the required changes. Could please take one more look at it? |
|
Hi @jeanPerier, @eugeneepshteyn. Just a quick follow‑up (no rush at all). I’ve addressed the requested changes on this PAUSE lowering PR and would really appreciate another review when you have time. |
jeanPerier
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the --hlfir=false option on the test, LGTM otherwise, thanks.
Remove hlfir and add an additional check for hlfir.declare
|
Removed |
jeanPerier
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks
|
Thank you for the review and approval! |
|
Hi @eugeneepshteyn, @jeanPerier has approved this PR. When you have a moment, could you please run the internal tests and handle the merge if everything looks good? Thanks! |
|
I started the tests. Once it's all good, I'll merge this PR. |
eugeneepshteyn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both code and testing LGTM
|
@sathvikreddy853 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
|
Thanks a lot, @eugeneepshteyn and @jeanPerier. Sincerely appreciate your help on my first pull request. |
llvm#167115) 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: llvm#166821 --------- Co-authored-by: aditya nath <[email protected]> Co-authored-by: Eugene Epshteyn <[email protected]>
llvm#167115) 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: llvm#166821 --------- Co-authored-by: aditya nath <[email protected]> Co-authored-by: Eugene Epshteyn <[email protected]>
llvm#167115) 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: llvm#166821 --------- Co-authored-by: aditya nath <[email protected]> Co-authored-by: Eugene Epshteyn <[email protected]>
Implements lowering for the Fortran
PAUSEstatement.Unlike STOP, PAUSE does not unconditionally terminate control flow.
The lowering preserves labels and GOTOs, consistent with legacy Fortran behavior.
Fixes: #166821