[flang-commits] [flang] [flang][hlfir] Do not emit extra declare for dummy used in BLOCK (PR #69184)

via flang-commits flang-commits at lists.llvm.org
Mon Oct 16 02:43:06 PDT 2023


https://github.com/jeanPerier created https://github.com/llvm/llvm-project/pull/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.

>From 277ce0092b0a4dcbec1f1256956c113a1d7cd925 Mon Sep 17 00:00:00 2001
From: Jean Perier <jperier at nvidia.com>
Date: Mon, 16 Oct 2023 02:34:32 -0700
Subject: [PATCH] [flang][hlfir] Do not emit extra declare for dummy used in
 BLOCK

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.
---
 flang/lib/Lower/Bridge.cpp                    |  8 ++++--
 .../Lower/HLFIR/convert-variable-block.f90    | 25 +++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 flang/test/Lower/HLFIR/convert-variable-block.f90

diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 5ac4d822faaae58..e6d6a19009e9de6 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -2610,8 +2610,12 @@ class FirConverter : public Fortran::lower::AbstractConverter {
         scopeBlockIdMap.try_emplace(&scope, ++blockId);
         Fortran::lower::AggregateStoreMap storeMap;
         for (const Fortran::lower::pft::Variable &var :
-             Fortran::lower::pft::getScopeVariableList(scope))
-          instantiateVar(var, storeMap);
+             Fortran::lower::pft::getScopeVariableList(scope)) {
+          // Do no instantiate again variables from the block host
+          // that appears in specification of block variables.
+          if (!var.hasSymbol() || !lookupSymbol(var.getSymbol()))
+            instantiateVar(var, storeMap);
+        }
       } else if (e.getIf<Fortran::parser::EndBlockStmt>()) {
         if (eval.lowerAsUnstructured())
           maybeStartBlock(e.block);
diff --git a/flang/test/Lower/HLFIR/convert-variable-block.f90 b/flang/test/Lower/HLFIR/convert-variable-block.f90
new file mode 100644
index 000000000000000..30f8eacaaed17bd
--- /dev/null
+++ b/flang/test/Lower/HLFIR/convert-variable-block.f90
@@ -0,0 +1,25 @@
+! Test that hlfir.declare is not created again for dummy arguments
+! used in specifications of BLOCK variables.
+! RUN: bbc -emit-hlfir %s -o - | FileCheck %s
+
+subroutine test(n)
+  integer(8) :: n
+  call before_block()
+  block
+    real :: x(n)
+    call foo(x)
+  end block
+end subroutine
+! CHECK-LABEL:   func.func @_QPtest(
+! CHECK-SAME:                       %[[VAL_0:.*]]: !fir.ref<i64> {fir.bindc_name = "n"}) {
+! CHECK:           %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFtestEn"} : (!fir.ref<i64>) -> (!fir.ref<i64>, !fir.ref<i64>)
+! CHECK:           fir.call @_QPbefore_block() {{.*}}: () -> ()
+! CHECK:           %[[VAL_3:.*]] = fir.load %[[VAL_1]]#0 : !fir.ref<i64>
+! CHECK:           %[[VAL_4:.*]] = fir.convert %[[VAL_3]] : (i64) -> index
+! CHECK:           %[[VAL_5:.*]] = arith.constant 0 : index
+! CHECK:           %[[VAL_6:.*]] = arith.cmpi sgt, %[[VAL_4]], %[[VAL_5]] : index
+! CHECK:           %[[VAL_7:.*]] = arith.select %[[VAL_6]], %[[VAL_4]], %[[VAL_5]] : index
+! CHECK:           %[[VAL_8:.*]] = fir.alloca !fir.array<?xf32>, %[[VAL_7]] {bindc_name = "x", uniq_name = "_QFtestB1Ex"}
+! CHECK:           %[[VAL_9:.*]] = fir.shape %[[VAL_7]] : (index) -> !fir.shape<1>
+! 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>>)
+! CHECK:           fir.call @_QPfoo(%[[VAL_10]]#1) {{.*}}: (!fir.ref<!fir.array<?xf32>>) -> ()



More information about the flang-commits mailing list