[flang-commits] [flang] [Flang] Do not provide a shape operand for box addresses (PR #157732)

Carlos Seo via flang-commits flang-commits at lists.llvm.org
Tue Sep 9 11:28:40 PDT 2025


https://github.com/ceseo created https://github.com/llvm/llvm-project/pull/157732

Box addresses already contain shape information. Do not provide an additional shape operand.

Fixes #132648

>From 86baf6681ada00fdc8b5065fb34bef662836d690 Mon Sep 17 00:00:00 2001
From: Carlos Seo <carlos.seo at linaro.org>
Date: Tue, 9 Sep 2025 18:24:49 +0000
Subject: [PATCH] [Flang] Do not provide a shape operand for box addresses

Box addresses already contain shape information. Do not provide an
additional shape operand.

Fixes #132648
---
 flang/lib/Lower/ConvertVariable.cpp |  8 +++++--
 flang/test/Lower/box-address.f90    | 34 +++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 2 deletions(-)
 create mode 100644 flang/test/Lower/box-address.f90

diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index c79c9b1ab0f51..4b6523705a42b 100644
--- a/flang/lib/Lower/ConvertVariable.cpp
+++ b/flang/lib/Lower/ConvertVariable.cpp
@@ -2008,9 +2008,13 @@ static void genDeclareSymbol(Fortran::lower::AbstractConverter &converter,
     if (converter.isRegisteredDummySymbol(sym))
       dummyScope = converter.dummyArgsScopeValue();
     auto [storage, storageOffset] = converter.getSymbolStorage(sym);
+    // For box addresses, shape information is already included, so we must
+    // not provide a shape operand.
+    mlir::Value shapeForDeclare =
+        fir::isBoxAddress(base.getType()) ? mlir::Value{} : shapeOrShift;
     auto newBase = hlfir::DeclareOp::create(
-        builder, loc, base, name, shapeOrShift, lenParams, dummyScope, storage,
-        storageOffset, attributes, dataAttr);
+        builder, loc, base, name, shapeForDeclare, lenParams, dummyScope,
+        storage, storageOffset, attributes, dataAttr);
     symMap.addVariableDefinition(sym, newBase, force);
     return;
   }
diff --git a/flang/test/Lower/box-address.f90 b/flang/test/Lower/box-address.f90
new file mode 100644
index 0000000000000..04f14188a7bec
--- /dev/null
+++ b/flang/test/Lower/box-address.f90
@@ -0,0 +1,34 @@
+! RUN: flang -fc1 -emit-hlfir %s -o - | FileCheck %s
+
+module m3
+  type x1
+     integer::ix1
+  end type x1
+  type,extends(x1)::x2
+  end type x2
+  type,extends(x2)::x3
+  end type x3
+  class(x1),pointer,dimension(:)::cy1
+contains
+  subroutine dummy()
+  entry      chk(c1)
+   class(x1),dimension(3)::c1
+ end subroutine dummy
+end module m3
+! CHECK-LABEL: func.func @_QMm3Pchk(
+! CHECK-SAME:    %[[ARG0:.*]]: !fir.class<!fir.array<3x!fir.type<_QMm3Tx1{ix1:i32}>>> {fir.bindc_name = "c1"}) {
+! CHECK:         %[[DUMMY_SCOPE:.*]] = fir.dummy_scope : !fir.dscope
+! CHECK:         %[[DECLARE:.*]]:2 = hlfir.declare %[[ARG0]] dummy_scope %[[DUMMY_SCOPE]] {uniq_name = "_QMm3FdummyEc1"} : (!fir.class<!fir.array<3x!fir.type<_QMm3Tx1{ix1:i32}>>>, !fir.dscope) -> (!fir.class<!fir.array<3x!fir.type<_QMm3Tx1{ix1:i32}>>>, !fir.class<!fir.array<3x!fir.type<_QMm3Tx1{ix1:i32}>>>)
+
+subroutine s1
+  use m3
+  type(x1),target::ty1(3)
+  ty1%ix1=[1,2,3]
+  cy1=>ty1
+  call chk(cy1)
+end subroutine s1
+
+program main
+  call s1
+  print *,'pass'
+end program main



More information about the flang-commits mailing list