[flang-commits] [flang] 656b8d6 - [flang][NFC] Add array lowering tests

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Mon Jun 27 05:20:05 PDT 2022


Author: Valentin Clement
Date: 2022-06-27T14:19:54+02:00
New Revision: 656b8d6c0193ca5e7ac0504d51dd67c953963fa1

URL: https://github.com/llvm/llvm-project/commit/656b8d6c0193ca5e7ac0504d51dd67c953963fa1
DIFF: https://github.com/llvm/llvm-project/commit/656b8d6c0193ca5e7ac0504d51dd67c953963fa1.diff

LOG: [flang][NFC] Add array lowering tests

These tests were left behind during the upstreaming of parts lowering.

This patch is part of the upstreaming effort from fir-dev branch.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D128632

Co-authored-by: V Donaldson <vdonaldson at nvidia.com>
Co-authored-by: Jean Perier <jperier at nvidia.com>
Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>

Added: 
    flang/test/Lower/array-derived-assignments.f90
    flang/test/Lower/array-derived.f90
    flang/test/Lower/array-elemental-calls-char-byval.f90
    flang/test/Lower/array-elemental-calls-char.f90
    flang/test/Lower/array-elemental-subroutines.f90
    flang/test/Lower/array-expression-assumed-size.f90
    flang/test/Lower/array-expression-slice-1.f90
    flang/test/Lower/array-temp.f90

Modified: 
    

Removed: 
    


################################################################################
diff  --git a/flang/test/Lower/array-derived-assignments.f90 b/flang/test/Lower/array-derived-assignments.f90
new file mode 100644
index 0000000000000..f2e1105bdc254
--- /dev/null
+++ b/flang/test/Lower/array-derived-assignments.f90
@@ -0,0 +1,104 @@
+! Test derived type assignment lowering inside array expression
+! RUN: bbc %s -o - | FileCheck %s
+
+module array_derived_assign
+  type simple_copy
+    integer :: i
+    character(10) :: c(20)
+    real, pointer :: p(:)
+  end type
+  type deep_copy
+    integer :: i
+    real, allocatable :: a(:)
+  end type
+contains
+
+! Simple copies are implemented inline component by component.
+! CHECK-LABEL: func @_QMarray_derived_assignPtest_simple_copy(
+! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<10x!fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>>{{.*}}, %[[VAL_1:.*]]: !fir.ref<!fir.array<10x!fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>>{{.*}}) {
+subroutine test_simple_copy(t1, t2)
+  type(simple_copy) :: t1(10), t2(10)
+  ! CHECK-DAG:         %[[VAL_2:.*]] = arith.constant 20 : index
+  ! CHECK-DAG:         %[[VAL_3:.*]] = arith.constant 10 : index
+  ! CHECK-DAG:         %[[VAL_4:.*]] = arith.constant false
+  ! CHECK-DAG:         %[[VAL_5:.*]] = arith.constant 0 : index
+  ! CHECK-DAG:         %[[VAL_6:.*]] = arith.constant 1 : index
+  ! CHECK-DAG:         %[[VAL_7:.*]] = fir.shape %[[VAL_3]] : (index) -> !fir.shape<1>
+  ! CHECK:         br ^bb1(%[[VAL_5]], %[[VAL_3]] : index, index)
+  ! CHECK:       ^bb1(%[[VAL_8:.*]]: index, %[[VAL_9:.*]]: index):
+  ! CHECK:         %[[VAL_10:.*]] = arith.cmpi sgt, %[[VAL_9]], %[[VAL_5]] : index
+  ! CHECK:         cond_br %[[VAL_10]], ^bb2, ^bb6
+  ! CHECK:       ^bb2:
+  ! CHECK:         %[[VAL_11:.*]] = arith.addi %[[VAL_8]], %[[VAL_6]] : index
+  ! CHECK:         %[[VAL_12:.*]] = fir.array_coor %[[VAL_1]](%[[VAL_7]]) %[[VAL_11]] : (!fir.ref<!fir.array<10x!fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>>, !fir.shape<1>, index) -> !fir.ref<!fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>
+  ! CHECK:         %[[VAL_13:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_7]]) %[[VAL_11]] : (!fir.ref<!fir.array<10x!fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>>, !fir.shape<1>, index) -> !fir.ref<!fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>
+  ! CHECK:         %[[VAL_14:.*]] = fir.field_index i, !fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
+  ! CHECK:         %[[VAL_15:.*]] = fir.coordinate_of %[[VAL_12]], %[[VAL_14]] : (!fir.ref<!fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, !fir.field) -> !fir.ref<i32>
+  ! CHECK:         %[[VAL_16:.*]] = fir.coordinate_of %[[VAL_13]], %[[VAL_14]] : (!fir.ref<!fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, !fir.field) -> !fir.ref<i32>
+  ! CHECK:         %[[VAL_17:.*]] = fir.load %[[VAL_15]] : !fir.ref<i32>
+  ! CHECK:         fir.store %[[VAL_17]] to %[[VAL_16]] : !fir.ref<i32>
+  ! CHECK:         %[[VAL_18:.*]] = fir.field_index c, !fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
+  ! CHECK:         %[[VAL_19:.*]] = fir.coordinate_of %[[VAL_12]], %[[VAL_18]] : (!fir.ref<!fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, !fir.field) -> !fir.ref<!fir.array<20x!fir.char<1,10>>>
+  ! CHECK:         %[[VAL_20:.*]] = fir.coordinate_of %[[VAL_13]], %[[VAL_18]] : (!fir.ref<!fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, !fir.field) -> !fir.ref<!fir.array<20x!fir.char<1,10>>>
+  ! CHECK:         br ^bb3(%[[VAL_5]], %[[VAL_2]] : index, index)
+  ! CHECK:       ^bb3(%[[VAL_21:.*]]: index, %[[VAL_22:.*]]: index):
+  ! CHECK:         %[[VAL_23:.*]] = arith.cmpi sgt, %[[VAL_22]], %[[VAL_5]] : index
+  ! CHECK:         cond_br %[[VAL_23]], ^bb4, ^bb5
+  ! CHECK:       ^bb4:
+  ! CHECK:         %[[VAL_24:.*]] = fir.coordinate_of %[[VAL_20]], %[[VAL_21]] : (!fir.ref<!fir.array<20x!fir.char<1,10>>>, index) -> !fir.ref<!fir.char<1,10>>
+  ! CHECK:         %[[VAL_25:.*]] = fir.coordinate_of %[[VAL_19]], %[[VAL_21]] : (!fir.ref<!fir.array<20x!fir.char<1,10>>>, index) -> !fir.ref<!fir.char<1,10>>
+  ! CHECK:         %[[VAL_26:.*]] = fir.convert %[[VAL_3]] : (index) -> i64
+  ! CHECK:         %[[VAL_27:.*]] = fir.convert %[[VAL_24]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<i8>
+  ! CHECK:         %[[VAL_28:.*]] = fir.convert %[[VAL_25]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<i8>
+  ! CHECK:         fir.call @llvm.memmove.p0.p0.i64(%[[VAL_27]], %[[VAL_28]], %[[VAL_26]], %[[VAL_4]]) : (!fir.ref<i8>, !fir.ref<i8>, i64, i1) -> ()
+  ! CHECK:         %[[VAL_29:.*]] = arith.addi %[[VAL_21]], %[[VAL_6]] : index
+  ! CHECK:         %[[VAL_30:.*]] = arith.subi %[[VAL_22]], %[[VAL_6]] : index
+  ! CHECK:         br ^bb3(%[[VAL_29]], %[[VAL_30]] : index, index)
+  ! CHECK:       ^bb5:
+  ! CHECK:         %[[VAL_31:.*]] = fir.field_index p, !fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
+  ! CHECK:         %[[VAL_32:.*]] = fir.coordinate_of %[[VAL_12]], %[[VAL_31]] : (!fir.ref<!fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, !fir.field) -> !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
+  ! CHECK:         %[[VAL_33:.*]] = fir.coordinate_of %[[VAL_13]], %[[VAL_31]] : (!fir.ref<!fir.type<_QMarray_derived_assignTsimple_copy{i:i32,c:!fir.array<20x!fir.char<1,10>>,p:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, !fir.field) -> !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
+  ! CHECK:         %[[VAL_34:.*]] = fir.load %[[VAL_32]] : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
+  ! CHECK:         fir.store %[[VAL_34]] to %[[VAL_33]] : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
+  ! CHECK:         %[[VAL_35:.*]] = arith.subi %[[VAL_9]], %[[VAL_6]] : index
+  ! CHECK:         br ^bb1(%[[VAL_11]], %[[VAL_35]] : index, index)
+  ! CHECK:       ^bb6:
+  t1 = t2
+  ! CHECK:         return
+  ! CHECK:       }
+end subroutine
+
+! Types require more complex assignments are passed to the runtime
+! CHECK-LABEL: func @_QMarray_derived_assignPtest_deep_copy(
+! CHECK-SAME:    %[[VAL_0:.*]]: !fir.ref<!fir.array<10x!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>>{{.*}}, %[[VAL_1:.*]]: !fir.ref<!fir.array<10x!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>>{{.*}}) {
+subroutine test_deep_copy(t1, t2)
+  ! CHECK-DAG:     %[[VAL_3:.*]] = arith.constant 10 : index
+  ! CHECK-DAG:     %[[VAL_4:.*]] = arith.constant 0 : index
+  ! CHECK-DAG:     %[[VAL_5:.*]] = arith.constant 1 : index
+  ! CHECK:         %[[VAL_6:.*]] = fir.alloca !fir.box<!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>
+  ! CHECK:         %[[VAL_7:.*]] = fir.shape %[[VAL_3]] : (index) -> !fir.shape<1>
+  ! CHECK:         br ^bb1(%[[VAL_4]], %[[VAL_3]] : index, index)
+  ! CHECK:       ^bb1(%[[VAL_8:.*]]: index, %[[VAL_9:.*]]: index):
+  ! CHECK:         %[[VAL_10:.*]] = arith.cmpi sgt, %[[VAL_9]], %[[VAL_4]] : index
+  ! CHECK:         cond_br %[[VAL_10]], ^bb2, ^bb3
+  ! CHECK:       ^bb2:
+  ! CHECK:         %[[VAL_11:.*]] = arith.addi %[[VAL_8]], %[[VAL_5]] : index
+  ! CHECK:         %[[VAL_12:.*]] = fir.array_coor %[[VAL_1]](%[[VAL_7]]) %[[VAL_11]] : (!fir.ref<!fir.array<10x!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>>, !fir.shape<1>, index) -> !fir.ref<!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>
+  ! CHECK:         %[[VAL_13:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_7]]) %[[VAL_11]] : (!fir.ref<!fir.array<10x!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>>, !fir.shape<1>, index) -> !fir.ref<!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>
+  ! CHECK:         %[[VAL_14:.*]] = fir.embox %[[VAL_13]] : (!fir.ref<!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>) -> !fir.box<!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>
+  ! CHECK:         %[[VAL_15:.*]] = fir.embox %[[VAL_12]] : (!fir.ref<!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>) -> !fir.box<!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>
+  ! CHECK:         fir.store %[[VAL_14]] to %[[VAL_6]] : !fir.ref<!fir.box<!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>>
+  ! CHECK:         %[[VAL_16:.*]] = fir.address_of(@_QQcl.{{.*}}) : !fir.ref<!fir.char<1,
+  ! CHECK:         %[[VAL_17:.*]] = fir.convert %[[VAL_6]] : (!fir.ref<!fir.box<!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>>) -> !fir.ref<!fir.box<none>>
+  ! CHECK:         %[[VAL_18:.*]] = fir.convert %[[VAL_15]] : (!fir.box<!fir.type<_QMarray_derived_assignTdeep_copy{i:i32,a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>) -> !fir.box<none>
+  ! CHECK:         %[[VAL_19:.*]] = fir.convert %[[VAL_16]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
+  ! CHECK:         %[[VAL_20:.*]] = fir.call @_FortranAAssign(%[[VAL_17]], %[[VAL_18]], %[[VAL_19]], %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.ref<i8>, i32) -> none
+  ! CHECK:         %[[VAL_21:.*]] = arith.subi %[[VAL_9]], %[[VAL_5]] : index
+  ! CHECK:         br ^bb1(%[[VAL_11]], %[[VAL_21]] : index, index)
+  type(deep_copy) :: t1(10), t2(10)
+  t1 = t2
+  ! CHECK:         return
+  ! CHECK:       }
+end subroutine
+  
+end module

diff  --git a/flang/test/Lower/array-derived.f90 b/flang/test/Lower/array-derived.f90
new file mode 100644
index 0000000000000..00df7beef9afd
--- /dev/null
+++ b/flang/test/Lower/array-derived.f90
@@ -0,0 +1,141 @@
+! RUN: bbc %s -o - | FileCheck %s
+
+module cs
+  type r
+     integer n, d
+  end type r
+
+  type t2
+     integer :: f1(5)
+     type(r) :: f2
+  end type t2
+
+  type t3
+     type(t2) :: f(3,3)
+  end type t3
+
+contains
+
+  ! CHECK: func @_QMcsPc1(
+  ! CHECK-SAME:   %[[arg0:[^:]+]]: !fir.box<!fir.array<?x!fir.type<_QMcsTr{n:i32,d:i32}>>>{{.*}}, %[[arg1:[^:]+]]: !fir.box<!fir.array<?x!fir.type<_QMcsTr{n:i32,d:i32}>>>{{.*}})
+  function c1(e, c)
+    type(r), intent(in) :: e(:), c(:)
+    ! CHECK-DAG: fir.alloca !fir.logical<1> {bindc_name = "c1", uniq_name = "_QMcsFc1Ec1"}
+    logical*1 :: c1
+    ! CHECK-DAG: %[[fldn:.*]] = fir.field_index n, !fir.type<_QMcsTr{n:i32,d:i32}>
+    ! CHECK: %[[ext1:.*]]:3 = fir.box_dims %[[arg1]], %c0{{.*}} : (!fir.box<!fir.array<?x!fir.type<_QMcsTr{n:i32,d:i32}>>>, index) -> (index, index, index)
+    ! CHECK-DAG: %[[slice1:.*]] = fir.slice %c1{{.*}}, %[[ext1]]#1, %c1{{.*}} path %[[fldn]] : (index, index, index, !fir.field) -> !fir.slice<1>
+    ! CHECK-DAG: %[[ext0:.*]]:3 = fir.box_dims %[[arg0]], %c0{{.*}} : (!fir.box<!fir.array<?x!fir.type<_QMcsTr{n:i32,d:i32}>>>, index) -> (index, index, index)
+    ! CHECK: %[[slice0:.*]] = fir.slice %c1{{.*}}, %[[ext0]]#1, %c1{{.*}} path %[[fldn]] : (index, index, index, !fir.field) -> !fir.slice<1>
+    ! CHECK-DAG: = fir.array_coor %[[arg1]] [%[[slice1]]] %[[index:.*]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTr{n:i32,d:i32}>>>, !fir.slice<1>, index) -> !fir.ref<i32>
+    ! CHECK-DAG: = fir.array_coor %[[arg0]] [%[[slice0]]] %[[index]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTr{n:i32,d:i32}>>>, !fir.slice<1>, index) -> !fir.ref<i32>
+    ! CHECK: = fir.call @_FortranAAll(
+    c1 = all(c%n == e%n)
+  end function c1
+
+! CHECK-LABEL: func @_QMcsPtest2(
+! CHECK-SAME:    %[[VAL_0:.*]]: !fir.box<!fir.array<?x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>>{{.*}}, %[[VAL_1:.*]]: !fir.box<!fir.array<?x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>>{{.*}}) {
+! CHECK-DAG:     %[[VAL_2:.*]] = arith.constant 2 : index
+! CHECK-DAG:     %[[VAL_3:.*]] = arith.constant 4 : index
+! CHECK-DAG:     %[[VAL_4:.*]] = arith.constant 0 : index
+! CHECK-DAG:     %[[VAL_5:.*]] = arith.constant 1 : index
+! CHECK:         %[[VAL_6:.*]] = fir.field_index f2, !fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>
+! CHECK:         %[[VAL_7:.*]] = fir.field_index d, !fir.type<_QMcsTr{n:i32,d:i32}>
+! CHECK:         %[[VAL_8:.*]]:3 = fir.box_dims %[[VAL_0]], %[[VAL_4]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>>, index) -> (index, index, index)
+! CHECK:         %[[VAL_9:.*]] = fir.slice %[[VAL_5]], %[[VAL_8]]#1, %[[VAL_5]] path %[[VAL_6]], %[[VAL_7]] : (index, index, index, !fir.field, !fir.field) -> !fir.slice<1>
+! CHECK:         %[[VAL_8_2:.*]] = arith.cmpi sgt, %[[VAL_8]]#1, %[[VAL_4]] : index
+! CHECK:         %[[VAL_8_3:.*]] = arith.select %[[VAL_8_2]], %[[VAL_8]]#1, %[[VAL_4]] : index
+! CHECK:         %[[VAL_10:.*]] = fir.field_index f1, !fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>
+! CHECK:         %[[VAL_11:.*]]:3 = fir.box_dims %[[VAL_1]], %[[VAL_4]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>>, index) -> (index, index, index)
+! CHECK:         %[[VAL_12:.*]] = fir.slice %[[VAL_5]], %[[VAL_11]]#1, %[[VAL_5]] path %[[VAL_10]], %[[VAL_4]] : (index, index, index, !fir.field, index) -> !fir.slice<1>
+! CHECK:         %[[VAL_13:.*]] = fir.slice %[[VAL_5]], %[[VAL_11]]#1, %[[VAL_5]] path %[[VAL_10]], %[[VAL_3]] : (index, index, index, !fir.field, index) -> !fir.slice<1>
+! CHECK:         %[[VAL_14:.*]] = fir.slice %[[VAL_5]], %[[VAL_11]]#1, %[[VAL_5]] path %[[VAL_10]], %[[VAL_2]] : (index, index, index, !fir.field, index) -> !fir.slice<1>
+! CHECK:         br ^bb1(%[[VAL_4]], %[[VAL_8_3]] : index, index)
+! CHECK:       ^bb1(%[[VAL_15:.*]]: index, %[[VAL_16:.*]]: index):
+! CHECK:         %[[VAL_17:.*]] = arith.cmpi sgt, %[[VAL_16]], %[[VAL_4]] : index
+! CHECK:         cond_br %[[VAL_17]], ^bb2, ^bb3
+! CHECK:       ^bb2:
+! CHECK:         %[[VAL_18:.*]] = arith.addi %[[VAL_15]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_19:.*]] = fir.array_coor %[[VAL_1]] {{\[}}%[[VAL_12]]] %[[VAL_18]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>>, !fir.slice<1>, index) -> !fir.ref<i32>
+! CHECK:         %[[VAL_20:.*]] = fir.load %[[VAL_19]] : !fir.ref<i32>
+! CHECK:         %[[VAL_21:.*]] = fir.array_coor %[[VAL_1]] {{\[}}%[[VAL_13]]] %[[VAL_18]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>>, !fir.slice<1>, index) -> !fir.ref<i32>
+! CHECK:         %[[VAL_22:.*]] = fir.load %[[VAL_21]] : !fir.ref<i32>
+! CHECK:         %[[VAL_23:.*]] = fir.array_coor %[[VAL_1]] {{\[}}%[[VAL_14]]] %[[VAL_18]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>>, !fir.slice<1>, index) -> !fir.ref<i32>
+! CHECK:         %[[VAL_24:.*]] = fir.load %[[VAL_23]] : !fir.ref<i32>
+! CHECK:         %[[VAL_25:.*]] = arith.divsi %[[VAL_22]], %[[VAL_24]] : i32
+! CHECK:         %[[VAL_26:.*]] = arith.addi %[[VAL_20]], %[[VAL_25]] : i32
+! CHECK:         %[[VAL_27:.*]] = fir.array_coor %[[VAL_0]] {{\[}}%[[VAL_9]]] %[[VAL_18]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>>, !fir.slice<1>, index) -> !fir.ref<i32>
+! CHECK:         fir.store %[[VAL_26]] to %[[VAL_27]] : !fir.ref<i32>
+! CHECK:         %[[VAL_28:.*]] = arith.subi %[[VAL_16]], %[[VAL_5]] : index
+! CHECK:         br ^bb1(%[[VAL_18]], %[[VAL_28]] : index, index)
+! CHECK:       ^bb3:
+! CHECK:         return
+! CHECK:       }
+
+
+  subroutine test2(a1, a2)
+    type(t2) :: a1(:), a2(:)
+    a1%f2%d = a2%f1(1) + a2%f1(5) / a2%f1(3)
+  end subroutine test2
+
+! CHECK-LABEL: func @_QMcsPtest3(
+! CHECK-SAME:    %[[VAL_0:.*]]: !fir.box<!fir.array<?x!fir.type<_QMcsTt3{f:!fir.array<3x3x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>}>>>{{.*}}, %[[VAL_1:.*]]: !fir.box<!fir.array<?x!fir.type<_QMcsTt3{f:!fir.array<3x3x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>}>>>{{.*}}) {
+! CHECK-DAG:     %[[VAL_2:.*]] = arith.constant 2 : index
+! CHECK-DAG:     %[[VAL_3:.*]] = arith.constant 3 : index
+! CHECK-DAG:     %[[VAL_4:.*]] = arith.constant 4 : i32
+! CHECK-DAG:     %[[VAL_5:.*]] = arith.constant 1 : index
+! CHECK-DAG:     %[[VAL_6:.*]] = arith.constant 0 : index
+! CHECK:         %[[VAL_7:.*]] = fir.field_index f, !fir.type<_QMcsTt3{f:!fir.array<3x3x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>}>
+! CHECK:         %[[VAL_8:.*]] = fir.field_index f2, !fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>
+! CHECK:         %[[VAL_9:.*]] = fir.field_index n, !fir.type<_QMcsTr{n:i32,d:i32}>
+! CHECK:         %[[VAL_10:.*]]:3 = fir.box_dims %[[VAL_0]], %[[VAL_6]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTt3{f:!fir.array<3x3x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>}>>>, index) -> (index, index, index)
+! CHECK:         %[[VAL_11:.*]] = fir.slice %[[VAL_5]], %[[VAL_10]]#1, %[[VAL_5]] path %[[VAL_7]], %[[VAL_6]], %[[VAL_6]], %[[VAL_8]], %[[VAL_9]] : (index, index, index, !fir.field, index, index, !fir.field, !fir.field) -> !fir.slice<1>
+! CHECK:         %[[VAL_10_2:.*]] = arith.cmpi sgt, %[[VAL_10]]#1, %[[VAL_6]] : index
+! CHECK:         %[[VAL_10_3:.*]] = arith.select %[[VAL_10_2]], %[[VAL_10]]#1, %[[VAL_6]] : index
+! CHECK:         %[[VAL_12:.*]] = fir.field_index f1, !fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>
+! CHECK:         %[[VAL_13:.*]]:3 = fir.box_dims %[[VAL_1]], %[[VAL_6]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTt3{f:!fir.array<3x3x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>}>>>, index) -> (index, index, index)
+! CHECK:         %[[VAL_14:.*]] = fir.slice %[[VAL_5]], %[[VAL_13]]#1, %[[VAL_5]] path %[[VAL_7]], %[[VAL_5]], %[[VAL_5]], %[[VAL_12]], %[[VAL_3]] : (index, index, index, !fir.field, index, index, !fir.field, index) -> !fir.slice<1>
+! CHECK:         br ^bb1(%[[VAL_6]], %[[VAL_10_3]] : index, index)
+! CHECK:       ^bb1(%[[VAL_15:.*]]: index, %[[VAL_16:.*]]: index):
+! CHECK:         %[[VAL_17:.*]] = arith.cmpi sgt, %[[VAL_16]], %[[VAL_6]] : index
+! CHECK:         cond_br %[[VAL_17]], ^bb2, ^bb3
+! CHECK:       ^bb2:
+! CHECK:         %[[VAL_18:.*]] = arith.addi %[[VAL_15]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_19:.*]] = fir.array_coor %[[VAL_1]] {{\[}}%[[VAL_14]]] %[[VAL_18]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTt3{f:!fir.array<3x3x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>}>>>, !fir.slice<1>, index) -> !fir.ref<i32>
+! CHECK:         %[[VAL_20:.*]] = fir.load %[[VAL_19]] : !fir.ref<i32>
+! CHECK:         %[[VAL_21:.*]] = arith.subi %[[VAL_20]], %[[VAL_4]] : i32
+! CHECK:         %[[VAL_22:.*]] = fir.array_coor %[[VAL_0]] {{\[}}%[[VAL_11]]] %[[VAL_18]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTt3{f:!fir.array<3x3x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>}>>>, !fir.slice<1>, index) -> !fir.ref<i32>
+! CHECK:         fir.store %[[VAL_21]] to %[[VAL_22]] : !fir.ref<i32>
+! CHECK:         %[[VAL_23:.*]] = arith.subi %[[VAL_16]], %[[VAL_5]] : index
+! CHECK:         br ^bb1(%[[VAL_18]], %[[VAL_23]] : index, index)
+! CHECK:       ^bb3:
+! CHECK:         %[[VAL_24:.*]] = fir.slice %[[VAL_5]], %[[VAL_13]]#1, %[[VAL_5]] path %[[VAL_7]], %[[VAL_2]], %[[VAL_2]], %[[VAL_12]], %[[VAL_5]] : (index, index, index, !fir.field, index, index, !fir.field, index) -> !fir.slice<1>
+! CHECK:         %[[VAL_13_2:.*]] = arith.cmpi sgt, %[[VAL_13]]#1, %[[VAL_6]] : index
+! CHECK:         %[[VAL_13_3:.*]] = arith.select %[[VAL_13_2]], %[[VAL_13]]#1, %[[VAL_6]] : index
+! CHECK:         %[[VAL_25:.*]] = fir.field_index d, !fir.type<_QMcsTr{n:i32,d:i32}>
+! CHECK:         %[[VAL_26:.*]] = fir.slice %[[VAL_5]], %[[VAL_10]]#1, %[[VAL_5]] path %[[VAL_7]], %[[VAL_6]], %[[VAL_5]], %[[VAL_8]], %[[VAL_25]] : (index, index, index, !fir.field, index, index, !fir.field, !fir.field) -> !fir.slice<1>
+! CHECK:         br ^bb4(%[[VAL_6]], %[[VAL_13_3]] : index, index)
+! CHECK:       ^bb4(%[[VAL_27:.*]]: index, %[[VAL_28:.*]]: index):
+! CHECK:         %[[VAL_29:.*]] = arith.cmpi sgt, %[[VAL_28]], %[[VAL_6]] : index
+! CHECK:         cond_br %[[VAL_29]], ^bb5, ^bb6
+! CHECK:       ^bb5:
+! CHECK:         %[[VAL_30:.*]] = arith.addi %[[VAL_27]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_31:.*]] = fir.array_coor %[[VAL_0]] {{\[}}%[[VAL_26]]] %[[VAL_30]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTt3{f:!fir.array<3x3x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>}>>>, !fir.slice<1>, index) -> !fir.ref<i32>
+! CHECK:         %[[VAL_32:.*]] = fir.load %[[VAL_31]] : !fir.ref<i32>
+! CHECK:         %[[VAL_33:.*]] = arith.addi %[[VAL_32]], %[[VAL_4]] : i32
+! CHECK:         %[[VAL_34:.*]] = fir.array_coor %[[VAL_1]] {{\[}}%[[VAL_24]]] %[[VAL_30]] : (!fir.box<!fir.array<?x!fir.type<_QMcsTt3{f:!fir.array<3x3x!fir.type<_QMcsTt2{f1:!fir.array<5xi32>,f2:!fir.type<_QMcsTr{n:i32,d:i32}>}>>}>>>, !fir.slice<1>, index) -> !fir.ref<i32>
+! CHECK:         fir.store %[[VAL_33]] to %[[VAL_34]] : !fir.ref<i32>
+! CHECK:         %[[VAL_35:.*]] = arith.subi %[[VAL_28]], %[[VAL_5]] : index
+! CHECK:         br ^bb4(%[[VAL_30]], %[[VAL_35]] : index, index)
+! CHECK:       ^bb6:
+! CHECK:         return
+! CHECK:       }
+
+  subroutine test3(a3, a4)
+    type(t3) :: a3(:), a4(:)
+    a3%f(1,1)%f2%n = a4%f(2,2)%f1(4) - 4
+    a4%f(3,3)%f1(2) = a3%f(1,2)%f2%d + 4
+  end subroutine test3
+end module cs
+
+! CHECK: func private @_FortranAAll(!fir.box<none>, !fir.ref<i8>, i32, i32) -> i1 attributes {fir.runtime}

diff  --git a/flang/test/Lower/array-elemental-calls-char-byval.f90 b/flang/test/Lower/array-elemental-calls-char-byval.f90
new file mode 100644
index 0000000000000..4f5c020790449
--- /dev/null
+++ b/flang/test/Lower/array-elemental-calls-char-byval.f90
@@ -0,0 +1,198 @@
+! Test lowering of elemental calls with character argument
+! with the VALUE attribute.
+! RUN: bbc -o - %s | FileCheck %s
+
+
+module char_elem_byval
+
+interface
+elemental integer function elem(c, j)
+  character(*), value :: c
+  integer, intent(in) :: j
+end function
+end interface
+
+contains
+! CHECK-LABEL: func @_QMchar_elem_byvalPfoo1(
+! CHECK-SAME: %[[VAL_22:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_19:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_5:.*]]: !fir.boxchar<1>{{.*}}) {
+subroutine foo1(i, j, c)
+  integer :: i(10), j(10)
+  character(*) :: c(10)
+! CHECK-DAG:   %[[VAL_0:.*]] = arith.constant false
+! CHECK-DAG:   %[[VAL_1:.*]] = arith.constant 10 : index
+! CHECK-DAG:   %[[VAL_2:.*]] = arith.constant 0 : index
+! CHECK-DAG:   %[[VAL_3:.*]] = arith.constant 1 : index
+! CHECK:   %[[VAL_4:.*]]:2 = fir.unboxchar %[[VAL_5]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+! CHECK:   %[[VAL_6:.*]] = fir.convert %[[VAL_4]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<10x!fir.char<1,?>>>
+! CHECK:   %[[VAL_7:.*]] = fir.shape %[[VAL_1]] : (index) -> !fir.shape<1>
+! CHECK:   br ^bb1(%[[VAL_2]], %[[VAL_1]] : index, index)
+! CHECK: ^bb1(%[[VAL_8:.*]]: index, %[[VAL_9:.*]]: index):
+! CHECK:   %[[VAL_10:.*]] = arith.cmpi sgt, %[[VAL_9]], %[[VAL_2]] : index
+! CHECK:   cond_br %[[VAL_10]], ^bb2, ^bb3
+! CHECK: ^bb2:
+! CHECK:   %[[VAL_11:.*]] = arith.addi %[[VAL_8]], %[[VAL_3]] : index
+! CHECK:   %[[VAL_12:.*]] = fir.array_coor %[[VAL_6]](%[[VAL_7]]) %[[VAL_11]] typeparams %[[VAL_4]]#1 : (!fir.ref<!fir.array<10x!fir.char<1,?>>>, !fir.shape<1>, index, index) -> !fir.ref<!fir.char<1,?>>
+! CHECK:   %[[VAL_13:.*]] = fir.alloca !fir.char<1,?>(%[[VAL_4]]#1 : index) {bindc_name = ".chrtmp"}
+! CHECK:   %[[VAL_14:.*]] = fir.convert %[[VAL_4]]#1 : (index) -> i64
+! CHECK:   %[[VAL_15:.*]] = fir.convert %[[VAL_13]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
+! CHECK:   %[[VAL_16:.*]] = fir.convert %[[VAL_12]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
+! CHECK:   fir.call @llvm.memmove.p0.p0.i64(%[[VAL_15]], %[[VAL_16]], %[[VAL_14]], %[[VAL_0]]) : (!fir.ref<i8>, !fir.ref<i8>, i64, i1) -> ()
+! CHECK:   %[[VAL_17:.*]] = fir.emboxchar %[[VAL_13]], %[[VAL_4]]#1 : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:   %[[VAL_18:.*]] = fir.array_coor %[[VAL_19]](%[[VAL_7]]) %[[VAL_11]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   %[[VAL_20:.*]] = fir.call @_QPelem(%[[VAL_17]], %[[VAL_18]]) : (!fir.boxchar<1>, !fir.ref<i32>) -> i32
+! CHECK:   %[[VAL_21:.*]] = fir.array_coor %[[VAL_22]](%[[VAL_7]]) %[[VAL_11]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   fir.store %[[VAL_20]] to %[[VAL_21]] : !fir.ref<i32>
+! CHECK:   %[[VAL_23:.*]] = arith.subi %[[VAL_9]], %[[VAL_3]] : index
+! CHECK:   br ^bb1(%[[VAL_11]], %[[VAL_23]] : index, index)
+! CHECK: ^bb3:
+! CHECK:   return
+  i = elem(c, j)
+end subroutine
+
+! CHECK-LABEL: func @_QMchar_elem_byvalPfoo2(
+! CHECK-SAME: %[[VAL_44:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_41:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_29:.*]]: !fir.boxchar<1>{{.*}}) {
+subroutine foo2(i, j, c)
+  integer :: i(10), j(10)
+  character(*) :: c
+! CHECK-DAG:   %[[VAL_24:.*]] = arith.constant false
+! CHECK-DAG:   %[[VAL_25:.*]] = arith.constant 10 : index
+! CHECK-DAG:   %[[VAL_26:.*]] = arith.constant 0 : index
+! CHECK-DAG:   %[[VAL_27:.*]] = arith.constant 1 : index
+! CHECK:   %[[VAL_28:.*]]:2 = fir.unboxchar %[[VAL_29]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+! CHECK:   %[[VAL_30:.*]] = fir.shape %[[VAL_25]] : (index) -> !fir.shape<1>
+! CHECK:   %[[VAL_31:.*]] = fir.alloca !fir.char<1,?>(%[[VAL_28]]#1 : index) {bindc_name = ".chrtmp"}
+! CHECK:   %[[VAL_32:.*]] = fir.convert %[[VAL_28]]#1 : (index) -> i64
+! CHECK:   %[[VAL_33:.*]] = fir.convert %[[VAL_31]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
+! CHECK:   %[[VAL_34:.*]] = fir.convert %[[VAL_28]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
+! CHECK:   fir.call @llvm.memmove.p0.p0.i64(%[[VAL_33]], %[[VAL_34]], %[[VAL_32]], %[[VAL_24]]) : (!fir.ref<i8>, !fir.ref<i8>, i64, i1) -> ()
+! CHECK:   br ^bb1(%[[VAL_26]], %[[VAL_25]] : index, index)
+! CHECK: ^bb1(%[[VAL_35:.*]]: index, %[[VAL_36:.*]]: index):
+! CHECK:   %[[VAL_37:.*]] = arith.cmpi sgt, %[[VAL_36]], %[[VAL_26]] : index
+! CHECK:   cond_br %[[VAL_37]], ^bb2, ^bb3
+! CHECK: ^bb2:
+! CHECK:   %[[VAL_38:.*]] = fir.emboxchar %[[VAL_31]], %[[VAL_28]]#1 : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:   %[[VAL_39:.*]] = arith.addi %[[VAL_35]], %[[VAL_27]] : index
+! CHECK:   %[[VAL_40:.*]] = fir.array_coor %[[VAL_41]](%[[VAL_30]]) %[[VAL_39]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   %[[VAL_42:.*]] = fir.call @_QPelem(%[[VAL_38]], %[[VAL_40]]) : (!fir.boxchar<1>, !fir.ref<i32>) -> i32
+! CHECK:   %[[VAL_43:.*]] = fir.array_coor %[[VAL_44]](%[[VAL_30]]) %[[VAL_39]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   fir.store %[[VAL_42]] to %[[VAL_43]] : !fir.ref<i32>
+! CHECK:   %[[VAL_45:.*]] = arith.subi %[[VAL_36]], %[[VAL_27]] : index
+! CHECK:   br ^bb1(%[[VAL_39]], %[[VAL_45]] : index, index)
+! CHECK: ^bb3:
+! CHECK:   return
+  i = elem(c, j)
+end subroutine
+
+! CHECK-LABEL: func @_QMchar_elem_byvalPfoo3(
+! CHECK-SAME: %[[VAL_65:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_55:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}) {
+subroutine foo3(i, j)
+  integer :: i(10), j(10)
+! CHECK-DAG:   %[[VAL_46:.*]] = arith.constant 10 : index
+! CHECK-DAG:   %[[VAL_47:.*]] = arith.constant 0 : index
+! CHECK-DAG:   %[[VAL_48:.*]] = arith.constant 1 : index
+! CHECK:   %[[VAL_49:.*]] = fir.shape %[[VAL_46]] : (index) -> !fir.shape<1>
+! CHECK:   br ^bb1(%[[VAL_47]], %[[VAL_46]] : index, index)
+! CHECK: ^bb1(%[[VAL_50:.*]]: index, %[[VAL_51:.*]]: index):
+! CHECK:   %[[VAL_52:.*]] = arith.cmpi sgt, %[[VAL_51]], %[[VAL_47]] : index
+! CHECK:   cond_br %[[VAL_52]], ^bb2, ^bb3
+! CHECK: ^bb2:
+! CHECK:   %[[VAL_53:.*]] = arith.addi %[[VAL_50]], %[[VAL_48]] : index
+! CHECK:   %[[VAL_54:.*]] = fir.array_coor %[[VAL_55]](%[[VAL_49]]) %[[VAL_53]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   %[[VAL_56:.*]] = fir.load %[[VAL_54]] : !fir.ref<i32>
+! CHECK:   %[[VAL_57:.*]] = fir.convert %[[VAL_56]] : (i32) -> i8
+! CHECK:   %[[VAL_58:.*]] = fir.undefined !fir.char<1>
+! CHECK:   %[[VAL_59:.*]] = fir.insert_value %[[VAL_58]], %[[VAL_57]], [0 : index] : (!fir.char<1>, i8) -> !fir.char<1>
+! CHECK:   %[[VAL_60:.*]] = fir.alloca !fir.char<1> {bindc_name = ".chrtmp"}
+! CHECK:   fir.store %[[VAL_59]] to %[[VAL_60]] : !fir.ref<!fir.char<1>>
+! CHECK:   %[[VAL_61:.*]] = fir.convert %[[VAL_60]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
+! CHECK:   %[[VAL_62:.*]] = fir.emboxchar %[[VAL_61]], %[[VAL_48]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:   %[[VAL_63:.*]] = fir.call @_QPelem(%[[VAL_62]], %[[VAL_54]]) : (!fir.boxchar<1>, !fir.ref<i32>) -> i32
+! CHECK:   %[[VAL_64:.*]] = fir.array_coor %[[VAL_65]](%[[VAL_49]]) %[[VAL_53]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   fir.store %[[VAL_63]] to %[[VAL_64]] : !fir.ref<i32>
+! CHECK:   %[[VAL_66:.*]] = arith.subi %[[VAL_51]], %[[VAL_48]] : index
+! CHECK:   br ^bb1(%[[VAL_53]], %[[VAL_66]] : index, index)
+! CHECK: ^bb3:
+! CHECK:   return
+  i = elem(char(j), j)
+end subroutine
+
+! CHECK-LABEL: func @_QMchar_elem_byvalPfoo4(
+! CHECK-SAME: %[[VAL_93:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_74:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}) {
+subroutine foo4(i, j)
+  integer :: i(10), j(10)
+! CHECK-DAG:   %[[VAL_67:.*]] = arith.constant 0 : i64
+! CHECK-DAG:   %[[VAL_68:.*]] = arith.constant false
+! CHECK-DAG:   %[[VAL_69:.*]] = arith.constant 10 : index
+! CHECK-DAG:   %[[VAL_70:.*]] = arith.constant 0 : index
+! CHECK-DAG:   %[[VAL_71:.*]] = arith.constant 1 : index
+! CHECK:   %[[VAL_66:.*]] = fir.alloca !fir.char<1> {adapt.valuebyref}
+! CHECK:   %[[VAL_72:.*]] = fir.shape %[[VAL_69]] : (index) -> !fir.shape<1>
+! CHECK:   %[[VAL_73:.*]] = fir.coordinate_of %[[VAL_74]], %[[VAL_67]] : (!fir.ref<!fir.array<10xi32>>, i64) -> !fir.ref<i32>
+! CHECK:   %[[VAL_75:.*]] = fir.load %[[VAL_73]] : !fir.ref<i32>
+! CHECK:   %[[VAL_76:.*]] = fir.convert %[[VAL_75]] : (i32) -> i8
+! CHECK:   %[[VAL_77:.*]] = fir.undefined !fir.char<1>
+! CHECK:   %[[VAL_78:.*]] = fir.insert_value %[[VAL_77]], %[[VAL_76]], [0 : index] : (!fir.char<1>, i8) -> !fir.char<1>
+! CHECK:   fir.store %[[VAL_78]] to %[[VAL_66]] : !fir.ref<!fir.char<1>>
+! CHECK:   %[[VAL_80:.*]] = fir.alloca !fir.char<1> {bindc_name = ".chrtmp"}
+! CHECK:   %[[VAL_81:.*]] = fir.convert %[[VAL_71]] : (index) -> i64
+! CHECK:   %[[VAL_82:.*]] = fir.convert %[[VAL_80]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<i8>
+! CHECK:   %[[VAL_83:.*]] = fir.convert %[[VAL_66]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<i8>
+! CHECK:   fir.call @llvm.memmove.p0.p0.i64(%[[VAL_82]], %[[VAL_83]], %[[VAL_81]], %[[VAL_68]]) : (!fir.ref<i8>, !fir.ref<i8>, i64, i1) -> ()
+! CHECK:   br ^bb1(%[[VAL_70]], %[[VAL_69]] : index, index)
+! CHECK: ^bb1(%[[VAL_84:.*]]: index, %[[VAL_85:.*]]: index):
+! CHECK:   %[[VAL_86:.*]] = arith.cmpi sgt, %[[VAL_85]], %[[VAL_70]] : index
+! CHECK:   cond_br %[[VAL_86]], ^bb2, ^bb3
+! CHECK: ^bb2:
+! CHECK:   %[[VAL_87:.*]] = fir.convert %[[VAL_80]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
+! CHECK:   %[[VAL_88:.*]] = fir.emboxchar %[[VAL_87]], %[[VAL_71]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:   %[[VAL_89:.*]] = arith.addi %[[VAL_84]], %[[VAL_71]] : index
+! CHECK:   %[[VAL_90:.*]] = fir.array_coor %[[VAL_74]](%[[VAL_72]]) %[[VAL_89]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   %[[VAL_91:.*]] = fir.call @_QPelem(%[[VAL_88]], %[[VAL_90]]) : (!fir.boxchar<1>, !fir.ref<i32>) -> i32
+! CHECK:   %[[VAL_92:.*]] = fir.array_coor %[[VAL_93]](%[[VAL_72]]) %[[VAL_89]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   fir.store %[[VAL_91]] to %[[VAL_92]] : !fir.ref<i32>
+! CHECK:   %[[VAL_94:.*]] = arith.subi %[[VAL_85]], %[[VAL_71]] : index
+! CHECK:   br ^bb1(%[[VAL_89]], %[[VAL_94]] : index, index)
+! CHECK: ^bb3:
+! CHECK:   return
+  i = elem(char(j(1)), j)
+end subroutine
+
+! Note: the copy of the constant is important because VALUE argument can be
+! modified on the caller side.
+
+! CHECK-LABEL: func @_QMchar_elem_byvalPfoo5(
+! CHECK-SAME: %[[VAL_116:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_113:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}) {
+subroutine foo5(i, j)
+  integer :: i(10), j(10)
+! CHECK-DAG:   %[[VAL_95:.*]] = arith.constant 5 : index
+! CHECK-DAG:   %[[VAL_96:.*]] = arith.constant false
+! CHECK-DAG:   %[[VAL_97:.*]] = arith.constant 10 : index
+! CHECK-DAG:   %[[VAL_98:.*]] = arith.constant 0 : index
+! CHECK-DAG:   %[[VAL_99:.*]] = arith.constant 1 : index
+! CHECK:   %[[VAL_100:.*]] = fir.shape %[[VAL_97]] : (index) -> !fir.shape<1>
+! CHECK:   %[[VAL_101:.*]] = fir.address_of(@{{.*}}) : !fir.ref<!fir.char<1,5>>
+! CHECK:   %[[VAL_102:.*]] = fir.alloca !fir.char<1,5> {bindc_name = ".chrtmp"}
+! CHECK:   %[[VAL_103:.*]] = fir.convert %[[VAL_95]] : (index) -> i64
+! CHECK:   %[[VAL_104:.*]] = fir.convert %[[VAL_102]] : (!fir.ref<!fir.char<1,5>>) -> !fir.ref<i8>
+! CHECK:   %[[VAL_105:.*]] = fir.convert %[[VAL_101]] : (!fir.ref<!fir.char<1,5>>) -> !fir.ref<i8>
+! CHECK:   fir.call @llvm.memmove.p0.p0.i64(%[[VAL_104]], %[[VAL_105]], %[[VAL_103]], %[[VAL_96]]) : (!fir.ref<i8>, !fir.ref<i8>, i64, i1) -> ()
+! CHECK:   br ^bb1(%[[VAL_98]], %[[VAL_97]] : index, index)
+! CHECK: ^bb1(%[[VAL_106:.*]]: index, %[[VAL_107:.*]]: index):
+! CHECK:   %[[VAL_108:.*]] = arith.cmpi sgt, %[[VAL_107]], %[[VAL_98]] : index
+! CHECK:   cond_br %[[VAL_108]], ^bb2, ^bb3
+! CHECK: ^bb2:
+! CHECK:   %[[VAL_109:.*]] = fir.convert %[[VAL_102]] : (!fir.ref<!fir.char<1,5>>) -> !fir.ref<!fir.char<1,?>>
+! CHECK:   %[[VAL_110:.*]] = fir.emboxchar %[[VAL_109]], %[[VAL_95]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:   %[[VAL_111:.*]] = arith.addi %[[VAL_106]], %[[VAL_99]] : index
+! CHECK:   %[[VAL_112:.*]] = fir.array_coor %[[VAL_113]](%[[VAL_100]]) %[[VAL_111]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   %[[VAL_114:.*]] = fir.call @_QPelem(%[[VAL_110]], %[[VAL_112]]) : (!fir.boxchar<1>, !fir.ref<i32>) -> i32
+! CHECK:   %[[VAL_115:.*]] = fir.array_coor %[[VAL_116]](%[[VAL_100]]) %[[VAL_111]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   fir.store %[[VAL_114]] to %[[VAL_115]] : !fir.ref<i32>
+! CHECK:   %[[VAL_117:.*]] = arith.subi %[[VAL_107]], %[[VAL_99]] : index
+! CHECK:   br ^bb1(%[[VAL_111]], %[[VAL_117]] : index, index)
+! CHECK: ^bb3:
+! CHECK:   return
+  i = elem("hello", j)
+end subroutine
+
+end module

diff  --git a/flang/test/Lower/array-elemental-calls-char.f90 b/flang/test/Lower/array-elemental-calls-char.f90
new file mode 100644
index 0000000000000..e695f9bb8b89b
--- /dev/null
+++ b/flang/test/Lower/array-elemental-calls-char.f90
@@ -0,0 +1,267 @@
+! Test lowering of elemental calls with character argument
+! without the VALUE attribute.
+! RUN: bbc -o - %s | FileCheck %s
+
+module char_elem
+
+interface
+elemental integer function elem(c)
+  character(*), intent(in) :: c
+end function
+
+elemental integer function elem2(c, j)
+  character(*), intent(in) :: c
+  integer, intent(in) :: j
+end function
+
+end interface
+
+contains
+
+! CHECK-LABEL: func @_QMchar_elemPfoo1(
+! CHECK-SAME: %[[VAL_15:.*]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_4:.*]]: !fir.boxchar<1>{{.*}}) {
+subroutine foo1(i, c)
+  integer :: i(10)
+  character(*) :: c(10)
+! CHECK-DAG:   %[[VAL_0:.*]] = arith.constant 10 : index
+! CHECK-DAG:   %[[VAL_1:.*]] = arith.constant 0 : index
+! CHECK-DAG:   %[[VAL_2:.*]] = arith.constant 1 : index
+! CHECK:   %[[VAL_3:.*]]:2 = fir.unboxchar %[[VAL_4]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+! CHECK:   %[[VAL_5:.*]] = fir.convert %[[VAL_3]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<10x!fir.char<1,?>>>
+! CHECK:   %[[VAL_6:.*]] = fir.shape %[[VAL_0]] : (index) -> !fir.shape<1>
+! CHECK:   br ^bb1(%[[VAL_1]], %[[VAL_0]] : index, index)
+! CHECK: ^bb1(%[[VAL_7:.*]]: index, %[[VAL_8:.*]]: index):
+! CHECK:   %[[VAL_9:.*]] = arith.cmpi sgt, %[[VAL_8]], %[[VAL_1]] : index
+! CHECK:   cond_br %[[VAL_9]], ^bb2, ^bb3
+! CHECK: ^bb2:
+! CHECK:   %[[VAL_10:.*]] = arith.addi %[[VAL_7]], %[[VAL_2]] : index
+! CHECK:   %[[VAL_11:.*]] = fir.array_coor %[[VAL_5]](%[[VAL_6]]) %[[VAL_10]] typeparams %[[VAL_3]]#1 : (!fir.ref<!fir.array<10x!fir.char<1,?>>>, !fir.shape<1>, index, index) -> !fir.ref<!fir.char<1,?>>
+! CHECK:   %[[VAL_12:.*]] = fir.emboxchar %[[VAL_11]], %[[VAL_3]]#1 : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:   %[[VAL_13:.*]] = fir.call @_QPelem(%[[VAL_12]]) : (!fir.boxchar<1>) -> i32
+! CHECK:   %[[VAL_14:.*]] = fir.array_coor %[[VAL_15]](%[[VAL_6]]) %[[VAL_10]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   fir.store %[[VAL_13]] to %[[VAL_14]] : !fir.ref<i32>
+! CHECK:   %[[VAL_16:.*]] = arith.subi %[[VAL_8]], %[[VAL_2]] : index
+! CHECK:   br ^bb1(%[[VAL_10]], %[[VAL_16]] : index, index)
+! CHECK: ^bb3:
+! CHECK:   return
+  i = elem(c)
+end subroutine
+
+! CHECK-LABEL: func @_QMchar_elemPfoo1b(
+! CHECK-SAME: %[[VAL_33:.*]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_21:.*]]: !fir.boxchar<1>{{.*}}) {
+subroutine foo1b(i, c)
+  integer :: i(10)
+  character(10) :: c(10)
+! CHECK-DAG:   %[[VAL_17:.*]] = arith.constant 10 : index
+! CHECK-DAG:   %[[VAL_18:.*]] = arith.constant 0 : index
+! CHECK-DAG:   %[[VAL_19:.*]] = arith.constant 1 : index
+! CHECK:   %[[VAL_20:.*]]:2 = fir.unboxchar %[[VAL_21]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+! CHECK:   %[[VAL_22:.*]] = fir.convert %[[VAL_20]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<10x!fir.char<1,10>>>
+! CHECK:   %[[VAL_23:.*]] = fir.shape %[[VAL_17]] : (index) -> !fir.shape<1>
+! CHECK:   br ^bb1(%[[VAL_18]], %[[VAL_17]] : index, index)
+! CHECK: ^bb1(%[[VAL_24:.*]]: index, %[[VAL_25:.*]]: index):
+! CHECK:   %[[VAL_26:.*]] = arith.cmpi sgt, %[[VAL_25]], %[[VAL_18]] : index
+! CHECK:   cond_br %[[VAL_26]], ^bb2, ^bb3
+! CHECK: ^bb2:
+! CHECK:   %[[VAL_27:.*]] = arith.addi %[[VAL_24]], %[[VAL_19]] : index
+! CHECK:   %[[VAL_28:.*]] = fir.array_coor %[[VAL_22]](%[[VAL_23]]) %[[VAL_27]] : (!fir.ref<!fir.array<10x!fir.char<1,10>>>, !fir.shape<1>, index) -> !fir.ref<!fir.char<1,10>>
+! CHECK:   %[[VAL_29:.*]] = fir.convert %[[VAL_28]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<!fir.char<1,?>>
+! CHECK:   %[[VAL_30:.*]] = fir.emboxchar %[[VAL_29]], %[[VAL_17]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:   %[[VAL_31:.*]] = fir.call @_QPelem(%[[VAL_30]]) : (!fir.boxchar<1>) -> i32
+! CHECK:   %[[VAL_32:.*]] = fir.array_coor %[[VAL_33]](%[[VAL_23]]) %[[VAL_27]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   fir.store %[[VAL_31]] to %[[VAL_32]] : !fir.ref<i32>
+! CHECK:   %[[VAL_34:.*]] = arith.subi %[[VAL_25]], %[[VAL_19]] : index
+! CHECK:   br ^bb1(%[[VAL_27]], %[[VAL_34]] : index, index)
+! CHECK: ^bb3:
+! CHECK:   return
+  i = elem(c)
+end subroutine
+
+! CHECK-LABEL: func @_QMchar_elemPfoo2(
+! CHECK-SAME: %[[VAL_50:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_47:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_39:.*]]: !fir.boxchar<1>{{.*}}) {
+subroutine foo2(i, j, c)
+! CHECK-DAG:   %[[VAL_35:.*]] = arith.constant 10 : index
+! CHECK-DAG:   %[[VAL_36:.*]] = arith.constant 0 : index
+! CHECK-DAG:   %[[VAL_37:.*]] = arith.constant 1 : index
+! CHECK:   %[[VAL_38:.*]]:2 = fir.unboxchar %[[VAL_39]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+! CHECK:   %[[VAL_40:.*]] = fir.shape %[[VAL_35]] : (index) -> !fir.shape<1>
+! CHECK:   br ^bb1(%[[VAL_36]], %[[VAL_35]] : index, index)
+! CHECK: ^bb1(%[[VAL_41:.*]]: index, %[[VAL_42:.*]]: index):
+! CHECK:   %[[VAL_43:.*]] = arith.cmpi sgt, %[[VAL_42]], %[[VAL_36]] : index
+! CHECK:   cond_br %[[VAL_43]], ^bb2, ^bb3
+! CHECK: ^bb2:
+! CHECK:   %[[VAL_44:.*]] = fir.emboxchar %[[VAL_38]]#0, %[[VAL_38]]#1 : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:   %[[VAL_45:.*]] = arith.addi %[[VAL_41]], %[[VAL_37]] : index
+! CHECK:   %[[VAL_46:.*]] = fir.array_coor %[[VAL_47]](%[[VAL_40]]) %[[VAL_45]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   %[[VAL_48:.*]] = fir.call @_QPelem2(%[[VAL_44]], %[[VAL_46]]) : (!fir.boxchar<1>, !fir.ref<i32>) -> i32
+! CHECK:   %[[VAL_49:.*]] = fir.array_coor %[[VAL_50]](%[[VAL_40]]) %[[VAL_45]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   fir.store %[[VAL_48]] to %[[VAL_49]] : !fir.ref<i32>
+! CHECK:   %[[VAL_51:.*]] = arith.subi %[[VAL_42]], %[[VAL_37]] : index
+! CHECK:   br ^bb1(%[[VAL_45]], %[[VAL_51]] : index, index)
+! CHECK: ^bb3:
+! CHECK:   return
+  integer :: i(10), j(10)
+  character(*) :: c
+  i = elem2(c, j)
+end subroutine
+
+! CHECK-LABEL: func @_QMchar_elemPfoo2b(
+! CHECK-SAME: %[[VAL_67:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_64:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_56:.*]]: !fir.boxchar<1>{{.*}}) {
+subroutine foo2b(i, j, c)
+  integer :: i(10), j(10)
+  character(10) :: c
+! CHECK-DAG:   %[[VAL_52:.*]] = arith.constant 10 : index
+! CHECK-DAG:   %[[VAL_53:.*]] = arith.constant 0 : index
+! CHECK-DAG:   %[[VAL_54:.*]] = arith.constant 1 : index
+! CHECK:   %[[VAL_55:.*]]:2 = fir.unboxchar %[[VAL_56]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+! CHECK:   %[[VAL_57:.*]] = fir.shape %[[VAL_52]] : (index) -> !fir.shape<1>
+! CHECK:   br ^bb1(%[[VAL_53]], %[[VAL_52]] : index, index)
+! CHECK: ^bb1(%[[VAL_58:.*]]: index, %[[VAL_59:.*]]: index):
+! CHECK:   %[[VAL_60:.*]] = arith.cmpi sgt, %[[VAL_59]], %[[VAL_53]] : index
+! CHECK:   cond_br %[[VAL_60]], ^bb2, ^bb3
+! CHECK: ^bb2:
+! CHECK:   %[[VAL_61:.*]] = fir.emboxchar %[[VAL_55]]#0, %[[VAL_52]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:   %[[VAL_62:.*]] = arith.addi %[[VAL_58]], %[[VAL_54]] : index
+! CHECK:   %[[VAL_63:.*]] = fir.array_coor %[[VAL_64]](%[[VAL_57]]) %[[VAL_62]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   %[[VAL_65:.*]] = fir.call @_QPelem2(%[[VAL_61]], %[[VAL_63]]) : (!fir.boxchar<1>, !fir.ref<i32>) -> i32
+! CHECK:   %[[VAL_66:.*]] = fir.array_coor %[[VAL_67]](%[[VAL_57]]) %[[VAL_62]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   fir.store %[[VAL_65]] to %[[VAL_66]] : !fir.ref<i32>
+! CHECK:   %[[VAL_68:.*]] = arith.subi %[[VAL_59]], %[[VAL_54]] : index
+! CHECK:   br ^bb1(%[[VAL_62]], %[[VAL_68]] : index, index)
+! CHECK: ^bb3:
+! CHECK:   return
+  i = elem2(c, j)
+end subroutine
+
+! CHECK-LABEL: func @_QMchar_elemPfoo3(
+! CHECK-SAME: %[[VAL_88:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_79:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}})
+subroutine foo3(i, j)
+  integer :: i(10), j(10)
+! CHECK-DAG:   %[[VAL_69:.*]] = arith.constant 10 : index
+! CHECK-DAG:   %[[VAL_70:.*]] = arith.constant 0 : index
+! CHECK-DAG:   %[[VAL_71:.*]] = arith.constant 1 : index
+! CHECK-DAG:   %[[VAL_72:.*]] = fir.alloca !fir.char<1>
+! CHECK:   %[[VAL_73:.*]] = fir.shape %[[VAL_69]] : (index) -> !fir.shape<1>
+! CHECK:   br ^bb1(%[[VAL_70]], %[[VAL_69]] : index, index)
+! CHECK: ^bb1(%[[VAL_74:.*]]: index, %[[VAL_75:.*]]: index):
+! CHECK:   %[[VAL_76:.*]] = arith.cmpi sgt, %[[VAL_75]], %[[VAL_70]] : index
+! CHECK:   cond_br %[[VAL_76]], ^bb2, ^bb3
+! CHECK: ^bb2:
+! CHECK:   %[[VAL_77:.*]] = arith.addi %[[VAL_74]], %[[VAL_71]] : index
+! CHECK:   %[[VAL_78:.*]] = fir.array_coor %[[VAL_79]](%[[VAL_73]]) %[[VAL_77]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   %[[VAL_80:.*]] = fir.load %[[VAL_78]] : !fir.ref<i32>
+! CHECK:   %[[VAL_81:.*]] = fir.convert %[[VAL_80]] : (i32) -> i8
+! CHECK:   %[[VAL_82:.*]] = fir.undefined !fir.char<1>
+! CHECK:   %[[VAL_83:.*]] = fir.insert_value %[[VAL_82]], %[[VAL_81]], [0 : index] : (!fir.char<1>, i8) -> !fir.char<1>
+! CHECK:   fir.store %[[VAL_83]] to %[[VAL_72]] : !fir.ref<!fir.char<1>>
+! CHECK:   %[[VAL_84:.*]] = fir.convert %[[VAL_72]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
+! CHECK:   %[[VAL_85:.*]] = fir.emboxchar %[[VAL_84]], %[[VAL_71]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:   %[[VAL_86:.*]] = fir.call @_QPelem(%[[VAL_85]]) : (!fir.boxchar<1>) -> i32
+! CHECK:   %[[VAL_87:.*]] = fir.array_coor %[[VAL_88]](%[[VAL_73]]) %[[VAL_77]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   fir.store %[[VAL_86]] to %[[VAL_87]] : !fir.ref<i32>
+! CHECK:   %[[VAL_89:.*]] = arith.subi %[[VAL_75]], %[[VAL_71]] : index
+! CHECK:   br ^bb1(%[[VAL_77]], %[[VAL_89]] : index, index)
+! CHECK: ^bb3:
+! CHECK:   return
+  i = elem(char(j))
+end subroutine
+
+! CHECK-LABEL: func @_QMchar_elemPfoo4(
+! CHECK-SAME: %[[VAL_106:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}}, %[[VAL_103:[^:]+]]: !fir.ref<!fir.array<10xi32>>{{.*}})
+subroutine foo4(i, j)
+  integer :: i(10), j(10)
+! CHECK-DAG:   %[[VAL_90:.*]] = arith.constant 5 : index
+! CHECK-DAG:   %[[VAL_91:.*]] = arith.constant 10 : index
+! CHECK-DAG:   %[[VAL_92:.*]] = arith.constant 0 : index
+! CHECK-DAG:   %[[VAL_93:.*]] = arith.constant 1 : index
+! CHECK:   %[[VAL_94:.*]] = fir.shape %[[VAL_91]] : (index) -> !fir.shape<1>
+! CHECK:   %[[VAL_95:.*]] = fir.address_of(@{{.*}}) : !fir.ref<!fir.char<1,5>>
+! CHECK:   br ^bb1(%[[VAL_92]], %[[VAL_91]] : index, index)
+! CHECK: ^bb1(%[[VAL_96:.*]]: index, %[[VAL_97:.*]]: index):
+! CHECK:   %[[VAL_98:.*]] = arith.cmpi sgt, %[[VAL_97]], %[[VAL_92]] : index
+! CHECK:   cond_br %[[VAL_98]], ^bb2, ^bb3
+! CHECK: ^bb2:
+! CHECK:   %[[VAL_99:.*]] = fir.convert %[[VAL_95]] : (!fir.ref<!fir.char<1,5>>) -> !fir.ref<!fir.char<1,?>>
+! CHECK:   %[[VAL_100:.*]] = fir.emboxchar %[[VAL_99]], %[[VAL_90]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:   %[[VAL_101:.*]] = arith.addi %[[VAL_96]], %[[VAL_93]] : index
+! CHECK:   %[[VAL_102:.*]] = fir.array_coor %[[VAL_103]](%[[VAL_94]]) %[[VAL_101]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   %[[VAL_104:.*]] = fir.call @_QPelem2(%[[VAL_100]], %[[VAL_102]]) : (!fir.boxchar<1>, !fir.ref<i32>) -> i32
+! CHECK:   %[[VAL_105:.*]] = fir.array_coor %[[VAL_106]](%[[VAL_94]]) %[[VAL_101]] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:   fir.store %[[VAL_104]] to %[[VAL_105]] : !fir.ref<i32>
+! CHECK:   %[[VAL_107:.*]] = arith.subi %[[VAL_97]], %[[VAL_93]] : index
+! CHECK:   br ^bb1(%[[VAL_101]], %[[VAL_107]] : index, index)
+! CHECK: ^bb3:
+! CHECK:   return
+  i = elem2("hello", j)
+end subroutine
+
+! Test character return for elemental functions.
+
+! CHECK-LABEL: func @_QMchar_elemPelem_return_char(
+! CHECK-SAME: %{{.*}}: !fir.ref<!fir.char<1,?>>{{.*}}, %{{.*}}: index{{.*}}, %{{.*}}: !fir.boxchar<1>{{.*}}) -> !fir.boxchar<1>
+elemental function elem_return_char(c)
+ character(*), intent(in) :: c
+ character(len(c)) :: elem_return_char
+ elem_return_char = "ab" // c
+end function
+
+! CHECK-LABEL: func @_QMchar_elemPfoo6(
+! CHECK-SAME:         %[[VAL_0:.*]]: !fir.boxchar<1> {fir.bindc_name = "c"}) {
+subroutine foo6(c)
+  ! CHECK-DAG:     %[[VAL_1:.*]] = arith.constant 10 : index
+  ! CHECK-DAG:     %[[VAL_2:.*]] = arith.constant 1 : index
+  ! CHECK-DAG:     %[[VAL_3:.*]] = arith.constant 0 : index
+  ! CHECK-DAG:     %[[VAL_4:.*]] = arith.constant false
+  ! CHECK-DAG:     %[[VAL_5:.*]] = arith.constant 32 : i8
+  ! CHECK:         %[[VAL_6:.*]]:2 = fir.unboxchar %[[VAL_0]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+  ! CHECK:         %[[VAL_7:.*]] = fir.convert %[[VAL_6]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<10x!fir.char<1,?>>>
+  ! CHECK:         %[[VAL_8:.*]] = fir.shape %[[VAL_1]] : (index) -> !fir.shape<1>
+  ! CHECK:         br ^bb1(%[[VAL_3]], %[[VAL_1]] : index, index)
+  ! CHECK:       ^bb1(%[[VAL_9:.*]]: index, %[[VAL_10:.*]]: index):
+  ! CHECK:         %[[VAL_11:.*]] = arith.cmpi sgt, %[[VAL_10]], %[[VAL_3]] : index
+  ! CHECK:         cond_br %[[VAL_11]], ^bb2, ^bb6
+  ! CHECK:       ^bb2:
+  ! CHECK:         %[[VAL_12:.*]] = arith.addi %[[VAL_9]], %[[VAL_2]] : index
+  ! CHECK:         %[[VAL_13:.*]] = fir.array_coor %[[VAL_7]](%[[VAL_8]]) %[[VAL_12]] typeparams %[[VAL_6]]#1 : (!fir.ref<!fir.array<10x!fir.char<1,?>>>, !fir.shape<1>, index, index) -> !fir.ref<!fir.char<1,?>>
+  ! CHECK:         %[[VAL_14:.*]] = fir.emboxchar %[[VAL_13]], %[[VAL_6]]#1 : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+  ! CHECK:         %[[VAL_15:.*]] = fir.convert %[[VAL_6]]#1 : (index) -> i32
+  ! CHECK:         %[[VAL_16:.*]] = fir.convert %[[VAL_15]] : (i32) -> index
+  ! CHECK:         %[[VAL_17:.*]] = fir.call @llvm.stacksave() : () -> !fir.ref<i8>
+  ! CHECK:         %[[VAL_18:.*]] = fir.alloca !fir.char<1,?>(%[[VAL_16]] : index) {bindc_name = ".result"}
+  ! CHECK:         %[[VAL_19:.*]] = fir.call @_QMchar_elemPelem_return_char(%[[VAL_18]], %[[VAL_16]], %[[VAL_14]]) : (!fir.ref<!fir.char<1,?>>, index, !fir.boxchar<1>) -> !fir.boxchar<1>
+  ! CHECK:         %[[VAL_20:.*]] = arith.cmpi slt, %[[VAL_6]]#1, %[[VAL_16]] : index
+  ! CHECK:         %[[VAL_21:.*]] = arith.select %[[VAL_20]], %[[VAL_6]]#1, %[[VAL_16]] : index
+  ! CHECK:         %[[VAL_22:.*]] = fir.convert %[[VAL_21]] : (index) -> i64
+  ! CHECK:         %[[VAL_23:.*]] = fir.convert %[[VAL_13]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
+  ! CHECK:         %[[VAL_24:.*]] = fir.convert %[[VAL_18]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
+  ! CHECK:         fir.call @llvm.memmove.p0.p0.i64(%[[VAL_23]], %[[VAL_24]], %[[VAL_22]], %[[VAL_4]]) : (!fir.ref<i8>, !fir.ref<i8>, i64, i1) -> ()
+  ! CHECK:         %[[VAL_25:.*]] = arith.subi %[[VAL_6]]#1, %[[VAL_2]] : index
+  ! CHECK:         %[[VAL_26:.*]] = fir.undefined !fir.char<1>
+  ! CHECK:         %[[VAL_27:.*]] = fir.insert_value %[[VAL_26]], %[[VAL_5]], [0 : index] : (!fir.char<1>, i8) -> !fir.char<1>
+  ! CHECK:         %[[VAL_28:.*]] = arith.subi %[[VAL_25]], %[[VAL_21]] : index
+  ! CHECK:         %[[VAL_29:.*]] = arith.addi %[[VAL_28]], %[[VAL_2]] : index
+  ! CHECK:         br ^bb3(%[[VAL_21]], %[[VAL_29]] : index, index)
+  ! CHECK:       ^bb3(%[[VAL_30:.*]]: index, %[[VAL_31:.*]]: index):
+  ! CHECK:         %[[VAL_32:.*]] = arith.cmpi sgt, %[[VAL_31]], %[[VAL_3]] : index
+  ! CHECK:         cond_br %[[VAL_32]], ^bb4, ^bb5
+  ! CHECK:       ^bb4:
+  ! CHECK:         %[[VAL_33:.*]] = fir.convert %[[VAL_13]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
+  ! CHECK:         %[[VAL_34:.*]] = fir.coordinate_of %[[VAL_33]], %[[VAL_30]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
+  ! CHECK:         fir.store %[[VAL_27]] to %[[VAL_34]] : !fir.ref<!fir.char<1>>
+  ! CHECK:         %[[VAL_35:.*]] = arith.addi %[[VAL_30]], %[[VAL_2]] : index
+  ! CHECK:         %[[VAL_36:.*]] = arith.subi %[[VAL_31]], %[[VAL_2]] : index
+  ! CHECK:         br ^bb3(%[[VAL_35]], %[[VAL_36]] : index, index)
+  ! CHECK:       ^bb5:
+  ! CHECK:         fir.call @llvm.stackrestore(%[[VAL_17]]) : (!fir.ref<i8>) -> ()
+  ! CHECK:         %[[VAL_37:.*]] = arith.subi %[[VAL_10]], %[[VAL_2]] : index
+  ! CHECK:         br ^bb1(%[[VAL_12]], %[[VAL_37]] : index, index)
+  ! CHECK:       ^bb6:
+
+  implicit none
+  character(*) :: c(10)
+  c = elem_return_char(c)
+  ! CHECK: return
+  ! CHECK: }
+end subroutine
+
+end module

diff  --git a/flang/test/Lower/array-elemental-subroutines.f90 b/flang/test/Lower/array-elemental-subroutines.f90
new file mode 100644
index 0000000000000..652e614fa9fab
--- /dev/null
+++ b/flang/test/Lower/array-elemental-subroutines.f90
@@ -0,0 +1,64 @@
+! Test lowering of elemental subroutine calls with array arguments
+! RUN: bbc -o - -emit-fir %s | FileCheck %s
+
+! CHECK-LABEL: func @_QPtest_elem_sub(
+! CHECK-SAME:    %[[VAL_0:.*]]: !fir.box<!fir.array<?xf32>>{{.*}}, %[[VAL_1:.*]]: !fir.box<!fir.array<?x!fir.char<1,?>>>{{.*}}, %[[VAL_2:.*]]: !fir.ref<i32>{{.*}}, %[[VAL_3:.*]]: !fir.ref<!fir.complex<4>>{{.*}}) {
+! CHECK:         %[[VAL_4:.*]] = fir.alloca !fir.complex<4> {adapt.valuebyref}
+! CHECK:         %[[VAL_5:.*]] = arith.constant 0 : index
+! CHECK:         %[[VAL_6:.*]]:3 = fir.box_dims %[[VAL_0]], %[[VAL_5]] : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
+! CHECK:         %[[VAL_7:.*]] = arith.constant 10 : i64
+! CHECK:         %[[VAL_8:.*]] = fir.convert %[[VAL_7]] : (i64) -> index
+! CHECK:         %[[VAL_9:.*]] = arith.constant -1 : i64
+! CHECK:         %[[VAL_10:.*]] = fir.convert %[[VAL_9]] : (i64) -> index
+! CHECK:         %[[VAL_11:.*]] = arith.constant 1 : i64
+! CHECK:         %[[VAL_12:.*]] = fir.convert %[[VAL_11]] : (i64) -> index
+! CHECK:         %[[VAL_13:.*]] = fir.slice %[[VAL_8]], %[[VAL_12]], %[[VAL_10]] : (index, index, index) -> !fir.slice<1>
+! CHECK:         %[[VAL_14:.*]] = fir.load %[[VAL_3]] : !fir.ref<!fir.complex<4>>
+! CHECK:         fir.store %[[VAL_14]] to %[[VAL_4]] : !fir.ref<!fir.complex<4>>
+! CHECK:         %[[VAL_15:.*]] = arith.constant 1 : index
+! CHECK:         %[[VAL_16:.*]] = arith.constant 0 : index
+! CHECK:         %[[VAL_17:.*]] = arith.subi %[[VAL_6]]#1, %[[VAL_15]] : index
+! CHECK:         fir.do_loop %[[VAL_18:.*]] = %[[VAL_16]] to %[[VAL_17]] step %[[VAL_15]] {
+! CHECK:           %[[VAL_19:.*]] = arith.constant 1 : index
+! CHECK:           %[[VAL_20:.*]] = arith.addi %[[VAL_18]], %[[VAL_19]] : index
+! CHECK:           %[[VAL_21:.*]] = fir.array_coor %[[VAL_0]] %[[VAL_20]] : (!fir.box<!fir.array<?xf32>>, index) -> !fir.ref<f32>
+! CHECK:           %[[VAL_22:.*]] = arith.constant 1 : index
+! CHECK:           %[[VAL_23:.*]] = arith.addi %[[VAL_18]], %[[VAL_22]] : index
+! CHECK:           %[[VAL_24:.*]] = fir.array_coor %[[VAL_1]] {{\[}}%[[VAL_13]]] %[[VAL_23]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>, !fir.slice<1>, index) -> !fir.ref<!fir.char<1,?>>
+! CHECK:           %[[VAL_25:.*]] = fir.box_elesize %[[VAL_1]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> index
+! CHECK:           %[[VAL_26:.*]] = fir.emboxchar %[[VAL_24]], %[[VAL_25]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
+! CHECK:           fir.call @_QPfoo(%[[VAL_21]], %[[VAL_26]], %[[VAL_2]], %[[VAL_4]]) : (!fir.ref<f32>, !fir.boxchar<1>, !fir.ref<i32>, !fir.ref<!fir.complex<4>>) -> ()
+! CHECK:         }
+! CHECK:         return
+! CHECK:       }
+
+subroutine test_elem_sub(x, c, i, z)
+  real :: x(:)
+  character(*) :: c(:)
+  integer :: i
+  complex :: z
+  interface
+    elemental subroutine foo(x, c, i, z)
+      real, intent(out) :: x
+      character(*), intent(inout) :: c
+      integer, intent(in) :: i
+      complex, value :: z
+    end subroutine
+  end interface
+
+  call foo(x, c(10:1:-1), i, z)
+end subroutine
+
+! CHECK-LABEL: func @_QPtest_elem_sub_no_array_args(
+! CHECK-SAME:    %[[VAL_0:.*]]: !fir.ref<i32>{{.*}}, %[[VAL_1:.*]]: !fir.ref<i32>{{.*}}) {
+subroutine test_elem_sub_no_array_args(i, j)
+  integer :: i, j
+  interface
+    elemental subroutine bar(i, j)
+      integer, intent(out) :: i
+      integer, intent(in) :: j
+    end subroutine
+  end interface
+  call bar(i, j)
+  ! CHECK:         fir.call @_QPbar(%[[VAL_0]], %[[VAL_1]]) : (!fir.ref<i32>, !fir.ref<i32>) -> ()
+end subroutine

diff  --git a/flang/test/Lower/array-expression-assumed-size.f90 b/flang/test/Lower/array-expression-assumed-size.f90
new file mode 100644
index 0000000000000..8fc3e94ffaf80
--- /dev/null
+++ b/flang/test/Lower/array-expression-assumed-size.f90
@@ -0,0 +1,303 @@
+! RUN: bbc --emit-fir %s -o - | FileCheck %s
+! RUN: bbc %s -o - | FileCheck --check-prefix=PostOpt %s
+
+
+subroutine assumed_size_test(a)
+  integer :: a(10,*)
+  a(:, 1:2) = a(:, 3:4)
+end subroutine assumed_size_test
+
+subroutine assumed_size_forall_test(b)
+  integer :: b(10,*)
+  forall (i=2:6)
+     b(i, 1:2) = b(i, 3:4)
+  end forall
+end subroutine assumed_size_forall_test
+
+! CHECK-LABEL: func @_QPassumed_size_test(
+! CHECK-SAME:    %[[VAL_0:.*]]: !fir.ref<!fir.array<10x?xi32>>{{.*}}) {
+! CHECK:         %[[VAL_1A:.*]] = fir.convert %c10{{.*}} : (i64) -> index 
+! CHECK:         %[[VAL_1B:.*]] = arith.cmpi sgt, %[[VAL_1A]], %c0{{.*}} : index 
+! CHECK:         %[[VAL_1:.*]] = arith.select %[[VAL_1B]], %[[VAL_1A]], %c0{{.*}} : index 
+! CHECK:         %[[VAL_2:.*]] = fir.undefined index
+! CHECK:         %[[VAL_3:.*]] = arith.constant 1 : index
+! CHECK:         %[[VAL_4:.*]] = arith.constant 1 : i64
+! CHECK:         %[[VAL_5:.*]] = fir.convert %[[VAL_4]] : (i64) -> index
+! CHECK:         %[[VAL_6:.*]] = arith.addi %[[VAL_3]], %[[VAL_1]] : index
+! CHECK:         %[[VAL_7:.*]] = arith.subi %[[VAL_6]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_8:.*]] = arith.constant 0 : index
+! CHECK:         %[[VAL_9:.*]] = arith.subi %[[VAL_7]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_10:.*]] = arith.addi %[[VAL_9]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_11:.*]] = arith.divsi %[[VAL_10]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_12:.*]] = arith.cmpi sgt, %[[VAL_11]], %[[VAL_8]] : index
+! CHECK:         %[[VAL_13:.*]] = arith.select %[[VAL_12]], %[[VAL_11]], %[[VAL_8]] : index
+! CHECK:         %[[VAL_14:.*]] = arith.constant 1 : i64
+! CHECK:         %[[VAL_15:.*]] = fir.convert %[[VAL_14]] : (i64) -> index
+! CHECK:         %[[VAL_16:.*]] = arith.constant 1 : i64
+! CHECK:         %[[VAL_17:.*]] = fir.convert %[[VAL_16]] : (i64) -> index
+! CHECK:         %[[VAL_18:.*]] = arith.constant 2 : i64
+! CHECK:         %[[VAL_19:.*]] = fir.convert %[[VAL_18]] : (i64) -> index
+! CHECK:         %[[VAL_20:.*]] = arith.constant 0 : index
+! CHECK:         %[[VAL_21:.*]] = arith.subi %[[VAL_19]], %[[VAL_15]] : index
+! CHECK:         %[[VAL_22:.*]] = arith.addi %[[VAL_21]], %[[VAL_17]] : index
+! CHECK:         %[[VAL_23:.*]] = arith.divsi %[[VAL_22]], %[[VAL_17]] : index
+! CHECK:         %[[VAL_24:.*]] = arith.cmpi sgt, %[[VAL_23]], %[[VAL_20]] : index
+! CHECK:         %[[VAL_25:.*]] = arith.select %[[VAL_24]], %[[VAL_23]], %[[VAL_20]] : index
+! CHECK:         %[[VAL_26:.*]] = fir.shape %[[VAL_1]], %[[VAL_2]] : (index, index) -> !fir.shape<2>
+! CHECK:         %[[VAL_27:.*]] = fir.slice %[[VAL_3]], %[[VAL_7]], %[[VAL_5]], %[[VAL_15]], %[[VAL_19]], %[[VAL_17]] : (index, index, index, index, index, index) -> !fir.slice<2>
+! CHECK:         %[[VAL_28:.*]] = fir.array_load %[[VAL_0]](%[[VAL_26]]) {{\[}}%[[VAL_27]]] : (!fir.ref<!fir.array<10x?xi32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.array<10x?xi32>
+! CHECK:         %[[VAL_29:.*]] = arith.constant 1 : index
+! CHECK:         %[[VAL_30:.*]] = arith.constant 1 : i64
+! CHECK:         %[[VAL_31:.*]] = fir.convert %[[VAL_30]] : (i64) -> index
+! CHECK:         %[[VAL_32:.*]] = arith.addi %[[VAL_29]], %[[VAL_1]] : index
+! CHECK:         %[[VAL_33:.*]] = arith.subi %[[VAL_32]], %[[VAL_29]] : index
+! CHECK:         %[[VAL_34:.*]] = arith.constant 3 : i64
+! CHECK:         %[[VAL_35:.*]] = fir.convert %[[VAL_34]] : (i64) -> index
+! CHECK:         %[[VAL_36:.*]] = arith.constant 1 : i64
+! CHECK:         %[[VAL_37:.*]] = fir.convert %[[VAL_36]] : (i64) -> index
+! CHECK:         %[[VAL_38:.*]] = arith.constant 4 : i64
+! CHECK:         %[[VAL_39:.*]] = fir.convert %[[VAL_38]] : (i64) -> index
+! CHECK:         %[[VAL_40:.*]] = fir.shape %[[VAL_1]], %[[VAL_2]] : (index, index) -> !fir.shape<2>
+! CHECK:         %[[VAL_41:.*]] = fir.slice %[[VAL_29]], %[[VAL_33]], %[[VAL_31]], %[[VAL_35]], %[[VAL_39]], %[[VAL_37]] : (index, index, index, index, index, index) -> !fir.slice<2>
+! CHECK:         %[[VAL_42:.*]] = fir.array_load %[[VAL_0]](%[[VAL_40]]) {{\[}}%[[VAL_41]]] : (!fir.ref<!fir.array<10x?xi32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.array<10x?xi32>
+! CHECK:         %[[VAL_43:.*]] = arith.constant 1 : index
+! CHECK:         %[[VAL_44:.*]] = arith.constant 0 : index
+! CHECK:         %[[VAL_45:.*]] = arith.subi %[[VAL_13]], %[[VAL_43]] : index
+! CHECK:         %[[VAL_46:.*]] = arith.subi %[[VAL_25]], %[[VAL_43]] : index
+! CHECK:         %[[VAL_47:.*]] = fir.do_loop %[[VAL_48:.*]] = %[[VAL_44]] to %[[VAL_46]] step %[[VAL_43]] unordered iter_args(%[[VAL_49:.*]] = %[[VAL_28]]) -> (!fir.array<10x?xi32>) {
+! CHECK:           %[[VAL_50:.*]] = fir.do_loop %[[VAL_51:.*]] = %[[VAL_44]] to %[[VAL_45]] step %[[VAL_43]] unordered iter_args(%[[VAL_52:.*]] = %[[VAL_49]]) -> (!fir.array<10x?xi32>) {
+! CHECK:             %[[VAL_53:.*]] = fir.array_fetch %[[VAL_42]], %[[VAL_51]], %[[VAL_48]] : (!fir.array<10x?xi32>, index, index) -> i32
+! CHECK:             %[[VAL_54:.*]] = fir.array_update %[[VAL_52]], %[[VAL_53]], %[[VAL_51]], %[[VAL_48]] : (!fir.array<10x?xi32>, i32, index, index) -> !fir.array<10x?xi32>
+! CHECK:             fir.result %[[VAL_54]] : !fir.array<10x?xi32>
+! CHECK:           }
+! CHECK:           fir.result %[[VAL_55:.*]] : !fir.array<10x?xi32>
+! CHECK:         }
+! CHECK:         fir.array_merge_store %[[VAL_28]], %[[VAL_56:.*]] to %[[VAL_0]]{{\[}}%[[VAL_27]]] : !fir.array<10x?xi32>, !fir.array<10x?xi32>, !fir.ref<!fir.array<10x?xi32>>, !fir.slice<2>
+! CHECK:         return
+! CHECK:       }
+
+! CHECK-LABEL: func @_QPassumed_size_forall_test(
+! CHECK-SAME:       %[[VAL_0:.*]]: !fir.ref<!fir.array<10x?xi32>>{{.*}}) {
+! CHECK:         %[[VAL_1:.*]] = fir.alloca i32 {adapt.valuebyref, bindc_name = "i"}
+! CHECK:         %[[VAL_2A:.*]] = fir.convert %c10{{.*}} : (i64) -> index 
+! CHECK:         %[[VAL_2B:.*]] = arith.cmpi sgt, %[[VAL_2A]], %c0{{.*}} : index 
+! CHECK:         %[[VAL_2:.*]] = arith.select %[[VAL_2B]], %[[VAL_2A]], %c0{{.*}} : index 
+! CHECK:         %[[VAL_3:.*]] = fir.undefined index
+! CHECK:         %[[VAL_4:.*]] = arith.constant 2 : i32
+! CHECK:         %[[VAL_5:.*]] = fir.convert %[[VAL_4]] : (i32) -> index
+! CHECK:         %[[VAL_6:.*]] = arith.constant 6 : i32
+! CHECK:         %[[VAL_7:.*]] = fir.convert %[[VAL_6]] : (i32) -> index
+! CHECK:         %[[VAL_8:.*]] = arith.constant 1 : index
+! CHECK:         %[[VAL_9:.*]] = fir.shape %[[VAL_2]], %[[VAL_3]] : (index, index) -> !fir.shape<2>
+! CHECK:         %[[VAL_10:.*]] = fir.array_load %[[VAL_0]](%[[VAL_9]]) : (!fir.ref<!fir.array<10x?xi32>>, !fir.shape<2>) -> !fir.array<10x?xi32>
+! CHECK:         %[[VAL_11:.*]] = fir.shape %[[VAL_2]], %[[VAL_3]] : (index, index) -> !fir.shape<2>
+! CHECK:         %[[VAL_12:.*]] = fir.array_load %[[VAL_0]](%[[VAL_11]]) : (!fir.ref<!fir.array<10x?xi32>>, !fir.shape<2>) -> !fir.array<10x?xi32>
+! CHECK:         %[[VAL_13:.*]] = fir.do_loop %[[VAL_14:.*]] = %[[VAL_5]] to %[[VAL_7]] step %[[VAL_8]] unordered iter_args(%[[VAL_15:.*]] = %[[VAL_10]]) -> (!fir.array<10x?xi32>) {
+! CHECK:           %[[VAL_16:.*]] = fir.convert %[[VAL_14]] : (index) -> i32
+! CHECK:           fir.store %[[VAL_16]] to %[[VAL_1]] : !fir.ref<i32>
+! CHECK:           %[[VAL_17:.*]] = arith.constant 1 : index
+! CHECK:           %[[VAL_18:.*]] = fir.load %[[VAL_1]] : !fir.ref<i32>
+! CHECK:           %[[VAL_19:.*]] = fir.convert %[[VAL_18]] : (i32) -> i64
+! CHECK:           %[[VAL_20:.*]] = fir.convert %[[VAL_19]] : (i64) -> index
+! CHECK:           %[[VAL_21:.*]] = arith.subi %[[VAL_20]], %[[VAL_17]] : index
+! CHECK:           %[[VAL_22:.*]] = arith.constant 1 : i64
+! CHECK:           %[[VAL_23:.*]] = fir.convert %[[VAL_22]] : (i64) -> index
+! CHECK:           %[[VAL_24:.*]] = arith.constant 1 : i64
+! CHECK:           %[[VAL_25:.*]] = fir.convert %[[VAL_24]] : (i64) -> index
+! CHECK:           %[[VAL_26:.*]] = arith.constant 2 : i64
+! CHECK:           %[[VAL_27:.*]] = fir.convert %[[VAL_26]] : (i64) -> index
+! CHECK:           %[[VAL_28:.*]] = arith.constant 0 : index
+! CHECK:           %[[VAL_29:.*]] = arith.subi %[[VAL_27]], %[[VAL_23]] : index
+! CHECK:           %[[VAL_30:.*]] = arith.addi %[[VAL_29]], %[[VAL_25]] : index
+! CHECK:           %[[VAL_31:.*]] = arith.divsi %[[VAL_30]], %[[VAL_25]] : index
+! CHECK:           %[[VAL_32:.*]] = arith.cmpi sgt, %[[VAL_31]], %[[VAL_28]] : index
+! CHECK:           %[[VAL_33:.*]] = arith.select %[[VAL_32]], %[[VAL_31]], %[[VAL_28]] : index
+! CHECK:           %[[VAL_34:.*]] = arith.constant 1 : index
+! CHECK:           %[[VAL_35:.*]] = fir.load %[[VAL_1]] : !fir.ref<i32>
+! CHECK:           %[[VAL_36:.*]] = fir.convert %[[VAL_35]] : (i32) -> i64
+! CHECK:           %[[VAL_37:.*]] = fir.convert %[[VAL_36]] : (i64) -> index
+! CHECK:           %[[VAL_38:.*]] = arith.subi %[[VAL_37]], %[[VAL_34]] : index
+! CHECK:           %[[VAL_39:.*]] = arith.constant 3 : i64
+! CHECK:           %[[VAL_40:.*]] = fir.convert %[[VAL_39]] : (i64) -> index
+! CHECK:           %[[VAL_41:.*]] = arith.constant 1 : i64
+! CHECK:           %[[VAL_42:.*]] = fir.convert %[[VAL_41]] : (i64) -> index
+! CHECK:           %[[VAL_43:.*]] = arith.constant 1 : index
+! CHECK:           %[[VAL_44:.*]] = arith.constant 0 : index
+! CHECK:           %[[VAL_45:.*]] = arith.subi %[[VAL_33]], %[[VAL_43]] : index
+! CHECK:           %[[VAL_46:.*]] = fir.do_loop %[[VAL_47:.*]] = %[[VAL_44]] to %[[VAL_45]] step %[[VAL_43]] unordered iter_args(%[[VAL_48:.*]] = %[[VAL_15]]) -> (!fir.array<10x?xi32>) {
+! CHECK:             %[[VAL_49:.*]] = arith.subi %[[VAL_40]], %[[VAL_34]] : index
+! CHECK:             %[[VAL_50:.*]] = arith.muli %[[VAL_47]], %[[VAL_42]] : index
+! CHECK:             %[[VAL_51:.*]] = arith.addi %[[VAL_49]], %[[VAL_50]] : index
+! CHECK:             %[[VAL_52:.*]] = fir.array_fetch %[[VAL_12]], %[[VAL_38]], %[[VAL_51]] : (!fir.array<10x?xi32>, index, index) -> i32
+! CHECK:             %[[VAL_53:.*]] = arith.subi %[[VAL_23]], %[[VAL_17]] : index
+! CHECK:             %[[VAL_54:.*]] = arith.muli %[[VAL_47]], %[[VAL_25]] : index
+! CHECK:             %[[VAL_55:.*]] = arith.addi %[[VAL_53]], %[[VAL_54]] : index
+! CHECK:             %[[VAL_56:.*]] = fir.array_update %[[VAL_48]], %[[VAL_52]], %[[VAL_21]], %[[VAL_55]] : (!fir.array<10x?xi32>, i32, index, index) -> !fir.array<10x?xi32>
+! CHECK:             fir.result %[[VAL_56]] : !fir.array<10x?xi32>
+! CHECK:           }
+! CHECK:           fir.result %[[VAL_57:.*]] : !fir.array<10x?xi32>
+! CHECK:         }
+! CHECK:         fir.array_merge_store %[[VAL_10]], %[[VAL_58:.*]] to %[[VAL_0]] : !fir.array<10x?xi32>, !fir.array<10x?xi32>, !fir.ref<!fir.array<10x?xi32>>
+! CHECK:         return
+! CHECK:       }
+
+! PostOpt-LABEL: func @_QPassumed_size_test(
+! PostOpt-SAME:        %[[VAL_0:.*]]: !fir.ref<!fir.array<10x?xi32>>{{.*}}) {
+! PostOpt-DAG:         %[[VAL_1:.*]] = arith.constant 10 : index
+! PostOpt-DAG:         %[[VAL_2:.*]] = arith.constant 1 : index
+! PostOpt-DAG:         %[[VAL_3:.*]] = arith.constant 2 : index
+! PostOpt-DAG:         %[[VAL_4:.*]] = arith.constant 0 : index
+! PostOpt-DAG:         %[[VAL_5:.*]] = arith.constant 3 : index
+! PostOpt-DAG:         %[[VAL_6:.*]] = arith.constant 4 : index
+! PostOpt:         %[[VAL_7:.*]] = fir.undefined index
+! PostOpt:         %[[VAL_8:.*]] = fir.shape %[[VAL_1]], %[[VAL_7]] : (index, index) -> !fir.shape<2>
+! PostOpt:         %[[VAL_9:.*]] = fir.slice %[[VAL_2]], %[[VAL_1]], %[[VAL_2]], %[[VAL_2]], %[[VAL_3]], %[[VAL_2]] : (index, index, index, index, index, index) -> !fir.slice<2>
+! PostOpt:         %[[VAL_10:.*]] = fir.allocmem !fir.array<10x?xi32>, %[[VAL_3]]
+! PostOpt:         br ^bb1(%[[VAL_4]], %[[VAL_3]] : index, index)
+! PostOpt:       ^bb1(%[[VAL_11:.*]]: index, %[[VAL_12:.*]]: index):
+! PostOpt:         %[[VAL_13:.*]] = arith.cmpi sgt, %[[VAL_12]], %[[VAL_4]] : index
+! PostOpt:         cond_br %[[VAL_13]], ^bb2(%[[VAL_4]], %[[VAL_1]] : index, index), ^bb5
+! PostOpt:       ^bb2(%[[VAL_14:.*]]: index, %[[VAL_15:.*]]: index):
+! PostOpt:         %[[VAL_16:.*]] = arith.cmpi sgt, %[[VAL_15]], %[[VAL_4]] : index
+! PostOpt:         cond_br %[[VAL_16]], ^bb3, ^bb4
+! PostOpt:       ^bb3:
+! PostOpt:         %[[VAL_17:.*]] = arith.addi %[[VAL_14]], %[[VAL_2]] : index
+! PostOpt:         %[[VAL_18:.*]] = arith.addi %[[VAL_11]], %[[VAL_2]] : index
+! PostOpt:         %[[VAL_19:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_8]]) {{\[}}%[[VAL_9]]] %[[VAL_17]], %[[VAL_18]] : (!fir.ref<!fir.array<10x?xi32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<i32>
+! PostOpt:         %[[VAL_20:.*]] = fir.array_coor %[[VAL_10]](%[[VAL_8]]) %[[VAL_17]], %[[VAL_18]] : (!fir.heap<!fir.array<10x?xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
+! PostOpt:         %[[VAL_21:.*]] = fir.load %[[VAL_19]] : !fir.ref<i32>
+! PostOpt:         fir.store %[[VAL_21]] to %[[VAL_20]] : !fir.ref<i32>
+! PostOpt:         %[[VAL_22:.*]] = arith.subi %[[VAL_15]], %[[VAL_2]] : index
+! PostOpt:         br ^bb2(%[[VAL_17]], %[[VAL_22]] : index, index)
+! PostOpt:       ^bb4:
+! PostOpt:         %[[VAL_23:.*]] = arith.addi %[[VAL_11]], %[[VAL_2]] : index
+! PostOpt:         %[[VAL_24:.*]] = arith.subi %[[VAL_12]], %[[VAL_2]] : index
+! PostOpt:         br ^bb1(%[[VAL_23]], %[[VAL_24]] : index, index)
+! PostOpt:       ^bb5:
+! PostOpt:         %[[VAL_25:.*]] = fir.slice %[[VAL_2]], %[[VAL_1]], %[[VAL_2]], %[[VAL_5]], %[[VAL_6]], %[[VAL_2]] : (index, index, index, index, index, index) -> !fir.slice<2>
+! PostOpt:         br ^bb6(%[[VAL_4]], %[[VAL_3]] : index, index)
+! PostOpt:       ^bb6(%[[VAL_26:.*]]: index, %[[VAL_27:.*]]: index):
+! PostOpt:         %[[VAL_28:.*]] = arith.cmpi sgt, %[[VAL_27]], %[[VAL_4]] : index
+! PostOpt:         cond_br %[[VAL_28]], ^bb7(%[[VAL_4]], %[[VAL_1]] : index, index), ^bb10(%[[VAL_4]], %[[VAL_3]] : index, index)
+! PostOpt:       ^bb7(%[[VAL_29:.*]]: index, %[[VAL_30:.*]]: index):
+! PostOpt:         %[[VAL_31:.*]] = arith.cmpi sgt, %[[VAL_30]], %[[VAL_4]] : index
+! PostOpt:         cond_br %[[VAL_31]], ^bb8, ^bb9
+! PostOpt:       ^bb8:
+! PostOpt:         %[[VAL_32:.*]] = arith.addi %[[VAL_29]], %[[VAL_2]] : index
+! PostOpt:         %[[VAL_33:.*]] = arith.addi %[[VAL_26]], %[[VAL_2]] : index
+! PostOpt:         %[[VAL_34:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_8]]) {{\[}}%[[VAL_25]]] %[[VAL_32]], %[[VAL_33]] : (!fir.ref<!fir.array<10x?xi32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<i32>
+! PostOpt:         %[[VAL_35:.*]] = fir.load %[[VAL_34]] : !fir.ref<i32>
+! PostOpt:         %[[VAL_36:.*]] = fir.array_coor %[[VAL_10]](%[[VAL_8]]) %[[VAL_32]], %[[VAL_33]] : (!fir.heap<!fir.array<10x?xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
+! PostOpt:         fir.store %[[VAL_35]] to %[[VAL_36]] : !fir.ref<i32>
+! PostOpt:         %[[VAL_37:.*]] = arith.subi %[[VAL_30]], %[[VAL_2]] : index
+! PostOpt:         br ^bb7(%[[VAL_32]], %[[VAL_37]] : index, index)
+! PostOpt:       ^bb9:
+! PostOpt:         %[[VAL_38:.*]] = arith.addi %[[VAL_26]], %[[VAL_2]] : index
+! PostOpt:         %[[VAL_39:.*]] = arith.subi %[[VAL_27]], %[[VAL_2]] : index
+! PostOpt:         br ^bb6(%[[VAL_38]], %[[VAL_39]] : index, index)
+! PostOpt:       ^bb10(%[[VAL_40:.*]]: index, %[[VAL_41:.*]]: index):
+! PostOpt:         %[[VAL_42:.*]] = arith.cmpi sgt, %[[VAL_41]], %[[VAL_4]] : index
+! PostOpt:         cond_br %[[VAL_42]], ^bb11(%[[VAL_4]], %[[VAL_1]] : index, index), ^bb14
+! PostOpt:       ^bb11(%[[VAL_43:.*]]: index, %[[VAL_44:.*]]: index):
+! PostOpt:         %[[VAL_45:.*]] = arith.cmpi sgt, %[[VAL_44]], %[[VAL_4]] : index
+! PostOpt:         cond_br %[[VAL_45]], ^bb12, ^bb13
+! PostOpt:       ^bb12:
+! PostOpt:         %[[VAL_46:.*]] = arith.addi %[[VAL_43]], %[[VAL_2]] : index
+! PostOpt:         %[[VAL_47:.*]] = arith.addi %[[VAL_40]], %[[VAL_2]] : index
+! PostOpt:         %[[VAL_48:.*]] = fir.array_coor %[[VAL_10]](%[[VAL_8]]) %[[VAL_46]], %[[VAL_47]] : (!fir.heap<!fir.array<10x?xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
+! PostOpt:         %[[VAL_49:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_8]]) {{\[}}%[[VAL_9]]] %[[VAL_46]], %[[VAL_47]] : (!fir.ref<!fir.array<10x?xi32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<i32>
+! PostOpt:         %[[VAL_50:.*]] = fir.load %[[VAL_48]] : !fir.ref<i32>
+! PostOpt:         fir.store %[[VAL_50]] to %[[VAL_49]] : !fir.ref<i32>
+! PostOpt:         %[[VAL_51:.*]] = arith.subi %[[VAL_44]], %[[VAL_2]] : index
+! PostOpt:         br ^bb11(%[[VAL_46]], %[[VAL_51]] : index, index)
+! PostOpt:       ^bb13:
+! PostOpt:         %[[VAL_52:.*]] = arith.addi %[[VAL_40]], %[[VAL_2]] : index
+! PostOpt:         %[[VAL_53:.*]] = arith.subi %[[VAL_41]], %[[VAL_2]] : index
+! PostOpt:         br ^bb10(%[[VAL_52]], %[[VAL_53]] : index, index)
+! PostOpt:       ^bb14:
+! PostOpt:         fir.freemem %[[VAL_10]] : !fir.heap<!fir.array<10x?xi32>>
+! PostOpt:         return
+! PostOpt:       }
+
+! PostOpt-LABEL: func @_QPassumed_size_forall_test(
+! PostOpt-SAME:        %[[VAL_0:.*]]: !fir.ref<!fir.array<10x?xi32>>{{.*}}) {
+! PostOpt-DAG:         %[[VAL_1:.*]] = arith.constant 3 : index
+! PostOpt-DAG:         %[[VAL_2:.*]] = arith.constant 10 : index
+! PostOpt-DAG:         %[[VAL_3:.*]] = arith.constant 2 : index
+! PostOpt-DAG:         %[[VAL_4:.*]] = arith.constant 1 : index
+! PostOpt-DAG:         %[[VAL_5:.*]] = arith.constant 0 : index
+! PostOpt-DAG:         %[[VAL_6:.*]] = arith.constant 5 : index
+! PostOpt:         %[[VAL_7:.*]] = fir.alloca i32 {adapt.valuebyref, bindc_name = "i"}
+! PostOpt:         %[[VAL_8:.*]] = fir.undefined index
+! PostOpt:         %[[VAL_9:.*]] = fir.shape %[[VAL_2]], %[[VAL_8]] : (index, index) -> !fir.shape<2>
+! PostOpt:         %[[VAL_10:.*]] = fir.allocmem !fir.array<10x?xi32>, %[[VAL_4]]
+! PostOpt:         br ^bb1(%[[VAL_5]], %[[VAL_4]] : index, index)
+! PostOpt:       ^bb1(%[[VAL_11:.*]]: index, %[[VAL_12:.*]]: index):
+! PostOpt:         %[[VAL_13:.*]] = arith.cmpi sgt, %[[VAL_12]], %[[VAL_5]] : index
+! PostOpt:         cond_br %[[VAL_13]], ^bb2(%[[VAL_5]], %[[VAL_2]] : index, index), ^bb5
+! PostOpt:       ^bb2(%[[VAL_14:.*]]: index, %[[VAL_15:.*]]: index):
+! PostOpt:         %[[VAL_16:.*]] = arith.cmpi sgt, %[[VAL_15]], %[[VAL_5]] : index
+! PostOpt:         cond_br %[[VAL_16]], ^bb3, ^bb4
+! PostOpt:       ^bb3:
+! PostOpt:         %[[VAL_17:.*]] = arith.addi %[[VAL_14]], %[[VAL_4]] : index
+! PostOpt:         %[[VAL_18:.*]] = arith.addi %[[VAL_11]], %[[VAL_4]] : index
+! PostOpt:         %[[VAL_19:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_9]]) %[[VAL_17]], %[[VAL_18]] : (!fir.ref<!fir.array<10x?xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
+! PostOpt:         %[[VAL_20:.*]] = fir.array_coor %[[VAL_10]](%[[VAL_9]]) %[[VAL_17]], %[[VAL_18]] : (!fir.heap<!fir.array<10x?xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
+! PostOpt:         %[[VAL_21:.*]] = fir.load %[[VAL_19]] : !fir.ref<i32>
+! PostOpt:         fir.store %[[VAL_21]] to %[[VAL_20]] : !fir.ref<i32>
+! PostOpt:         %[[VAL_22:.*]] = arith.subi %[[VAL_15]], %[[VAL_4]] : index
+! PostOpt:         br ^bb2(%[[VAL_17]], %[[VAL_22]] : index, index)
+! PostOpt:       ^bb4:
+! PostOpt:         %[[VAL_23:.*]] = arith.addi %[[VAL_11]], %[[VAL_4]] : index
+! PostOpt:         %[[VAL_24:.*]] = arith.subi %[[VAL_12]], %[[VAL_4]] : index
+! PostOpt:         br ^bb1(%[[VAL_23]], %[[VAL_24]] : index, index)
+! PostOpt:       ^bb5:
+! PostOpt:         br ^bb6(%[[VAL_3]], %[[VAL_6]] : index, index)
+! PostOpt:       ^bb6(%[[VAL_25:.*]]: index, %[[VAL_26:.*]]: index):
+! PostOpt:         %[[VAL_27:.*]] = arith.cmpi sgt, %[[VAL_26]], %[[VAL_5]] : index
+! PostOpt:         cond_br %[[VAL_27]], ^bb7, ^bb11(%[[VAL_5]], %[[VAL_4]] : index, index)
+! PostOpt:       ^bb7:
+! PostOpt:         %[[VAL_28:.*]] = fir.convert %[[VAL_25]] : (index) -> i32
+! PostOpt:         fir.store %[[VAL_28]] to %[[VAL_7]] : !fir.ref<i32>
+! PostOpt:         %[[VAL_29:.*]] = fir.load %[[VAL_7]] : !fir.ref<i32>
+! PostOpt:         %[[VAL_30:.*]] = fir.convert %[[VAL_29]] : (i32) -> index
+! PostOpt:         br ^bb8(%[[VAL_5]], %[[VAL_3]] : index, index)
+! PostOpt:       ^bb8(%[[VAL_31:.*]]: index, %[[VAL_32:.*]]: index):
+! PostOpt:         %[[VAL_33:.*]] = arith.cmpi sgt, %[[VAL_32]], %[[VAL_5]] : index
+! PostOpt:         cond_br %[[VAL_33]], ^bb9, ^bb10
+! PostOpt:       ^bb9:
+! PostOpt:         %[[VAL_34:.*]] = arith.addi %[[VAL_31]], %[[VAL_1]] : index
+! PostOpt:         %[[VAL_35:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_9]]) %[[VAL_30]], %[[VAL_34]] : (!fir.ref<!fir.array<10x?xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
+! PostOpt:         %[[VAL_36:.*]] = fir.load %[[VAL_35]] : !fir.ref<i32>
+! PostOpt:         %[[VAL_37:.*]] = arith.addi %[[VAL_31]], %[[VAL_4]] : index
+! PostOpt:         %[[VAL_38:.*]] = fir.array_coor %[[VAL_10]](%[[VAL_9]]) %[[VAL_30]], %[[VAL_37]] : (!fir.heap<!fir.array<10x?xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
+! PostOpt:         fir.store %[[VAL_36]] to %[[VAL_38]] : !fir.ref<i32>
+! PostOpt:         %[[VAL_39:.*]] = arith.subi %[[VAL_32]], %[[VAL_4]] : index
+! PostOpt:         br ^bb8(%[[VAL_37]], %[[VAL_39]] : index, index)
+! PostOpt:       ^bb10:
+! PostOpt:         %[[VAL_40:.*]] = arith.addi %[[VAL_25]], %[[VAL_4]] : index
+! PostOpt:         %[[VAL_41:.*]] = arith.subi %[[VAL_26]], %[[VAL_4]] : index
+! PostOpt:         br ^bb6(%[[VAL_40]], %[[VAL_41]] : index, index)
+! PostOpt:       ^bb11(%[[VAL_42:.*]]: index, %[[VAL_43:.*]]: index):
+! PostOpt:         %[[VAL_44:.*]] = arith.cmpi sgt, %[[VAL_43]], %[[VAL_5]] : index
+! PostOpt:         cond_br %[[VAL_44]], ^bb12(%[[VAL_5]], %[[VAL_2]] : index, index), ^bb15
+! PostOpt:       ^bb12(%[[VAL_45:.*]]: index, %[[VAL_46:.*]]: index):
+! PostOpt:         %[[VAL_47:.*]] = arith.cmpi sgt, %[[VAL_46]], %[[VAL_5]] : index
+! PostOpt:         cond_br %[[VAL_47]], ^bb13, ^bb14
+! PostOpt:       ^bb13:
+! PostOpt:         %[[VAL_48:.*]] = arith.addi %[[VAL_45]], %[[VAL_4]] : index
+! PostOpt:         %[[VAL_49:.*]] = arith.addi %[[VAL_42]], %[[VAL_4]] : index
+! PostOpt:         %[[VAL_50:.*]] = fir.array_coor %[[VAL_10]](%[[VAL_9]]) %[[VAL_48]], %[[VAL_49]] : (!fir.heap<!fir.array<10x?xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
+! PostOpt:         %[[VAL_51:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_9]]) %[[VAL_48]], %[[VAL_49]] : (!fir.ref<!fir.array<10x?xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
+! PostOpt:         %[[VAL_52:.*]] = fir.load %[[VAL_50]] : !fir.ref<i32>
+! PostOpt:         fir.store %[[VAL_52]] to %[[VAL_51]] : !fir.ref<i32>
+! PostOpt:         %[[VAL_53:.*]] = arith.subi %[[VAL_46]], %[[VAL_4]] : index
+! PostOpt:         br ^bb12(%[[VAL_48]], %[[VAL_53]] : index, index)
+! PostOpt:       ^bb14:
+! PostOpt:         %[[VAL_54:.*]] = arith.addi %[[VAL_42]], %[[VAL_4]] : index
+! PostOpt:         %[[VAL_55:.*]] = arith.subi %[[VAL_43]], %[[VAL_4]] : index
+! PostOpt:         br ^bb11(%[[VAL_54]], %[[VAL_55]] : index, index)
+! PostOpt:       ^bb15:
+! PostOpt:         fir.freemem %[[VAL_10]] : !fir.heap<!fir.array<10x?xi32>>
+! PostOpt:         return
+! PostOpt:       }

diff  --git a/flang/test/Lower/array-expression-slice-1.f90 b/flang/test/Lower/array-expression-slice-1.f90
new file mode 100644
index 0000000000000..30f17de23c04b
--- /dev/null
+++ b/flang/test/Lower/array-expression-slice-1.f90
@@ -0,0 +1,413 @@
+! RUN: bbc -o - %s | FileCheck %s
+
+! CHECK-LABEL: func @_QQmain() {
+! CHECK-DAG:         %[[VAL_0:.*]] = arith.constant 10 : index
+! CHECK-DAG:         %[[VAL_4:.*]] = arith.constant 2 : index
+! CHECK-DAG:         %[[VAL_5:.*]] = arith.constant 1 : index
+! CHECK-DAG:         %[[VAL_6:.*]] = arith.constant 0 : index
+! CHECK-DAG:         %[[VAL_8:.*]] = arith.constant 8 : i64
+! CHECK-DAG:         %[[VAL_11:.*]] = arith.constant 3 : index
+! CHECK-DAG:         %[[VAL_13:.*]] = arith.constant 2 : i64
+! CHECK-DAG:         %[[VAL_14:.*]] = arith.constant 7 : i64
+! CHECK-DAG:         %[[VAL_16:.*]] = arith.constant 4 : i64
+! CHECK-DAG:         %[[VAL_18:.*]] = arith.constant -1 : i32
+! CHECK-DAG:         %[[VAL_19:.*]] = arith.constant 0 : i64
+! CHECK-DAG:         %[[VAL_20:.*]] = arith.constant 1 : i64
+! CHECK-DAG:         %[[VAL_21:.*]] = arith.constant 3 : i64
+! CHECK-DAG:         %[[VAL_22:.*]] = arith.constant 4 : index
+! CHECK-DAG:         %[[VAL_23:.*]] = arith.constant 1 : i32
+! CHECK-DAG:         %[[VAL_24:.*]] = arith.constant 0 : i32
+! CHECK-DAG:         %[[VAL_25:.*]] = fir.address_of(@_QFEa1) : !fir.ref<!fir.array<10x10xf32>>
+! CHECK-DAG:         %[[VAL_26:.*]] = fir.address_of(@_QFEa2) : !fir.ref<!fir.array<3xf32>>
+! CHECK-DAG:         %[[VAL_27:.*]] = fir.address_of(@_QFEa3) : !fir.ref<!fir.array<10xf32>>
+! CHECK-DAG:         %[[VAL_28:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFEi"}
+! CHECK-DAG:         %[[VAL_29:.*]] = fir.address_of(@_QFEiv) : !fir.ref<!fir.array<3xi32>>
+! CHECK-DAG:         %[[VAL_30:.*]] = fir.alloca i32 {bindc_name = "j", uniq_name = "_QFEj"}
+! CHECK-DAG:         %[[VAL_31:.*]] = fir.alloca i32 {bindc_name = "k", uniq_name = "_QFEk"}
+! CHECK:         fir.store %[[VAL_24]] to %[[VAL_31]] : !fir.ref<i32>
+! CHECK:         br ^bb1(%[[VAL_5]], %[[VAL_0]] : index, index)
+! CHECK:       ^bb1(%[[VAL_32:.*]]: index, %[[VAL_33:.*]]: index):
+! CHECK:         %[[VAL_34:.*]] = arith.cmpi sgt, %[[VAL_33]], %[[VAL_6]] : index
+! CHECK:         cond_br %[[VAL_34]], ^bb2, ^bb6
+! CHECK:       ^bb2:
+! CHECK:         %[[VAL_35:.*]] = fir.convert %[[VAL_32]] : (index) -> i32
+! CHECK:         fir.store %[[VAL_35]] to %[[VAL_30]] : !fir.ref<i32>
+! CHECK:         br ^bb3(%[[VAL_5]], %[[VAL_0]] : index, index)
+! CHECK:       ^bb3(%[[VAL_36:.*]]: index, %[[VAL_37:.*]]: index):
+! CHECK:         %[[VAL_38:.*]] = arith.cmpi sgt, %[[VAL_37]], %[[VAL_6]] : index
+! CHECK:         cond_br %[[VAL_38]], ^bb4, ^bb5
+! CHECK:       ^bb4:
+! CHECK:         %[[VAL_39:.*]] = fir.convert %[[VAL_36]] : (index) -> i32
+! CHECK:         fir.store %[[VAL_39]] to %[[VAL_28]] : !fir.ref<i32>
+! CHECK:         %[[VAL_40:.*]] = fir.load %[[VAL_31]] : !fir.ref<i32>
+! CHECK:         %[[VAL_41:.*]] = arith.addi %[[VAL_40]], %[[VAL_23]] : i32
+! CHECK:         fir.store %[[VAL_41]] to %[[VAL_31]] : !fir.ref<i32>
+! CHECK:         %[[VAL_42:.*]] = fir.load %[[VAL_31]] : !fir.ref<i32>
+! CHECK:         %[[VAL_43:.*]] = fir.convert %[[VAL_42]] : (i32) -> f32
+! CHECK:         %[[VAL_44:.*]] = fir.call @fir.cos.f32.f32(%[[VAL_43]]) : (f32) -> f32
+! CHECK:         %[[VAL_45:.*]] = fir.load %[[VAL_28]] : !fir.ref<i32>
+! CHECK:         %[[VAL_46:.*]] = fir.convert %[[VAL_45]] : (i32) -> i64
+! CHECK:         %[[VAL_47:.*]] = arith.subi %[[VAL_46]], %[[VAL_20]] : i64
+! CHECK:         %[[VAL_48:.*]] = fir.load %[[VAL_30]] : !fir.ref<i32>
+! CHECK:         %[[VAL_49:.*]] = fir.convert %[[VAL_48]] : (i32) -> i64
+! CHECK:         %[[VAL_50:.*]] = arith.subi %[[VAL_49]], %[[VAL_20]] : i64
+! CHECK:         %[[VAL_51:.*]] = fir.coordinate_of %[[VAL_25]], %[[VAL_47]], %[[VAL_50]] : (!fir.ref<!fir.array<10x10xf32>>, i64, i64) -> !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_44]] to %[[VAL_51]] : !fir.ref<f32>
+! CHECK:         %[[VAL_52:.*]] = arith.addi %[[VAL_36]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_53:.*]] = arith.subi %[[VAL_37]], %[[VAL_5]] : index
+! CHECK:         br ^bb3(%[[VAL_52]], %[[VAL_53]] : index, index)
+! CHECK:       ^bb5:
+! CHECK:         %[[VAL_54:.*]] = fir.convert %[[VAL_36]] : (index) -> i32
+! CHECK:         fir.store %[[VAL_54]] to %[[VAL_28]] : !fir.ref<i32>
+! CHECK:         %[[VAL_55:.*]] = fir.load %[[VAL_31]] : !fir.ref<i32>
+! CHECK:         %[[VAL_56:.*]] = fir.convert %[[VAL_55]] : (i32) -> f32
+! CHECK:         %[[VAL_57:.*]] = fir.call @fir.sin.f32.f32(%[[VAL_56]]) : (f32) -> f32
+! CHECK:         %[[VAL_58:.*]] = fir.load %[[VAL_30]] : !fir.ref<i32>
+! CHECK:         %[[VAL_59:.*]] = fir.convert %[[VAL_58]] : (i32) -> i64
+! CHECK:         %[[VAL_60:.*]] = arith.subi %[[VAL_59]], %[[VAL_20]] : i64
+! CHECK:         %[[VAL_61:.*]] = fir.coordinate_of %[[VAL_27]], %[[VAL_60]] : (!fir.ref<!fir.array<10xf32>>, i64) -> !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_57]] to %[[VAL_61]] : !fir.ref<f32>
+! CHECK:         %[[VAL_62:.*]] = arith.addi %[[VAL_32]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_63:.*]] = arith.subi %[[VAL_33]], %[[VAL_5]] : index
+! CHECK:         br ^bb1(%[[VAL_62]], %[[VAL_63]] : index, index)
+! CHECK:       ^bb6:
+! CHECK:         %[[VAL_64:.*]] = fir.convert %[[VAL_32]] : (index) -> i32
+! CHECK:         fir.store %[[VAL_64]] to %[[VAL_30]] : !fir.ref<i32>
+! CHECK:         %[[VAL_65:.*]] = fir.shape %[[VAL_11]] : (index) -> !fir.shape<1>
+! CHECK:         %[[VAL_66:.*]] = fir.undefined index
+! CHECK:         %[[VAL_67:.*]] = fir.shape %[[VAL_0]], %[[VAL_0]] : (index, index) -> !fir.shape<2>
+! CHECK:         %[[VAL_68:.*]] = fir.slice %[[VAL_16]], %[[VAL_66]], %[[VAL_66]], %[[VAL_4]], %[[VAL_0]], %[[VAL_11]] : (i64, index, index, index, index, index) -> !fir.slice<2>
+! CHECK:         br ^bb7(%[[VAL_6]], %[[VAL_11]] : index, index)
+! CHECK:       ^bb7(%[[VAL_69:.*]]: index, %[[VAL_70:.*]]: index):
+! CHECK:         %[[VAL_71:.*]] = arith.cmpi sgt, %[[VAL_70]], %[[VAL_6]] : index
+! CHECK:         cond_br %[[VAL_71]], ^bb8, ^bb9
+! CHECK:       ^bb8:
+! CHECK:         %[[VAL_72:.*]] = arith.addi %[[VAL_69]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_73:.*]] = fir.array_coor %[[VAL_25]](%[[VAL_67]]) {{\[}}%[[VAL_68]]] %[[VAL_22]], %[[VAL_72]] : (!fir.ref<!fir.array<10x10xf32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_74:.*]] = fir.load %[[VAL_73]] : !fir.ref<f32>
+! CHECK:         %[[VAL_75:.*]] = fir.array_coor %[[VAL_26]](%[[VAL_65]]) %[[VAL_72]] : (!fir.ref<!fir.array<3xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_74]] to %[[VAL_75]] : !fir.ref<f32>
+! CHECK:         %[[VAL_76:.*]] = arith.subi %[[VAL_70]], %[[VAL_5]] : index
+! CHECK:         br ^bb7(%[[VAL_72]], %[[VAL_76]] : index, index)
+! CHECK:       ^bb9:
+! CHECK:         %[[VAL_77:.*]] = fir.coordinate_of %[[VAL_25]], %[[VAL_21]], %[[VAL_20]] : (!fir.ref<!fir.array<10x10xf32>>, i64, i64) -> !fir.ref<f32>
+! CHECK:         %[[VAL_78:.*]] = fir.load %[[VAL_77]] : !fir.ref<f32>
+! CHECK:         %[[VAL_79:.*]] = fir.coordinate_of %[[VAL_26]], %[[VAL_19]] : (!fir.ref<!fir.array<3xf32>>, i64) -> !fir.ref<f32>
+! CHECK:         %[[VAL_80:.*]] = fir.load %[[VAL_79]] : !fir.ref<f32>
+! CHECK:         %[[VAL_81:.*]] = arith.cmpf une, %[[VAL_78]], %[[VAL_80]] : f32
+! CHECK:         cond_br %[[VAL_81]], ^bb10, ^bb11
+! CHECK:       ^bb10:
+! CHECK:         %[[VAL_82:.*]] = fir.address_of(@_QQcl.{{.*}}) : !fir.ref<!fir.char<1,
+! CHECK:         %[[VAL_83:.*]] = fir.convert %[[VAL_82]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_84:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_18]], %[[VAL_83]], %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_85:.*]] = fir.address_of(@_QQcl.6D69736D617463682031) : !fir.ref<!fir.char<1,10>>
+! CHECK:         %[[VAL_86:.*]] = fir.convert %[[VAL_85]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_87:.*]] = fir.convert %[[VAL_0]] : (index) -> i64
+! CHECK:         %[[VAL_88:.*]] = fir.call @_FortranAioOutputAscii(%[[VAL_84]], %[[VAL_86]], %[[VAL_87]]) : (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
+! CHECK:         %[[VAL_89:.*]] = fir.load %[[VAL_79]] : !fir.ref<f32>
+! CHECK:         %[[VAL_90:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_84]], %[[VAL_89]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_91:.*]] = fir.load %[[VAL_77]] : !fir.ref<f32>
+! CHECK:         %[[VAL_92:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_84]], %[[VAL_91]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_93:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_84]]) : (!fir.ref<i8>) -> i32
+! CHECK:         br ^bb11
+! CHECK:       ^bb11:
+! CHECK:         %[[VAL_94:.*]] = fir.coordinate_of %[[VAL_25]], %[[VAL_21]], %[[VAL_16]] : (!fir.ref<!fir.array<10x10xf32>>, i64, i64) -> !fir.ref<f32>
+! CHECK:         %[[VAL_95:.*]] = fir.load %[[VAL_94]] : !fir.ref<f32>
+! CHECK:         %[[VAL_96:.*]] = fir.coordinate_of %[[VAL_26]], %[[VAL_20]] : (!fir.ref<!fir.array<3xf32>>, i64) -> !fir.ref<f32>
+! CHECK:         %[[VAL_97:.*]] = fir.load %[[VAL_96]] : !fir.ref<f32>
+! CHECK:         %[[VAL_98:.*]] = arith.cmpf une, %[[VAL_95]], %[[VAL_97]] : f32
+! CHECK:         cond_br %[[VAL_98]], ^bb12, ^bb13
+! CHECK:       ^bb12:
+! CHECK:         %[[VAL_99:.*]] = fir.address_of(@_QQcl.{{.*}}) : !fir.ref<!fir.char<1,
+! CHECK:         %[[VAL_100:.*]] = fir.convert %[[VAL_99]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_101:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_18]], %[[VAL_100]], %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_102:.*]] = fir.address_of(@_QQcl.6D69736D617463682032) : !fir.ref<!fir.char<1,10>>
+! CHECK:         %[[VAL_103:.*]] = fir.convert %[[VAL_102]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_104:.*]] = fir.convert %[[VAL_0]] : (index) -> i64
+! CHECK:         %[[VAL_105:.*]] = fir.call @_FortranAioOutputAscii(%[[VAL_101]], %[[VAL_103]], %[[VAL_104]]) : (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
+! CHECK:         %[[VAL_106:.*]] = fir.load %[[VAL_96]] : !fir.ref<f32>
+! CHECK:         %[[VAL_107:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_101]], %[[VAL_106]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_108:.*]] = fir.load %[[VAL_94]] : !fir.ref<f32>
+! CHECK:         %[[VAL_109:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_101]], %[[VAL_108]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_110:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_101]]) : (!fir.ref<i8>) -> i32
+! CHECK:         br ^bb13
+! CHECK:       ^bb13:
+! CHECK:         %[[VAL_111:.*]] = fir.coordinate_of %[[VAL_25]], %[[VAL_21]], %[[VAL_14]] : (!fir.ref<!fir.array<10x10xf32>>, i64, i64) -> !fir.ref<f32>
+! CHECK:         %[[VAL_112:.*]] = fir.load %[[VAL_111]] : !fir.ref<f32>
+! CHECK:         %[[VAL_113:.*]] = fir.coordinate_of %[[VAL_26]], %[[VAL_13]] : (!fir.ref<!fir.array<3xf32>>, i64) -> !fir.ref<f32>
+! CHECK:         %[[VAL_114:.*]] = fir.load %[[VAL_113]] : !fir.ref<f32>
+! CHECK:         %[[VAL_115:.*]] = arith.cmpf une, %[[VAL_112]], %[[VAL_114]] : f32
+! CHECK:         cond_br %[[VAL_115]], ^bb14, ^bb15
+! CHECK:       ^bb14:
+! CHECK:         %[[VAL_116:.*]] = fir.address_of(@_QQcl.{{.*}} : !fir.ref<!fir.char<1,
+! CHECK:         %[[VAL_117:.*]] = fir.convert %[[VAL_116]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_118:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_18]], %[[VAL_117]], %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_119:.*]] = fir.address_of(@_QQcl.6D69736D617463682033) : !fir.ref<!fir.char<1,10>>
+! CHECK:         %[[VAL_120:.*]] = fir.convert %[[VAL_119]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_121:.*]] = fir.convert %[[VAL_0]] : (index) -> i64
+! CHECK:         %[[VAL_122:.*]] = fir.call @_FortranAioOutputAscii(%[[VAL_118]], %[[VAL_120]], %[[VAL_121]]) : (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
+! CHECK:         %[[VAL_123:.*]] = fir.load %[[VAL_113]] : !fir.ref<f32>
+! CHECK:         %[[VAL_124:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_118]], %[[VAL_123]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_125:.*]] = fir.load %[[VAL_111]] : !fir.ref<f32>
+! CHECK:         %[[VAL_126:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_118]], %[[VAL_125]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_127:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_118]]) : (!fir.ref<i8>) -> i32
+! CHECK:         br ^bb15
+! CHECK:       ^bb15:
+! CHECK:         %[[VAL_128:.*]] = fir.shape %[[VAL_0]] : (index) -> !fir.shape<1>
+! CHECK:         %[[VAL_129:.*]] = fir.slice %[[VAL_5]], %[[VAL_0]], %[[VAL_22]] : (index, index, index) -> !fir.slice<1>
+! CHECK:         br ^bb16(%[[VAL_6]], %[[VAL_11]] : index, index)
+! CHECK:       ^bb16(%[[VAL_130:.*]]: index, %[[VAL_131:.*]]: index):
+! CHECK:         %[[VAL_132:.*]] = arith.cmpi sgt, %[[VAL_131]], %[[VAL_6]] : index
+! CHECK:         cond_br %[[VAL_132]], ^bb17, ^bb18
+! CHECK:       ^bb17:
+! CHECK:         %[[VAL_133:.*]] = arith.addi %[[VAL_130]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_134:.*]] = fir.array_coor %[[VAL_26]](%[[VAL_65]]) %[[VAL_133]] : (!fir.ref<!fir.array<3xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_135:.*]] = fir.load %[[VAL_134]] : !fir.ref<f32>
+! CHECK:         %[[VAL_136:.*]] = fir.array_coor %[[VAL_27]](%[[VAL_128]]) {{\[}}%[[VAL_129]]] %[[VAL_133]] : (!fir.ref<!fir.array<10xf32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_135]] to %[[VAL_136]] : !fir.ref<f32>
+! CHECK:         %[[VAL_137:.*]] = arith.subi %[[VAL_131]], %[[VAL_5]] : index
+! CHECK:         br ^bb16(%[[VAL_133]], %[[VAL_137]] : index, index)
+! CHECK:       ^bb18:
+! CHECK:         %[[VAL_138:.*]] = fir.load %[[VAL_77]] : !fir.ref<f32>
+! CHECK:         %[[VAL_139:.*]] = fir.coordinate_of %[[VAL_27]], %[[VAL_19]] : (!fir.ref<!fir.array<10xf32>>, i64) -> !fir.ref<f32>
+! CHECK:         %[[VAL_140:.*]] = fir.load %[[VAL_139]] : !fir.ref<f32>
+! CHECK:         %[[VAL_141:.*]] = arith.cmpf une, %[[VAL_138]], %[[VAL_140]] : f32
+! CHECK:         cond_br %[[VAL_141]], ^bb19, ^bb20
+! CHECK:       ^bb19:
+! CHECK:         %[[VAL_142:.*]] = fir.address_of(@_QQcl.{{.*}}) : !fir.ref<!fir.char<1,
+! CHECK:         %[[VAL_143:.*]] = fir.convert %[[VAL_142]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_144:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_18]], %[[VAL_143]], %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_145:.*]] = fir.address_of(@_QQcl.6D69736D617463682034) : !fir.ref<!fir.char<1,10>>
+! CHECK:         %[[VAL_146:.*]] = fir.convert %[[VAL_145]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_147:.*]] = fir.convert %[[VAL_0]] : (index) -> i64
+! CHECK:         %[[VAL_148:.*]] = fir.call @_FortranAioOutputAscii(%[[VAL_144]], %[[VAL_146]], %[[VAL_147]]) : (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
+! CHECK:         %[[VAL_149:.*]] = fir.load %[[VAL_77]] : !fir.ref<f32>
+! CHECK:         %[[VAL_150:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_144]], %[[VAL_149]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_151:.*]] = fir.load %[[VAL_139]] : !fir.ref<f32>
+! CHECK:         %[[VAL_152:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_144]], %[[VAL_151]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_153:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_144]]) : (!fir.ref<i8>) -> i32
+! CHECK:         br ^bb20
+! CHECK:       ^bb20:
+! CHECK:         %[[VAL_154:.*]] = fir.load %[[VAL_94]] : !fir.ref<f32>
+! CHECK:         %[[VAL_155:.*]] = fir.coordinate_of %[[VAL_27]], %[[VAL_16]] : (!fir.ref<!fir.array<10xf32>>, i64) -> !fir.ref<f32>
+! CHECK:         %[[VAL_156:.*]] = fir.load %[[VAL_155]] : !fir.ref<f32>
+! CHECK:         %[[VAL_157:.*]] = arith.cmpf une, %[[VAL_154]], %[[VAL_156]] : f32
+! CHECK:         cond_br %[[VAL_157]], ^bb21, ^bb22
+! CHECK:       ^bb21:
+! CHECK:         %[[VAL_158:.*]] = fir.address_of(@_QQcl.{{.*}}) : !fir.ref<!fir.char<1,
+! CHECK:         %[[VAL_159:.*]] = fir.convert %[[VAL_158]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_160:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_18]], %[[VAL_159]], %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_161:.*]] = fir.address_of(@_QQcl.6D69736D617463682035) : !fir.ref<!fir.char<1,10>>
+! CHECK:         %[[VAL_162:.*]] = fir.convert %[[VAL_161]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_163:.*]] = fir.convert %[[VAL_0]] : (index) -> i64
+! CHECK:         %[[VAL_164:.*]] = fir.call @_FortranAioOutputAscii(%[[VAL_160]], %[[VAL_162]], %[[VAL_163]]) : (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
+! CHECK:         %[[VAL_165:.*]] = fir.load %[[VAL_94]] : !fir.ref<f32>
+! CHECK:         %[[VAL_166:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_160]], %[[VAL_165]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_167:.*]] = fir.load %[[VAL_155]] : !fir.ref<f32>
+! CHECK:         %[[VAL_168:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_160]], %[[VAL_167]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_169:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_160]]) : (!fir.ref<i8>) -> i32
+! CHECK:         br ^bb22
+! CHECK:       ^bb22:
+! CHECK:         %[[VAL_170:.*]] = fir.load %[[VAL_111]] : !fir.ref<f32>
+! CHECK:         %[[VAL_171:.*]] = fir.coordinate_of %[[VAL_27]], %[[VAL_8]] : (!fir.ref<!fir.array<10xf32>>, i64) -> !fir.ref<f32>
+! CHECK:         %[[VAL_172:.*]] = fir.load %[[VAL_171]] : !fir.ref<f32>
+! CHECK:         %[[VAL_173:.*]] = arith.cmpf une, %[[VAL_170]], %[[VAL_172]] : f32
+! CHECK:         cond_br %[[VAL_173]], ^bb23, ^bb24
+! CHECK:       ^bb23:
+! CHECK:         %[[VAL_174:.*]] = fir.address_of(@_QQcl.{{.*}}) : !fir.ref<!fir.char<1,
+! CHECK:         %[[VAL_175:.*]] = fir.convert %[[VAL_174]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_176:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_18]], %[[VAL_175]], %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_177:.*]] = fir.address_of(@_QQcl.6D69736D617463682036) : !fir.ref<!fir.char<1,10>>
+! CHECK:         %[[VAL_178:.*]] = fir.convert %[[VAL_177]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_179:.*]] = fir.convert %[[VAL_0]] : (index) -> i64
+! CHECK:         %[[VAL_180:.*]] = fir.call @_FortranAioOutputAscii(%[[VAL_176]], %[[VAL_178]], %[[VAL_179]]) : (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
+! CHECK:         %[[VAL_181:.*]] = fir.load %[[VAL_111]] : !fir.ref<f32>
+! CHECK:         %[[VAL_182:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_176]], %[[VAL_181]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_183:.*]] = fir.load %[[VAL_171]] : !fir.ref<f32>
+! CHECK:         %[[VAL_184:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_176]], %[[VAL_183]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_185:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_176]]) : (!fir.ref<i8>) -> i32
+! CHECK:         br ^bb24
+! CHECK:       ^bb24:
+! CHECK:         %[[VAL_186:.*]] = fir.address_of(@_QQro.3xi4.b7f1b733471804c07debf489e49d9c2f) : !fir.ref<!fir.array<3xi32>>
+! CHECK:         br ^bb25(%[[VAL_6]], %[[VAL_11]] : index, index)
+! CHECK:       ^bb25(%[[VAL_187:.*]]: index, %[[VAL_188:.*]]: index):
+! CHECK:         %[[VAL_189:.*]] = arith.cmpi sgt, %[[VAL_188]], %[[VAL_6]] : index
+! CHECK:         cond_br %[[VAL_189]], ^bb26, ^bb27
+! CHECK:       ^bb26:
+! CHECK:         %[[VAL_190:.*]] = arith.addi %[[VAL_187]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_191:.*]] = fir.array_coor %[[VAL_186]](%[[VAL_65]]) %[[VAL_190]] : (!fir.ref<!fir.array<3xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:         %[[VAL_192:.*]] = fir.load %[[VAL_191]] : !fir.ref<i32>
+! CHECK:         %[[VAL_193:.*]] = fir.array_coor %[[VAL_29]](%[[VAL_65]]) %[[VAL_190]] : (!fir.ref<!fir.array<3xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:         fir.store %[[VAL_192]] to %[[VAL_193]] : !fir.ref<i32>
+! CHECK:         %[[VAL_194:.*]] = arith.subi %[[VAL_188]], %[[VAL_5]] : index
+! CHECK:         br ^bb25(%[[VAL_190]], %[[VAL_194]] : index, index)
+! CHECK:       ^bb27:
+! CHECK:         %[[VAL_195:.*]] = fir.allocmem !fir.array<3xf32>
+! CHECK:         br ^bb28(%[[VAL_6]], %[[VAL_11]] : index, index)
+! CHECK:       ^bb28(%[[VAL_196:.*]]: index, %[[VAL_197:.*]]: index):
+! CHECK:         %[[VAL_198:.*]] = arith.cmpi sgt, %[[VAL_197]], %[[VAL_6]] : index
+! CHECK:         cond_br %[[VAL_198]], ^bb29, ^bb30
+! CHECK:       ^bb29:
+! CHECK:         %[[VAL_199:.*]] = arith.addi %[[VAL_196]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_200:.*]] = fir.array_coor %[[VAL_26]](%[[VAL_65]]) %[[VAL_199]] : (!fir.ref<!fir.array<3xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_201:.*]] = fir.array_coor %[[VAL_195]](%[[VAL_65]]) %[[VAL_199]] : (!fir.heap<!fir.array<3xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_202:.*]] = fir.load %[[VAL_200]] : !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_202]] to %[[VAL_201]] : !fir.ref<f32>
+! CHECK:         %[[VAL_203:.*]] = arith.subi %[[VAL_197]], %[[VAL_5]] : index
+! CHECK:         br ^bb28(%[[VAL_199]], %[[VAL_203]] : index, index)
+! CHECK:       ^bb30(%[[VAL_205:.*]]: index, %[[VAL_206:.*]]: index):
+! CHECK:         %[[VAL_207:.*]] = arith.cmpi sgt, %[[VAL_206]], %[[VAL_6]] : index
+! CHECK:         cond_br %[[VAL_207]], ^bb31, ^bb32(%[[VAL_6]], %[[VAL_11]] : index, index)
+! CHECK:       ^bb31:
+! CHECK:         %[[VAL_208:.*]] = arith.addi %[[VAL_205]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_209:.*]] = fir.array_coor %[[VAL_29]](%[[VAL_65]]) %[[VAL_208]] : (!fir.ref<!fir.array<3xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
+! CHECK:         %[[VAL_210:.*]] = fir.load %[[VAL_209]] : !fir.ref<i32>
+! CHECK:         %[[VAL_211:.*]] = fir.convert %[[VAL_210]] : (i32) -> index
+! CHECK:         %[[VAL_212:.*]] = fir.array_coor %[[VAL_26]](%[[VAL_65]]) %[[VAL_211]] : (!fir.ref<!fir.array<3xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_213:.*]] = fir.load %[[VAL_212]] : !fir.ref<f32>
+! CHECK:         %[[VAL_214:.*]] = fir.array_coor %[[VAL_195]](%[[VAL_65]]) %[[VAL_208]] : (!fir.heap<!fir.array<3xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_213]] to %[[VAL_214]] : !fir.ref<f32>
+! CHECK:         %[[VAL_215:.*]] = arith.subi %[[VAL_206]], %[[VAL_5]] : index
+! CHECK:         br ^bb30(%[[VAL_208]], %[[VAL_215]] : index, index)
+! CHECK:       ^bb32(%[[VAL_216:.*]]: index, %[[VAL_217:.*]]: index):
+! CHECK:         %[[VAL_218:.*]] = arith.cmpi sgt, %[[VAL_217]], %[[VAL_6]] : index
+! CHECK:         cond_br %[[VAL_218]], ^bb33, ^bb34
+! CHECK:       ^bb33:
+! CHECK:         %[[VAL_219:.*]] = arith.addi %[[VAL_216]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_220:.*]] = fir.array_coor %[[VAL_195]](%[[VAL_65]]) %[[VAL_219]] : (!fir.heap<!fir.array<3xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_221:.*]] = fir.array_coor %[[VAL_26]](%[[VAL_65]]) %[[VAL_219]] : (!fir.ref<!fir.array<3xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_222:.*]] = fir.load %[[VAL_220]] : !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_222]] to %[[VAL_221]] : !fir.ref<f32>
+! CHECK:         %[[VAL_223:.*]] = arith.subi %[[VAL_217]], %[[VAL_5]] : index
+! CHECK:         br ^bb32(%[[VAL_219]], %[[VAL_223]] : index, index)
+! CHECK:       ^bb34:
+! CHECK:         fir.freemem %[[VAL_195]] : !fir.heap<!fir.array<3xf32>>
+! CHECK:         %[[VAL_224:.*]] = fir.load %[[VAL_77]] : !fir.ref<f32>
+! CHECK:         %[[VAL_225:.*]] = fir.load %[[VAL_96]] : !fir.ref<f32>
+! CHECK:         %[[VAL_226:.*]] = arith.cmpf une, %[[VAL_224]], %[[VAL_225]] : f32
+! CHECK:         cond_br %[[VAL_226]], ^bb35, ^bb36
+! CHECK:       ^bb35:
+! CHECK:         %[[VAL_227:.*]] = fir.address_of(@_QQcl.{{.*}}) : !fir.ref<!fir.char<1,
+! CHECK:         %[[VAL_228:.*]] = fir.convert %[[VAL_227]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_229:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_18]], %[[VAL_228]], %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_230:.*]] = fir.address_of(@_QQcl.6D69736D617463682037) : !fir.ref<!fir.char<1,10>>
+! CHECK:         %[[VAL_231:.*]] = fir.convert %[[VAL_230]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_232:.*]] = fir.convert %[[VAL_0]] : (index) -> i64
+! CHECK:         %[[VAL_233:.*]] = fir.call @_FortranAioOutputAscii(%[[VAL_229]], %[[VAL_231]], %[[VAL_232]]) : (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
+! CHECK:         %[[VAL_234:.*]] = fir.load %[[VAL_77]] : !fir.ref<f32>
+! CHECK:         %[[VAL_235:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_229]], %[[VAL_234]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_236:.*]] = fir.load %[[VAL_96]] : !fir.ref<f32>
+! CHECK:         %[[VAL_237:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_229]], %[[VAL_236]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_238:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_229]]) : (!fir.ref<i8>) -> i32
+! CHECK:         br ^bb36
+! CHECK:       ^bb36:
+! CHECK:         %[[VAL_239:.*]] = fir.load %[[VAL_94]] : !fir.ref<f32>
+! CHECK:         %[[VAL_240:.*]] = fir.load %[[VAL_113]] : !fir.ref<f32>
+! CHECK:         %[[VAL_241:.*]] = arith.cmpf une, %[[VAL_239]], %[[VAL_240]] : f32
+! CHECK:         cond_br %[[VAL_241]], ^bb37, ^bb38
+! CHECK:       ^bb37:
+! CHECK:         %[[VAL_242:.*]] = fir.address_of(@_QQcl.{{.*}}) : !fir.ref<!fir.char<1,
+! CHECK:         %[[VAL_243:.*]] = fir.convert %[[VAL_242]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_244:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_18]], %[[VAL_243]], %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_245:.*]] = fir.address_of(@_QQcl.6D69736D617463682038) : !fir.ref<!fir.char<1,10>>
+! CHECK:         %[[VAL_246:.*]] = fir.convert %[[VAL_245]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_247:.*]] = fir.convert %[[VAL_0]] : (index) -> i64
+! CHECK:         %[[VAL_248:.*]] = fir.call @_FortranAioOutputAscii(%[[VAL_244]], %[[VAL_246]], %[[VAL_247]]) : (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
+! CHECK:         %[[VAL_249:.*]] = fir.load %[[VAL_94]] : !fir.ref<f32>
+! CHECK:         %[[VAL_250:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_244]], %[[VAL_249]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_251:.*]] = fir.load %[[VAL_113]] : !fir.ref<f32>
+! CHECK:         %[[VAL_252:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_244]], %[[VAL_251]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_253:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_244]]) : (!fir.ref<i8>) -> i32
+! CHECK:         br ^bb38
+! CHECK:       ^bb38:
+! CHECK:         %[[VAL_254:.*]] = fir.load %[[VAL_111]] : !fir.ref<f32>
+! CHECK:         %[[VAL_255:.*]] = fir.load %[[VAL_79]] : !fir.ref<f32>
+! CHECK:         %[[VAL_256:.*]] = arith.cmpf une, %[[VAL_254]], %[[VAL_255]] : f32
+! CHECK:         cond_br %[[VAL_256]], ^bb39, ^bb40
+! CHECK:       ^bb39:
+! CHECK:         %[[VAL_257:.*]] = fir.address_of(@_QQcl.{{.*}}) : !fir.ref<!fir.char<1,
+! CHECK:         %[[VAL_258:.*]] = fir.convert %[[VAL_257]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_259:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_18]], %[[VAL_258]], %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_260:.*]] = fir.address_of(@_QQcl.6D69736D617463682039) : !fir.ref<!fir.char<1,10>>
+! CHECK:         %[[VAL_261:.*]] = fir.convert %[[VAL_260]] : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_262:.*]] = fir.convert %[[VAL_0]] : (index) -> i64
+! CHECK:         %[[VAL_263:.*]] = fir.call @_FortranAioOutputAscii(%[[VAL_259]], %[[VAL_261]], %[[VAL_262]]) : (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
+! CHECK:         %[[VAL_264:.*]] = fir.load %[[VAL_111]] : !fir.ref<f32>
+! CHECK:         %[[VAL_265:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_259]], %[[VAL_264]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_266:.*]] = fir.load %[[VAL_79]] : !fir.ref<f32>
+! CHECK:         %[[VAL_267:.*]] = fir.call @_FortranAioOutputReal32(%[[VAL_259]], %[[VAL_266]]) : (!fir.ref<i8>, f32) -> i1
+! CHECK:         %[[VAL_268:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_259]]) : (!fir.ref<i8>) -> i32
+! CHECK:         br ^bb40
+! CHECK:       ^bb40:
+! CHECK:         return
+! CHECK:       }
+
+program p
+  real :: a1(10,10)
+  real :: a2(3)
+  real :: a3(10)
+  integer iv(3)
+  integer k
+
+  k = 0
+  do j = 1, 10
+     do i = 1, 10
+        k = k + 1
+        a1(i,j) = cos(real(k))
+     end do
+     a3(j) = sin(real(k))
+  end do
+
+  a2 = a1(4, 2:10:3)
+
+  if (a1(4,2) .ne. a2(1)) print *, "mismatch 1", a2(1), a1(4,2)
+  if (a1(4,5) .ne. a2(2)) print *, "mismatch 2", a2(2), a1(4,5)
+  if (a1(4,8) .ne. a2(3)) print *, "mismatch 3", a2(3), a1(4,8)
+
+  a3(1:10:4) = a2
+
+  if (a1(4,2) .ne. a3(1)) print *, "mismatch 4", a1(4,2), a3(1)
+  if (a1(4,5) .ne. a3(5)) print *, "mismatch 5", a1(4,5), a3(5)
+  if (a1(4,8) .ne. a3(9)) print *, "mismatch 6", a1(4,8), a3(9)
+
+  iv = (/ 3, 1, 2 /)
+
+  a2 = a2(iv)
+
+  if (a1(4,2) .ne. a2(2)) print *, "mismatch 7", a1(4,2), a2(2)
+  if (a1(4,5) .ne. a2(3)) print *, "mismatch 8", a1(4,5), a2(3)
+  if (a1(4,8) .ne. a2(1)) print *, "mismatch 9", a1(4,8), a2(1)
+
+end program p
+
+! CHECK-LABEL: func @_QPsub(
+! CHECK-SAME:               %[[VAL_0:.*]]: !fir.boxchar<1>{{.*}}) {
+! CHECK-DAG:     %[[VAL_1:.*]] = arith.constant 5 : index
+! CHECK-DAG:     %[[VAL_2:.*]] = arith.constant 2 : index
+! CHECK-DAG:     %[[VAL_3:.*]] = arith.constant 1 : index
+! CHECK-DAG:     %[[VAL_4:.*]] = arith.constant 4 : index
+! CHECK-DAG:     %[[VAL_6:.*]] = arith.constant -1 : i32
+! CHECK-DAG:     %[[VAL_7:.*]] = arith.constant 10 : index
+! CHECK:         %[[VAL_8:.*]]:2 = fir.unboxchar %[[VAL_0]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+! CHECK:         %[[VAL_9:.*]] = fir.convert %[[VAL_8]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<10x!fir.char<1>>>
+! CHECK:         %[[VAL_10:.*]] = fir.address_of(@_QQcl.{{.*}}) : !fir.ref<!fir.char<1,
+! CHECK:         %[[VAL_11:.*]] = fir.convert %[[VAL_10]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_12:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_6]], %[[VAL_11]], %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_13:.*]] = fir.address_of(@_QQcl.61203D20) : !fir.ref<!fir.char<1,4>>
+! CHECK:         %[[VAL_14:.*]] = fir.convert %[[VAL_13]] : (!fir.ref<!fir.char<1,4>>) -> !fir.ref<i8>
+! CHECK:         %[[VAL_15:.*]] = fir.convert %[[VAL_4]] : (index) -> i64
+! CHECK:         %[[VAL_16:.*]] = fir.call @_FortranAioOutputAscii(%[[VAL_12]], %[[VAL_14]], %[[VAL_15]]) : (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
+! CHECK:         %[[VAL_17:.*]] = fir.shape %[[VAL_7]] : (index) -> !fir.shape<1>
+! CHECK:         %[[VAL_18:.*]] = fir.slice %[[VAL_3]], %[[VAL_1]], %[[VAL_2]] : (index, index, index) -> !fir.slice<1>
+! CHECK:         %[[VAL_19:.*]] = fir.embox %[[VAL_9]](%[[VAL_17]]) {{\[}}%[[VAL_18]]] : (!fir.ref<!fir.array<10x!fir.char<1>>>, !fir.shape<1>, !fir.slice<1>) -> !fir.box<!fir.array<?x!fir.char<1>>>
+! CHECK:         %[[VAL_20:.*]] = fir.convert %[[VAL_19]] : (!fir.box<!fir.array<?x!fir.char<1>>>) -> !fir.box<none>
+! CHECK:         %[[VAL_21:.*]] = fir.call @_FortranAioOutputDescriptor(%[[VAL_12]], %[[VAL_20]]) : (!fir.ref<i8>, !fir.box<none>) -> i1
+! CHECK:         %[[VAL_22:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_12]]) : (!fir.ref<i8>) -> i32
+! CHECK:         return
+! CHECK:       }
+
+! Slice operation on array of CHARACTER
+subroutine sub(a)
+  character :: a(10)
+  print *, "a = ", a(1:5:2)
+end subroutine sub

diff  --git a/flang/test/Lower/array-temp.f90 b/flang/test/Lower/array-temp.f90
new file mode 100644
index 0000000000000..4a58ae5315f44
--- /dev/null
+++ b/flang/test/Lower/array-temp.f90
@@ -0,0 +1,413 @@
+! RUN: bbc %s -o - | FileCheck %s
+
+! CHECK-LABEL: func @_QPss1()
+subroutine ss1
+  ! CHECK: %[[aa:[0-9]+]] = fir.alloca !fir.array<2650000xf32> {bindc_name = "aa", uniq_name = "_QFss1Eaa"}
+  ! CHECK: %[[shape:[0-9]+]] = fir.shape {{.*}} : (index) -> !fir.shape<1>
+  integer, parameter :: N = 2650000
+  real aa(N)
+  ! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}} : (!fir.ref<!fir.array<2650000xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+  aa = -2
+  ! CHECK: %[[temp:[0-9]+]] = fir.allocmem !fir.array<2650000xf32>
+  ! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}} : (!fir.ref<!fir.array<2650000xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+  ! CHECK: fir.array_coor %[[temp]](%[[shape]]) {{.*}} : (!fir.heap<!fir.array<2650000xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+  ! CHECK: fir.array_coor %[[aa]](%[[shape]]) [{{.*}}] {{.*}} : (!fir.ref<!fir.array<2650000xf32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<f32>
+  ! CHECK: fir.array_coor %[[temp]](%[[shape]]) [{{.*}}] {{.*}} : (!fir.heap<!fir.array<2650000xf32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<f32>
+  ! CHECK: fir.array_coor %[[temp]](%[[shape]]) {{.*}} : (!fir.heap<!fir.array<2650000xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+  ! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}} : (!fir.ref<!fir.array<2650000xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+  ! CHECK: fir.freemem %[[temp]] : !fir.heap<!fir.array<2650000xf32>>
+  aa(2:N) = aa(1:N-1) + 7.0
+! print*, aa(1:2), aa(N-1:N)
+end
+
+subroutine ss2(N)
+  real aa(N)
+  aa = -2
+  aa(2:N) = aa(1:N-1) + 7.0
+  print*, aa(1:2), aa(N-1:N)
+end
+
+subroutine ss3(N)
+  real aa(2,N)
+  aa = -2
+  aa(:,2:N) = aa(:,1:N-1) + 7.0
+  print*, aa(:,1:2), aa(:,N-1:N)
+end
+
+subroutine ss4(N)
+  real aa(N,2)
+  aa = -2
+  aa(2:N,:) = aa(1:N-1,:) + 7.0
+  print*, aa(1:2,:), aa(N-1:N,:)
+end
+
+! CHECK-LABEL: func @_QPss2(
+! CHECK-SAME:               %[[VAL_0:.*]]: !fir.ref<i32> {fir.bindc_name = "n"}) {
+! CHECK-DAG:     %[[VAL_1:.*]] = arith.constant -1 : index
+! CHECK-DAG:     %[[VAL_2:.*]] = arith.constant -2 : i32
+! CHECK-DAG:     %[[VAL_3:.*]] = arith.constant 1 : index
+! CHECK-DAG:     %[[VAL_4:.*]] = arith.constant 0 : index
+! CHECK-DAG:     %[[VAL_5:.*]] = arith.constant 2 : index
+! CHECK-DAG:     %[[VAL_6:.*]] = arith.constant 1 : i32
+! CHECK-DAG:     %[[VAL_7:.*]] = arith.constant 7.000000e+00 : f32
+! CHECK-DAG:     %[[VAL_8:.*]] = arith.constant -1 : i32
+! CHECK:         %[[VAL_10:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
+! CHECK:         %[[VAL_11A:.*]] = fir.convert %[[VAL_10]] : (i32) -> index
+! CHECK:         %[[CMP:.*]] = arith.cmpi sgt, %[[VAL_11A]], %[[VAL_4]] : index 
+! CHECK:         %[[VAL_11:.*]] = arith.select %[[CMP]], %[[VAL_11A]], %[[VAL_4]] : index 
+! CHECK:         %[[VAL_12:.*]] = fir.alloca !fir.array<?xf32>, %[[VAL_11]] {bindc_name = "aa", uniq_name = "_QFss2Eaa"}
+! CHECK:         %[[VAL_13:.*]] = fir.shape %[[VAL_11]] : (index) -> !fir.shape<1>
+! CHECK:         %[[VAL_14:.*]] = fir.convert %[[VAL_2]] : (i32) -> f32
+! CHECK:         br ^bb1(%[[VAL_4]], %[[VAL_11]] : index, index)
+! CHECK:       ^bb1(%[[VAL_15:.*]]: index, %[[VAL_16:.*]]: index):
+! CHECK:         %[[VAL_17:.*]] = arith.cmpi sgt, %[[VAL_16]], %[[VAL_4]] : index
+! CHECK:         cond_br %[[VAL_17]], ^bb2, ^bb3
+! CHECK:       ^bb2:
+! CHECK:         %[[VAL_18:.*]] = arith.addi %[[VAL_15]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_19:.*]] = fir.array_coor %[[VAL_12]](%[[VAL_13]]) %[[VAL_18]] : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_14]] to %[[VAL_19]] : !fir.ref<f32>
+! CHECK:         %[[VAL_20:.*]] = arith.subi %[[VAL_16]], %[[VAL_3]] : index
+! CHECK:         br ^bb1(%[[VAL_18]], %[[VAL_20]] : index, index)
+! CHECK:       ^bb3:
+! CHECK:         %[[VAL_21:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
+! CHECK:         %[[VAL_22:.*]] = fir.convert %[[VAL_21]] : (i32) -> index
+! CHECK:         %[[VAL_23:.*]] = arith.addi %[[VAL_22]], %[[VAL_1]] : index
+! CHECK:         %[[VAL_24:.*]] = arith.cmpi sgt, %[[VAL_23]], %[[VAL_4]] : index
+! CHECK:         %[[VAL_25:.*]] = arith.select %[[VAL_24]], %[[VAL_23]], %[[VAL_4]] : index
+! CHECK:         %[[VAL_26:.*]] = fir.slice %[[VAL_5]], %[[VAL_22]], %[[VAL_3]] : (index, index, index) -> !fir.slice<1>
+! CHECK:         %[[VAL_27:.*]] = fir.allocmem !fir.array<?xf32>, %[[VAL_11]]
+! CHECK:         br ^bb4(%[[VAL_4]], %[[VAL_11]] : index, index)
+! CHECK:       ^bb4(%[[VAL_28:.*]]: index, %[[VAL_29:.*]]: index):
+! CHECK:         %[[VAL_30:.*]] = arith.cmpi sgt, %[[VAL_29]], %[[VAL_4]] : index
+! CHECK:         cond_br %[[VAL_30]], ^bb5, ^bb6
+! CHECK:       ^bb5:
+! CHECK:         %[[VAL_31:.*]] = arith.addi %[[VAL_28]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_32:.*]] = fir.array_coor %[[VAL_12]](%[[VAL_13]]) %[[VAL_31]] : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_33:.*]] = fir.array_coor %[[VAL_27]](%[[VAL_13]]) %[[VAL_31]] : (!fir.heap<!fir.array<?xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_34:.*]] = fir.load %[[VAL_32]] : !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_34]] to %[[VAL_33]] : !fir.ref<f32>
+! CHECK:         %[[VAL_35:.*]] = arith.subi %[[VAL_29]], %[[VAL_3]] : index
+! CHECK:         br ^bb4(%[[VAL_31]], %[[VAL_35]] : index, index)
+! CHECK:       ^bb6:
+! CHECK:         %[[VAL_36:.*]] = arith.subi %[[VAL_21]], %[[VAL_6]] : i32
+! CHECK:         %[[VAL_37:.*]] = fir.convert %[[VAL_36]] : (i32) -> index
+! CHECK:         %[[VAL_38:.*]] = fir.slice %[[VAL_3]], %[[VAL_37]], %[[VAL_3]] : (index, index, index) -> !fir.slice<1>
+! CHECK:         br ^bb7(%[[VAL_4]], %[[VAL_25]] : index, index)
+! CHECK:       ^bb7(%[[VAL_39:.*]]: index, %[[VAL_40:.*]]: index):
+! CHECK:         %[[VAL_41:.*]] = arith.cmpi sgt, %[[VAL_40]], %[[VAL_4]] : index
+! CHECK:         cond_br %[[VAL_41]], ^bb8, ^bb9(%[[VAL_4]], %[[VAL_11]] : index, index)
+! CHECK:       ^bb8:
+! CHECK:         %[[VAL_42:.*]] = arith.addi %[[VAL_39]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_43:.*]] = fir.array_coor %[[VAL_12]](%[[VAL_13]]) {{\[}}%[[VAL_38]]] %[[VAL_42]] : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_44:.*]] = fir.load %[[VAL_43]] : !fir.ref<f32>
+! CHECK:         %[[VAL_45:.*]] = arith.addf %[[VAL_44]], %[[VAL_7]] : f32
+! CHECK:         %[[VAL_46:.*]] = fir.array_coor %[[VAL_27]](%[[VAL_13]]) {{\[}}%[[VAL_26]]] %[[VAL_42]] : (!fir.heap<!fir.array<?xf32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_45]] to %[[VAL_46]] : !fir.ref<f32>
+! CHECK:         %[[VAL_47:.*]] = arith.subi %[[VAL_40]], %[[VAL_3]] : index
+! CHECK:         br ^bb7(%[[VAL_42]], %[[VAL_47]] : index, index)
+! CHECK:       ^bb9(%[[VAL_48:.*]]: index, %[[VAL_49:.*]]: index):
+! CHECK:         %[[VAL_50:.*]] = arith.cmpi sgt, %[[VAL_49]], %[[VAL_4]] : index
+! CHECK:         cond_br %[[VAL_50]], ^bb10, ^bb11
+! CHECK:       ^bb10:
+! CHECK:         %[[VAL_51:.*]] = arith.addi %[[VAL_48]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_52:.*]] = fir.array_coor %[[VAL_27]](%[[VAL_13]]) %[[VAL_51]] : (!fir.heap<!fir.array<?xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_53:.*]] = fir.array_coor %[[VAL_12]](%[[VAL_13]]) %[[VAL_51]] : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_54:.*]] = fir.load %[[VAL_52]] : !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_54]] to %[[VAL_53]] : !fir.ref<f32>
+! CHECK:         %[[VAL_55:.*]] = arith.subi %[[VAL_49]], %[[VAL_3]] : index
+! CHECK:         br ^bb9(%[[VAL_51]], %[[VAL_55]] : index, index)
+! CHECK:       ^bb11:
+! CHECK:         fir.freemem %[[VAL_27]] : !fir.heap<!fir.array<?xf32>>
+! CHECK:         %[[VAL_58:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_8]], %{{.*}}, %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_59:.*]] = fir.slice %[[VAL_3]], %[[VAL_5]], %[[VAL_3]] : (index, index, index) -> !fir.slice<1>
+! CHECK:         %[[VAL_60:.*]] = fir.embox %[[VAL_12]](%[[VAL_13]]) {{\[}}%[[VAL_59]]] : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.box<!fir.array<?xf32>>
+! CHECK:         %[[VAL_61:.*]] = fir.convert %[[VAL_60]] : (!fir.box<!fir.array<?xf32>>) -> !fir.box<none>
+! CHECK:         %[[VAL_62:.*]] = fir.call @_FortranAioOutputDescriptor(%[[VAL_58]], %[[VAL_61]]) : (!fir.ref<i8>, !fir.box<none>) -> i1
+! CHECK:         %[[VAL_63:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
+! CHECK:         %[[VAL_64:.*]] = arith.subi %[[VAL_63]], %[[VAL_6]] : i32
+! CHECK:         %[[VAL_65:.*]] = fir.convert %[[VAL_64]] : (i32) -> index
+! CHECK:         %[[VAL_66:.*]] = fir.convert %[[VAL_63]] : (i32) -> index
+! CHECK:         %[[VAL_67:.*]] = fir.slice %[[VAL_65]], %[[VAL_66]], %[[VAL_3]] : (index, index, index) -> !fir.slice<1>
+! CHECK:         %[[VAL_68:.*]] = fir.embox %[[VAL_12]](%[[VAL_13]]) {{\[}}%[[VAL_67]]] : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.box<!fir.array<?xf32>>
+! CHECK:         %[[VAL_69:.*]] = fir.convert %[[VAL_68]] : (!fir.box<!fir.array<?xf32>>) -> !fir.box<none>
+! CHECK:         %[[VAL_70:.*]] = fir.call @_FortranAioOutputDescriptor(%[[VAL_58]], %[[VAL_69]]) : (!fir.ref<i8>, !fir.box<none>) -> i1
+! CHECK:         %[[VAL_71:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_58]]) : (!fir.ref<i8>) -> i32
+! CHECK:         return
+! CHECK:       }
+
+! CHECK-LABEL: func @_QPss3(
+! CHECK-SAME:               %[[VAL_0:.*]]: !fir.ref<i32> {fir.bindc_name = "n"}) {
+! CHECK-DAG:     %[[VAL_1:.*]] = arith.constant -1 : index
+! CHECK-DAG:     %[[VAL_2:.*]] = arith.constant 2 : index
+! CHECK-DAG:     %[[VAL_3:.*]] = arith.constant 1 : index
+! CHECK-DAG:     %[[VAL_4:.*]] = arith.constant -2 : i32
+! CHECK-DAG:     %[[VAL_5:.*]] = arith.constant 0 : index
+! CHECK-DAG:     %[[VAL_6:.*]] = arith.constant 1 : i32
+! CHECK-DAG:     %[[VAL_7:.*]] = arith.constant 7.000000e+00 : f32
+! CHECK-DAG:     %[[VAL_8:.*]] = arith.constant -1 : i32
+! CHECK:         %[[VAL_10:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
+! CHECK:         %[[VAL_11:.*]] = fir.convert %[[VAL_10]] : (i32) -> index
+! CHECK:         %[[CMP:.*]] = arith.cmpi sgt, %[[VAL_11A]], %[[VAL_5]] : index 
+! CHECK:         %[[VAL_11:.*]] = arith.select %[[CMP]], %[[VAL_11A]], %[[VAL_5]] : index 
+! CHECK:         %[[VAL_12:.*]] = fir.alloca !fir.array<2x?xf32>, %[[VAL_11]] {bindc_name = "aa", uniq_name = "_QFss3Eaa"}
+! CHECK:         %[[VAL_13:.*]] = fir.shape %[[VAL_2]], %[[VAL_11]] : (index, index) -> !fir.shape<2>
+! CHECK:         %[[VAL_14:.*]] = fir.convert %[[VAL_4]] : (i32) -> f32
+! CHECK:         br ^bb1(%[[VAL_5]], %[[VAL_11]] : index, index)
+! CHECK:       ^bb1(%[[VAL_15:.*]]: index, %[[VAL_16:.*]]: index):
+! CHECK:         %[[VAL_17:.*]] = arith.cmpi sgt, %[[VAL_16]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_17]], ^bb2(%[[VAL_5]], %[[VAL_2]] : index, index), ^bb5
+! CHECK:       ^bb2(%[[VAL_18:.*]]: index, %[[VAL_19:.*]]: index):
+! CHECK:         %[[VAL_20:.*]] = arith.cmpi sgt, %[[VAL_19]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_20]], ^bb3, ^bb4
+! CHECK:       ^bb3:
+! CHECK:         %[[VAL_21:.*]] = arith.addi %[[VAL_18]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_22:.*]] = arith.addi %[[VAL_15]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_23:.*]] = fir.array_coor %[[VAL_12]](%[[VAL_13]]) %[[VAL_21]], %[[VAL_22]] : (!fir.ref<!fir.array<2x?xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_14]] to %[[VAL_23]] : !fir.ref<f32>
+! CHECK:         %[[VAL_24:.*]] = arith.subi %[[VAL_19]], %[[VAL_3]] : index
+! CHECK:         br ^bb2(%[[VAL_21]], %[[VAL_24]] : index, index)
+! CHECK:       ^bb4:
+! CHECK:         %[[VAL_25:.*]] = arith.addi %[[VAL_15]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_26:.*]] = arith.subi %[[VAL_16]], %[[VAL_3]] : index
+! CHECK:         br ^bb1(%[[VAL_25]], %[[VAL_26]] : index, index)
+! CHECK:       ^bb5:
+! CHECK:         %[[VAL_27:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
+! CHECK:         %[[VAL_28:.*]] = fir.convert %[[VAL_27]] : (i32) -> index
+! CHECK:         %[[VAL_29:.*]] = arith.addi %[[VAL_28]], %[[VAL_1]] : index
+! CHECK:         %[[VAL_30:.*]] = arith.cmpi sgt, %[[VAL_29]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_31:.*]] = arith.select %[[VAL_30]], %[[VAL_29]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_32:.*]] = fir.slice %[[VAL_3]], %[[VAL_2]], %[[VAL_3]], %[[VAL_2]], %[[VAL_28]], %[[VAL_3]] : (index, index, index, index, index, index) -> !fir.slice<2>
+! CHECK:         %[[VAL_33:.*]] = fir.allocmem !fir.array<2x?xf32>, %[[VAL_11]]
+! CHECK:         br ^bb6(%[[VAL_5]], %[[VAL_11]] : index, index)
+! CHECK:       ^bb6(%[[VAL_34:.*]]: index, %[[VAL_35:.*]]: index):
+! CHECK:         %[[VAL_36:.*]] = arith.cmpi sgt, %[[VAL_35]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_36]], ^bb7(%[[VAL_5]], %[[VAL_2]] : index, index), ^bb10
+! CHECK:       ^bb7(%[[VAL_37:.*]]: index, %[[VAL_38:.*]]: index):
+! CHECK:         %[[VAL_39:.*]] = arith.cmpi sgt, %[[VAL_38]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_39]], ^bb8, ^bb9
+! CHECK:       ^bb8:
+! CHECK:         %[[VAL_40:.*]] = arith.addi %[[VAL_37]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_41:.*]] = arith.addi %[[VAL_34]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_42:.*]] = fir.array_coor %[[VAL_12]](%[[VAL_13]]) %[[VAL_40]], %[[VAL_41]] : (!fir.ref<!fir.array<2x?xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_43:.*]] = fir.array_coor %[[VAL_33]](%[[VAL_13]]) %[[VAL_40]], %[[VAL_41]] : (!fir.heap<!fir.array<2x?xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_44:.*]] = fir.load %[[VAL_42]] : !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_44]] to %[[VAL_43]] : !fir.ref<f32>
+! CHECK:         %[[VAL_45:.*]] = arith.subi %[[VAL_38]], %[[VAL_3]] : index
+! CHECK:         br ^bb7(%[[VAL_40]], %[[VAL_45]] : index, index)
+! CHECK:       ^bb9:
+! CHECK:         %[[VAL_46:.*]] = arith.addi %[[VAL_34]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_47:.*]] = arith.subi %[[VAL_35]], %[[VAL_3]] : index
+! CHECK:         br ^bb6(%[[VAL_46]], %[[VAL_47]] : index, index)
+! CHECK:       ^bb10:
+! CHECK:         %[[VAL_48:.*]] = arith.subi %[[VAL_27]], %[[VAL_6]] : i32
+! CHECK:         %[[VAL_49:.*]] = fir.convert %[[VAL_48]] : (i32) -> index
+! CHECK:         %[[VAL_50:.*]] = fir.slice %[[VAL_3]], %[[VAL_2]], %[[VAL_3]], %[[VAL_3]], %[[VAL_49]], %[[VAL_3]] : (index, index, index, index, index, index) -> !fir.slice<2>
+! CHECK:         br ^bb11(%[[VAL_5]], %[[VAL_31]] : index, index)
+! CHECK:       ^bb11(%[[VAL_51:.*]]: index, %[[VAL_52:.*]]: index):
+! CHECK:         %[[VAL_53:.*]] = arith.cmpi sgt, %[[VAL_52]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_53]], ^bb12(%[[VAL_5]], %[[VAL_2]] : index, index), ^bb15(%[[VAL_5]], %[[VAL_11]] : index, index)
+! CHECK:       ^bb12(%[[VAL_54:.*]]: index, %[[VAL_55:.*]]: index):
+! CHECK:         %[[VAL_56:.*]] = arith.cmpi sgt, %[[VAL_55]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_56]], ^bb13, ^bb14
+! CHECK:       ^bb13:
+! CHECK:         %[[VAL_57:.*]] = arith.addi %[[VAL_54]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_58:.*]] = arith.addi %[[VAL_51]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_59:.*]] = fir.array_coor %[[VAL_12]](%[[VAL_13]]) {{\[}}%[[VAL_50]]] %[[VAL_57]], %[[VAL_58]] : (!fir.ref<!fir.array<2x?xf32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_60:.*]] = fir.load %[[VAL_59]] : !fir.ref<f32>
+! CHECK:         %[[VAL_61:.*]] = arith.addf %[[VAL_60]], %[[VAL_7]] : f32
+! CHECK:         %[[VAL_62:.*]] = fir.array_coor %[[VAL_33]](%[[VAL_13]]) {{\[}}%[[VAL_32]]] %[[VAL_57]], %[[VAL_58]] : (!fir.heap<!fir.array<2x?xf32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_61]] to %[[VAL_62]] : !fir.ref<f32>
+! CHECK:         %[[VAL_63:.*]] = arith.subi %[[VAL_55]], %[[VAL_3]] : index
+! CHECK:         br ^bb12(%[[VAL_57]], %[[VAL_63]] : index, index)
+! CHECK:       ^bb14:
+! CHECK:         %[[VAL_64:.*]] = arith.addi %[[VAL_51]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_65:.*]] = arith.subi %[[VAL_52]], %[[VAL_3]] : index
+! CHECK:         br ^bb11(%[[VAL_64]], %[[VAL_65]] : index, index)
+! CHECK:       ^bb15(%[[VAL_66:.*]]: index, %[[VAL_67:.*]]: index):
+! CHECK:         %[[VAL_68:.*]] = arith.cmpi sgt, %[[VAL_67]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_68]], ^bb16(%[[VAL_5]], %[[VAL_2]] : index, index), ^bb19
+! CHECK:       ^bb16(%[[VAL_69:.*]]: index, %[[VAL_70:.*]]: index):
+! CHECK:         %[[VAL_71:.*]] = arith.cmpi sgt, %[[VAL_70]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_71]], ^bb17, ^bb18
+! CHECK:       ^bb17:
+! CHECK:         %[[VAL_72:.*]] = arith.addi %[[VAL_69]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_73:.*]] = arith.addi %[[VAL_66]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_74:.*]] = fir.array_coor %[[VAL_33]](%[[VAL_13]]) %[[VAL_72]], %[[VAL_73]] : (!fir.heap<!fir.array<2x?xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_75:.*]] = fir.array_coor %[[VAL_12]](%[[VAL_13]]) %[[VAL_72]], %[[VAL_73]] : (!fir.ref<!fir.array<2x?xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_76:.*]] = fir.load %[[VAL_74]] : !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_76]] to %[[VAL_75]] : !fir.ref<f32>
+! CHECK:         %[[VAL_77:.*]] = arith.subi %[[VAL_70]], %[[VAL_3]] : index
+! CHECK:         br ^bb16(%[[VAL_72]], %[[VAL_77]] : index, index)
+! CHECK:       ^bb18:
+! CHECK:         %[[VAL_78:.*]] = arith.addi %[[VAL_66]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_79:.*]] = arith.subi %[[VAL_67]], %[[VAL_3]] : index
+! CHECK:         br ^bb15(%[[VAL_78]], %[[VAL_79]] : index, index)
+! CHECK:       ^bb19:
+! CHECK:         fir.freemem %[[VAL_33]] : !fir.heap<!fir.array<2x?xf32>>
+! CHECK:         %[[VAL_82:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_8]], %{{.*}}, %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_83:.*]] = fir.slice %[[VAL_3]], %[[VAL_2]], %[[VAL_3]], %[[VAL_3]], %[[VAL_2]], %[[VAL_3]] : (index, index, index, index, index, index) -> !fir.slice<2>
+! CHECK:         %[[VAL_84:.*]] = fir.embox %[[VAL_12]](%[[VAL_13]]) {{\[}}%[[VAL_83]]] : (!fir.ref<!fir.array<2x?xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xf32>>
+! CHECK:         %[[VAL_85:.*]] = fir.convert %[[VAL_84]] : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
+! CHECK:         %[[VAL_86:.*]] = fir.call @_FortranAioOutputDescriptor(%[[VAL_82]], %[[VAL_85]]) : (!fir.ref<i8>, !fir.box<none>) -> i1
+! CHECK:         %[[VAL_87:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
+! CHECK:         %[[VAL_88:.*]] = arith.subi %[[VAL_87]], %[[VAL_6]] : i32
+! CHECK:         %[[VAL_89:.*]] = fir.convert %[[VAL_88]] : (i32) -> index
+! CHECK:         %[[VAL_90:.*]] = fir.convert %[[VAL_87]] : (i32) -> index
+! CHECK:         %[[VAL_91:.*]] = fir.slice %[[VAL_3]], %[[VAL_2]], %[[VAL_3]], %[[VAL_89]], %[[VAL_90]], %[[VAL_3]] : (index, index, index, index, index, index) -> !fir.slice<2>
+! CHECK:         %[[VAL_92:.*]] = fir.embox %[[VAL_12]](%[[VAL_13]]) {{\[}}%[[VAL_91]]] : (!fir.ref<!fir.array<2x?xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xf32>>
+! CHECK:         %[[VAL_93:.*]] = fir.convert %[[VAL_92]] : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
+! CHECK:         %[[VAL_94:.*]] = fir.call @_FortranAioOutputDescriptor(%[[VAL_82]], %[[VAL_93]]) : (!fir.ref<i8>, !fir.box<none>) -> i1
+! CHECK:         %[[VAL_95:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_82]]) : (!fir.ref<i8>) -> i32
+! CHECK:         return
+! CHECK:       }
+
+! CHECK-LABEL: func @_QPss4(
+! CHECK-SAME:               %[[VAL_0:.*]]: !fir.ref<i32> {fir.bindc_name = "n"}) {
+! CHECK-DAG:     %[[VAL_1:.*]] = arith.constant -1 : index
+! CHECK-DAG:     %[[VAL_2:.*]] = arith.constant 2 : index
+! CHECK-DAG:     %[[VAL_3:.*]] = arith.constant 1 : index
+! CHECK-DAG:     %[[VAL_4:.*]] = arith.constant -2 : i32
+! CHECK-DAG:     %[[VAL_5:.*]] = arith.constant 0 : index
+! CHECK-DAG:     %[[VAL_6:.*]] = arith.constant 1 : i32
+! CHECK-DAG:     %[[VAL_7:.*]] = arith.constant 7.000000e+00 : f32
+! CHECK-DAG:     %[[VAL_8:.*]] = arith.constant -1 : i32
+! CHECK:         %[[VAL_10:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
+! CHECK:         %[[VAL_11A:.*]] = fir.convert %[[VAL_10]] : (i32) -> index
+! CHECK:         %[[CMP:.*]] = arith.cmpi sgt, %[[VAL_11A]], %[[VAL_5]] : index 
+! CHECK:         %[[VAL_11:.*]] = arith.select %[[CMP]], %[[VAL_11A]], %[[VAL_5]] : index 
+! CHECK:         %[[VAL_12:.*]] = fir.alloca !fir.array<?x2xf32>, %[[VAL_11]] {bindc_name = "aa", uniq_name = "_QFss4Eaa"}
+! CHECK:         %[[VAL_13:.*]] = fir.shape %[[VAL_11]], %[[VAL_2]] : (index, index) -> !fir.shape<2>
+! CHECK:         %[[VAL_14:.*]] = fir.convert %[[VAL_4]] : (i32) -> f32
+! CHECK:         br ^bb1(%[[VAL_5]], %[[VAL_2]] : index, index)
+! CHECK:       ^bb1(%[[VAL_15:.*]]: index, %[[VAL_16:.*]]: index):
+! CHECK:         %[[VAL_17:.*]] = arith.cmpi sgt, %[[VAL_16]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_17]], ^bb2(%[[VAL_5]], %[[VAL_11]] : index, index), ^bb5
+! CHECK:       ^bb2(%[[VAL_18:.*]]: index, %[[VAL_19:.*]]: index):
+! CHECK:         %[[VAL_20:.*]] = arith.cmpi sgt, %[[VAL_19]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_20]], ^bb3, ^bb4
+! CHECK:       ^bb3:
+! CHECK:         %[[VAL_21:.*]] = arith.addi %[[VAL_18]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_22:.*]] = arith.addi %[[VAL_15]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_23:.*]] = fir.array_coor %[[VAL_12]](%[[VAL_13]]) %[[VAL_21]], %[[VAL_22]] : (!fir.ref<!fir.array<?x2xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_14]] to %[[VAL_23]] : !fir.ref<f32>
+! CHECK:         %[[VAL_24:.*]] = arith.subi %[[VAL_19]], %[[VAL_3]] : index
+! CHECK:         br ^bb2(%[[VAL_21]], %[[VAL_24]] : index, index)
+! CHECK:       ^bb4:
+! CHECK:         %[[VAL_25:.*]] = arith.addi %[[VAL_15]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_26:.*]] = arith.subi %[[VAL_16]], %[[VAL_3]] : index
+! CHECK:         br ^bb1(%[[VAL_25]], %[[VAL_26]] : index, index)
+! CHECK:       ^bb5:
+! CHECK:         %[[VAL_27:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
+! CHECK:         %[[VAL_28:.*]] = fir.convert %[[VAL_27]] : (i32) -> index
+! CHECK:         %[[VAL_29:.*]] = arith.addi %[[VAL_28]], %[[VAL_1]] : index
+! CHECK:         %[[VAL_30:.*]] = arith.cmpi sgt, %[[VAL_29]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_31:.*]] = arith.select %[[VAL_30]], %[[VAL_29]], %[[VAL_5]] : index
+! CHECK:         %[[VAL_32:.*]] = fir.slice %[[VAL_2]], %[[VAL_28]], %[[VAL_3]], %[[VAL_3]], %[[VAL_2]], %[[VAL_3]] : (index, index, index, index, index, index) -> !fir.slice<2>
+! CHECK:         %[[VAL_33:.*]] = fir.allocmem !fir.array<?x2xf32>, %[[VAL_11]]
+! CHECK:         br ^bb6(%[[VAL_5]], %[[VAL_2]] : index, index)
+! CHECK:       ^bb6(%[[VAL_34:.*]]: index, %[[VAL_35:.*]]: index):
+! CHECK:         %[[VAL_36:.*]] = arith.cmpi sgt, %[[VAL_35]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_36]], ^bb7(%[[VAL_5]], %[[VAL_11]] : index, index), ^bb10
+! CHECK:       ^bb7(%[[VAL_37:.*]]: index, %[[VAL_38:.*]]: index):
+! CHECK:         %[[VAL_39:.*]] = arith.cmpi sgt, %[[VAL_38]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_39]], ^bb8, ^bb9
+! CHECK:       ^bb8:
+! CHECK:         %[[VAL_40:.*]] = arith.addi %[[VAL_37]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_41:.*]] = arith.addi %[[VAL_34]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_42:.*]] = fir.array_coor %[[VAL_12]](%[[VAL_13]]) %[[VAL_40]], %[[VAL_41]] : (!fir.ref<!fir.array<?x2xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_43:.*]] = fir.array_coor %[[VAL_33]](%[[VAL_13]]) %[[VAL_40]], %[[VAL_41]] : (!fir.heap<!fir.array<?x2xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_44:.*]] = fir.load %[[VAL_42]] : !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_44]] to %[[VAL_43]] : !fir.ref<f32>
+! CHECK:         %[[VAL_45:.*]] = arith.subi %[[VAL_38]], %[[VAL_3]] : index
+! CHECK:         br ^bb7(%[[VAL_40]], %[[VAL_45]] : index, index)
+! CHECK:       ^bb9:
+! CHECK:         %[[VAL_46:.*]] = arith.addi %[[VAL_34]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_47:.*]] = arith.subi %[[VAL_35]], %[[VAL_3]] : index
+! CHECK:         br ^bb6(%[[VAL_46]], %[[VAL_47]] : index, index)
+! CHECK:       ^bb10:
+! CHECK:         %[[VAL_48:.*]] = arith.subi %[[VAL_27]], %[[VAL_6]] : i32
+! CHECK:         %[[VAL_49:.*]] = fir.convert %[[VAL_48]] : (i32) -> index
+! CHECK:         %[[VAL_50:.*]] = fir.slice %[[VAL_3]], %[[VAL_49]], %[[VAL_3]], %[[VAL_3]], %[[VAL_2]], %[[VAL_3]] : (index, index, index, index, index, index) -> !fir.slice<2>
+! CHECK:         br ^bb11(%[[VAL_5]], %[[VAL_2]] : index, index)
+! CHECK:       ^bb11(%[[VAL_51:.*]]: index, %[[VAL_52:.*]]: index):
+! CHECK:         %[[VAL_53:.*]] = arith.cmpi sgt, %[[VAL_52]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_53]], ^bb12(%[[VAL_5]], %[[VAL_31]] : index, index), ^bb15(%[[VAL_5]], %[[VAL_2]] : index, index)
+! CHECK:       ^bb12(%[[VAL_54:.*]]: index, %[[VAL_55:.*]]: index):
+! CHECK:         %[[VAL_56:.*]] = arith.cmpi sgt, %[[VAL_55]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_56]], ^bb13, ^bb14
+! CHECK:       ^bb13:
+! CHECK:         %[[VAL_57:.*]] = arith.addi %[[VAL_54]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_58:.*]] = arith.addi %[[VAL_51]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_59:.*]] = fir.array_coor %[[VAL_12]](%[[VAL_13]]) {{\[}}%[[VAL_50]]] %[[VAL_57]], %[[VAL_58]] : (!fir.ref<!fir.array<?x2xf32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_60:.*]] = fir.load %[[VAL_59]] : !fir.ref<f32>
+! CHECK:         %[[VAL_61:.*]] = arith.addf %[[VAL_60]], %[[VAL_7]] : f32
+! CHECK:         %[[VAL_62:.*]] = fir.array_coor %[[VAL_33]](%[[VAL_13]]) {{\[}}%[[VAL_32]]] %[[VAL_57]], %[[VAL_58]] : (!fir.heap<!fir.array<?x2xf32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_61]] to %[[VAL_62]] : !fir.ref<f32>
+! CHECK:         %[[VAL_63:.*]] = arith.subi %[[VAL_55]], %[[VAL_3]] : index
+! CHECK:         br ^bb12(%[[VAL_57]], %[[VAL_63]] : index, index)
+! CHECK:       ^bb14:
+! CHECK:         %[[VAL_64:.*]] = arith.addi %[[VAL_51]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_65:.*]] = arith.subi %[[VAL_52]], %[[VAL_3]] : index
+! CHECK:         br ^bb11(%[[VAL_64]], %[[VAL_65]] : index, index)
+! CHECK:       ^bb15(%[[VAL_66:.*]]: index, %[[VAL_67:.*]]: index):
+! CHECK:         %[[VAL_68:.*]] = arith.cmpi sgt, %[[VAL_67]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_68]], ^bb16(%[[VAL_5]], %[[VAL_11]] : index, index), ^bb19
+! CHECK:       ^bb16(%[[VAL_69:.*]]: index, %[[VAL_70:.*]]: index):
+! CHECK:         %[[VAL_71:.*]] = arith.cmpi sgt, %[[VAL_70]], %[[VAL_5]] : index
+! CHECK:         cond_br %[[VAL_71]], ^bb17, ^bb18
+! CHECK:       ^bb17:
+! CHECK:         %[[VAL_72:.*]] = arith.addi %[[VAL_69]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_73:.*]] = arith.addi %[[VAL_66]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_74:.*]] = fir.array_coor %[[VAL_33]](%[[VAL_13]]) %[[VAL_72]], %[[VAL_73]] : (!fir.heap<!fir.array<?x2xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_75:.*]] = fir.array_coor %[[VAL_12]](%[[VAL_13]]) %[[VAL_72]], %[[VAL_73]] : (!fir.ref<!fir.array<?x2xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
+! CHECK:         %[[VAL_76:.*]] = fir.load %[[VAL_74]] : !fir.ref<f32>
+! CHECK:         fir.store %[[VAL_76]] to %[[VAL_75]] : !fir.ref<f32>
+! CHECK:         %[[VAL_77:.*]] = arith.subi %[[VAL_70]], %[[VAL_3]] : index
+! CHECK:         br ^bb16(%[[VAL_72]], %[[VAL_77]] : index, index)
+! CHECK:       ^bb18:
+! CHECK:         %[[VAL_78:.*]] = arith.addi %[[VAL_66]], %[[VAL_3]] : index
+! CHECK:         %[[VAL_79:.*]] = arith.subi %[[VAL_67]], %[[VAL_3]] : index
+! CHECK:         br ^bb15(%[[VAL_78]], %[[VAL_79]] : index, index)
+! CHECK:       ^bb19:
+! CHECK:         fir.freemem %[[VAL_33]] : !fir.heap<!fir.array<?x2xf32>>
+! CHECK:         %[[VAL_82:.*]] = fir.call @_FortranAioBeginExternalListOutput(%[[VAL_8]], %{{.*}}, %{{.*}}) : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
+! CHECK:         %[[VAL_83:.*]] = fir.slice %[[VAL_3]], %[[VAL_2]], %[[VAL_3]], %[[VAL_3]], %[[VAL_2]], %[[VAL_3]] : (index, index, index, index, index, index) -> !fir.slice<2>
+! CHECK:         %[[VAL_84:.*]] = fir.embox %[[VAL_12]](%[[VAL_13]]) {{\[}}%[[VAL_83]]] : (!fir.ref<!fir.array<?x2xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xf32>>
+! CHECK:         %[[VAL_85:.*]] = fir.convert %[[VAL_84]] : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
+! CHECK:         %[[VAL_86:.*]] = fir.call @_FortranAioOutputDescriptor(%[[VAL_82]], %[[VAL_85]]) : (!fir.ref<i8>, !fir.box<none>) -> i1
+! CHECK:         %[[VAL_87:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
+! CHECK:         %[[VAL_88:.*]] = arith.subi %[[VAL_87]], %[[VAL_6]] : i32
+! CHECK:         %[[VAL_89:.*]] = fir.convert %[[VAL_88]] : (i32) -> index
+! CHECK:         %[[VAL_90:.*]] = fir.convert %[[VAL_87]] : (i32) -> index
+! CHECK:         %[[VAL_91:.*]] = fir.slice %[[VAL_89]], %[[VAL_90]], %[[VAL_3]], %[[VAL_3]], %[[VAL_2]], %[[VAL_3]] : (index, index, index, index, index, index) -> !fir.slice<2>
+! CHECK:         %[[VAL_92:.*]] = fir.embox %[[VAL_12]](%[[VAL_13]]) {{\[}}%[[VAL_91]]] : (!fir.ref<!fir.array<?x2xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xf32>>
+! CHECK:         %[[VAL_93:.*]] = fir.convert %[[VAL_92]] : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
+! CHECK:         %[[VAL_94:.*]] = fir.call @_FortranAioOutputDescriptor(%[[VAL_82]], %[[VAL_93]]) : (!fir.ref<i8>, !fir.box<none>) -> i1
+! CHECK:         %[[VAL_95:.*]] = fir.call @_FortranAioEndIoStatement(%[[VAL_82]]) : (!fir.ref<i8>) -> i32
+! CHECK:         return
+! CHECK:       }
+
+
+! CHECK-LABEL: func @_QPtt1
+subroutine tt1
+  ! CHECK: fir.call @_FortranAioBeginExternalListOutput
+  ! CHECK: %[[temp3:[0-9]+]] = fir.allocmem !fir.array<3xf32>
+  ! CHECK: br ^bb1(%[[temp3]]
+  ! CHECK-NEXT: ^bb1(%[[temp3arg:[0-9]+]]: !fir.heap<!fir.array<3xf32>>
+  ! CHECK: %[[temp1:[0-9]+]] = fir.allocmem !fir.array<1xf32>
+  ! CHECK: fir.call @_QFtt1Pr
+  ! CHECK: fir.call @realloc
+  ! CHECK: fir.freemem %[[temp1]] : !fir.heap<!fir.array<1xf32>>
+  ! CHECK: %[[temp3x:[0-9]+]] = fir.allocmem !fir.array<3xf32>
+  ! CHECK: fir.call @_FortranAioOutputDescriptor
+  ! CHECK-NEXT: fir.freemem %[[temp3x]] : !fir.heap<!fir.array<3xf32>>
+  ! CHECK-NEXT: fir.freemem %[[temp3arg]] : !fir.heap<!fir.array<3xf32>>
+  ! CHECK-NEXT: fir.call @_FortranAioEndIoStatement
+  print*, [(r([7.0]),i=1,3)]
+contains
+  ! CHECK-LABEL: func @_QFtt1Pr
+  function r(x)
+    real x(:)
+    r = x(1)
+  end
+end


        


More information about the flang-commits mailing list