From ca53e049e0130be8d922a6ae80a1dcbddea2b25b Mon Sep 17 00:00:00 2001 From: Valentin Clement Date: Thu, 3 Feb 2022 11:30:50 +0100 Subject: [PATCH] [flang] Lower integer constant code for STOP stmt This patch lower the integer constant code in the STOP statement. The code is lowered to `arith.constant`. This patch is part of the upstreaming effort from fir-dev branch. Reviewed By: schweitz, kiranchandramohan Differential Revision: https://reviews.llvm.org/D118787 --- flang/lib/Lower/Runtime.cpp | 23 +++++++++++++++++++++-- flang/test/Lower/stop-statement.f90 | 9 +++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/flang/lib/Lower/Runtime.cpp b/flang/lib/Lower/Runtime.cpp index a49b091..3ec2fed 100644 --- a/flang/lib/Lower/Runtime.cpp +++ b/flang/lib/Lower/Runtime.cpp @@ -42,8 +42,27 @@ void Fortran::lower::genStopStatement( mlir::FuncOp callee; mlir::FunctionType calleeType; // First operand is stop code (zero if absent) - if (std::get>(stmt.t)) { - TODO(loc, "STOP first operand not lowered yet"); + if (const auto &code = + std::get>(stmt.t)) { + auto expr = converter.genExprValue(*Fortran::semantics::GetExpr(*code)); + LLVM_DEBUG(llvm::dbgs() << "stop expression: "; expr.dump(); + llvm::dbgs() << '\n'); + expr.match( + [&](const fir::CharBoxValue &x) { + TODO(loc, "STOP CharBoxValue first operand not lowered yet"); + }, + [&](fir::UnboxedValue x) { + callee = fir::runtime::getRuntimeFunc( + loc, builder); + calleeType = callee.getType(); + mlir::Value cast = + builder.createConvert(loc, calleeType.getInput(0), x); + operands.push_back(cast); + }, + [&](auto) { + mlir::emitError(loc, "unhandled expression in STOP"); + std::exit(1); + }); } else { callee = fir::runtime::getRuntimeFunc(loc, builder); calleeType = callee.getType(); diff --git a/flang/test/Lower/stop-statement.f90 b/flang/test/Lower/stop-statement.f90 index 497b6db..062d835 100644 --- a/flang/test/Lower/stop-statement.f90 +++ b/flang/test/Lower/stop-statement.f90 @@ -19,3 +19,12 @@ subroutine stop_error() ! CHECK: fir.call @_Fortran{{.*}}StopStatement(%[[c0]], %[[true]], %[[false]]) ! CHECK-NEXT: fir.unreachable end subroutine + +! CHECK-LABEL stop_code +subroutine stop_code() + stop 42 + ! CHECK-DAG: %[[c42:.*]] = arith.constant 42 : i32 + ! CHECK-DAG: %[[false:.*]] = arith.constant false + ! CHECK: fir.call @_Fortran{{.*}}StopStatement(%[[c42]], %[[false]], %[[false]]) + ! CHECK-NEXT: fir.unreachable +end subroutine -- 2.7.4