Skip to content

Commit 9ceb0a7

Browse files
committed
Fix nested block constructs for SELECT CASE
In some scenarios, a SELECT CASE could cause an error while lowering to FIR. This was caused by a spurious extra branch added after the end statement. Fixes #62726 Differential Revision: https://reviews.llvm.org/D151118
1 parent 2a23de0 commit 9ceb0a7

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

flang/lib/Lower/Bridge.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,8 +1847,12 @@ class FirConverter : public Fortran::lower::AbstractConverter {
18471847
Fortran::lower::pft::Evaluation &eval = getEval();
18481848
Fortran::lower::StatementContext stmtCtx;
18491849
pushActiveConstruct(eval, stmtCtx);
1850-
for (Fortran::lower::pft::Evaluation &e : getEval().getNestedEvaluations())
1851-
genFIR(e);
1850+
for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations()) {
1851+
if (e.getIf<Fortran::parser::EndSelectStmt>())
1852+
maybeStartBlock(e.block);
1853+
else
1854+
genFIR(e);
1855+
}
18521856
popActiveConstruct();
18531857
}
18541858

@@ -2708,7 +2712,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
27082712
builder->restoreInsertionPoint(crtInsPt);
27092713
++typeGuardIdx;
27102714
} else if (eval.getIf<Fortran::parser::EndSelectStmt>()) {
2711-
genFIR(eval);
2715+
maybeStartBlock(eval.block);
27122716
if (hasLocalScope)
27132717
localSymbols.popScope();
27142718
} else {

flang/test/Lower/select-case-statement.f90

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,27 @@ subroutine sforall(num)
413413
print*, array(1)
414414
end subroutine sforall
415415

416+
! CHECK-LABEL: func @_QPsnested
417+
subroutine snested(str)
418+
character(*), optional :: str
419+
integer :: num
420+
421+
if (present(str)) then
422+
select case (trim(str))
423+
case ('a')
424+
num = 10
425+
case default
426+
num = 20
427+
end select
428+
! CHECK: ^bb5: // 2 preds: ^bb3, ^bb4
429+
! CHECK: fir.freemem %{{[0-9]+}} : !fir.heap<!fir.char<1,?>>
430+
! CHECK: cf.br ^bb7
431+
else
432+
num = 30
433+
end if
434+
! CHECK: ^bb7: // 2 preds: ^bb5, ^bb6
435+
end subroutine snested
436+
416437
! CHECK-LABEL: main
417438
program p
418439
integer sinteger, v(10)

0 commit comments

Comments
 (0)