Skip to content

Commit bfcd053

Browse files
authored
[flang][hlfir] Do not emit extra declare for dummy used in BLOCK (#69184)
When a variable is used in a specification expression in a scope, it is added to the list of variables that must be instantiated when lowering the scope. When lowering a BLOCK, this caused instantiateVar to be called again on all the host block variables appearing in block variable specification expressions. This caused an extra declare to be emitted for dummy inside block (for non dummy, instantiateVar is a no-op if the symbol is already mapped). Only call instantiateVar if the symbol is not mapped when lowering BLOCK variables.
1 parent 77ab08e commit bfcd053

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

flang/lib/Lower/Bridge.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,8 +2610,12 @@ class FirConverter : public Fortran::lower::AbstractConverter {
26102610
scopeBlockIdMap.try_emplace(&scope, ++blockId);
26112611
Fortran::lower::AggregateStoreMap storeMap;
26122612
for (const Fortran::lower::pft::Variable &var :
2613-
Fortran::lower::pft::getScopeVariableList(scope))
2614-
instantiateVar(var, storeMap);
2613+
Fortran::lower::pft::getScopeVariableList(scope)) {
2614+
// Do no instantiate again variables from the block host
2615+
// that appears in specification of block variables.
2616+
if (!var.hasSymbol() || !lookupSymbol(var.getSymbol()))
2617+
instantiateVar(var, storeMap);
2618+
}
26152619
} else if (e.getIf<Fortran::parser::EndBlockStmt>()) {
26162620
if (eval.lowerAsUnstructured())
26172621
maybeStartBlock(e.block);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
! Test that hlfir.declare is not created again for dummy arguments
2+
! used in specifications of BLOCK variables.
3+
! RUN: bbc -emit-hlfir %s -o - | FileCheck %s
4+
5+
subroutine test(n)
6+
integer(8) :: n
7+
call before_block()
8+
block
9+
real :: x(n)
10+
call foo(x)
11+
end block
12+
end subroutine
13+
! CHECK-LABEL: func.func @_QPtest(
14+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i64> {fir.bindc_name = "n"}) {
15+
! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFtestEn"} : (!fir.ref<i64>) -> (!fir.ref<i64>, !fir.ref<i64>)
16+
! CHECK: fir.call @_QPbefore_block() {{.*}}: () -> ()
17+
! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_1]]#0 : !fir.ref<i64>
18+
! CHECK: %[[VAL_4:.*]] = fir.convert %[[VAL_3]] : (i64) -> index
19+
! CHECK: %[[VAL_5:.*]] = arith.constant 0 : index
20+
! CHECK: %[[VAL_6:.*]] = arith.cmpi sgt, %[[VAL_4]], %[[VAL_5]] : index
21+
! CHECK: %[[VAL_7:.*]] = arith.select %[[VAL_6]], %[[VAL_4]], %[[VAL_5]] : index
22+
! CHECK: %[[VAL_8:.*]] = fir.alloca !fir.array<?xf32>, %[[VAL_7]] {bindc_name = "x", uniq_name = "_QFtestB1Ex"}
23+
! CHECK: %[[VAL_9:.*]] = fir.shape %[[VAL_7]] : (index) -> !fir.shape<1>
24+
! CHECK: %[[VAL_10:.*]]:2 = hlfir.declare %[[VAL_8]](%[[VAL_9]]) {uniq_name = "_QFtestB1Ex"} : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>) -> (!fir.box<!fir.array<?xf32>>, !fir.ref<!fir.array<?xf32>>)
25+
! CHECK: fir.call @_QPfoo(%[[VAL_10]]#1) {{.*}}: (!fir.ref<!fir.array<?xf32>>) -> ()

0 commit comments

Comments
 (0)