Skip to content

Commit 8914d5e

Browse files
[flang] add check for number of inputs and remove unreachable block code
1 parent faf6544 commit 8914d5e

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

flang/lib/Lower/Runtime.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,32 +264,39 @@ void Fortran::lower::genSyncTeamStatement(
264264
void Fortran::lower::genPauseStatement(
265265
Fortran::lower::AbstractConverter &converter,
266266
const Fortran::parser::PauseStmt &stmt) {
267+
267268
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
268269
mlir::Location loc = converter.getCurrentLocation();
269270
Fortran::lower::StatementContext stmtCtx;
271+
270272
llvm::SmallVector<mlir::Value> operands;
271273
mlir::func::FuncOp callee;
272274
mlir::FunctionType calleeType;
273275

274276
if (stmt.v.has_value()) {
275-
const Fortran::parser::StopCode &code = stmt.v.value();
277+
const auto &code = stmt.v.value();
276278
auto expr = converter.genExprValue(*Fortran::semantics::GetExpr(code), stmtCtx);
277279
LLVM_DEBUG(llvm::dbgs() << "pause expression: "; expr.dump(); llvm::dbgs() << '\n');
278280
expr.match(
281+
// Character-valued expression -> call PauseStatementText (CHAR, LEN)
279282
[&](const fir::CharBoxValue &x) {
280283
callee = fir::runtime::getRuntimeFunc<mkRTKey(PauseStatementText)>(loc, builder);
281284
calleeType = callee.getFunctionType();
285+
282286
operands.push_back(
283287
builder.createConvert(loc, calleeType.getInput(0), x.getAddr()));
284288
operands.push_back(
285289
builder.createConvert(loc, calleeType.getInput(1), x.getLen()));
286290
},
291+
// Numeric/unboxed value -> call PauseStatement which accepts an integer code.
287292
[&](fir::UnboxedValue x) {
288-
callee = fir::runtime::getRuntimeFunc<mkRTKey(PauseStatement)>(loc, builder);
289-
calleeType = callee.getFunctionType();
290-
mlir::Value cast =
291-
builder.createConvert(loc, calleeType.getInput(0), x);
292-
operands.push_back(cast);
293+
callee = fir::runtime::getRuntimeFunc<mkRTKey(PauseStatement)>(loc, builder);
294+
calleeType = callee.getFunctionType();
295+
if (calleeType.getNumInputs() >= 1) {
296+
mlir::Value cast =
297+
builder.createConvert(loc, calleeType.getInput(0), x);
298+
operands.push_back(cast);
299+
}
293300
},
294301
[&](auto) {
295302
mlir::emitError(loc, "unhandled expression in PAUSE");
@@ -299,16 +306,13 @@ void Fortran::lower::genPauseStatement(
299306
callee = fir::runtime::getRuntimeFunc<mkRTKey(PauseStatement)>(loc, builder);
300307
calleeType = callee.getFunctionType();
301308
}
302-
309+
303310
fir::CallOp::create(builder, loc, callee, operands);
304311

305-
auto blockIsUnterminated = [&builder]() {
306-
mlir::Block *currentBlock = builder.getBlock();
307-
return currentBlock->empty() ||
308-
!currentBlock->back().hasTrait<mlir::OpTrait::IsTerminator>();
309-
};
310-
if (blockIsUnterminated())
311-
genUnreachable(builder, loc);
312+
// NOTE: PAUSE should not unconditionally terminate the current block.
313+
// Unlike STOP, PAUSE does not necessarily abandon control flow, so do not
314+
// subsequent control flow (e.g. GOTO/branches) to be generated.
315+
// insert genUnreachable() here. Leaving the block un-terminated allows
312316
}
313317

314318
void Fortran::lower::genPointerAssociate(fir::FirOpBuilder &builder,

0 commit comments

Comments
 (0)